-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-field-references.test.js
71 lines (70 loc) · 2.18 KB
/
remove-field-references.test.js
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
const removeFieldReferences = require('./remove-field-references');
test('should remove top-level fields', () => {
const query = {
hello: 'value'
};
const fieldName = 'hello';
expect(removeFieldReferences(query, fieldName).hello).toBeUndefined();
});
test('should return passed query when maxDepth is hit (avoids busting the stack by default)', () => {
const query = {
hello: 'value'
};
const fieldName = 'hello';
expect(removeFieldReferences(query, fieldName, 0)).toEqual(query);
});
test('should remove references to the field in top-level $or queries', () => {
const query = {
$or: [
{hello: 'value', otherField: 'not-related'},
{hello: 'othervalue', otherField: 'even-less-related'}
]
};
const fieldName = 'hello';
expect(removeFieldReferences(query, fieldName)).toEqual({
$or: [{otherField: 'not-related'}, {otherField: 'even-less-related'}]
});
});
test('should remove $or clauses where the query becomes empty on omission of a field', () => {
const query = {
$or: [{hello: 'value'}, {otherField: 'not-related'}]
};
const fieldName = 'hello';
expect(removeFieldReferences(query, fieldName)).toEqual({
$or: [{otherField: 'not-related'}]
});
});
test('should remove references to field in top-level queries inside of $and', () => {
const query = {
$and: [
{hello: 'value', otherField: 'value'},
{hello: 'other-value', otherField: 'value'}
]
};
const fieldName = 'hello';
expect(removeFieldReferences(query, fieldName)).toEqual({
$and: [{otherField: 'value'}, {otherField: 'value'}]
});
});
test('should remove $and clause if all queries end up filtered out', () => {
const query = {
foo: 'bar',
$and: [{hello: 'value'}, {hello: 'other-value'}]
};
const fieldName = 'hello';
expect(removeFieldReferences(query, fieldName)).toEqual({foo: 'bar'});
});
test('should remove references to field in nested $or inside of $and', () => {
const query = {
$and: [
{
$or: [{hello: 'value'}, {hello: null}]
},
{otherField: 'not-related'}
]
};
const fieldName = 'hello';
expect(removeFieldReferences(query, fieldName)).toEqual({
$and: [{otherField: 'not-related'}]
});
});