Skip to content

Commit

Permalink
feat(cb2-14394): test filter MOT recalls funciton
Browse files Browse the repository at this point in the history
  • Loading branch information
naathanbrown committed Nov 15, 2024
1 parent c398215 commit 3c3dac0
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/util/recalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import logger from "./logger";
* @returns
*/
export const filterMotRecalls = (vehicleRecalls: MotRecalls) => {
const time = new Date();
const recall = vehicleRecalls.recalls.find((recall) => {
if (recall.repairStatus == "NOT_FIXED" && Date.parse(recall.recallCampaignStartDate) < time.getDate()) {
if (recall.repairStatus == "NOT_FIXED" && Date.parse(recall.recallCampaignStartDate) < Date.now()) {
return recall;
}
});
Expand Down Expand Up @@ -38,7 +37,7 @@ export const getMotRecallsByVin = async (vin: string, cache: Map<string, string
}
})

logger.debug(`first recall response: ${recallResponse}`);
logger.debug(`first recall response: ${JSON.stringify(recallResponse)}`);

if(recallResponse.status == 403 || recallResponse.status == 401) {
const newBearerToken = await getBearerToken(motSecret);
Expand All @@ -54,7 +53,7 @@ export const getMotRecallsByVin = async (vin: string, cache: Map<string, string
"X-API-Key": motSecret.apiKey,
}
});
logger.debug(`second recall response if called: ${recallResponse}`);
logger.debug(`second recall response if called: ${JSON.stringify(recallResponse)}`);
}

return await recallResponse.json()
Expand Down
158 changes: 158 additions & 0 deletions tests/unit/util/recalls.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { MotRecalls } from "../../../src/models/motRecalls";
import { filterMotRecalls } from "../../../src/util/recalls";

describe('Recalls util functions', () => {

beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date());
})

describe('filterMotRecalls', () => {
let date: Date;
const emptyResponse = {
manufacturer: null,
hasRecall: false,
}

beforeEach(() => {
date = new Date()
})

it('should correctly filter a list of 3 when one is a recall', () => {
const recallResponse = {
vin: '1234',
manufacturer: 'test manufacturer',
recalls: [
{
manufacturerCampaignReference: '123ABC',
dvsaCampaignReference: '1234ABC',
recallCampaignStartDate: new Date((date.setDate(date.getDate() - 1))).toISOString().split("T")[0],
repairStatus: "NOT_FIXED"
},
{
manufacturerCampaignReference: '123CBA',
dvsaCampaignReference: '1234CBA',
recallCampaignStartDate: new Date((date.setDate(date.getDate() - 1))).toISOString().split("T")[0],
repairStatus: "FIXED"
},
{
manufacturerCampaignReference: 'CBA123',
dvsaCampaignReference: 'CBA1234',
recallCampaignStartDate: new Date((date.setDate(date.getDate() - 1))).toISOString().split("T")[0],
repairStatus: "FIXED"
}
],
lastUpdatedDate: '123456'
}
const res = filterMotRecalls(recallResponse as MotRecalls)
expect(res).toStrictEqual({
manufacturer: 'test manufacturer',
hasRecall: true,
})
})
it('should correctly filter a list of 3 when two are recalls', () => {
const recallResponse = {
vin: '1234',
manufacturer: 'test manufacturer',
recalls: [
{
manufacturerCampaignReference: '123ABC',
dvsaCampaignReference: '1234ABC',
recallCampaignStartDate: new Date((date.setDate(date.getDate() - 1))).toISOString().split("T")[0],
repairStatus: "NOT_FIXED"
},
{
manufacturerCampaignReference: '123CBA',
dvsaCampaignReference: '1234CBA',
recallCampaignStartDate: new Date((date.setDate(date.getDate() - 1))).toISOString().split("T")[0],
repairStatus: "NOT_FIXED"
},
{
manufacturerCampaignReference: 'CBA123',
dvsaCampaignReference: 'CBA1234',
recallCampaignStartDate: new Date((date.setDate(date.getDate() - 1))).toISOString().split("T")[0],
repairStatus: "FIXED"
}
],
lastUpdatedDate: '123456'
}
const res = filterMotRecalls(recallResponse as MotRecalls)
expect(res).toStrictEqual({
manufacturer: 'test manufacturer',
hasRecall: true,
})
})
it('should filter to nothing when all dates are in the future', () => {
const recallResponse = {
vin: '1234',
manufacturer: 'test manufacturer',
recalls: [
{
manufacturerCampaignReference: '123ABC',
dvsaCampaignReference: '1234ABC',
recallCampaignStartDate: new Date((date.setDate(date.getDate() + 1))).toISOString().split("T")[0],
repairStatus: "NOT_FIXED"
},
{
manufacturerCampaignReference: '123CBA',
dvsaCampaignReference: '1234CBA',
recallCampaignStartDate: new Date((date.setDate(date.getDate() + 2))).toISOString().split("T")[0],
repairStatus: "NOT_FIXED"
}
],
lastUpdatedDate: '123456'
}
const res = filterMotRecalls(recallResponse as MotRecalls)
expect(res).toStrictEqual(emptyResponse)
})
it('should filter to nothing when all vehicles are fixed', () => {
const recallResponse = {
vin: '1234',
manufacturer: 'test manufacturer',
recalls: [
{
manufacturerCampaignReference: '123ABC',
dvsaCampaignReference: '1234ABC',
recallCampaignStartDate: new Date((date.setDate(date.getDate() - 1))).toISOString().split("T")[0],
repairStatus: "FIXED"
},
{
manufacturerCampaignReference: '123CBA',
dvsaCampaignReference: '1234CBA',
recallCampaignStartDate: new Date((date.setDate(date.getDate() - 1))).toISOString().split("T")[0],
repairStatus: "FIXED"
}
],
lastUpdatedDate: '123456'
}
const res = filterMotRecalls(recallResponse as MotRecalls)
expect(res).toStrictEqual(emptyResponse)
})
it('should filter to nothing when all vehicles are fixed and all dates are in the future', () => {
const recallResponse = {
vin: '1234',
manufacturer: 'test manufacturer',
recalls: [
{
manufacturerCampaignReference: '123ABC',
dvsaCampaignReference: '1234ABC',
recallCampaignStartDate: new Date((date.setDate(date.getDate() + 1))).toISOString().split("T")[0],
repairStatus: "FIXED"
},
{
manufacturerCampaignReference: '123CBA',
dvsaCampaignReference: '1234CBA',
recallCampaignStartDate: new Date((date.setDate(date.getDate() + 1))).toISOString().split("T")[0],
repairStatus: "FIXED"
}
],
lastUpdatedDate: '123456'
}
const res = filterMotRecalls(recallResponse as MotRecalls)
expect(res).toStrictEqual(emptyResponse)
})
})


})

0 comments on commit 3c3dac0

Please sign in to comment.