Skip to content

Commit

Permalink
Add i18n unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
querkmachine committed Aug 30, 2022
1 parent ad073f9 commit 0e8b26e
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/govuk/i18n.unit.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @jest-environment jsdom
*/
/* eslint-env jest */

import I18n from './i18n.mjs'

describe('I18n', () => {

describe('retrieving translations', () => {
let config = {}

beforeEach(() => {
config = {
textString: 'Hello world',
htmlString: 'Hello<span class="govuk-visually-hidden"> world</span>'
}
})

test('returns the text for a given lookup key', () => {
const i18n = new I18n(config)

let returnString = i18n.t('textString')
expect(returnString).toBe('Hello world')
})

test('returns the HTML for a given lookup key', () => {
const i18n = new I18n(config)

let returnString = i18n.t('htmlString')
expect(returnString).toBe('Hello<span class="govuk-visually-hidden"> world</span>')
})

test('returns the lookup key if no translation is defined', () => {
const i18n = new I18n(config)

let returnString = i18n.t('missingString')
expect(returnString).toBe('missingString')
})

test('throws an error if no lookup key is provided', () => {
const i18n = new I18n(config)

expect(() => i18n.t()).toThrow('i18n lookup key missing.')
})

})
})

0 comments on commit 0e8b26e

Please sign in to comment.