Skip to content

Commit 065b48c

Browse files
Merge pull request #244 from rayfoss/patch-3
Current getter should support id 0
2 parents 3b2c63d + 2bc018e commit 065b48c

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/service-module/getters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default function makeServiceGetters (servicePath) {
5555
return keyedById[id] ? select(params, idField)(keyedById[id]) : undefined
5656
},
5757
current (state) {
58-
return state.currentId ? state.keyedById[state.currentId] : null
58+
return state.currentId === null ? null : state.keyedById[state.currentId]
5959
},
6060
getCopy (state) {
6161
return state.copy ? state.copy : null

test/service-module/getters.test.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ const options = {
88
autoRemove: false
99
}
1010

11-
const { find, list } = makeServiceGetters('todos', options)
11+
const { find, list, current } = makeServiceGetters('todos', options)
1212
const { addItems } = makeServiceMutations('todos', options)
1313

1414
describe('Service Module - Getters', function () {
1515
beforeEach(function () {
1616
const state = makeServiceState('todos', options)
1717
this.items = [
18+
{ _id: 0, otherField: true, test: true },
1819
{ _id: 1, otherField: true, test: true },
1920
{
2021
_id: 2,
@@ -35,6 +36,7 @@ describe('Service Module - Getters', function () {
3536
}
3637
]
3738
addItems(state, this.items)
39+
state.currentId = 0
3840
this.state = state
3941
})
4042

@@ -45,6 +47,13 @@ describe('Service Module - Getters', function () {
4547
assert.deepEqual(results, items, 'the list was correct')
4648
})
4749

50+
it('current with id 0', function () {
51+
const { state, items } = this
52+
const result = current(state)
53+
54+
assert.deepEqual(result, items[0], 'current was correct')
55+
})
56+
4857
it('find', function () {
4958
const { state, items } = this
5059
const params = { query: {} }
@@ -53,7 +62,7 @@ describe('Service Module - Getters', function () {
5362
assert.deepEqual(results.data, items, 'the list was correct')
5463
assert(results.limit === 0, 'limit was correct')
5564
assert(results.skip === 0, 'skip was correct')
56-
assert(results.total === 3, 'total was correct')
65+
assert(results.total === 4, 'total was correct')
5766
})
5867

5968
it('find with query', function () {
@@ -148,23 +157,23 @@ describe('Service Module - Getters', function () {
148157
const results = find(state)(params)
149158

150159
assert(results.data.length === 1, 'the length was correct')
151-
assert(results.data[0]._id === 1, 'the correct record was returned')
160+
assert(results.data[0]._id === 0, 'the correct record was returned')
152161
assert(results.limit === 1, 'limit was correct')
153162
assert(results.skip === 0, 'skip was correct')
154-
assert(results.total === 3, 'total was correct')
163+
assert(results.total === 4, 'total was correct')
155164
})
156165

157166
it('find with skip', function () {
158167
const { state } = this
159168
const params = { query: { $skip: 1 } }
160169
const results = find(state)(params)
161170

162-
assert(results.data.length === 2, 'the length was correct')
163-
assert(results.data[0]._id === 2, 'the correct record was returned')
164-
assert(results.data[1]._id === 3, 'the correct record was returned')
171+
assert(results.data.length === 3, 'the length was correct')
172+
assert(results.data[0]._id === 1, 'the correct record was returned')
173+
assert(results.data[1]._id === 2, 'the correct record was returned')
165174
assert(results.limit === 0, 'limit was correct')
166175
assert(results.skip === 1, 'skip was correct')
167-
assert(results.total === 3, 'total was correct')
176+
assert(results.total === 4, 'total was correct')
168177
})
169178

170179
it('find with limit and skip', function () {
@@ -173,24 +182,24 @@ describe('Service Module - Getters', function () {
173182
const results = find(state)(params)
174183

175184
assert(results.data.length === 1, 'the length was correct')
176-
assert(results.data[0]._id === 2, 'the correct record was returned')
185+
assert(results.data[0]._id === 1, 'the correct record was returned')
177186
assert(results.limit === 1, 'limit was correct')
178187
assert(results.skip === 1, 'skip was correct')
179-
assert(results.total === 3, 'total was correct')
188+
assert(results.total === 4, 'total was correct')
180189
})
181190

182191
it('find with select', function () {
183192
const { state } = this
184193
const params = { query: { $select: ['otherField'] } }
185194
const results = find(state)(params)
186195

187-
assert(results.data.length === 3, 'the length was correct')
196+
assert(results.data.length === 4, 'the length was correct')
188197
results.data.forEach(result => {
189198
assert(Object.keys(result).length === 1, 'only one field was returned')
190199
assert(result.otherField, 'the correct field was returned')
191200
})
192201
assert(results.limit === 0, 'limit was correct')
193202
assert(results.skip === 0, 'skip was correct')
194-
assert(results.total === 3, 'total was correct')
203+
assert(results.total === 4, 'total was correct')
195204
})
196205
})

0 commit comments

Comments
 (0)