Skip to content

Commit 5b29803

Browse files
committed
Make q fiter case insensitive
1 parent 1010c53 commit 5b29803

File tree

2 files changed

+166
-1
lines changed

2 files changed

+166
-1
lines changed

src/jsonGraphqlExpress.spec.js

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import express from 'express';
2+
import request from 'supertest';
3+
import jsonGraphqlExpress from './jsonGraphqlExpress';
4+
5+
const data = {
6+
posts: [
7+
{
8+
id: 1,
9+
title: 'Lorem Ipsum',
10+
views: 254,
11+
user_id: 123,
12+
},
13+
{
14+
id: 2,
15+
title: 'Sic Dolor amet',
16+
views: 65,
17+
user_id: 456,
18+
},
19+
],
20+
users: [
21+
{
22+
id: 123,
23+
name: 'John Doe',
24+
},
25+
{
26+
id: 456,
27+
name: 'Jane Doe',
28+
},
29+
],
30+
};
31+
32+
let agent;
33+
34+
beforeAll(() => {
35+
const app = express();
36+
app.use('/', jsonGraphqlExpress(data));
37+
agent = request.agent(app);
38+
});
39+
40+
const gqlAgent = (query, variables) =>
41+
agent.post('/').send({
42+
query,
43+
variables,
44+
});
45+
46+
describe('all* route', () => {
47+
it('returns all entities by default', () =>
48+
gqlAgent('{ allPosts { id } }').expect({
49+
data: {
50+
allPosts: [{ id: 1 }, { id: 2 }],
51+
},
52+
}));
53+
describe('pagination', () => {
54+
it('does not paginate when page is not set', () =>
55+
gqlAgent('{ allPosts(perPage: 1) { id } }').expect({
56+
data: {
57+
allPosts: [{ id: 1 }, { id: 2 }],
58+
},
59+
}));
60+
it('uses page to set page number', () =>
61+
Promise.all([
62+
gqlAgent('{ allPosts(page: 0) { id } }').expect({
63+
data: {
64+
allPosts: [{ id: 1 }, { id: 2 }],
65+
},
66+
}),
67+
gqlAgent('{ allPosts(page: 1) { id } }').expect({
68+
data: {
69+
allPosts: [],
70+
},
71+
}),
72+
]));
73+
it('uses perPage to set number of results per page', () =>
74+
Promise.all([
75+
gqlAgent('{ allPosts(page: 0, perPage: 1) { id } }').expect({
76+
data: {
77+
allPosts: [{ id: 1 }],
78+
},
79+
}),
80+
gqlAgent('{ allPosts(page: 1, perPage: 1) { id } }').expect({
81+
data: {
82+
allPosts: [{ id: 2 }],
83+
},
84+
}),
85+
]));
86+
});
87+
describe('sort', () => {
88+
it('sorts data using sortField for the field', () =>
89+
Promise.all([
90+
gqlAgent('{ allPosts(sortField: "views") { id } }').expect({
91+
data: {
92+
allPosts: [{ id: 2 }, { id: 1 }],
93+
},
94+
}),
95+
gqlAgent('{ allPosts(sortField: "title") { id } }').expect({
96+
data: {
97+
allPosts: [{ id: 1 }, { id: 2 }],
98+
},
99+
}),
100+
]));
101+
it('sorts data using sortOrder for the sort direction', () =>
102+
Promise.all([
103+
gqlAgent(
104+
'{ allPosts(sortField: "views", sortOrder: "asc") { id } }',
105+
).expect({
106+
data: {
107+
allPosts: [{ id: 2 }, { id: 1 }],
108+
},
109+
}),
110+
gqlAgent(
111+
'{ allPosts(sortField: "views", sortOrder: "desc") { id } }',
112+
).expect({
113+
data: {
114+
allPosts: [{ id: 1 }, { id: 2 }],
115+
},
116+
}),
117+
]));
118+
});
119+
describe('filters', () => {
120+
it('filters by string on all text fields using the q filter', () =>
121+
gqlAgent('{ allPosts(filter: { q: "Lorem" }) { id } }').expect({
122+
data: {
123+
allPosts: [{ id: 1 }],
124+
},
125+
}));
126+
it('filters by string using the q filter in a case-insensitive way', () =>
127+
gqlAgent('{ allPosts(filter: { q: "lorem" }) { id } }').expect({
128+
data: {
129+
allPosts: [{ id: 1 }],
130+
},
131+
}));
132+
it('filters by value on each field using the related filter', () =>
133+
Promise.all([
134+
gqlAgent('{ allPosts(filter: { id: 2 }) { id } }').expect({
135+
data: {
136+
allPosts: [{ id: 2 }],
137+
},
138+
}),
139+
gqlAgent(
140+
'{ allPosts(filter: { title: "Sic Dolor amet" }) { id } }',
141+
).expect({
142+
data: {
143+
allPosts: [{ id: 2 }],
144+
},
145+
}),
146+
gqlAgent('{ allPosts(filter: { views: 65 }) { id } }').expect({
147+
data: {
148+
allPosts: [{ id: 2 }],
149+
},
150+
}),
151+
gqlAgent(
152+
'{ allPosts(filter: { user_id: 456 }) { id } }',
153+
).expect({
154+
data: {
155+
allPosts: [{ id: 2 }],
156+
},
157+
}),
158+
]));
159+
});
160+
});

src/resolver/Query/all.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ export default entityData => (
5151
if (filter.q) {
5252
items = items.filter(d =>
5353
Object.keys(d).some(
54-
key => d[key] && d[key].toString().includes(filter.q),
54+
key =>
55+
d[key] &&
56+
d[key]
57+
.toString()
58+
.toLowerCase()
59+
.includes(filter.q.toLowerCase()),
5560
),
5661
);
5762
}

0 commit comments

Comments
 (0)