Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmhaig committed Oct 20, 2023
1 parent 98c5d04 commit e2c3f3d
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 42 deletions.
9 changes: 6 additions & 3 deletions app/webpack/javascripts/modules/determination.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ export class Determination {
]

this.fields = fieldIds.map(id => document.querySelector(`#claim_assessment_attributes_${id}`)).filter(field => field)
this.fields.forEach(element => element.addEventListener('change', () => this.calculateTotalRows()))
this.fields.forEach(element => element.addEventListener('change', () => {
this.calculateTotalRows()
return true
}))
}

async calculateTotalRows () {
calculateTotalRows () {
const total = this.fields.reduce((n, field) => n + (parseFloat(field?.value) || 0.0), 0).toFixed(2)
this.applyVat(total).then(data => {
return this.applyVat(total).then(data => {
this.$totalExclVat.innerHTML = data.net_amount
if (this.$totalVat) { this.$totalVat.innerHTML = data.vat_amount }
this.$totalInclVat.innerHTML = data.total_inc_vat
Expand Down
217 changes: 178 additions & 39 deletions app/webpack/javascripts/modules/determination_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,199 @@ import { Determination } from './determination.mjs'

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

beforeEach(() => {
document.body.classList.add('govuk-frontend-supported')
describe('calculateTotalRows', () => {
const inputRow = (id, klass) => {
const row = document.createElement('tr')
const cell = document.createElement('td')
row.append(cell)
const input = document.createElement('input')
input.type = 'text'
input.value = 0.0
input.id = id
if (klass) { input.classList.add(klass) }
cell.append(input)
return row
}

const table = document.createElement('table')
table.id = 'determinations'
table.setAttribute('data-module', 'govuk-determination')
table.setAttribute('data-apply-vat', 'true')
table.setAttribute('data-vat-url', '/vat.json')
table.setAttribute('data-submitted-date', '2023-07-18')
table.setAttribute('data-scheme', 'agfs')
const attributeRow = (id) => {
const row = document.createElement('tr')
const cell = document.createElement('td')
cell.classList.add(id)
cell.innerHTML = 0.0
row.append(cell)
return row
}

determination = new Determination(table)
determination.init()
})
describe('AGFS claim', () => {
beforeEach(() => {
document.body.classList.add('govuk-frontend-supported')

afterEach(() => {
document.body.classList.remove('govuk-frontend-supported')
})
const testArea = document.createElement('div')
testArea.classList.add('test-area')
document.body.appendChild(testArea)

describe('parsedValue', () => {
it('should parse a positive integer', () => {
const field = document.createElement('input')
field.value = '100'
const table = document.createElement('table')
table.id = 'determinations'
table.setAttribute('data-module', 'govuk-determination')
table.setAttribute('data-apply-vat', 'true')
table.setAttribute('data-vat-url', '/vat.json')
table.setAttribute('data-submitted-date', '2023-07-18')
table.setAttribute('data-scheme', 'agfs')

expect(determination.parsedValue(field)).toEqual(100.0)
})
const tableBody = document.createElement('thead')
tableBody.appendChild(inputRow('claim_assessment_attributes_fees'))
tableBody.appendChild(inputRow('claim_assessment_attributes_expenses'))
tableBody.appendChild(attributeRow('js-total-exc-vat-determination'))
tableBody.appendChild(attributeRow('js-vat-determination'))
tableBody.appendChild(attributeRow('js-total-determination'))

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

expect(determination.parsedValue(field)).toEqual(99.99)
})
console.log(testArea)
determination = new Determination(table)
determination.init()

it('should return zero for a negative integer', () => {
const field = document.createElement('input')
field.value = '-100'
fetchSpy = spyOn(window, 'fetch').and.resolveTo(
new Response(
JSON.stringify({
net_amount: '£196.50',
vat_amount: '£39.30',
total_inc_vat: '£235.80'
}),
{ status: 200, statusText: 'OK' }
)
)
})

expect(determination.parsedValue(field)).toEqual(0.0)
})
afterEach(() => {
document.body.classList.remove('govuk-frontend-supported')
document.querySelector('.test-area').remove()
})

it('makes a request to the API', () => {
document.querySelector('#claim_assessment_attributes_fees').value = 3.14
document.querySelector('#claim_assessment_attributes_expenses').value = 2.72

return determination.calculateTotalRows().then(() => {
const searchParams = new URLSearchParams()
searchParams.set('scheme', 'agfs')
searchParams.set('lgfs_vat_amount', 'undefined')
searchParams.set('date', '2023-07-18')
searchParams.set('apply_vat', 'true')
searchParams.set('net_amount', '5.86')

it('should return zero for a negative float', () => {
const field = document.createElement('input')
field.value = '-99.99'
expect(fetchSpy).toHaveBeenCalledWith('/vat.json?' + searchParams)
})
})

expect(determination.parsedValue(field)).toEqual(0.0)
it('sets the net amount', () => {
return determination.calculateTotalRows().then(() => {
const netAmount = document.querySelector('.js-total-exc-vat-determination')

expect(netAmount.innerHTML).toEqual('£196.50')
})
})

it('sets the vat amount', () => {
return determination.calculateTotalRows().then(() => {
const netAmount = document.querySelector('.js-vat-determination')

expect(netAmount.innerHTML).toEqual('£39.30')
})
})

it('sets the total amount', () => {
return determination.calculateTotalRows().then(() => {
const totalAmount = document.querySelector('.js-total-determination')

expect(totalAmount.innerHTML).toEqual('£235.80')
})
})
})

it('should return zero for a value that cannot be parsed', () => {
const field = document.createElement('input')
field.value = 'test'
describe('LGFS claim', () => {
beforeEach(() => {
document.body.classList.add('govuk-frontend-supported')

const testArea = document.createElement('div')
testArea.classList.add('test-area')
document.body.appendChild(testArea)

const table = document.createElement('table')
table.id = 'determinations'
table.setAttribute('data-module', 'govuk-determination')
table.setAttribute('data-apply-vat', 'true')
table.setAttribute('data-vat-url', '/vat.json')
table.setAttribute('data-submitted-date', '2023-07-18')
table.setAttribute('data-scheme', 'lgfs')

const tableBody = document.createElement('thead')
tableBody.appendChild(inputRow('claim_assessment_attributes_fees'))
tableBody.appendChild(inputRow('claim_assessment_attributes_expenses'))
tableBody.appendChild(inputRow('claim_assessment_attributes_disbursements'))
tableBody.appendChild(attributeRow('js-total-exc-vat-determination'))
tableBody.appendChild(inputRow('claim_assessment_attributes_vat_amount', 'js-lgfs-vat-determination'))
tableBody.appendChild(attributeRow('js-total-determination'))

table.appendChild(tableBody)
testArea.appendChild(table)

console.log(testArea)
determination = new Determination(table)
determination.init()

fetchSpy = spyOn(window, 'fetch').and.resolveTo(
new Response(
JSON.stringify({
net_amount: '£196.50',
vat_amount: '£39.30',
total_inc_vat: '£235.80'
}),
{ status: 200, statusText: 'OK' }
)
)
})

afterEach(() => {
document.body.classList.remove('govuk-frontend-supported')
document.querySelector('.test-area').remove()
})

it('makes a request to the API', () => {
document.querySelector('#claim_assessment_attributes_fees').value = 3.14
document.querySelector('#claim_assessment_attributes_vat_amount').value = 1.17
document.querySelector('#claim_assessment_attributes_expenses').value = 2.72

return determination.calculateTotalRows().then(() => {
const searchParams = new URLSearchParams()
searchParams.set('scheme', 'lgfs')
searchParams.set('lgfs_vat_amount', '1.17')
searchParams.set('date', '2023-07-18')
searchParams.set('apply_vat', 'true')
searchParams.set('net_amount', '7.03')

expect(fetchSpy).toHaveBeenCalledWith('/vat.json?' + searchParams)
})
})

it('sets the net amount', () => {
return determination.calculateTotalRows().then(() => {
const netAmount = document.querySelector('.js-total-exc-vat-determination')

expect(netAmount.innerHTML).toEqual('£196.50')
})
})

it('sets the total amount', () => {
return determination.calculateTotalRows().then(() => {
const totalAmount = document.querySelector('.js-total-determination')

expect(determination.parsedValue(field)).toEqual(0.0)
expect(totalAmount.innerHTML).toEqual('£235.80')
})
})
})
})
})

0 comments on commit e2c3f3d

Please sign in to comment.