-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added gotOneInspectionPdf to MonkState
- Loading branch information
Showing
6 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/common/test/state/actions/gotOneInspectionPdf.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters