@@ -9,13 +9,16 @@ import { cloneDeep } from 'lodash'
99import embeddedSingleEntity from './resources/embedded-single-entity'
1010import referenceToSingleEntity from './resources/reference-to-single-entity'
1111import embeddedCollection from './resources/embedded-collection'
12+ import embeddedLinkedCollection from './resources/embedded-linked-collection'
1213import linkedSingleEntity from './resources/linked-single-entity'
1314import linkedCollection from './resources/linked-collection'
1415import collectionFirstPage from './resources/collection-firstPage'
1516import collectionPage1 from './resources/collection-page1'
1617import circularReference from './resources/circular-reference'
1718import multipleReferencesToUser from './resources/multiple-references-to-user'
1819import templatedLink from './resources/templated-link'
20+ import objectProperty from './resources/object-property'
21+ import arrayProperty from './resources/array-property'
1922
2023async function letNetworkRequestFinish ( ) {
2124 await new Promise ( resolve => {
@@ -29,11 +32,9 @@ let vm
2932let stateCopy
3033
3134describe ( 'API store' , ( ) => {
32-
3335 ( [ true , false ] ) . forEach ( avoidNPlusOneRequests => {
3436 const title = avoidNPlusOneRequests ? 'avoiding n+1 queries' : 'not avoiding n+1 queries'
3537 describe ( title , ( ) => {
36-
3738 beforeAll ( ( ) => {
3839 axios . defaults . baseURL = 'http://localhost'
3940 Vue . use ( Vuex )
@@ -112,6 +113,27 @@ describe('API store', () => {
112113 done ( )
113114 } )
114115
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+
115137 it ( 'imports linked single entity' , async done => {
116138 // given
117139 axiosMock . onGet ( 'http://localhost/camps/1' ) . reply ( 200 , linkedSingleEntity . serverResponse )
@@ -474,7 +496,7 @@ describe('API store', () => {
474496 expect ( ( ) => vm . api . get ( { } ) . _meta )
475497
476498 // then
477- . toThrow ( Error )
499+ . toThrow ( Error )
478500 } )
479501
480502 it ( 'purges and later re-fetches a URI from the store' , async done => {
@@ -730,7 +752,7 @@ describe('API store', () => {
730752 const bookResponse = {
731753 id : 555 ,
732754 _embedded : {
733- chapters : [ chapter1Response , chapter2Response , chapter3Response ]
755+ chapters : [ chapter1Response , chapter2Response , chapter3Response ]
734756 } ,
735757 _links : {
736758 self : {
@@ -1317,6 +1339,46 @@ describe('API store', () => {
13171339 // then
13181340 return expect ( load ) . rejects . toThrow ( 'Failed Validation' )
13191341 } )
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+ } )
13201382 } )
13211383 } )
13221384} )
0 commit comments