1
+ import * as sinon from 'sinon' ;
2
+ import { expect } from 'chai' ;
3
+ import { store } from '../../ds' ;
4
+ import * as Resources from '../../resources' ;
5
+
6
+ function respondJson ( route : string , content ?: any , statusCode ?: number ) : any {
7
+ let [ method , endpoint ] = route . split ( ' ' )
8
+ let reqPointer : any = { } ;
9
+
10
+ this . respondWith ( method , `api/${ endpoint } .json` , function ( request : any ) {
11
+ let body = null ;
12
+ try { body = JSON . parse ( request . requestBody ) ; } catch ( e ) { }
13
+ reqPointer . body = body ;
14
+ reqPointer . headers = request . requestHeaders ;
15
+
16
+ request . respond (
17
+ statusCode || 200 ,
18
+ { 'Content-Type' : 'application/json' } ,
19
+ JSON . stringify ( content || null )
20
+ ) ;
21
+ } ) ;
22
+
23
+ return reqPointer ;
24
+ }
25
+
26
+ describe ( 'CREATE' , ( ) => {
27
+ let requests :Array < any > = [ ] ;
28
+ let server :any = null
29
+
30
+ beforeEach ( ( ) => {
31
+ server = sinon . fakeServer . create ( ) ;
32
+ server . autoRespond = true ;
33
+ server . answer = respondJson ;
34
+ } )
35
+
36
+ afterEach ( ( ) => {
37
+ server . restore ( ) ;
38
+ } )
39
+
40
+ describe ( 'when creating a simple object with a belongsTo link' , ( ) => {
41
+ const
42
+ MAPPER_NAME :string = 'Article' ,
43
+ MAPPER_NAME_LINK :string = 'User' ,
44
+ RESPONSE_ID :string = '99633a09-9047-41bf-955f-4a8d72a38d58' ,
45
+ RECORD :any = {
46
+ title : 'hello' ,
47
+ content : 'what\'s up ?' ,
48
+ authorId : 'e81fea3d-6379-4137-8068-7d70a90a1a7c'
49
+ } ,
50
+ RESPONSE :any = {
51
+ data : {
52
+ id : RESPONSE_ID ,
53
+ type : MAPPER_NAME ,
54
+ attributes : {
55
+ title : RECORD . title ,
56
+ content : RECORD . content
57
+ }
58
+ }
59
+ }
60
+
61
+ let data :any = null ;
62
+ let req :any = null ;
63
+
64
+ beforeEach ( ( ) => {
65
+ req = server . answer ( 'POST articles' , RESPONSE ) ;
66
+ return store . create ( 'Article' , RECORD ) . then ( ( _data ) => {
67
+ data = _data
68
+ } ) ;
69
+ } )
70
+
71
+ afterEach ( ( ) => {
72
+ data = req = null ;
73
+ } )
74
+
75
+ it ( 'the request should include the correct JSONApi structure' , ( ) => {
76
+ expect ( req . body ) . to . be . an ( 'object' ) ;
77
+ expect ( req . body . data ) . to . be . an ( 'object' ) ;
78
+ expect ( req . body . data . type ) . to . equal ( 'Article' ) ;
79
+ expect ( req . body . data . attributes ) . to . be . an ( 'object' ) ;
80
+ } ) ;
81
+
82
+ it ( 'the request should contain correct attributes' , ( ) => {
83
+ expect ( req . body . data . attributes ) . to . be . deep . equal ( {
84
+ title : RECORD . title ,
85
+ content : RECORD . content
86
+ } ) ;
87
+ } ) ;
88
+
89
+ it ( 'the request should also get a relationships object' , ( ) => {
90
+ expect ( req . body . data . relationships ) . to . be . an ( 'object' ) ;
91
+ expect ( req . body . data . relationships . author ) . to . be . an ( 'object' ) ;
92
+ expect ( req . body . data . relationships . author . data ) . to . be . deep . equal ( {
93
+ type : 'User' ,
94
+ id : RECORD . authorId
95
+ } ) ;
96
+ } ) ;
97
+
98
+ it ( 'the response' )
99
+
100
+ it ( 'the record should be injected in the datastore' , ( ) => {
101
+ let articles :Array < any > = store . getAll ( 'Article' ) ;
102
+ expect ( articles ) . to . have . lengthOf ( 1 ) ;
103
+ expect ( articles [ 0 ] . id ) . to . equal ( RESPONSE_ID ) ;
104
+ expect ( articles [ 0 ] . title ) . to . equal ( RECORD . title ) ;
105
+ expect ( articles [ 0 ] . content ) . to . equal ( RECORD . content ) ;
106
+ } ) ;
107
+ } )
108
+ } ) ;
0 commit comments