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
+ } ) ;
0 commit comments