Skip to content

Commit c5fc60d

Browse files
committed
add service tests for count getter/action
1 parent 54970a5 commit c5fc60d

File tree

3 files changed

+117
-48
lines changed

3 files changed

+117
-48
lines changed

test/service-module/model-methods.test.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,18 @@ function makeContext() {
129129

130130
export { makeContext }
131131

132-
describe('Models - Methods', function() {
132+
describe('Models - Methods', function () {
133133
beforeEach(() => {
134134
clearModels()
135135
})
136136

137-
it('Model.find is a function', function() {
137+
it('Model.find is a function', function () {
138138
const { Task } = makeContext()
139139

140140
assert(typeof Task.find === 'function')
141141
})
142142

143-
it('Model.find returns a Promise', function() {
143+
it('Model.find returns a Promise', function () {
144144
const { Task } = makeContext()
145145
const result = Task.find()
146146
assert(typeof result.then !== 'undefined')
@@ -149,46 +149,46 @@ describe('Models - Methods', function() {
149149
})
150150
})
151151

152-
it('Model.findInStore', function() {
152+
it('Model.findInStore', function () {
153153
const { Task } = makeContext()
154154

155155
assert(typeof Task.findInStore === 'function')
156156
})
157157

158-
it('Model.count is a function', function() {
158+
it('Model.count is a function', function () {
159159
const { Task } = makeContext()
160160

161161
assert(typeof Task.count === 'function')
162162
})
163163

164-
it('Model.count returns a Promise', function() {
164+
it('Model.count returns a Promise', function () {
165165
const { Task } = makeContext()
166-
const result = Task.count()
166+
const result = Task.count({ query: {} })
167167
assert(typeof result.then !== 'undefined')
168168
result.catch(err => {
169169
/* noop -- prevents UnhandledPromiseRejectionWarning */
170170
})
171171
})
172172

173-
it('Model.countInStore', function() {
173+
it('Model.countInStore', function () {
174174
const { Task } = makeContext()
175175

176176
assert(typeof Task.countInStore === 'function')
177177
})
178178

179-
it('Model.get', function() {
179+
it('Model.get', function () {
180180
const { Task } = makeContext()
181181

182182
assert(typeof Task.get === 'function')
183183
})
184184

185-
it('Model.getFromStore', function() {
185+
it('Model.getFromStore', function () {
186186
const { Task } = makeContext()
187187

188188
assert(typeof Task.getFromStore === 'function')
189189
})
190190

191-
it('allows listening to Feathers events on Model', function(done) {
191+
it('allows listening to Feathers events on Model', function (done) {
192192
const { Letter } = makeContext()
193193

194194
Letter.on('created', data => {
@@ -203,7 +203,7 @@ describe('Models - Methods', function() {
203203
}).save()
204204
})
205205

206-
it('instance.save calls create with correct arguments', function() {
206+
it('instance.save calls create with correct arguments', function () {
207207
const { Task } = makeContext()
208208
const task = new Task({ test: true })
209209

@@ -220,7 +220,7 @@ describe('Models - Methods', function() {
220220
task.save()
221221
})
222222

223-
it('instance.save passes params to create', function() {
223+
it('instance.save passes params to create', function () {
224224
const { Task } = makeContext()
225225
const task = new Task({ test: true })
226226
let called = false
@@ -237,7 +237,7 @@ describe('Models - Methods', function() {
237237
assert(called, 'create should have been called')
238238
})
239239

240-
it('instance.save passes params to patch', function() {
240+
it('instance.save passes params to patch', function () {
241241
const { Todo } = makeContext()
242242
const todo = new Todo({ id: 1, test: true })
243243
let called = false
@@ -254,7 +254,7 @@ describe('Models - Methods', function() {
254254
assert(called, 'patch should have been called')
255255
})
256256

257-
it('instance.save passes params to update', function() {
257+
it('instance.save passes params to update', function () {
258258
const { Task } = makeContext()
259259
Task.preferUpdate = true
260260

@@ -273,7 +273,7 @@ describe('Models - Methods', function() {
273273
assert(called, 'update should have been called')
274274
})
275275

276-
it('instance.remove works with temp records', function() {
276+
it('instance.remove works with temp records', function () {
277277
const { Task, store } = makeContext()
278278
const task = new Task({ test: true })
279279
const tempId = task.__id
@@ -284,11 +284,11 @@ describe('Models - Methods', function() {
284284
assert(!store.state.tasks.tempsById[tempId], 'temp was removed')
285285
})
286286

287-
it.skip('instance.remove removes cloned records from the store', function() {})
288-
it.skip('instance.remove removes cloned records from the Model.copiesById', function() {})
289-
it.skip('removes clone and original upon calling clone.remove()', function() {})
287+
it.skip('instance.remove removes cloned records from the store', function () {})
288+
it.skip('instance.remove removes cloned records from the Model.copiesById', function () {})
289+
it.skip('removes clone and original upon calling clone.remove()', function () {})
290290

291-
it('instance methods still available in store data after updateItem mutation (or socket event)', async function() {
291+
it('instance methods still available in store data after updateItem mutation (or socket event)', async function () {
292292
const { Letter, store, lettersService } = makeContext()
293293
let letter = new Letter({ name: 'Garmadon', age: 1025 })
294294

@@ -319,7 +319,7 @@ describe('Models - Methods', function() {
319319
)
320320
})
321321

322-
it('Dates remain as dates after changes', async function() {
322+
it('Dates remain as dates after changes', async function () {
323323
const { Letter, store, lettersService } = makeContext()
324324
let letter = new Letter({
325325
name: 'Garmadon',
@@ -336,7 +336,7 @@ describe('Models - Methods', function() {
336336
assert(isDate(letter.createdAt), 'createdAt should be a date')
337337
})
338338

339-
it('instance.toJSON', function() {
339+
it('instance.toJSON', function () {
340340
const { Task } = makeContext()
341341
const task = new Task({ id: 1, test: true })
342342

test/service-module/module.getters.test.ts

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ const options = {
2121
service: null
2222
}
2323

24-
const { find, list, get, getCopyById } = makeServiceGetters()
24+
const { find, count, list, get, getCopyById } = makeServiceGetters()
2525
const { addItems } = makeServiceMutations()
2626

27-
describe('Service Module - Getters', function() {
28-
beforeEach(function() {
27+
describe('Service Module - Getters', function () {
28+
beforeEach(function () {
2929
const state = makeServiceState(options)
3030
this.items = [
3131
{
@@ -63,7 +63,7 @@ describe('Service Module - Getters', function() {
6363
this.state = state
6464
})
6565

66-
it('list', function() {
66+
it('list', function () {
6767
const { state, items } = this
6868
const results = list(state)
6969

@@ -74,7 +74,7 @@ describe('Service Module - Getters', function() {
7474
})
7575
})
7676

77-
it('getCopyById with keepCopiesInStore: true', function() {
77+
it('getCopyById with keepCopiesInStore: true', function () {
7878
const state = {
7979
keepCopiesInStore: true,
8080
copiesById: {
@@ -88,7 +88,7 @@ describe('Service Module - Getters', function() {
8888
assert(result.test, 'got the copy')
8989
})
9090

91-
it('getCopyById with keepCopiesInStore: false', function() {
91+
it('getCopyById with keepCopiesInStore: false', function () {
9292
const state = {
9393
keepCopiesInStore: false,
9494
servicePath: 'todos',
@@ -117,7 +117,7 @@ describe('Service Module - Getters', function() {
117117
clearModels()
118118
})
119119

120-
it('get works on keyedById', function() {
120+
it('get works on keyedById', function () {
121121
const { state, items } = this
122122
// @ts-ignore
123123
const result = get(state)(1)
@@ -126,7 +126,7 @@ describe('Service Module - Getters', function() {
126126
assert.deepEqual(result, items[0])
127127
})
128128

129-
it('get works on tempsById', function() {
129+
it('get works on tempsById', function () {
130130
const { state } = this
131131
const tempId = Object.keys(state.tempsById)[0]
132132
// @ts-ignore
@@ -136,7 +136,7 @@ describe('Service Module - Getters', function() {
136136
assert(result.__id === tempId)
137137
})
138138

139-
it('find - no temps by default', function() {
139+
it('find - no temps by default', function () {
140140
const { state, items } = this
141141
const params = { query: {} }
142142
const results = find(state)(params)
@@ -151,7 +151,7 @@ describe('Service Module - Getters', function() {
151151
assert(results.total === 3, 'total was correct')
152152
})
153153

154-
it('find with temps', function() {
154+
it('find with temps', function () {
155155
const { state, items } = this
156156
// Set temps: false to skip the temps.
157157
const params = { query: {}, temps: true }
@@ -163,7 +163,7 @@ describe('Service Module - Getters', function() {
163163
assert(results.total === 4, 'total was correct')
164164
})
165165

166-
it('find with query', function() {
166+
it('find with query', function () {
167167
const { state } = this
168168
const params = { query: { test: false } }
169169
const results = find(state)(params)
@@ -175,7 +175,7 @@ describe('Service Module - Getters', function() {
175175
assert(results.total === 1, 'total was correct')
176176
})
177177

178-
it('find with custom operator', function() {
178+
it('find with custom operator', function () {
179179
const { state } = this
180180
const params = { query: { test: false, $populateQuery: 'test' } }
181181
const results = find(state)(params)
@@ -187,7 +187,7 @@ describe('Service Module - Getters', function() {
187187
assert(results.total === 1, 'total was correct')
188188
})
189189

190-
it('find with paramsForServer option', function() {
190+
it('find with paramsForServer option', function () {
191191
const { state } = this
192192
state.paramsForServer = ['_$client']
193193
const params = { query: { test: false, _$client: 'test' } }
@@ -200,7 +200,7 @@ describe('Service Module - Getters', function() {
200200
assert(results.total === 1, 'total was correct')
201201
})
202202

203-
it('find with non-whitelisted custom operator fails', function() {
203+
it('find with non-whitelisted custom operator fails', function () {
204204
const { state } = this
205205
const params = { query: { $client: 'test' } }
206206
let results
@@ -212,7 +212,7 @@ describe('Service Module - Getters', function() {
212212
assert(!results[0])
213213
})
214214

215-
it('find with whitelisted custom operators', function() {
215+
it('find with whitelisted custom operators', function () {
216216
const { state } = this
217217
state.whitelist = ['$regex', '$options']
218218
const query = {
@@ -232,7 +232,7 @@ describe('Service Module - Getters', function() {
232232
assert(results.total === 1, 'total was correct')
233233
})
234234

235-
it('find works with $elemMatch', function() {
235+
it('find works with $elemMatch', function () {
236236
const { state } = this
237237
const query = {
238238
movies: {
@@ -249,7 +249,7 @@ describe('Service Module - Getters', function() {
249249
assert(results.total === 1, 'total was correct')
250250
})
251251

252-
it('find with limit', function() {
252+
it('find with limit', function () {
253253
const { state } = this
254254
const params = { query: { $limit: 1 } }
255255
const results = find(state)(params)
@@ -261,7 +261,7 @@ describe('Service Module - Getters', function() {
261261
assert(results.total === 3, 'total was correct')
262262
})
263263

264-
it('find with skip', function() {
264+
it('find with skip', function () {
265265
const { state } = this
266266
const params = { query: { $skip: 1 } }
267267
const results = find(state)(params)
@@ -274,7 +274,7 @@ describe('Service Module - Getters', function() {
274274
assert(results.total === 3, 'total was correct')
275275
})
276276

277-
it('find with limit and skip', function() {
277+
it('find with limit and skip', function () {
278278
const { state } = this
279279
const params = { query: { $limit: 1, $skip: 1 } }
280280
const results = find(state)(params)
@@ -286,7 +286,7 @@ describe('Service Module - Getters', function() {
286286
assert(results.total === 3, 'total was correct')
287287
})
288288

289-
it('find with select', function() {
289+
it('find with select', function () {
290290
const { state } = this
291291
const params = { query: { $select: ['otherField'] } }
292292
const results = find(state)(params)
@@ -305,7 +305,7 @@ describe('Service Module - Getters', function() {
305305
assert(results.total === 3, 'total was correct')
306306
})
307307

308-
it('find with sort ascending on integers', function() {
308+
it('find with sort ascending on integers', function () {
309309
const { state } = this
310310
const params = {
311311
query: {
@@ -322,7 +322,7 @@ describe('Service Module - Getters', function() {
322322
}, 0)
323323
})
324324

325-
it('find with sort descending on integers', function() {
325+
it('find with sort descending on integers', function () {
326326
const { state } = this
327327
const params = {
328328
query: {
@@ -339,7 +339,7 @@ describe('Service Module - Getters', function() {
339339
}, 100)
340340
})
341341

342-
it('find with sort ascending on floats', function() {
342+
it('find with sort ascending on floats', function () {
343343
const { state } = this
344344
const params = {
345345
query: {
@@ -359,7 +359,7 @@ describe('Service Module - Getters', function() {
359359
}, 0)
360360
})
361361

362-
it('find with sort descending on floats', function() {
362+
it('find with sort descending on floats', function () {
363363
const { state } = this
364364
const params = {
365365
query: {
@@ -378,4 +378,31 @@ describe('Service Module - Getters', function() {
378378
return current
379379
}, 100)
380380
})
381+
382+
it('count without params fails', function () {
383+
const { state } = this
384+
385+
try {
386+
count(state, { find })(null)
387+
} catch (error) {
388+
assert(error)
389+
}
390+
})
391+
392+
it('count without query fails', function () {
393+
const { state } = this
394+
395+
try {
396+
count(state, { find })({})
397+
} catch (error) {
398+
assert(error)
399+
}
400+
})
401+
402+
it('count returns the number of records in the store', function () {
403+
const { state } = this
404+
405+
const total = count(state, { find })({ query: {} })
406+
assert(total === 3, 'count is 3')
407+
})
381408
})

0 commit comments

Comments
 (0)