Skip to content

Commit

Permalink
Reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmhaig committed Dec 13, 2023
1 parent 6315cda commit df855cd
Showing 1 changed file with 72 additions and 98 deletions.
170 changes: 72 additions & 98 deletions app/webpack/javascripts/modules/determination_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ describe('Determination', () => {
let fetchSpy = null

describe('calculateTotalRows', () => {
const createTestArea = ({ content }) => {
const testArea = document.createElement('div')
testArea.classList.add('test-area')
document.body.appendChild(testArea)
testArea.append(content)

determination = new Determination(content)
determination.init()

return testArea
}

const createTable = ({ scheme, body }) => {
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', scheme)
table.appendChild(body)

return table
}

const inputRow = (id, klass) => {
const row = document.createElement('tr')
const cell = document.createElement('td')
Expand All @@ -27,67 +52,62 @@ describe('Determination', () => {
return row
}

describe('AGFS claim', () => {
beforeEach(() => {
document.body.classList.add('govuk-frontend-supported')
const itMakesARequestToTheAPI = (params) => {
document.querySelector('#claim_assessment_attributes_fees').value = params.fees
document.querySelector('#claim_assessment_attributes_expenses').value = params.expenses
let vatAmountParam
if (params.vat_amount) {
document.querySelector('#claim_assessment_attributes_vat_amount').value = params.vat_amount
vatAmountParam = `${params.vat_amount}`
}
const netAmountParam = `${params.fees + params.expenses + (params.vat_amount || 0)}`

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

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

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

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' }
)
)
})

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')
afterEach(() => {
document.body.classList.remove('govuk-frontend-supported')
document.querySelector('.test-area').remove()
})

const tableBody = document.createElement('thead')
describe('AGFS claim', () => {
beforeEach(() => {
const tableBody = document.createElement('tbody')
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'))

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()
createTestArea({ content: createTable({ scheme: 'agfs', body: tableBody }) })
})

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')

expect(fetchSpy).toHaveBeenCalledWith('/vat.json?' + searchParams)
})
itMakesARequestToTheAPI({ scheme: 'agfs', fees: 3.14, expenses: 2.72 })
})

it('sets the net amount', () => {
Expand Down Expand Up @@ -119,65 +139,19 @@ describe('Determination', () => {
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')
const tableBody = document.createElement('tbody')
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()
createTestArea({ content: createTable({ scheme: 'lgfs', body: tableBody }) })
})

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)
})
itMakesARequestToTheAPI({ scheme: 'lgfs', fees: 3.14, expenses: 2.72, vat_amount: 1.17 })
})

it('sets the net amount', () => {
Expand Down

0 comments on commit df855cd

Please sign in to comment.