Skip to content

Commit eab82b0

Browse files
committed
Fix constructor in EquipmentCategory, add test
1 parent 80c1159 commit eab82b0

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/models/EquipmentCategory.ts

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

88
constructor(data?: any) {
99
super(data);
10-
this.name = data?.attributes?.name ?? '';
10+
this.name = data?.attributes?.name ?? data?.name ?? '';
1111
}
1212

1313
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 { EquipmentCategory } from "@models/EquipmentCategory";
3+
import { JsonApiSerializer } from '../../src/utils/JsonSerializer';
4+
import { Hydrator } from '../../src/utils/Hydrator';
5+
6+
describe('EquipmentCategory Model', () => {
7+
const newId = 'b9cbbac7-65aa-40d1-aed8-f68b71aa6b6e';
8+
const newCategoryData = {
9+
name: 'Test Category'
10+
};
11+
12+
let newCategory;
13+
let serializer;
14+
15+
beforeEach(() => {
16+
newCategory = new EquipmentCategory(newCategoryData);
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-categories",
25+
attributes: {
26+
name: newCategoryData.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(newCategory.type).toBe('equipment-categories');
41+
expect(newCategory.name).toBe(newCategoryData.name);
42+
});
43+
44+
test('create payload should have correct attributes', () => {
45+
const payload = serializer.buildCreatePayload(newCategory);
46+
verifyPayloadStructure(payload);
47+
});
48+
49+
test('patch payload should have correct attributes', () => {
50+
newCategory.id = newId;
51+
const payload = serializer.buildUpdatePayload(newCategory);
52+
verifyPayloadStructure(payload, true);
53+
});
54+
});

0 commit comments

Comments
 (0)