|
| 1 | +import { describe, expect, test, beforeEach } from "bun:test"; |
| 2 | +import { EquipmentExposure } from "@models/EquipmentExposure"; |
| 3 | +import { JsonApiSerializer } from '../../src/utils/JsonSerializer'; |
| 4 | +import { Hydrator } from '../../src/utils/Hydrator'; |
| 5 | + |
| 6 | +describe('EquipmentExposure Model', () => { |
| 7 | + const newId = 'c8dbbac7-65aa-40d1-aed8-f68b71aa6b6e'; |
| 8 | + const newExposureData = { |
| 9 | + location: { |
| 10 | + type: "Point", |
| 11 | + coordinates: [51.5074, -0.1278] |
| 12 | + }, |
| 13 | + ppe: { |
| 14 | + mask: true, |
| 15 | + ear_defenders: false |
| 16 | + } |
| 17 | + }; |
| 18 | + |
| 19 | + let newExposure; |
| 20 | + let serializer; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + newExposure = new EquipmentExposure(newExposureData); |
| 24 | + const hydrator = new Hydrator(); |
| 25 | + serializer = new JsonApiSerializer(hydrator.getModelMap()); |
| 26 | + }); |
| 27 | + |
| 28 | + const verifyPayloadStructure = (payload, includeId = false) => { |
| 29 | + const expectedPayload = { |
| 30 | + data: { |
| 31 | + type: "equipment-exposures", |
| 32 | + attributes: { |
| 33 | + location: newExposureData.location, |
| 34 | + ppe: newExposureData.ppe |
| 35 | + }, |
| 36 | + relationships: {}, |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + if (includeId) { |
| 41 | + expectedPayload.data['id'] = newId; |
| 42 | + } |
| 43 | + |
| 44 | + expect(payload).toEqual(expectedPayload); |
| 45 | + }; |
| 46 | + |
| 47 | + test('newly created model should have correct attributes', () => { |
| 48 | + expect(newExposure.type).toBe('equipment-exposures'); |
| 49 | + expect(newExposure.location).toEqual(newExposureData.location); |
| 50 | + expect(newExposure.ppe).toEqual(newExposureData.ppe); |
| 51 | + }); |
| 52 | + |
| 53 | + test('create payload should have correct attributes', () => { |
| 54 | + const payload = serializer.buildCreatePayload(newExposure); |
| 55 | + verifyPayloadStructure(payload); |
| 56 | + }); |
| 57 | + |
| 58 | + test('patch payload should have correct attributes', () => { |
| 59 | + newExposure.id = newId; |
| 60 | + const payload = serializer.buildUpdatePayload(newExposure); |
| 61 | + verifyPayloadStructure(payload, true); |
| 62 | + }); |
| 63 | +}); |
0 commit comments