Skip to content

Commit 74e1b2d

Browse files
committed
test: add tests for operators (exists, regex, nested operators)
1 parent 65722de commit 74e1b2d

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

src/__mocks__/userModel.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ const UserSchema = new Schema(
1616

1717
user: {
1818
type: Schema.Types.ObjectId,
19-
ref: 'UserModel',
19+
ref: 'User',
2020
},
2121

2222
users: {
2323
type: [Schema.Types.ObjectId],
24-
ref: 'UserModel',
24+
ref: 'User',
2525
},
2626

2727
n: {
@@ -144,6 +144,7 @@ export interface IUser extends Document {
144144
relocation?: boolean;
145145
contacts: {
146146
email: string;
147+
skype?: string;
147148
};
148149
}
149150

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* eslint-disable no-param-reassign */
2+
3+
import { schemaComposer, graphql } from 'graphql-compose';
4+
import { composeWithMongoose } from '../../index';
5+
import { UserModel } from '../../__mocks__/userModel';
6+
7+
beforeAll(async () => {
8+
await UserModel.base.createConnection();
9+
await UserModel.create({
10+
name: 'AAAAA',
11+
age: 10,
12+
contacts: { email: '[email protected]' },
13+
});
14+
await UserModel.create({
15+
name: 'BBBBB',
16+
age: 20,
17+
gender: 'male',
18+
contacts: { email: '[email protected]', skype: 'aaa' },
19+
});
20+
await UserModel.create({
21+
name: 'CCCCC',
22+
age: 30,
23+
contacts: { email: '[email protected]' },
24+
});
25+
});
26+
afterAll(() => UserModel.base.disconnect());
27+
28+
const UserTC = composeWithMongoose(UserModel);
29+
schemaComposer.Query.addFields({
30+
findMany: UserTC.getResolver('findMany'),
31+
});
32+
const schema = schemaComposer.buildSchema();
33+
34+
describe('issue #250 - Adds support for nested `_operators`, add `exists`, `regex` operators', () => {
35+
it('check `exist` operator', async () => {
36+
expect(
37+
await graphql.graphql(
38+
schema,
39+
`query {
40+
findMany(filter: { _operators: { gender: { exists: true } } }) {
41+
name
42+
gender
43+
}
44+
}`
45+
)
46+
).toEqual({ data: { findMany: [{ gender: 'male', name: 'BBBBB' }] } });
47+
});
48+
49+
it('check nested `exist` operator', async () => {
50+
expect(
51+
await graphql.graphql(
52+
schema,
53+
`query {
54+
findMany(filter: { _operators: { contacts: { skype: { exists: true } } } }) {
55+
name
56+
}
57+
}`
58+
)
59+
).toEqual({ data: { findMany: [{ name: 'BBBBB' }] } });
60+
});
61+
62+
it('check `regex` operator', async () => {
63+
expect(
64+
await graphql.graphql(
65+
schema,
66+
`query {
67+
findMany(filter: { _operators: { name: { regex: "^AA|CC.*" } } }) {
68+
name
69+
}
70+
}`
71+
)
72+
).toEqual({ data: { findMany: [{ name: 'AAAAA' }, { name: 'CCCCC' }] } });
73+
});
74+
75+
it('check nested `regex` operator', async () => {
76+
expect(
77+
await graphql.graphql(
78+
schema,
79+
`query {
80+
findMany(filter: { _operators: { contacts: { email: { regex: "/3.COM/i" } } } }) {
81+
name
82+
}
83+
}`
84+
)
85+
).toEqual({ data: { findMany: [{ name: 'CCCCC' }] } });
86+
});
87+
88+
it('check combined nested `regex` operator', async () => {
89+
expect(
90+
await graphql.graphql(
91+
schema,
92+
`query {
93+
findMany(
94+
filter: { OR: [
95+
{ _operators: { contacts: { email: { regex: "/3.COM/i" } } } },
96+
{ _operators: { contacts: { skype: { exists: true } } } }
97+
]}
98+
) {
99+
name
100+
}
101+
}`
102+
)
103+
).toEqual({ data: { findMany: [{ name: 'BBBBB' }, { name: 'CCCCC' }] } });
104+
});
105+
});

0 commit comments

Comments
 (0)