Skip to content

Commit

Permalink
Merge pull request #2819 from alphagov/unit-test-helpers-2
Browse files Browse the repository at this point in the history
Add unit tests for helpers
  • Loading branch information
Vanita Barrett-Smith authored Sep 1, 2022
2 parents 040db6d + e2e9b98 commit 8f8fa6f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/file-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const getComponentData = componentName => {
fs.readFileSync(yamlPath, 'utf8'), { json: true }
)
} catch (error) {
return new Error(error)
throw new Error(error)
}
}

Expand Down
70 changes: 70 additions & 0 deletions lib/file-helper.unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-env jest */

const path = require('path')
const fileHelper = require('../lib/file-helper')

describe('getComponentData', () => {
it('returns an error if unable to load component data', () => {
expect(() => { fileHelper.getComponentData('not-a-real-component') }).toThrow(Error)
})

it('looks up the correct component path', () => {
jest.spyOn(path, 'join')

fileHelper.getComponentData('accordion')

expect(path.join).toHaveBeenCalledWith('src/govuk/components/', 'accordion', 'accordion.yaml')
})

it('outputs objects with an array of params and examples', () => {
var componentData = fileHelper.getComponentData('accordion')

expect(componentData).toEqual(expect.objectContaining({
params: expect.any(Array),
examples: expect.any(Array)
}))
})

it('outputs a param for each object with the expected attributes', () => {
var componentData = fileHelper.getComponentData('accordion')

componentData.params.forEach((param) => {
expect(param).toEqual(
expect.objectContaining({
name: expect.any(String),
type: expect.any(String),
required: expect.any(Boolean),
description: expect.any(String)
})
)
})
})

it('contains example objects with the expected attributes', () => {
var componentData = fileHelper.getComponentData('accordion')

componentData.examples.forEach((example) => {
expect(example).toEqual(
expect.objectContaining({
name: expect.any(String),
data: expect.any(Object)
})
)
})
})
})

describe('fullPageExamples', () => {
it('contains name and path of each example, at a minimum', () => {
var fullPageExamples = fileHelper.fullPageExamples()

fullPageExamples.forEach((example) => {
expect(example).toEqual(
expect.objectContaining({
name: expect.any(String),
path: expect.any(String)
})
)
})
})
})
31 changes: 31 additions & 0 deletions lib/helper-functions.unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-env jest */

const helperFunctions = require('../lib/helper-functions')

describe('componentNameToMacroName', () => {
it('transfoms a single word component name', () => {
var macroName = helperFunctions.componentNameToMacroName('button')

expect(macroName).toBe('govukButton')
})

it('transfoms a multi-word component name', () => {
var macroName = helperFunctions.componentNameToMacroName('character-count')

expect(macroName).toBe('govukCharacterCount')
})
})

describe('componentNameToJavaScriptModuleName', () => {
it('transfoms a single word component name', () => {
var moduleName = helperFunctions.componentNameToJavaScriptModuleName('button')

expect(moduleName).toBe('GOVUKFrontend.Button')
})

it('transfoms a multi-word component name', () => {
var moduleName = helperFunctions.componentNameToJavaScriptModuleName('character-count')

expect(moduleName).toBe('GOVUKFrontend.CharacterCount')
})
})

0 comments on commit 8f8fa6f

Please sign in to comment.