Skip to content

Commit

Permalink
Use Node test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Feb 11, 2024
1 parent d26406a commit 9c5438b
Show file tree
Hide file tree
Showing 13 changed files with 2,235 additions and 3,905 deletions.
5,851 changes: 2,102 additions & 3,749 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 2 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
},
"scripts": {
"lint": "standard",
"test": "ava",
"test:watch": "ava --watch",
"coverage": "c8 ava"
"test": "NODE_ENV=test node --test",
"coverage": "NODE_ENV=test node --test --experimental-test-coverage"
},
"publishConfig": {
"access": "public"
Expand All @@ -35,19 +34,11 @@
"lodash": "^4.17.21"
},
"devDependencies": {
"ava": "^4.0.1",
"c8": "^7.11.0",
"nhsuk-frontend": "^6.1.2",
"nunjucks": "^3.2.3",
"standard": "^17.0.0"
},
"engines": {
"node": ">=20"
},
"c8": {
"reporter": [
"text",
"lcovonly"
]
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
131 changes: 131 additions & 0 deletions test/lib/decorate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
const assert = require('node:assert/strict')
const test = require('node:test')
const nunjucks = require('nunjucks')
const { decorate } = require('../../lib/decorate.js')

const env = nunjucks.configure([
'./',
'./node_modules/nhsuk-frontend/packages/components',
'./test/fixtures'
])
env.addGlobal('decorate', decorate)

const data = {
data: {
account: {
'email-address': '[email protected]'
},
country: 'england',
'passport-issued': {
day: '31',
month: '12',
year: '1999'
},
status: 'published'
}
}

test('Returns form component without decorate attribute', () => {
const result = env.render('input-not-decorated.njk', data)

assert.match(result, /id=""/)
assert.match(result, /name=""/)
})

test('Returns form component without any session data', () => {
const result = env.render('input.njk', {})

assert.match(result, /id=""/)
assert.match(result, /name=""/)
})

test('Decorates form component', t => {
const result = env.render('input.njk', data)

assert.match(result, /for="account-email-address"/)
assert.match(result, /id="account-email-address"/)
assert.match(result, /name="\[account\]\[email-address\]"/)
assert.match(result, /value="test@example.org"/)
})

test('Decorates form component with items', () => {
const result = env.render('radios.njk', data)

assert.match(result, /for="country-1"/)
assert.match(result, /for="country-2"/)
assert.match(result, /id="country-1".*name="\[country\].*value="england".*checked/)
assert.match(result, /id="country-2".*name="\[country\].*value="scotland"/)
})

test('Decorates form component with items (data stored in array)', () => {
const result = env.render('radios.njk', {
data: { country: ['england'] }
})

assert.match(result, /for="country-1"/)
assert.match(result, /for="country-2"/)
assert.match(result, /id="country-1".*name="\[country\].*value="england".*checked/)
assert.match(result, /id="country-2".*name="\[country\].*value="scotland"/)
})

test('Decorates button component', () => {
const result = env.render('button.njk', data)

assert.match(result, /name="\[status\]"/)
assert.match(result, /value="published"/)
})

test('Does not decorate button link component', () => {
const result = env.render('button-href.njk', data)

assert.doesNotMatch(result, /name="\[status\]"/)
assert.doesNotMatch(result, /value="published"/)
})

test('Uses label text if no value given for option', () => {
const result = env.render('radios-no-values.njk', data)

assert.match(result, /id="country-1".*name="\[country\].*value="England"/)
assert.match(result, /id="country-2".*name="\[country\].*value="Scotland"/)
assert.match(result, /id="country-3".*name="\[country\].*value="Wales"/)
assert.match(result, /id="country-4".*name="\[country\].*value="Northern Ireland"/)
assert.match(result, /id="country-6".*name="\[country\].*value="Another country"/)
})

test('Decorates date input component', () => {
const result = env.render('date-input.njk', data)

assert.match(result, /for="passport-issued-day"/)
assert.match(result, /for="passport-issued-month"/)
assert.match(result, /for="passport-issued-year"/)
assert.match(result, /id="passport-issued-day".*name="\[passport-issued\]\[day\].*value="31"/)
assert.match(result, /id="passport-issued-month".*name="\[passport-issued\]\[month\].*value="12"/)
assert.match(result, /id="passport-issued-year".*name="\[passport-issued\]\[year\].*value="1999"/)
})

test('Strips data from key path', () => {
const result = env.render('input-data-in-key-path.njk', data)

assert.match(result, /for="account-email-address"/)
assert.match(result, /id="account-email-address"/)
assert.match(result, /name="\[account\]\[email-address\]"/)
assert.match(result, /value="test@example.org"/)
})

test('Add message if error', () => {
const result = env.render('input.njk', {
...data,
...{
errors: {
'account.email-address': {
msg: 'Enter an email address in the correct format'
}
}
}
})

assert.match(result, /id="account-email-address-error"/)
assert.match(result, /Error:/)
assert.match(result, /Enter an email address in the correct format/)
assert.match(result, /aria-describedby="account-email-address-error"/)
})
145 changes: 0 additions & 145 deletions tests/lib/decorate.mjs

This file was deleted.

0 comments on commit 9c5438b

Please sign in to comment.