Skip to content

Commit

Permalink
create tests for the mergeSectorAndSubSectorFunction, amend function …
Browse files Browse the repository at this point in the history
…so that it does not include the sector_depends key value pair when there is no value
  • Loading branch information
Richard Pentecost committed Mar 1, 2024
1 parent e1b4bfc commit bed113d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/modules/search/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ const mergeSectorAndSubSectorParams = (requestBody) => {
...(sub_sector_descends ? sub_sector_descends : []),
]

return {
...reqBody,
sector_descends: mergedSectors,
}
return mergedSectors.length
? { ...reqBody, sector_descends: mergedSectors }
: reqBody
}

function search({
Expand Down
70 changes: 70 additions & 0 deletions src/modules/search/transformers/__test__/services.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { mergeSectorAndSubSectorParams } = require('../../services')

describe('mergeSectorAndSubSectorParams', () => {
const basicRequestBody = {
sortby: 'desc',
archived: 'false',
area: [],
uk_postcode: undefined,
}
it('should merge the sector and sub sector params when both are present', () => {
const reqBody = {
...basicRequestBody,
sector_descends: ['sector_uuid1'],
sub_sector_descends: ['sub_sector_uuid1'],
}

expect(mergeSectorAndSubSectorParams(reqBody)).to.deep.equal({
...basicRequestBody,
sector_descends: ['sector_uuid1', 'sub_sector_uuid1'],
})
})

it('should merge all sector and sub sector params when multiple of both are present', () => {
const reqBody = {
...basicRequestBody,
sector_descends: ['sector_uuid1', 'sector_uuid2'],
sub_sector_descends: ['sub_sector_uuid1', 'sub_sector_uuid2'],
}

expect(mergeSectorAndSubSectorParams(reqBody)).to.deep.equal({
...basicRequestBody,
sector_descends: [
'sector_uuid1',
'sector_uuid2',
'sub_sector_uuid1',
'sub_sector_uuid2',
],
})
})

it('should include just the sector params if there are not any sub sector params', () => {
const reqBody = {
...basicRequestBody,
sector_descends: ['sector_uuid1', 'sector_uuid2'],
}

expect(mergeSectorAndSubSectorParams(reqBody)).to.deep.equal({
...basicRequestBody,
sector_descends: ['sector_uuid1', 'sector_uuid2'],
})
})

it('should include just the sub sector params if there are not any sector params', () => {
const reqBody = {
...basicRequestBody,
sub_sector_descends: ['sub_sector_uuid1', 'sub_sector_uuid2'],
}

expect(mergeSectorAndSubSectorParams(reqBody)).to.deep.equal({
...basicRequestBody,
sector_descends: ['sub_sector_uuid1', 'sub_sector_uuid2'],
})
})

it('should not include any sector or sub sector params if neither are present', () => {
expect(mergeSectorAndSubSectorParams(basicRequestBody)).to.deep.equal(
basicRequestBody
)
})
})

0 comments on commit bed113d

Please sign in to comment.