-
Notifications
You must be signed in to change notification settings - Fork 134
/
query-test.ts
194 lines (177 loc) · 5.88 KB
/
query-test.ts
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
189
190
191
192
193
194
import { buildQuery } from '../src/query';
import { QueryTerm } from '../src/query-term';
import { FindRecords } from './support/record-data';
import {
RecordQueryBuilder,
RecordQueryExpression
} from './support/record-data';
const { module, test } = QUnit;
///////////////////////////////////////////////////////////////////////////////
module('buildQuery', function () {
test('can instantiate a query from an expression', function (assert) {
let expression: FindRecords = {
op: 'findRecords',
type: 'planet'
};
let query = buildQuery(expression);
assert.ok(query);
});
test('can instantiate a query that will be assigned an `id`', function (assert) {
let expression: FindRecords = {
op: 'findRecords',
type: 'planet'
};
let query = buildQuery(expression);
assert.ok(query.id, 'query has an id');
});
test('can instantiate a query with a single expression, options, and an id', function (assert) {
let expression: FindRecords = {
op: 'findRecords',
type: 'planet'
};
let options = { sources: { jsonapi: { include: 'comments' } } };
let query = buildQuery(expression, options, 'abc123');
assert.strictEqual(query.id, 'abc123', 'id was populated');
assert.strictEqual(
query.expressions,
expression,
'expressions was populated'
);
assert.strictEqual(query.options, options, 'options was populated');
});
test('can instantiate a query with an array of expressions, options, and an id', function (assert) {
let expression: FindRecords = {
op: 'findRecords',
type: 'planet'
};
let expressions = [expression];
let options = { sources: { jsonapi: { include: 'comments' } } };
let query = buildQuery(expressions, options, 'abc123');
assert.strictEqual(query.id, 'abc123', 'id was populated');
assert.deepEqual(
query.expressions,
expressions,
'expressions was populated'
);
assert.strictEqual(query.options, options, 'options was populated');
});
test('can instantiate a query with a single query expression term, options, and an id', function (assert) {
let term1 = {
toQueryExpression: () => {
return { op: 'findRecords', type: 'planet' };
}
} as QueryTerm<RecordQueryExpression>;
let options = { sources: { jsonapi: { include: 'comments' } } };
let query = buildQuery<RecordQueryExpression>(term1, options, 'abc123');
assert.strictEqual(query.id, 'abc123', 'id was populated');
assert.deepEqual(
query.expressions,
{ op: 'findRecords', type: 'planet' },
'expressions was populated'
);
assert.strictEqual(query.options, options, 'options was populated');
});
test('can instantiate a query with multiple query expression terms, options, and an id', function (assert) {
let term1 = {
toQueryExpression: () => {
return { op: 'findRecords', type: 'planet' };
}
} as QueryTerm<RecordQueryExpression>;
let term2 = {
toQueryExpression: () => {
return { op: 'findRecords', type: 'moon' };
}
} as QueryTerm<RecordQueryExpression>;
let expressions = [term1, term2];
let options = { sources: { jsonapi: { include: 'comments' } } };
let query = buildQuery<RecordQueryExpression>(
expressions,
options,
'abc123'
);
assert.strictEqual(query.id, 'abc123', 'id was populated');
assert.deepEqual(
query.expressions,
[
{ op: 'findRecords', type: 'planet' },
{ op: 'findRecords', type: 'moon' }
],
'expressions was populated'
);
assert.strictEqual(query.options, options, 'options was populated');
});
test('will return a query passed into it if no options or id are passed', function (assert) {
let expression: FindRecords = {
op: 'findRecords',
type: 'planet'
};
let query = buildQuery(expression);
assert.strictEqual(buildQuery(query), query);
});
test('will return a new query if a query is passed as well as options / id', function (assert) {
let expression: FindRecords = {
op: 'findRecords',
type: 'planet'
};
let query1 = buildQuery(expression, { a: '1', c: '1' }, '1');
let query2 = buildQuery(query1, { a: '2', b: '2' }, '2');
assert.notStrictEqual(query1, query2);
assert.deepEqual(query2, {
expressions: {
op: 'findRecords',
type: 'planet'
},
options: {
a: '2',
b: '2',
c: '1'
},
id: '2'
});
});
test('will create a query using a QueryBuilder if a function is passed into it', function (assert) {
let qb = new RecordQueryBuilder();
let expression: FindRecords = {
op: 'findRecords',
type: 'planet'
};
let query = buildQuery<RecordQueryExpression, RecordQueryBuilder>(
(q) => q.findRecords('planet'),
undefined,
undefined,
qb
);
assert.deepEqual(
query.expressions,
expression,
'expressions was populated'
);
});
test('should call toQueryExpression() if available', function (assert) {
let expression: FindRecords = {
op: 'findRecords',
type: 'planet'
};
let queryFactory = new QueryTerm(expression);
let query = buildQuery<RecordQueryExpression>(queryFactory);
assert.strictEqual(
query.expressions,
expression,
'expressions was populated'
);
});
test('will create a query with multiple expressions', function (assert) {
let expression1: FindRecords = {
op: 'findRecords',
type: 'planet'
};
let expression2: FindRecords = {
op: 'findRecords',
type: 'moon'
};
let query = buildQuery<RecordQueryExpression>([expression1, expression2]);
let qe = query.expressions as RecordQueryExpression[];
assert.strictEqual(qe[0], expression1, 'expression1 was populated');
assert.strictEqual(qe[1], expression2, 'expression2 was populated');
});
});