-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-or-clause.test.js
54 lines (53 loc) · 1.78 KB
/
add-or-clause.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
const addOrClause = require('./add-or-clause');
test('should add the passed or clause if no $or on the current query', () => {
const orClause = {$or: [{myField: 'value'}, {myField: null}]};
const query = {foo: 'bar'};
expect(addOrClause(query, orClause)).toEqual({
$or: [{myField: 'value'}, {myField: null}],
foo: 'bar'
});
});
describe('when the query already has an $or', () => {
test('should add the passed or clause to and $and that also contains the current query', () => {
const orClause = {$or: [{myField: 'value'}, {myField: null}]};
const query = {$or: [{foo: 'bar'}, {foo: {$exists: false}}]};
expect(addOrClause(query, orClause)).toEqual({
$and: [
{$or: [{foo: 'bar'}, {foo: {$exists: false}}]},
{
$or: [{myField: 'value'}, {myField: null}]
}
]
});
});
describe('when the query has an $and', () => {
test('should keep the $and, add the $or and the current query', () => {
const orClause = {$or: [{myField: 'value'}, {myField: null}]};
const query = {
$or: [{hello: 'world'}],
$and: [{foo: 'bar'}, {bar: 'baz'}]
};
expect(addOrClause(query, orClause)).toEqual({
$and: [
{foo: 'bar'},
{bar: 'baz'},
{$or: [{hello: 'world'}]},
{$or: [{myField: 'value'}, {myField: null}]}
]
});
});
});
});
describe('when the query has an $and query', () => {
test('should add the new or clause to the $and', () => {
const orClause = {$or: [{myField: 'value'}, {myField: null}]};
const query = {$and: [{foo: 'bar'}, {bar: 'baz'}]};
expect(addOrClause(query, orClause)).toEqual({
$and: [
{foo: 'bar'},
{bar: 'baz'},
{$or: [{myField: 'value'}, {myField: null}]}
]
});
});
});