Skip to content

Commit f431889

Browse files
committed
Fix constructor in EquipmentManufacturer, add test
1 parent b64932d commit f431889

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/models/EquipmentManufacturer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class EquipmentManufacturer extends BaseModel {
1010

1111
constructor(data?: any) {
1212
super(data);
13-
this.name = data?.attributes?.name ?? '';
13+
this.name = data?.attributes?.name ?? data?.name ?? '';
1414
}
1515

1616
jsonApiMapping() {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { describe, expect, test, beforeEach } from "bun:test";
2+
import { EquipmentManufacturer } from "@models/EquipmentManufacturer";
3+
import { JsonApiSerializer } from '../../src/utils/JsonSerializer';
4+
import { Hydrator } from '../../src/utils/Hydrator';
5+
6+
describe('EquipmentManufacturer Model', () => {
7+
const newId = 'b9cbbac7-65aa-40d1-aed8-f68b71aa6b6e';
8+
const newManufacturerData = {
9+
name: 'Test Manufacturer'
10+
};
11+
12+
let newManufacturer;
13+
let serializer;
14+
15+
beforeEach(() => {
16+
newManufacturer = new EquipmentManufacturer(newManufacturerData);
17+
const hydrator = new Hydrator();
18+
serializer = new JsonApiSerializer(hydrator.getModelMap());
19+
});
20+
21+
const verifyPayloadStructure = (payload, includeId = false) => {
22+
const expectedPayload = {
23+
data: {
24+
type: "equipment-manufacturers",
25+
attributes: {
26+
name: newManufacturerData.name
27+
},
28+
relationships: {}
29+
}
30+
};
31+
32+
if (includeId) {
33+
expectedPayload.data['id'] = newId;
34+
}
35+
36+
expect(payload).toEqual(expectedPayload);
37+
};
38+
39+
test('newly created model should have correct attributes', () => {
40+
expect(newManufacturer.type).toBe('equipment-manufacturers');
41+
expect(newManufacturer.name).toBe(newManufacturerData.name);
42+
});
43+
44+
test('create payload should have correct attributes and relationships', () => {
45+
const payload = serializer.buildCreatePayload(newManufacturer);
46+
verifyPayloadStructure(payload);
47+
});
48+
49+
test('patch payload should have correct attributes and relationships', () => {
50+
newManufacturer.id = newId;
51+
const payload = serializer.buildUpdatePayload(newManufacturer);
52+
verifyPayloadStructure(payload, true);
53+
});
54+
});

0 commit comments

Comments
 (0)