-
-
Notifications
You must be signed in to change notification settings - Fork 795
Expand file tree
/
Copy pathindex.test.ts
More file actions
188 lines (161 loc) · 4.23 KB
/
index.test.ts
File metadata and controls
188 lines (161 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import assert from 'assert';
import adapterTests from '@feathersjs/adapter-tests';
import errors from '@feathersjs/errors';
import { feathers } from '@feathersjs/feathers';
import { memory } from '../src';
const testSuite = adapterTests([
'.options',
'.events',
'._get',
'._find',
'._create',
'._update',
'._patch',
'._remove',
'.get',
'.get + $select',
'.get + id + query',
'.get + NotFound',
'.get + id + query id',
'.find',
'.remove',
'.remove + $select',
'.remove + id + query',
'.remove + multi',
'.remove + id + query id',
'.update',
'.update + $select',
'.update + id + query',
'.update + NotFound',
'.update + id + query id',
'.patch',
'.patch + $select',
'.patch + id + query',
'.patch multiple',
'.patch multi query',
'.patch + NotFound',
'.patch + id + query id',
'.create',
'.create + $select',
'.create multi',
'internal .find',
'internal .get',
'internal .create',
'internal .update',
'internal .patch',
'internal .remove',
'.find + equal',
'.find + equal multiple',
'.find + $sort',
'.find + $sort + string',
'.find + $limit',
'.find + $limit 0',
'.find + $skip',
'.find + $select',
'.find + $or',
'.find + $in',
'.find + $nin',
'.find + $lt',
'.find + $lte',
'.find + $gt',
'.find + $gte',
'.find + $ne',
'.find + $gt + $lt + $sort',
'.find + $or nested + $sort',
'.find + paginate',
'.find + paginate + $limit + $skip',
'.find + paginate + $limit 0',
'.find + paginate + params'
]);
describe('Feathers Memory Service', () => {
const events = [ 'testing' ];
const app = feathers()
.use('/people', memory({ events }))
.use('/people-customid', memory({
id: 'customid', events
}));
it('update with string id works', async () => {
const people = app.service('people');
const person = await people.create({
name: 'Tester',
age: 33
});
const updatedPerson: any = await people.update(person.id.toString(), person);
assert.strictEqual(typeof updatedPerson.id, 'number');
await people.remove(person.id.toString());
});
it('patch record with prop also in query', async () => {
app.use('/animals', memory({ multi: true }));
const animals = app.service('animals');
await animals.create([{
type: 'cat',
age: 30
}, {
type: 'dog',
age: 10
}]);
const [updated] = await animals.patch(null, { age: 40 }, { query: { age: 30 } });
assert.strictEqual(updated.age, 40);
await animals.remove(null, {});
});
it('allows to pass custom find and sort matcher', async () => {
let sorterCalled = false;
let matcherCalled = false;
app.use('/matcher', memory({
matcher () {
matcherCalled = true;
return function () {
return true;
};
},
sorter () {
sorterCalled = true;
return function () {
return 0;
};
}
}));
await app.service('matcher').find({
query: { $sort: { something: 1 } }
});
assert.ok(sorterCalled, 'sorter called');
assert.ok(matcherCalled, 'matcher called');
});
it('does not modify the original data', async () => {
const people = app.service('people');
const person = await people.create({
name: 'Delete tester',
age: 33
});
delete person.age;
const otherPerson = await people.get(person.id);
assert.strictEqual(otherPerson.age, 33);
await people.remove(person.id);
});
it('does not $select the id', async () => {
const people = app.service('people');
const person = await people.create({
name: 'Tester'
});
const results = await people.find({
query: {
name: 'Tester',
$select: ['name']
}
});
assert.deepStrictEqual(results[0], { name: 'Tester' },
'deepEquals the same'
);
await people.remove(person.id);
});
it('update with null throws error', async () => {
try {
await app.service('people').update(null, {});
throw new Error('Should never get here');
} catch (error) {
assert.strictEqual(error.message, 'You can not replace multiple instances. Did you mean \'patch\'?');
}
});
testSuite(app, errors, 'people');
testSuite(app, errors, 'people-customid', 'customid');
});