Skip to content

Commit 80c1159

Browse files
committed
Tests for Equipment model
1 parent d9bf3f6 commit 80c1159

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

tests/models/Equipment.test.ts

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 { Equipment } from "@models/Equipment";
3+
import { JsonApiSerializer } from '../../src/utils/JsonSerializer';
4+
import { Hydrator } from '../../src/utils/Hydrator';
5+
6+
describe('Equipment Model', () => {
7+
const newId = 'b9cbbac7-65aa-40d1-aed8-f68b71aa6b6e';
8+
const newEquipmentData = {
9+
serial: '123456',
10+
model: 'a354c5cc-2c9a-44fa-80f6-de3d97946ccb'
11+
};
12+
13+
let newEquipment;
14+
let serializer;
15+
16+
beforeEach(() => {
17+
newEquipment = new Equipment(newEquipmentData);
18+
const hydrator = new Hydrator();
19+
serializer = new JsonApiSerializer(hydrator.getModelMap());
20+
});
21+
22+
const verifyPayloadStructure = (payload, includeId = false) => {
23+
const expectedPayload = {
24+
data: {
25+
type: "equipment-items",
26+
attributes: {
27+
serial: newEquipmentData.serial
28+
},
29+
relationships: {
30+
model: {
31+
data: {
32+
type: "equipment-models",
33+
id: newEquipmentData.model
34+
}
35+
}
36+
}
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(newEquipment.type).toBe('equipment-items');
49+
expect(newEquipment.serial).toBe(newEquipmentData.serial);
50+
expect(newEquipment.model).toBe(newEquipmentData.model);
51+
});
52+
53+
test('create payload should have correct attributes and relationships', () => {
54+
const payload = serializer.buildCreatePayload(newEquipment);
55+
verifyPayloadStructure(payload);
56+
});
57+
58+
test('patch payload should have correct attributes and relationships', () => {
59+
newEquipment.id = newId;
60+
const payload = serializer.buildUpdatePayload(newEquipment);
61+
verifyPayloadStructure(payload, true);
62+
});
63+
});

tests/utils/JsonSerializer/EquipmentAndEquipmentModel.test.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,6 @@ describe('JsonApiSerializer for Equipment and Model', () => {
99
const hydrator = new Hydrator();
1010

1111
describe('buildCreatePayload', () => {
12-
it('should transform equipment input to correct JSONAPI format', () => {
13-
14-
const equipment = new Equipment();
15-
equipment.serial = "TestSerial";
16-
equipment.model = "a354c5cc-2c9a-44fa-80f6-de3d97946ccb";
17-
18-
const payload = (new JsonApiSerializer(hydrator.getModelMap())).buildCreatePayload(equipment);
19-
20-
let expectedPayload = {
21-
data: {
22-
type: "equipment-items",
23-
attributes: {
24-
serial: "TestSerial"
25-
},
26-
relationships: {
27-
model: {
28-
data: {
29-
type: "equipment-models",
30-
id: "a354c5cc-2c9a-44fa-80f6-de3d97946ccb"
31-
}
32-
}
33-
}
34-
}
35-
}
36-
37-
// Exact expected output format
38-
expect(payload).toEqual(expectedPayload);
39-
});
4012

4113
it('should transform a model whcih does not have jsonApiMapping', () => {
4214
const formCategory = new FormCategory();

0 commit comments

Comments
 (0)