Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[L&D] Randomise jasmine tests #6267

Merged
merged 11 commits into from
Dec 18, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ moj.Modules.OffenceSearchInput = {
feeScheme: '.fx-fee-scheme',

// delay in miliseconds
debouce: 500,
debounce: 500,

// min input length to trigger search
minLength: 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ moj.Modules.SideBar = {
const self = this
let selector
let value
this.sanitzeFeeToFloat()
this.sanitizeFeeToFloat()
$.each(this.totals, function (key, val) {
selector = '.total-' + key
value = moj.Helpers.Blocks.formatNumber(val)
Expand Down Expand Up @@ -136,7 +136,7 @@ moj.Modules.SideBar = {
})
},

sanitzeFeeToFloat: function () {
sanitizeFeeToFloat: function () {
const self = this
$.each(this.totals, function (key, val) {
if (typeof self.totals[key] === 'string') {
Expand Down
19 changes: 10 additions & 9 deletions spec/javascripts/Modules.AllocationDataTable_spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
describe('Modules.AllocationDataTable.js', function () {
// tooooo long to type
const module = moj.Modules.AllocationDataTable
const options = module.options

it('...should exist', function () {
expect(moj.Modules.AllocationDataTable).toBeDefined()
expect(module).toBeDefined()
})

it('...should have options set', function () {
Expand Down Expand Up @@ -111,6 +110,7 @@ describe('Modules.AllocationDataTable.js', function () {
})

it('...should have `ajax`', function () {
module.init()
expect(options.ajax).toEqual({
url: '/api/search/unallocated?api_key={0}&scheme=agfs',
dataSrc: '' // this is the important setting
Expand Down Expand Up @@ -203,6 +203,10 @@ describe('Modules.AllocationDataTable.js', function () {
}).appendTo('body')
})

beforeEach(function () {
module.init()
})

afterAll(function () {
$('#api-key').remove()
})
Expand All @@ -216,24 +220,21 @@ describe('Modules.AllocationDataTable.js', function () {
})

it('...should use `defaultScheme` as a fallback', function () {
// call init again
module.init()

expect(module.options.ajax.url).toEqual('/api/search/unallocated?api_key=1234567890&scheme=agfs')
expect(options.ajax.url).toEqual('/api/search/unallocated?api_key=1234567890&scheme=agfs')

module.setAjaxURL('abcd')

expect(module.options.ajax.url).toEqual('/api/search/unallocated?api_key=1234567890&scheme=abcd')
expect(options.ajax.url).toEqual('/api/search/unallocated?api_key=1234567890&scheme=abcd')

module.setAjaxURL()

expect(module.options.ajax.url).toEqual('/api/search/unallocated?api_key=1234567890&scheme=agfs')
expect(options.ajax.url).toEqual('/api/search/unallocated?api_key=1234567890&scheme=agfs')
})

it('...should set the scheme as passed in', function () {
module.setAjaxURL('abcd')

expect(module.options.ajax.url).toEqual('/api/search/unallocated?api_key=1234567890&scheme=abcd')
expect(options.ajax.url).toEqual('/api/search/unallocated?api_key=1234567890&scheme=abcd')
})

it('...should return a string', function () {
Expand Down
4 changes: 4 additions & 0 deletions spec/javascripts/Modules.AllocationScheme_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@ describe('Modules.AllocationScheme.js', function () {
$('#allocation-filters input').trigger('change')

expect($.publish).toHaveBeenCalledWith('/scheme/change/', { scheme: 'input-value' })

// reset the filter for future tests
$(filtersFixtureDOM).append($('<input name="myname" value="agfs" type="radio" />'))
$('#allocation-filters input').trigger('change')
})
})
2 changes: 1 addition & 1 deletion spec/javascripts/Modules.DisbursementCtrl_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Modules.DisbursementCtrl.js', function () {
})

afterEach(function () {
$('body #offence-view').remove()
$('body #disbursements-view').remove()
})

describe('...defaults', function () {
Expand Down
12 changes: 8 additions & 4 deletions spec/javascripts/Modules.OffenceSearchInput_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ describe('Modules.OffenceSearchInput.js', function () {
}

beforeEach(function () {
$('body .mod-search-input').remove()
$('body').append(view())
})

afterEach(function () {
$('body .mod-search-input').remove()
})

describe('...defaults', function () {
it('`this.el`', function () {
expect(module.el).toEqual('.mod-search-input')
Expand All @@ -38,8 +41,8 @@ describe('Modules.OffenceSearchInput.js', function () {
it('`this.feeScheme`', function () {
expect(module.feeScheme).toEqual('.fx-fee-scheme')
})
it('`this.debouce`', function () {
expect(module.debouce).toEqual(500)
it('`this.debounce`', function () {
expect(module.debounce).toEqual(500)
})
it('`this.subscribers`', function () {
expect(module.subscribers).toEqual({
Expand Down Expand Up @@ -176,10 +179,11 @@ describe('Modules.OffenceSearchInput.js', function () {
describe('...trackUserInput', function () {
it('should use `moj.Modules.Debounce`', function () {
spyOn(moj.Modules.Debounce, 'init')
module.$input.val('mudr')

module.init()

module.$input.val('mudr')

// trigger keyup
module.$input.trigger($.Event('keyup', {
keyCode: 65
Expand Down
1 change: 1 addition & 0 deletions spec/javascripts/helpers-sidebar_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ describe('Helpers.Blocks.js', function () {

afterEach(function () {
$('#claim-form').remove()
$('.js-block').remove()
})

describe('...init', function () {
Expand Down
6 changes: 3 additions & 3 deletions spec/javascripts/modules-DuplicateExpenseCtrl_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ describe('Modules.DuplicateExpenseCtrl', function () {
].join('')

beforeEach(function () {
domFixture.empty()
domFixture.append($(view))
// $('body').append(domFixture);
$('body').append(domFixture)

// reset to default state
moj.Modules.DuplicateExpenseCtrl.init()
})

afterAll(function () {
afterEach(function () {
domFixture.empty()
})

Expand Down
38 changes: 20 additions & 18 deletions spec/javascripts/modules-sidebar_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Modules.SideBar.js', function () {
})

afterEach(function () {
moj.Modules.SideBar.phantomBlockList = ['fixedFees', 'gradFees', 'miscFees', 'warrantFees', 'interimFees', 'transferFees', 'disbursements', 'expenses']
moj.Modules.SideBar.phantomBlockList = ['fixedFees', 'gradFees', 'miscFees', 'warrantFees', 'interimFees', 'transferFees', 'hardshipFees', 'disbursements', 'expenses']
moj.Modules.SideBar.blocks = []
moj.Modules.SideBar.totals = {
fixedFees: 0,
Expand All @@ -74,6 +74,7 @@ describe('Modules.SideBar.js', function () {
warrantFees: 0,
interimFees: 0,
transferFees: 0,
hardshipFees: 0,
disbursements: 0,
expenses: 0,
vat: 0,
Expand Down Expand Up @@ -104,6 +105,7 @@ describe('Modules.SideBar.js', function () {
warrantFees: 0,
interimFees: 0,
transferFees: 0,
hardshipFees: 0,
disbursements: 0,
expenses: 0,
vat: 0,
Expand All @@ -112,7 +114,7 @@ describe('Modules.SideBar.js', function () {
})

it('should have a `phantomBlockList` property defined', function () {
expect(moj.Modules.SideBar.phantomBlockList).toEqual(['fixedFees', 'gradFees', 'miscFees', 'warrantFees', 'interimFees', 'transferFees', 'disbursements', 'expenses'])
expect(moj.Modules.SideBar.phantomBlockList).toEqual(['fixedFees', 'gradFees', 'miscFees', 'warrantFees', 'interimFees', 'transferFees', 'hardshipFees', 'disbursements', 'expenses'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpw5 this is just me attempting to code review so please ignore if not important. Is it possible to change the setup so that you don't have to make this change twice? Or is that an unnecessary refactor and a faff?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Katy. It's a good question. I guess it could be optimised slightly to remove the repetition and I think if that list of fees was being used in more places then I probably would pull it out into a variable so that we could re-use it, but as it's only being used once in the setup and once in the expectation it doesn't feel too bad.

})

it('should have a `blocks` property defined', function () {
Expand Down Expand Up @@ -156,8 +158,6 @@ describe('Modules.SideBar.js', function () {
moj.Modules.SideBar.loadBlocks()

expect(moj.Modules.SideBar.blocks.length).toBe(3)

jsBlockFixtureDOM.empty()
})

it('should cache an instance of `FeeBlock` for every `.js-block` el', function () {
Expand All @@ -171,8 +171,6 @@ describe('Modules.SideBar.js', function () {

moj.Modules.SideBar.loadBlocks()
expect(moj.Modules.SideBar.blocks.length).toEqual(3)

jsBlockFixtureDOM.empty()
})

it('should update the `phantomBlockList` by removing types', function () {
Expand All @@ -193,19 +191,17 @@ describe('Modules.SideBar.js', function () {
moj.Modules.SideBar.loadBlocks()

expect(moj.Modules.SideBar.removePhantomKey).toHaveBeenCalled()
expect(moj.Modules.SideBar.phantomBlockList.length).toEqual(6)

jsBlockFixtureDOM.empty()
expect(moj.Modules.SideBar.phantomBlockList.length).toEqual(7)
})
})

describe('...removePhantomKey', function () {
it('should remove items from the array', function () {
const module = moj.Modules.SideBar
module.removePhantomKey('Hellos')
expect(module.phantomBlockList.length).toEqual(8)
expect(module.phantomBlockList.length).toEqual(9)
module.removePhantomKey('fixedFees')
expect(module.phantomBlockList.length).toEqual(7)
expect(module.phantomBlockList.length).toEqual(8)
})
})

Expand All @@ -230,10 +226,10 @@ describe('Modules.SideBar.js', function () {
expect($el.find('.total-grandTotal')[0].innerHTML).toBe('£333.00')
})

it('should call `sanitzeFeeToFloat`', function () {
spyOn(moj.Modules.SideBar, 'sanitzeFeeToFloat')
it('should call `sanitizeFeeToFloat`', function () {
spyOn(moj.Modules.SideBar, 'sanitizeFeeToFloat')
moj.Modules.SideBar.render()
expect(moj.Modules.SideBar.sanitzeFeeToFloat).toHaveBeenCalled()
expect(moj.Modules.SideBar.sanitizeFeeToFloat).toHaveBeenCalled()
})
})

Expand Down Expand Up @@ -286,7 +282,8 @@ describe('Modules.SideBar.js', function () {

describe('...calculations', function () {
it('should add to the correct `this.type` property', function () {
$('#claim-form').append(jsBlockViewCalculated())
jsBlockFixtureDOM.append(jsBlockViewCalculated())
$('body').append(jsBlockFixtureDOM)

moj.Modules.SideBar.init()

Expand Down Expand Up @@ -357,6 +354,11 @@ describe('Modules.SideBar.js', function () {
})

describe('...bindListeners', function () {
beforeEach(function () {
jsBlockFixtureDOM.append(jsBlockViewCalculated())
$('body').append(jsBlockFixtureDOM)
})

it('should bind the listeners to the dom elements', function () {
spyOn(jQuery.fn, 'on')
moj.Modules.SideBar.bindListeners()
Expand Down Expand Up @@ -386,8 +388,8 @@ describe('Modules.SideBar.js', function () {
})
})

describe('...sanitzeFeeToFloat', function () {
it('should sanitze the totals correctly', function () {
describe('...sanitizeFeeToFloat', function () {
it('should sanitize the totals correctly', function () {
const expected = {
fees: 10.20,
disbursements: 4558.99,
Expand All @@ -403,7 +405,7 @@ describe('Modules.SideBar.js', function () {
grandTotal: 0
}

moj.Modules.SideBar.sanitzeFeeToFloat()
moj.Modules.SideBar.sanitizeFeeToFloat()
expect(moj.Modules.SideBar.totals).toEqual(expected)
})
})
Expand Down
10 changes: 8 additions & 2 deletions spec/javascripts/supporting-evidence_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('supportingEvidence', function () {
const submitCallback = jasmine.createSpy('submit').and.returnValue(false)
let confirmAlert
let submitCallback

const $form = function () {
return $('#supporting-evidence-fixture-form')
Expand Down Expand Up @@ -40,14 +40,16 @@ describe('supportingEvidence', function () {
</fieldset>
<div class="button-holder">
<button type="submit" name="commit_submit_claim" value="Continue" class="govuk-button" data-module="govuk-button">Save and continue</button>
<button class="govuk-button govuk-button--secondary" data-module="govuk-button">Save a draft</button>
<button class="govuk-button govuk-button--secondary" name="commit_save_draft" data-module="govuk-button">Save a draft</button>
</div>
</form>
</div>
`)

beforeEach(function () {
$('body').append(fixtureDom)

submitCallback = jasmine.createSpy('submit').and.returnValue(false)
$form().submit(submitCallback)
confirmAlert = spyOn(window, 'confirm')

Expand Down Expand Up @@ -80,6 +82,10 @@ describe('supportingEvidence', function () {
})

describe('should not alert when copy of the indictment is selected in the supporting evidence checklist', function () {
afterEach(function () {
$indictmentEvidence().trigger('click')
})

it('and submit the form', function () {
$indictmentEvidence().trigger('click')

Expand Down
4 changes: 2 additions & 2 deletions spec/support/jasmine-browser.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"env": {
"stopSpecOnExpectationFailure": false,
"stopOnSpecFailure": false,
"random": false
"random": true
},
"browser": {
"name": "headlessChrome"
}
}
}