-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests for character-count script #1055
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @jest-environment ./lib/puppeteer/environment.js | ||
*/ | ||
/* eslint-env jest */ | ||
|
||
const configPaths = require('../../../config/paths.json') | ||
const PORT = configPaths.ports.test | ||
|
||
let browser | ||
let page | ||
let baseUrl = 'http://localhost:' + PORT | ||
|
||
beforeEach(async () => { | ||
browser = global.__BROWSER__ | ||
page = await browser.newPage() | ||
}) | ||
|
||
afterEach(async () => { | ||
await page.close() | ||
}) | ||
|
||
describe('Character count', () => { | ||
describe('when JavaScript is unavailable or fails', () => { | ||
it('shows the static message', async () => { | ||
await page.setJavaScriptEnabled(false) | ||
|
||
await page.goto(baseUrl + '/components/character-count/preview', { waitUntil: 'load' }) | ||
const message = await page.$eval('.govuk-character-count__message', el => el.innerHTML.trim()) | ||
|
||
expect(message).toEqual('You can enter up to 10 characters') | ||
}) | ||
}) | ||
describe('when JavaScript is available', () => { | ||
it('shows the dynamic message', async () => { | ||
await page.goto(baseUrl + '/components/character-count/preview', { waitUntil: 'load' }) | ||
|
||
const message = await page.$eval('.govuk-character-count__message', el => el.innerHTML.trim()) | ||
|
||
expect(message).toEqual('You have 10 characters remaining') | ||
}) | ||
|
||
it('counts down while typing and within limit', async () => { | ||
await page.goto(baseUrl + '/components/character-count/preview', { waitUntil: 'load' }) | ||
|
||
// Press 1 character | ||
await page.focus('.js-character-count') | ||
await page.keyboard.press('A') | ||
const message = await page.$eval('.govuk-character-count__message', el => el.innerHTML.trim()) | ||
|
||
expect(message).toEqual('You have 9 characters remaining') | ||
}) | ||
|
||
it('counts up while typing and the limit is exceeded', async () => { | ||
await page.goto(baseUrl + '/components/character-count/preview', { waitUntil: 'load' }) | ||
|
||
// Press 11 characters | ||
await page.focus('.js-character-count') | ||
for (let i = 0; i < 11; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not blocking: would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Repeat doesn't seem to be working, unfortunately, so I'll have to leave the for. |
||
await page.keyboard.press('A') | ||
} | ||
|
||
const message = await page.$eval('.govuk-character-count__message', el => el.innerHTML.trim()) | ||
|
||
expect(message).toEqual('You have 1 character too many') | ||
}) | ||
|
||
it('adds error styles to the textarea when the limit is exceeded', async () => { | ||
await page.goto(baseUrl + '/components/character-count/preview', { waitUntil: 'load' }) | ||
|
||
// Press 11 characters | ||
await page.focus('.js-character-count') | ||
for (let i = 0; i < 11; i++) { | ||
await page.keyboard.press('A') | ||
} | ||
|
||
const textareaClasses = await page.$eval('.js-character-count', el => el.className) | ||
|
||
expect(textareaClasses).toContain('govuk-textarea--error') | ||
alex-ju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
it('adds error styles to the count message when the limit is exceeded', async () => { | ||
await page.goto(baseUrl + '/components/character-count/preview', { waitUntil: 'load' }) | ||
|
||
// Press 11 characters | ||
await page.focus('.js-character-count') | ||
for (let i = 0; i < 11; i++) { | ||
await page.keyboard.press('A') | ||
} | ||
|
||
const messageClasses = await page.$eval('.govuk-character-count__message', el => el.className) | ||
|
||
expect(messageClasses).toContain('govuk-error-message') | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌 Great to see tests for this.