Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall committed Dec 12, 2024
1 parent 7e7c2cf commit 0dba092
Showing 1 changed file with 113 additions and 1 deletion.
114 changes: 113 additions & 1 deletion test/unit/middleware/datasetTaskList.middleware.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, vi, expect } from 'vitest'
import { prepareDatasetTaskListTemplateParams, prepareDatasetTaskListErrorTemplateParams } from '../../../src/middleware/datasetTaskList.middleware.js'
import { prepareDatasetTaskListTemplateParams, prepareDatasetTaskListErrorTemplateParams, prepareTasks } from '../../../src/middleware/datasetTaskList.middleware.js'
import performanceDbApi from '../../../src/services/performanceDbApi.js'

vi.mock('../../../src/services/performanceDbApi.js')

Expand Down Expand Up @@ -54,4 +55,115 @@ describe('datasetTaskList.middleware.js', () => {
expect(templateParams.errorData.latest_200_date).toMatch(dataTimeRegex)
})
})

describe('prepareTasks', () => {
it('prepares the task list with issues', async () => {
const req = {
parsedParams: {
lpa: 'some-lpa',
dataset: 'some-dataset'
},
entities: ['entity1', 'entity2'],
resources: [{ entry_count: 10 }],
entryIssueCounts: [{ field: 'field1', issue_type: 'issue-type1' }],
entityIssueCounts: [{ field: 'field2', issue_type: 'issue-type2' }]
}

const res = {
status: vi.fn()
}

const next = vi.fn()

prepareTasks(req, res, next)

expect(performanceDbApi.getTaskMessage).toHaveBeenCalledWith({
issue_type: 'issue-type1',
num_issues: 1,
rowCount: 2,
field: 'field1'
})

expect(performanceDbApi.getTaskMessage).toHaveBeenCalledWith({
issue_type: 'issue-type2',
num_issues: 1,
rowCount: 2,
field: 'field2'
})

expect(req.taskList).toEqual([
{
title: {
text: undefined
},
href: '/organisations/some-lpa/some-dataset/issue-type1/field1',
status: {
tag: {
classes: 'govuk-tag--yellow',
text: 'Needs fixing'
}
}
},
{
title: {
text: undefined
},
href: '/organisations/some-lpa/some-dataset/issue-type2/field2',
status: {
tag: {
classes: 'govuk-tag--yellow',
text: 'Needs fixing'
}
}
}
])

expect(next).toHaveBeenCalledTimes(1)
})

it('uses resource row count for special issue types', async () => {
const req = {
parsedParams: {
lpa: 'some-lpa',
dataset: 'some-dataset'
},
entities: [],
resources: [{ entry_count: 10 }],
entryIssueCounts: [{ field: 'field1', issue_type: 'reference values are not unique' }],
entityIssueCounts: []
}

const res = {
status: vi.fn()
}

const next = vi.fn()

prepareTasks(req, res, next)

expect(performanceDbApi.getTaskMessage).toHaveBeenCalledWith({
issue_type: 'reference values are not unique',
num_issues: 1,
rowCount: 10,
field: 'field1'
})

expect(req.taskList).toEqual([
{
title: {
text: undefined
},
href: '/organisations/some-lpa/some-dataset/reference values are not unique/field1',
status: {
tag: {
classes: 'govuk-tag--yellow',
text: 'Needs fixing'
}
}
}
])

expect(next).toHaveBeenCalledTimes(1)
})
})
})

0 comments on commit 0dba092

Please sign in to comment.