1
+ import { describe , it , expect } from "bun:test" ;
2
+ import { JsonApiSerializer } from "@utils/JsonSerializer" ;
3
+ import { Hydrator } from '@utils/Hydrator' ;
4
+ import { CustomerAccount , Property , Contact } from '../../../src' ;
5
+
6
+ describe ( 'JsonApiSerializer for Customer Account and Relationships' , ( ) => {
7
+ const hydrator = new Hydrator ( ) ;
8
+ const jsonApiSerializer = new JsonApiSerializer ( hydrator . getModelMap ( ) ) ;
9
+
10
+ describe ( 'buildCreatePayload' , ( ) => {
11
+ it ( 'should transform customer account and relationships to correct JSONAPI format' , ( ) => {
12
+
13
+ const customerAccount = new CustomerAccount ( ) ;
14
+ customerAccount . id = "cust-123" ;
15
+
16
+ const property1 = new Property ( ) ;
17
+ property1 . id = "prop-001" ;
18
+ property1 . uprn = 10023456 ;
19
+ property1 . address = {
20
+ description : "Main Office" ,
21
+ department : "Operations" ,
22
+ organisation : "ABC Ltd" ,
23
+ number : "42" ,
24
+ name : "" ,
25
+ thoroughfare : "High Street" ,
26
+ dependent_thoroughfare : "" ,
27
+ post_town : "London" ,
28
+ postcode : "W1A 1AA" ,
29
+ pobox : "" ,
30
+ country : "UK"
31
+ } ;
32
+
33
+ const property2 = new Property ( ) ;
34
+ property2 . id = "prop-002" ;
35
+ property2 . uprn = 10023457 ;
36
+ property2 . address = {
37
+ description : "Warehouse" ,
38
+ department : "Logistics" ,
39
+ organisation : "ABC Ltd" ,
40
+ number : "15" ,
41
+ name : "Industrial Estate" ,
42
+ thoroughfare : "Factory Road" ,
43
+ dependent_thoroughfare : "" ,
44
+ post_town : "Manchester" ,
45
+ postcode : "M1 1BB" ,
46
+ pobox : "" ,
47
+ country : "UK"
48
+ } ;
49
+
50
+ // Create contacts
51
+ const contact1 = new Contact ( ) ;
52
+ contact1 . id = "cont-001" ;
53
+ contact1 . first_name = "John" ;
54
+ contact1 . last_name = "Smith" ;
55
+ contact1 . email = "[email protected] " ;
56
+ contact1 . telephone = "07700 900123" ;
57
+
58
+ const contact2 = new Contact ( ) ;
59
+ contact2 . id = "cont-002" ;
60
+ contact2 . first_name = "Jane" ;
61
+ contact2 . last_name = "Doe" ;
62
+ contact2 . email = "[email protected] " ;
63
+ contact2 . telephone = "07700 900456" ;
64
+
65
+ customerAccount . contacts = [ contact1 , contact2 ] ;
66
+ customerAccount . properties = [ property1 , property2 ] ;
67
+
68
+ const payload = ( new JsonApiSerializer ( hydrator . getModelMap ( ) ) ) . buildCreatePayload ( customerAccount ) ;
69
+ const { data } = payload ;
70
+
71
+ expect ( data . type ) . toBe ( "customer-accounts" ) ;
72
+ expect ( data . relationships ) . toBeDefined ( ) ;
73
+ expect ( data . relationships . contacts ) . toBeDefined ( ) ;
74
+ expect ( data . relationships . properties ) . toBeDefined ( ) ;
75
+
76
+ expect ( data . relationships ) . toEqual ( {
77
+ "properties" :{
78
+ "data" :[
79
+ {
80
+ "type" :"properties" ,
81
+ "id" :"prop-001"
82
+ } ,
83
+ {
84
+ "type" :"properties" ,
85
+ "id" :"prop-002"
86
+ }
87
+ ]
88
+ } ,
89
+ "contacts" :{
90
+ "data" :[
91
+ {
92
+ "type" :"contacts" ,
93
+ "id" :"cont-001"
94
+ } ,
95
+ {
96
+ "type" :"contacts" ,
97
+ "id" :"cont-002"
98
+ }
99
+ ]
100
+ }
101
+ } ) ;
102
+ } ) ;
103
+ } ) ;
104
+ } ) ;
0 commit comments