-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SPL-466c - more unit test updates (#285)
* exclude assets files from unit tests * add accessible planner filters tests * add weekUtils tests * add unit tests for hidden fields filters * Update filters.test.js
- Loading branch information
Showing
4 changed files
with
223 additions
and
0 deletions.
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,60 @@ | ||
const { describe, it } = require('mocha') | ||
const { expect } = require('chai') | ||
const { getWeekByNumber } = require('../../../../app/lib/weekUtils') | ||
|
||
describe('weekUtils', () => { | ||
describe('getWeekByNumber', () => { | ||
it('returns the correct week when the number exists', () => { | ||
const weeks = [ | ||
{ number: 1, name: 'Week 1' }, | ||
{ number: 2, name: 'Week 2' }, | ||
{ number: 3, name: 'Week 3' } | ||
] | ||
const result = getWeekByNumber(weeks, 2) | ||
expect(result).to.deep.equal({ number: 2, name: 'Week 2' }) | ||
}) | ||
|
||
it('returns undefined when the number does not exist', () => { | ||
const weeks = [ | ||
{ number: 1, name: 'Week 1' }, | ||
{ number: 2, name: 'Week 2' } | ||
] | ||
const result = getWeekByNumber(weeks, 3) | ||
expect(result).to.equal(undefined) | ||
}) | ||
|
||
it('returns undefined for an empty array', () => { | ||
const weeks = [] | ||
const result = getWeekByNumber(weeks, 1) | ||
expect(result).to.equal(undefined) | ||
}) | ||
|
||
it('handles a number that is not an integer', () => { | ||
const weeks = [ | ||
{ number: 1, name: 'Week 1' }, | ||
{ number: 2, name: 'Week 2' } | ||
] | ||
const result = getWeekByNumber(weeks, 1.5) | ||
expect(result).to.equal(undefined) | ||
}) | ||
|
||
it('handles an array with duplicate week numbers', () => { | ||
const weeks = [ | ||
{ number: 1, name: 'Week 1' }, | ||
{ number: 1, name: 'Duplicate Week 1' } | ||
] | ||
const result = getWeekByNumber(weeks, 1) | ||
expect(result).to.deep.equal({ number: 1, name: 'Week 1' }) // Verifies it returns the first match | ||
}) | ||
|
||
it('does not mutate the original array', () => { | ||
const weeks = [ | ||
{ number: 1, name: 'Week 1' }, | ||
{ number: 2, name: 'Week 2' } | ||
] | ||
const originalWeeks = [...weeks] | ||
getWeekByNumber(weeks, 1) | ||
expect(weeks).to.deep.equal(originalWeeks) | ||
}) | ||
}) | ||
}) |
110 changes: 110 additions & 0 deletions
110
test/unit/app/views/accessible-planner/answers-so-far/filters.test.js
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,110 @@ | ||
const { describe, it, beforeEach } = require('mocha') | ||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
|
||
const mockEnv = { | ||
getFilter: sinon.stub().returns((data, parent) => `Parent Name: ${parent}`) | ||
} | ||
|
||
const filters = require('../../../../../../app/views/accessible-planner/answers-so-far/filters')(mockEnv) | ||
|
||
describe('Filters', () => { | ||
let rows, data, dateMacroStub, dateEndMacroStub, formattedStartDate | ||
|
||
beforeEach(() => { | ||
rows = [] | ||
|
||
const startDate = new Date() | ||
startDate.setMonth(startDate.getMonth() - 9) | ||
|
||
formattedStartDate = startDate.toISOString().split('T')[0] | ||
const endDate = new Date(startDate) | ||
endDate.setMonth(startDate.getMonth() + 1) | ||
|
||
data = { | ||
'leave-blocks': { | ||
primary: { spl: { _0: { start: formattedStartDate, end: endDate.toISOString().split('T')[0] } } }, | ||
secondary: { spl: { _0: { start: formattedStartDate, end: endDate.toISOString().split('T')[0] } } }, | ||
'spl-block-planning-order': ['primary', 'secondary'] | ||
} | ||
} | ||
dateMacroStub = sinon.stub().returns('Formatted Start Date') | ||
dateEndMacroStub = sinon.stub().returns('Formatted End Date') | ||
}) | ||
|
||
describe('appendSplAnswerRows', () => { | ||
it('should append rows for SPL blocks with start and end dates', () => { | ||
filters.appendSplAnswerRows(rows, data, dateMacroStub, dateEndMacroStub) | ||
|
||
expect(rows).to.have.lengthOf(4) | ||
|
||
// First SPL block (Primary) | ||
expect(rows[0].key.text).to.include('SPL block 1 of 2 start') | ||
expect(rows[0].value.text).to.equal('Formatted Start Date') | ||
expect(rows[0].actions.items[0].href).to.include('/planner/shared-parental-leave/start?block=1') | ||
|
||
expect(rows[1].key.text).to.include('SPL block 1 of 2 end') | ||
expect(rows[1].value.text).to.equal('Formatted End Date') | ||
expect(rows[1].actions.items[0].href).to.include('/planner/shared-parental-leave/end?block=1') | ||
|
||
// Second SPL block (Secondary) | ||
expect(rows[2].key.text).to.include('SPL block 2 of 2 start') | ||
expect(rows[2].value.text).to.equal('Formatted Start Date') | ||
expect(rows[2].actions.items[0].href).to.include('/planner/shared-parental-leave/start?block=2') | ||
|
||
expect(rows[3].key.text).to.include('SPL block 2 of 2 end') | ||
expect(rows[3].value.text).to.equal('Formatted End Date') | ||
expect(rows[3].actions.items[0].href).to.include('/planner/shared-parental-leave/end?block=2') | ||
}) | ||
|
||
it('should handle cases with no SPL blocks', () => { | ||
data['leave-blocks'].primary.spl = null | ||
data['leave-blocks'].secondary.spl = null | ||
|
||
filters.appendSplAnswerRows(rows, data, dateMacroStub, dateEndMacroStub) | ||
|
||
expect(rows).to.have.lengthOf(0) | ||
}) | ||
|
||
it('should skip blocks without start or end dates', () => { | ||
data['leave-blocks'].secondary.spl._0 = { start: formattedStartDate } | ||
|
||
filters.appendSplAnswerRows(rows, data, dateMacroStub, dateEndMacroStub) | ||
|
||
expect(rows).to.have.lengthOf(3) | ||
|
||
expect(rows[0].key.text).to.include('SPL block 1 of 2 start') | ||
expect(rows[1].key.text).to.include('SPL block 1 of 2 end') | ||
expect(rows[2].key.text).to.include('SPL block 2 of 2 start') | ||
}) | ||
}) | ||
|
||
describe('removeRowsWithEmptyValues', () => { | ||
it('should remove rows with empty values', () => { | ||
rows = [ | ||
{ key: { text: 'Row 1' }, value: { text: 'Not Empty' } }, | ||
{ key: { text: 'Row 2' }, value: { text: '' } }, | ||
{ key: { text: 'Row 3' }, value: { text: ' ' } }, | ||
{ key: { text: 'Row 4' }, value: { html: '<p>Not Empty</p>' } }, | ||
{ key: { text: 'Row 5' }, value: { html: ' ' } } | ||
] | ||
|
||
const filteredRows = filters.removeRowsWithEmptyValues(rows) | ||
|
||
expect(filteredRows).to.have.lengthOf(2) | ||
expect(filteredRows[0].key.text).to.equal('Row 1') | ||
expect(filteredRows[1].key.text).to.equal('Row 4') | ||
}) | ||
|
||
it('should retain all rows if all values are non-empty', () => { | ||
rows = [ | ||
{ key: { text: 'Row 1' }, value: { text: 'Not Empty' } }, | ||
{ key: { text: 'Row 2' }, value: { html: '<p>Non-empty</p>' } } | ||
] | ||
|
||
const filteredRows = filters.removeRowsWithEmptyValues(rows) | ||
|
||
expect(filteredRows).to.deep.equal(rows) | ||
}) | ||
}) | ||
}) |
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,52 @@ | ||
const { describe, it } = require('mocha') | ||
const { expect } = require('chai') | ||
const filters = require('../../../../../common/macros/hidden-fields/filters')() | ||
|
||
describe('filters', () => { | ||
describe('castArray', () => { | ||
it('returns an array when given a non-array value', () => { | ||
expect(filters.castArray(1)).to.deep.equal([1]) | ||
}) | ||
|
||
it('returns the same array when given an array', () => { | ||
expect(filters.castArray([1, 2])).to.deep.equal([1, 2]) | ||
}) | ||
}) | ||
|
||
describe('isObject', () => { | ||
it('returns true for objects', () => { | ||
expect(filters.isObject({ key: 'value' })).to.equal(true) | ||
}) | ||
|
||
it('returns false for non-objects', () => { | ||
expect(filters.isObject(42)).to.equal(false) | ||
expect(filters.isObject(null)).to.equal(false) | ||
expect(filters.isObject(undefined)).to.equal(false) | ||
}) | ||
}) | ||
|
||
describe('fieldNames', () => { | ||
it('returns the names of the fields in the form inner HTML', () => { | ||
const formInnerHtml = ` | ||
<input name="name" /> | ||
<input name="email" /> | ||
` | ||
expect(filters.fieldNames(formInnerHtml)).to.deep.equal([ | ||
'name', | ||
'email' | ||
]) | ||
}) | ||
}) | ||
|
||
describe('matchesAnyField', () => { | ||
it('returns true when the name matches a field', () => { | ||
const match = filters.matchesAnyField('name', ['name=', 'email=']) | ||
expect(match).to.equal('name=') | ||
}) | ||
|
||
it('returns false when the name does not match any field', () => { | ||
const match = filters.matchesAnyField('name', ['email=']) | ||
expect(match).to.equal(undefined) | ||
}) | ||
}) | ||
}) |