Skip to content

Commit 6f6d69f

Browse files
Merge pull request #244 from usu/feature/next/array-property
Handle array properties correctly (next branch)
2 parents 398cbbc + 6aed6f8 commit 6f6d69f

File tree

5 files changed

+238
-2
lines changed

5 files changed

+238
-2
lines changed

src/StoreValue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class StoreValue implements Resource {
4040
.forEach(key => {
4141
const value = storeData[key]
4242

43-
// storeData[key] is an embedded collection
44-
if (Array.isArray(value)) {
43+
// storeData[key] is an embedded collection (need min. 1 item to detect an embedded collection)
44+
if (Array.isArray(value) && value.length > 0 && isEntityReference(value[0])) {
4545
// build complete Collection class = EmbeddedCollection + HasItems mixin
4646
const EmbeddedCollectionClass = HasItems(EmbeddedCollection, this.apiActions, this.config, storeData._meta.self, key)
4747

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"serverResponse": {
3+
"id": 1,
4+
"arrayProperty": [
5+
{
6+
"a": 1,
7+
"nested": [
8+
{
9+
"b": 2
10+
}
11+
]
12+
}
13+
],
14+
"emptyArray": [],
15+
"_links": {
16+
"self": {
17+
"href": "/camps/1"
18+
}
19+
}
20+
},
21+
"storeState": {
22+
"/camps/1": {
23+
"id": 1,
24+
"arrayProperty": [
25+
{
26+
"a": 1,
27+
"nested": [
28+
{
29+
"b": 2
30+
}
31+
]
32+
}
33+
],
34+
"emptyArray": [],
35+
"_meta": {
36+
"self": "/camps/1"
37+
}
38+
}
39+
}
40+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"serverResponse": {
3+
"id": 1,
4+
"_embedded": {
5+
"periods": [
6+
{
7+
"id": 104,
8+
"start": "01-01-2019",
9+
"end": "03-01-2019",
10+
"_links": {
11+
"self": {
12+
"href": "/periods/104"
13+
},
14+
"camp": {
15+
"href": "/camps/1"
16+
},
17+
"activities": {
18+
"href": "/periods/104/activities"
19+
}
20+
}
21+
},
22+
{
23+
"id": 128,
24+
"start": "12-04-2019",
25+
"end": "19-04-2019",
26+
"_links": {
27+
"self": {
28+
"href": "/periods/128"
29+
},
30+
"camp": {
31+
"href": "/camps/1"
32+
},
33+
"activities": {
34+
"href": "/periods/128/activities"
35+
}
36+
}
37+
}
38+
]
39+
},
40+
"_links": {
41+
"self": {
42+
"href": "/camps/1"
43+
},
44+
"periods": {
45+
"href": "/camps/1/periods"
46+
}
47+
}
48+
},
49+
"storeState": {
50+
"/periods/104": {
51+
"id": 104,
52+
"start": "01-01-2019",
53+
"end": "03-01-2019",
54+
"camp": {
55+
"href": "/camps/1"
56+
},
57+
"activities": {
58+
"href": "/periods/104/activities"
59+
},
60+
"_meta": {
61+
"self": "/periods/104"
62+
}
63+
},
64+
"/periods/128": {
65+
"id": 128,
66+
"start": "12-04-2019",
67+
"end": "19-04-2019",
68+
"camp": {
69+
"href": "/camps/1"
70+
},
71+
"activities": {
72+
"href": "/periods/128/activities"
73+
},
74+
"_meta": {
75+
"self": "/periods/128"
76+
}
77+
},
78+
"/camps/1": {
79+
"id": 1,
80+
"periods": {
81+
"href": "/camps/1/periods"
82+
},
83+
"_meta": {
84+
"self": "/camps/1"
85+
}
86+
},
87+
"/camps/1/periods": {
88+
"_meta": {
89+
"self": "/camps/1/periods"
90+
},
91+
"items": [
92+
{
93+
"href": "/periods/104"
94+
},
95+
{
96+
"href": "/periods/128"
97+
}
98+
]
99+
}
100+
}
101+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"serverResponse": {
3+
"id": 1,
4+
"objectProperty": {
5+
"a": 1,
6+
"nested": {
7+
"b": 2
8+
}
9+
},
10+
"emptyObject": {},
11+
"_links": {
12+
"self": {
13+
"href": "/camps/1"
14+
}
15+
}
16+
},
17+
"storeState": {
18+
"/camps/1": {
19+
"id": 1,
20+
"objectProperty": {
21+
"a": 1,
22+
"nested": {
23+
"b": 2
24+
}
25+
},
26+
"emptyObject": {},
27+
"_meta": {
28+
"self": "/camps/1"
29+
}
30+
}
31+
}
32+
}

tests/store.spec.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import { cloneDeep } from 'lodash'
99
import embeddedSingleEntity from './resources/embedded-single-entity'
1010
import referenceToSingleEntity from './resources/reference-to-single-entity'
1111
import embeddedCollection from './resources/embedded-collection'
12+
import embeddedLinkedCollection from './resources/embedded-linked-collection'
1213
import linkedSingleEntity from './resources/linked-single-entity'
1314
import linkedCollection from './resources/linked-collection'
1415
import collectionFirstPage from './resources/collection-firstPage'
1516
import collectionPage1 from './resources/collection-page1'
1617
import circularReference from './resources/circular-reference'
1718
import multipleReferencesToUser from './resources/multiple-references-to-user'
1819
import templatedLink from './resources/templated-link'
20+
import objectProperty from './resources/object-property'
21+
import arrayProperty from './resources/array-property'
1922
import root from './resources/root'
2023

2124
import LoadingStoreValue from '../src/LoadingStoreValue'
@@ -143,6 +146,26 @@ describe('API store', () => {
143146
expect(vm.api.get('/periods/128').camp()._meta.self).toEqual('http://localhost/camps/1')
144147
})
145148

149+
it('imports embedded collection with link', async () => {
150+
// given
151+
axiosMock.onGet('http://localhost/camps/1').reply(200, embeddedLinkedCollection.serverResponse)
152+
153+
// when
154+
vm.api.get('/camps/1')
155+
156+
// then
157+
expect(vm.$store.state.api).toMatchObject({ '/camps/1': { _meta: { self: '/camps/1', loading: true } } })
158+
await letNetworkRequestFinish()
159+
expect(vm.$store.state.api).toMatchObject(embeddedLinkedCollection.storeState)
160+
expect(vm.api.get('/camps/1')._meta.self).toEqual('http://localhost/camps/1')
161+
expect(vm.api.get('/camps/1').periods().items[0]._meta.self).toEqual('http://localhost/periods/104')
162+
expect(vm.api.get('/camps/1').periods().items[1]._meta.self).toEqual('http://localhost/periods/128')
163+
expect(vm.api.get('/periods/104')._meta.self).toEqual('http://localhost/periods/104')
164+
expect(vm.api.get('/periods/104').camp()._meta.self).toEqual('http://localhost/camps/1')
165+
expect(vm.api.get('/periods/128')._meta.self).toEqual('http://localhost/periods/128')
166+
expect(vm.api.get('/periods/128').camp()._meta.self).toEqual('http://localhost/camps/1')
167+
})
168+
146169
it('imports linked single entity', async () => {
147170
// given
148171
axiosMock.onGet('http://localhost/camps/1').reply(200, linkedSingleEntity.serverResponse)
@@ -1404,6 +1427,46 @@ describe('API store', () => {
14041427
// then
14051428
await expect(load).rejects.toThrow('Error trying to patch \"/camps/1\" (status 422): Request failed with status code 422')
14061429
})
1430+
1431+
it('can handle object property', async done => {
1432+
// given
1433+
axiosMock.onGet('http://localhost/camps/1').reply(200, objectProperty.serverResponse)
1434+
1435+
// when
1436+
vm.api.get('/camps/1')
1437+
await letNetworkRequestFinish()
1438+
1439+
// then
1440+
expect(vm.$store.state.api).toMatchObject(objectProperty.storeState)
1441+
1442+
expect(vm.api.get('/camps/1').objectProperty).toBeInstanceOf(Object)
1443+
expect(vm.api.get('/camps/1').objectProperty.a).toEqual(1)
1444+
expect(vm.api.get('/camps/1').objectProperty.nested.b).toEqual(2)
1445+
1446+
expect(vm.api.get('/camps/1').emptyObject).toBeInstanceOf(Object)
1447+
expect(vm.api.get('/camps/1').emptyObject).toEqual({})
1448+
done()
1449+
})
1450+
1451+
it('can handle array property', async done => {
1452+
// given
1453+
axiosMock.onGet('http://localhost/camps/1').reply(200, arrayProperty.serverResponse)
1454+
1455+
// when
1456+
vm.api.get('/camps/1')
1457+
await letNetworkRequestFinish()
1458+
1459+
// then
1460+
expect(vm.$store.state.api).toMatchObject(arrayProperty.storeState)
1461+
1462+
expect(vm.api.get('/camps/1').arrayProperty).toBeInstanceOf(Array)
1463+
expect(vm.api.get('/camps/1').arrayProperty[0].a).toEqual(1)
1464+
expect(vm.api.get('/camps/1').arrayProperty[0].nested[0].b).toEqual(2)
1465+
1466+
expect(vm.api.get('/camps/1').emptyArray).toBeInstanceOf(Array)
1467+
expect(vm.api.get('/camps/1').emptyArray).toEqual([])
1468+
done()
1469+
})
14071470
})
14081471
})
14091472
})

0 commit comments

Comments
 (0)