@@ -9,13 +9,16 @@ import { cloneDeep } from 'lodash'
9
9
import embeddedSingleEntity from './resources/embedded-single-entity'
10
10
import referenceToSingleEntity from './resources/reference-to-single-entity'
11
11
import embeddedCollection from './resources/embedded-collection'
12
+ import embeddedLinkedCollection from './resources/embedded-linked-collection'
12
13
import linkedSingleEntity from './resources/linked-single-entity'
13
14
import linkedCollection from './resources/linked-collection'
14
15
import collectionFirstPage from './resources/collection-firstPage'
15
16
import collectionPage1 from './resources/collection-page1'
16
17
import circularReference from './resources/circular-reference'
17
18
import multipleReferencesToUser from './resources/multiple-references-to-user'
18
19
import templatedLink from './resources/templated-link'
20
+ import objectProperty from './resources/object-property'
21
+ import arrayProperty from './resources/array-property'
19
22
20
23
async function letNetworkRequestFinish ( ) {
21
24
await new Promise ( resolve => {
@@ -29,11 +32,9 @@ let vm
29
32
let stateCopy
30
33
31
34
describe ( 'API store' , ( ) => {
32
-
33
35
( [ true , false ] ) . forEach ( avoidNPlusOneRequests => {
34
36
const title = avoidNPlusOneRequests ? 'avoiding n+1 queries' : 'not avoiding n+1 queries'
35
37
describe ( title , ( ) => {
36
-
37
38
beforeAll ( ( ) => {
38
39
axios . defaults . baseURL = 'http://localhost'
39
40
Vue . use ( Vuex )
@@ -112,6 +113,27 @@ describe('API store', () => {
112
113
done ( )
113
114
} )
114
115
116
+ it ( 'imports embedded collection with link' , async done => {
117
+ // given
118
+ axiosMock . onGet ( 'http://localhost/camps/1' ) . reply ( 200 , embeddedLinkedCollection . serverResponse )
119
+
120
+ // when
121
+ vm . api . get ( '/camps/1' )
122
+
123
+ // then
124
+ expect ( vm . $store . state . api ) . toMatchObject ( { '/camps/1' : { _meta : { self : '/camps/1' , loading : true } } } )
125
+ await letNetworkRequestFinish ( )
126
+ expect ( vm . $store . state . api ) . toMatchObject ( embeddedLinkedCollection . storeState )
127
+ expect ( vm . api . get ( '/camps/1' ) . _meta . self ) . toEqual ( 'http://localhost/camps/1' )
128
+ expect ( vm . api . get ( '/camps/1' ) . periods ( ) . items [ 0 ] . _meta . self ) . toEqual ( 'http://localhost/periods/104' )
129
+ expect ( vm . api . get ( '/camps/1' ) . periods ( ) . items [ 1 ] . _meta . self ) . toEqual ( 'http://localhost/periods/128' )
130
+ expect ( vm . api . get ( '/periods/104' ) . _meta . self ) . toEqual ( 'http://localhost/periods/104' )
131
+ expect ( vm . api . get ( '/periods/104' ) . camp ( ) . _meta . self ) . toEqual ( 'http://localhost/camps/1' )
132
+ expect ( vm . api . get ( '/periods/128' ) . _meta . self ) . toEqual ( 'http://localhost/periods/128' )
133
+ expect ( vm . api . get ( '/periods/128' ) . camp ( ) . _meta . self ) . toEqual ( 'http://localhost/camps/1' )
134
+ done ( )
135
+ } )
136
+
115
137
it ( 'imports linked single entity' , async done => {
116
138
// given
117
139
axiosMock . onGet ( 'http://localhost/camps/1' ) . reply ( 200 , linkedSingleEntity . serverResponse )
@@ -474,7 +496,7 @@ describe('API store', () => {
474
496
expect ( ( ) => vm . api . get ( { } ) . _meta )
475
497
476
498
// then
477
- . toThrow ( Error )
499
+ . toThrow ( Error )
478
500
} )
479
501
480
502
it ( 'purges and later re-fetches a URI from the store' , async done => {
@@ -730,7 +752,7 @@ describe('API store', () => {
730
752
const bookResponse = {
731
753
id : 555 ,
732
754
_embedded : {
733
- chapters : [ chapter1Response , chapter2Response , chapter3Response ]
755
+ chapters : [ chapter1Response , chapter2Response , chapter3Response ]
734
756
} ,
735
757
_links : {
736
758
self : {
@@ -1317,6 +1339,46 @@ describe('API store', () => {
1317
1339
// then
1318
1340
return expect ( load ) . rejects . toThrow ( 'Failed Validation' )
1319
1341
} )
1342
+
1343
+ it ( 'can handle object property' , async done => {
1344
+ // given
1345
+ axiosMock . onGet ( 'http://localhost/camps/1' ) . reply ( 200 , objectProperty . serverResponse )
1346
+
1347
+ // when
1348
+ vm . api . get ( '/camps/1' )
1349
+ await letNetworkRequestFinish ( )
1350
+
1351
+ // then
1352
+ expect ( vm . $store . state . api ) . toMatchObject ( objectProperty . storeState )
1353
+
1354
+ expect ( vm . api . get ( '/camps/1' ) . objectProperty ) . toBeInstanceOf ( Object )
1355
+ expect ( vm . api . get ( '/camps/1' ) . objectProperty . a ) . toEqual ( 1 )
1356
+ expect ( vm . api . get ( '/camps/1' ) . objectProperty . nested . b ) . toEqual ( 2 )
1357
+
1358
+ expect ( vm . api . get ( '/camps/1' ) . emptyObject ) . toBeInstanceOf ( Object )
1359
+ expect ( vm . api . get ( '/camps/1' ) . emptyObject ) . toEqual ( { } )
1360
+ done ( )
1361
+ } )
1362
+
1363
+ it ( 'can handle array property' , async done => {
1364
+ // given
1365
+ axiosMock . onGet ( 'http://localhost/camps/1' ) . reply ( 200 , arrayProperty . serverResponse )
1366
+
1367
+ // when
1368
+ vm . api . get ( '/camps/1' )
1369
+ await letNetworkRequestFinish ( )
1370
+
1371
+ // then
1372
+ expect ( vm . $store . state . api ) . toMatchObject ( arrayProperty . storeState )
1373
+
1374
+ expect ( vm . api . get ( '/camps/1' ) . arrayProperty ) . toBeInstanceOf ( Array )
1375
+ expect ( vm . api . get ( '/camps/1' ) . arrayProperty [ 0 ] . a ) . toEqual ( 1 )
1376
+ expect ( vm . api . get ( '/camps/1' ) . arrayProperty [ 0 ] . nested [ 0 ] . b ) . toEqual ( 2 )
1377
+
1378
+ expect ( vm . api . get ( '/camps/1' ) . emptyArray ) . toBeInstanceOf ( Array )
1379
+ expect ( vm . api . get ( '/camps/1' ) . emptyArray ) . toEqual ( [ ] )
1380
+ done ( )
1381
+ } )
1320
1382
} )
1321
1383
} )
1322
1384
} )
0 commit comments