|
| 1 | +import { Damage, Image, Inspection, Part, RenderedOutput, View } from '@monkvision/types'; |
| 2 | +import { |
| 3 | + createEmptyMonkState, |
| 4 | + deletedOneImage, |
| 5 | + isDeletedOneImageAction, |
| 6 | + MonkActionType, |
| 7 | + MonkDeletedOneImageAction, |
| 8 | +} from '../../../src'; |
| 9 | + |
| 10 | +const action: MonkDeletedOneImageAction = { |
| 11 | + type: MonkActionType.DELETED_ONE_IMAGE, |
| 12 | + payload: { |
| 13 | + inspectionId: 'inspections-test', |
| 14 | + imageId: 'image-id-test', |
| 15 | + }, |
| 16 | +}; |
| 17 | + |
| 18 | +describe('DeletedOneImage action handlers', () => { |
| 19 | + describe('Action matcher', () => { |
| 20 | + it('should return true if the action has the proper type', () => { |
| 21 | + expect(isDeletedOneImageAction({ type: MonkActionType.DELETED_ONE_IMAGE })).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return false if the action does not have the proper type', () => { |
| 25 | + expect(isDeletedOneImageAction({ type: MonkActionType.RESET_STATE })).toBe(false); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('Action handler', () => { |
| 30 | + it('should return a new state', () => { |
| 31 | + const state = createEmptyMonkState(); |
| 32 | + expect(Object.is(deletedOneImage(state, action), state)).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should delete image in the state', () => { |
| 36 | + const state = createEmptyMonkState(); |
| 37 | + const outputId = 'rendered-output-id-test'; |
| 38 | + const damageId = 'damage-id-test'; |
| 39 | + const partId = 'part-id-test'; |
| 40 | + const viewId = 'view-id-test'; |
| 41 | + state.inspections.push({ |
| 42 | + id: action.payload.inspectionId, |
| 43 | + images: [action.payload.imageId] as string[], |
| 44 | + } as Inspection); |
| 45 | + state.images.push({ |
| 46 | + id: action.payload.imageId, |
| 47 | + inspectionId: action.payload.inspectionId, |
| 48 | + views: [viewId], |
| 49 | + renderedOutputs: [outputId], |
| 50 | + } as Image); |
| 51 | + state.damages.push({ |
| 52 | + id: damageId, |
| 53 | + relatedImages: [action.payload.imageId], |
| 54 | + inspectionId: action.payload.inspectionId, |
| 55 | + } as Damage); |
| 56 | + state.parts.push({ |
| 57 | + id: partId, |
| 58 | + relatedImages: [action.payload.imageId], |
| 59 | + inspectionId: action.payload.inspectionId, |
| 60 | + } as Part); |
| 61 | + state.renderedOutputs.push({ |
| 62 | + id: outputId, |
| 63 | + baseImageId: action.payload.imageId, |
| 64 | + } as RenderedOutput); |
| 65 | + state.views.push({ |
| 66 | + id: viewId, |
| 67 | + renderedOutputs: [outputId], |
| 68 | + } as View); |
| 69 | + const newState = deletedOneImage(state, action); |
| 70 | + const inspectionImages = newState.inspections.find( |
| 71 | + (ins) => ins.id === action.payload.inspectionId, |
| 72 | + )?.images; |
| 73 | + |
| 74 | + expect(inspectionImages?.length).toBe(0); |
| 75 | + expect(inspectionImages).not.toContainEqual(action.payload.imageId); |
| 76 | + expect(newState.images.length).toBe(0); |
| 77 | + expect(newState.damages.length).toBe(1); |
| 78 | + expect(newState.parts.length).toBe(1); |
| 79 | + expect(newState.views.length).toBe(1); |
| 80 | + expect( |
| 81 | + newState.damages.find((damage) => damage.relatedImages.includes(action.payload.imageId)), |
| 82 | + ).toBeUndefined(); |
| 83 | + expect( |
| 84 | + newState.parts.find((part) => part.relatedImages.includes(action.payload.imageId)), |
| 85 | + ).toBeUndefined(); |
| 86 | + expect(newState.renderedOutputs.length).toBe(0); |
| 87 | + expect( |
| 88 | + newState.renderedOutputs.find((output) => output.baseImageId === action.payload.imageId), |
| 89 | + ).toBeUndefined(); |
| 90 | + expect( |
| 91 | + newState.views.find((view) => |
| 92 | + view.renderedOutputs.find((id) => id === action.payload.imageId), |
| 93 | + ), |
| 94 | + ).toBeUndefined(); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments