Skip to content

Commit

Permalink
Added gotOneInspectionPdf to MonkState
Browse files Browse the repository at this point in the history
  • Loading branch information
dlymonkai committed Nov 26, 2024
1 parent 840adbc commit d5abe2f
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
60 changes: 60 additions & 0 deletions packages/common/src/state/actions/gotOneInspectionPdf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { MonkAction, MonkActionType } from './monkAction';
import { MonkState } from '../state';

/**
* The payload of a MonkGotOneInspectionPdfPayload.
*/
export interface MonkGotOneInspectionPdfPayload {
/**
* The ID of the inspection to which the PDF was updated.
*/
inspectionId: string;
/**
* The URL of the PDF.
*/
pdfUrl: string;
}

/**
* Action dispatched when a inspection have been updated.
*/
export interface MonkGotOneInspectionPdfAction extends MonkAction {
/**
* The type of the action : `MonkActionType.GOT_ONE_INSPECTION_PDF`.
*/
type: MonkActionType.GOT_ONE_INSPECTION_PDF;
/**
* The payload of the action containing the fetched entities.
*/
payload: MonkGotOneInspectionPdfPayload;
}

/**
* Matcher function that matches a UpdatedOneInspection while also inferring its type using TypeScript's type predicate
* feature.
*/
export function isGotOneInspectionPdfAction(
action: MonkAction,
): action is MonkGotOneInspectionPdfAction {
return action.type === MonkActionType.GOT_ONE_INSPECTION_PDF;
}

/**
* Reducer function for a UpdatedOneInspection action.
*/
export function gotOneInspectionPdf(
state: MonkState,
action: MonkGotOneInspectionPdfAction,
): MonkState {
const { inspections } = state;
const { payload } = action;

const inspection = inspections.find((value) => value.id === payload.inspectionId);
if (inspection) {
inspection.pdfUrl = payload.pdfUrl;
}
return {
...state,
inspections: [...inspections],
};
}
1 change: 1 addition & 0 deletions packages/common/src/state/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './updatedOnePricing';
export * from './updatedOneInspectionAdditionalData';
export * from './createdOneDamage';
export * from './deletedOneDamage';
export * from './gotOneInspectionPdf';
4 changes: 4 additions & 0 deletions packages/common/src/state/actions/monkAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export enum MonkActionType {
* Clear and reset the state.
*/
RESET_STATE = 'reset_state',
/**
* An inspection PDF has been fetched from the API.
*/
GOT_ONE_INSPECTION_PDF = 'got_one_inspection_pdf',
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/common/src/state/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
isCreatedOneDamageAction,
deletedOneDamage,
isDeletedOneDamageAction,
isGotOneInspectionPdfAction,
gotOneInspectionPdf,
} from './actions';
import { MonkState } from './state';

Expand Down Expand Up @@ -62,5 +64,8 @@ export function monkReducer(state: MonkState, action: MonkAction): MonkState {
if (isDeletedOneDamageAction(action)) {
return deletedOneDamage(state, action);
}
if (isGotOneInspectionPdfAction(action)) {
return gotOneInspectionPdf(state, action);
}
return state;
}
49 changes: 49 additions & 0 deletions packages/common/test/state/actions/gotOneInspectionPdf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
createEmptyMonkState,
MonkActionType,
isGotOneInspectionPdfAction,
gotOneInspectionPdf,
MonkGotOneInspectionPdfAction,
} from '../../../src';
import { Inspection } from '@monkvision/types';

const action: MonkGotOneInspectionPdfAction = {
type: MonkActionType.GOT_ONE_INSPECTION_PDF,
payload: {
inspectionId: 'inspections-test-111111',
pdfUrl: 'pdf-url-test',
},
};

describe('gotOneInspectionPdf action handlers', () => {
describe('Action matcher', () => {
it('should return true if the action has the proper type', () => {
expect(isGotOneInspectionPdfAction({ type: MonkActionType.GOT_ONE_INSPECTION_PDF })).toBe(
true,
);
});

it('should return false if the action does not have the proper type', () => {
expect(isGotOneInspectionPdfAction({ type: MonkActionType.RESET_STATE })).toBe(false);
});
});

describe('Action handler', () => {
it('should return a new state', () => {
const state = createEmptyMonkState();
expect(Object.is(gotOneInspectionPdf(state, action), state)).toBe(false);
});

it('should update inspection in the state', () => {
const state = createEmptyMonkState();
state.inspections.push({
id: 'inspections-test-111111',
} as Inspection);
const newState = gotOneInspectionPdf(state, action);
expect(newState.inspections).toContainEqual({
id: action.payload.inspectionId,
pdfUrl: action.payload.pdfUrl,
});
});
});
});
5 changes: 5 additions & 0 deletions packages/common/test/state/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jest.mock('../../src/state/actions', () => ({
isDeletedOneDamageAction: jest.fn(() => false),
isUpdatedOneInspectionAdditionalDataAction: jest.fn(() => false),
isUpdatedVehicleAction: jest.fn(() => false),
isGotOneInspectionPdfAction: jest.fn(() => false),
createdOneImage: jest.fn(() => null),
gotOneInspection: jest.fn(() => null),
resetState: jest.fn(() => null),
Expand All @@ -21,6 +22,7 @@ jest.mock('../../src/state/actions', () => ({
deletedOneDamage: jest.fn(() => null),
updatedOneInspectionAdditionalData: jest.fn(() => null),
updatedVehicle: jest.fn(() => null),
gotOneInspectionPdf: jest.fn(() => null),
}));

import {
Expand All @@ -33,6 +35,7 @@ import {
deletedOneDamage,
updatedOneInspectionAdditionalData,
updatedVehicle,
gotOneInspectionPdf,
isCreatedOneImageAction,
isGotOneInspectionAction,
isResetStateAction,
Expand All @@ -44,6 +47,7 @@ import {
isDeletedOneDamageAction,
isUpdatedOneInspectionAdditionalDataAction,
isUpdatedVehicleAction,
isGotOneInspectionPdfAction,
MonkAction,
monkReducer,
MonkState,
Expand All @@ -66,6 +70,7 @@ const actions = [
handler: updatedOneInspectionAdditionalData,
},
{ matcher: isUpdatedVehicleAction, handler: updatedVehicle },
{ matcher: isGotOneInspectionPdfAction, handler: gotOneInspectionPdf },
] as unknown as { matcher: jest.Mock; handler: jest.Mock; noParams?: boolean }[];

describe('Monk state reducer', () => {
Expand Down

0 comments on commit d5abe2f

Please sign in to comment.