Skip to content

Commit b64932d

Browse files
committed
Fix constructor in EquipmentExposure, add test
1 parent eab82b0 commit b64932d

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/models/EquipmentExposure.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,30 @@ export class EquipmentExposure extends BaseModel implements Partial<JsonApiMappi
6262
}
6363
}
6464

65+
if (data?.location) {
66+
const locationData = data.location;
67+
this.location = {
68+
type: locationData.type ?? '',
69+
coordinates: locationData.coordinates ?? []
70+
}
71+
}
72+
6573
if (data?.attributes?.ppe) {
66-
const ppeData = data.attributes.ppe;
74+
const ppeData = data.attributes?.ppe;
6775

6876
this.ppe = {
6977
mask: ppeData.ppe?.mask ?? false,
7078
ear_defenders: ppeData.ppe?.ear_defenders ?? false
7179
}
7280
}
81+
82+
if (data?.ppe) {
83+
const ppeData = data.ppe;
84+
85+
this.ppe = {
86+
mask: ppeData.mask ?? false,
87+
ear_defenders: ppeData.ear_defenders ?? false
88+
}
89+
}
7390
}
7491
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)