Skip to content

Commit

Permalink
Add some unit tests for Determination class
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmhaig committed Jul 28, 2023
1 parent 1b7b08e commit a31fd59
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
54 changes: 54 additions & 0 deletions app/webpack/javascripts/modules/determination_spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Determination } from './determination.mjs'

describe('Determination', () => {
let determination = null

beforeEach(() => {
document.body.classList.add("govuk-frontend-supported")
document.body.append(`
<table id="determinations" data-module="govuk-determination" data-apply-vat="true" data-vat-url="/vat.json" data-submitted-date="2023-07-18" data-scheme="agfs">
</table>
`)

const elements = window.document.querySelectorAll('[data-module="govuk-determination"]')
determination = new Determination(elements[0])
determination.init()
})

describe('parsedValue', () => {
it('should parse a positive integer', () => {
const field = document.createElement('input')
field.value = '100'

expect(determination.parsedValue(field)).toEqual(100.0)
})

it('should parse a positive float', () => {
const field = document.createElement('input')
field.value = '99.99'

expect(determination.parsedValue(field)).toEqual(99.99)
})

it('should return zero for a negative integer', () => {
const field = document.createElement('input')
field.value = '-100'

expect(determination.parsedValue(field)).toEqual(0.0)
})

it('should return zero for a negative float', () => {
const field = document.createElement('input')
field.value = '-99.99'

expect(determination.parsedValue(field)).toEqual(0.0)
})

it('should return zero for a value that cannot be parsed', () => {
const field = document.createElement('input')
field.value = 'test'

expect(determination.parsedValue(field)).toEqual(0.0)
})
})
})
2 changes: 1 addition & 1 deletion app/webpack/packs/govuk-frontend.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '../stylesheets/govuk-frontend.scss'

import { Determination } from '../javascripts/modules/determination.js'
import { Determination } from '../javascripts/modules/determination.mjs'

// fonts & images
require.context('govuk-frontend/govuk/assets', true)
Expand Down
7 changes: 4 additions & 3 deletions spec/support/jasmine-browser.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"srcFiles": [
"app/assets/builds/*.js"
],
"specDir": "spec/javascripts",
"specDir": "./",
"specFiles": [
"**/*[sS]pec.?(m)js"
"app/webpack/javascripts/modules/**/*[sS]pec.?(m)js",
"spec/javascripts/**/*[sS]pec.?(m)js"
],
"helpers": [
"helpers/**/*.?(m)js"
"spec/javascripts/helpers/**/*.?(m)js"
],
"env": {
"stopSpecOnExpectationFailure": false,
Expand Down

0 comments on commit a31fd59

Please sign in to comment.