Skip to content
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

Merged
merged 1 commit into from
Nov 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/components/character-count/character-count.test.js
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', () => {
Copy link
Contributor

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.

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++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking: would page.keyboard.type('A'.repeat(11)) have the same effect?

Copy link
Contributor Author

@alex-ju alex-ju Nov 7, 2018

Choose a reason for hiding this comment

The 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')
})
})
})