-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2819 from alphagov/unit-test-helpers-2
Add unit tests for helpers
- Loading branch information
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |