Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 433f9ee

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
feat(tests): add GraphqlQueryBuilder unit tests
1 parent 2c4ca59 commit 433f9ee

File tree

2 files changed

+208
-5
lines changed

2 files changed

+208
-5
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import GraphqlQueryBuilder from '../graphqlQueryBuilder';
2+
import * as moment from 'moment-mini';
3+
4+
function removeSpaces(textS) {
5+
return `${textS}`.replace(/\s+/g, '');
6+
}
7+
8+
describe('GraphqlQueryBuilder', () => {
9+
it('should accept a single find value', () => {
10+
const expectation = `user{age}`;
11+
const user = new GraphqlQueryBuilder('user').find('age');
12+
13+
expect(removeSpaces(expectation)).toEqual(removeSpaces(user));
14+
});
15+
16+
it('should create a Query with function name & alia', () => {
17+
const expectation = `sam : user{name}`;
18+
const user = new GraphqlQueryBuilder('user', 'sam').find('name');
19+
20+
expect(removeSpaces(expectation)).toEqual(removeSpaces(user));
21+
});
22+
23+
it('should create a Query with function name & input', () => {
24+
const expectation = `user(id:12345){name}`;
25+
const user = new GraphqlQueryBuilder('user', { id: 12345 }).find('name');
26+
27+
expect(removeSpaces(expectation)).toEqual(removeSpaces(user));
28+
});
29+
30+
it('should create a Query with function name & input(s)', () => {
31+
const expectation = `user(id:12345, age:34){name}`;
32+
const user = new GraphqlQueryBuilder('user', { id: 12345, age: 34 }).find('name');
33+
34+
expect(removeSpaces(expectation)).toEqual(removeSpaces(user));
35+
});
36+
37+
it('should accept a single find value with alia', () => {
38+
const expectation = `user{nickname:name}`;
39+
const user = new GraphqlQueryBuilder('user').find({ nickname: 'name' });
40+
41+
expect(removeSpaces(expectation)).toEqual(removeSpaces(user));
42+
});
43+
44+
it('should accept a multiple find values', () => {
45+
const expectation = `user{firstname, lastname}`;
46+
const user = new GraphqlQueryBuilder('user').find('firstname', 'lastname');
47+
48+
expect(removeSpaces(expectation)).toEqual(removeSpaces(user));
49+
});
50+
51+
it('should accept an array find values', () => {
52+
const expectation = `user{firstname, lastname}`;
53+
const user = new GraphqlQueryBuilder('user').find(['firstname', 'lastname']);
54+
55+
expect(removeSpaces(expectation)).toEqual(removeSpaces(user));
56+
});
57+
58+
it('should work with nesting Querys', () => {
59+
const expectation = `user( id:12345 ) {
60+
id, nickname : name, isViewerFriend,
61+
image : profilePicture( size:50 ) {
62+
uri, width, height } }`;
63+
64+
const profilePicture = new GraphqlQueryBuilder('profilePicture', { size: 50 });
65+
profilePicture.find('uri', 'width', 'height');
66+
67+
const user = new GraphqlQueryBuilder('user', { id: 12345 });
68+
user.find(['id', { 'nickname': 'name' }, 'isViewerFriend', { 'image': profilePicture }]);
69+
70+
expect(removeSpaces(expectation)).toEqual(removeSpaces(user));
71+
});
72+
73+
it('should work with simple nesting Querys', () => {
74+
const expectation = `user { profilePicture { uri, width, height } }`;
75+
76+
const user = new GraphqlQueryBuilder('user');
77+
user.find({ 'profilePicture': ['uri', 'width', 'height'] });
78+
79+
expect(removeSpaces(expectation)).toEqual(removeSpaces(user));
80+
});
81+
82+
it('should be able to Query a Date field', () => {
83+
const now = new Date();
84+
const expectation = `FetchLeeAndSam { lee: user(modified: "${moment(now).toISOString()}") { name, modified },
85+
sam: user(modified: "${moment(now).toISOString()}") { name, modified } }`;
86+
87+
const fetchLeeAndSam = new GraphqlQueryBuilder('FetchLeeAndSam');
88+
89+
const lee = new GraphqlQueryBuilder('user', { modified: now });
90+
lee.setAlias('lee');
91+
lee.find(['name', 'modified']);
92+
93+
const sam = new GraphqlQueryBuilder('user', 'sam');
94+
sam.filter({ modified: now });
95+
sam.find(['name', 'modified']);
96+
97+
fetchLeeAndSam.find(lee, sam);
98+
99+
expect(removeSpaces(fetchLeeAndSam)).toEqual(removeSpaces(expectation));
100+
});
101+
102+
it('should be able to group Querys', () => {
103+
const expectation = `FetchLeeAndSam { lee: user(id: "1") { name }, sam: user(id: "2") { name } }`;
104+
105+
const fetchLeeAndSam = new GraphqlQueryBuilder('FetchLeeAndSam');
106+
107+
const lee = new GraphqlQueryBuilder('user', { id: '1' });
108+
lee.setAlias('lee');
109+
lee.find(['name']);
110+
111+
const sam = new GraphqlQueryBuilder('user', 'sam');
112+
sam.filter({ id: '2' });
113+
sam.find('name');
114+
115+
fetchLeeAndSam.find(lee, sam);
116+
117+
expect(removeSpaces(fetchLeeAndSam)).toEqual(removeSpaces(expectation));
118+
});
119+
120+
it('should work with nasted objects and lists', () => {
121+
const expectation = `myPost:Message(type:"chat",message:"yoyo",
122+
user:{name:"bob",screen:{height:1080,width:1920}},
123+
friends:[{id:1,name:"ann"},{id:2,name:"tom"}]) {
124+
messageId : id, postedTime : createTime }`;
125+
126+
const messageRequest = {
127+
type: 'chat',
128+
message: 'yoyo',
129+
user: {
130+
name: 'bob',
131+
screen: { height: 1080, width: 1920 }
132+
},
133+
friends: [{ id: 1, name: 'ann' }, { id: 2, name: 'tom' }]
134+
};
135+
136+
const messageQuery = new GraphqlQueryBuilder('Message', 'myPost');
137+
messageQuery.filter(messageRequest);
138+
messageQuery.find({ messageId: 'id' }, { postedTime: 'createTime' });
139+
140+
expect(removeSpaces(messageQuery)).toEqual(removeSpaces(expectation));
141+
});
142+
143+
it('should work with objects that have help functions(will skip function name)', () => {
144+
const expectation = 'inventory(toy:"jack in the box") { id }';
145+
const childsToy = { toy: 'jack in the box', getState: () => { } };
146+
147+
childsToy.getState(); // for istanbul(coverage) to say all fn was called
148+
const itemQuery = new GraphqlQueryBuilder('inventory', childsToy);
149+
itemQuery.find('id');
150+
151+
expect(removeSpaces(itemQuery)).toEqual(removeSpaces(expectation));
152+
});
153+
154+
it('should work with nasted objects that have help functions(will skip function name)', () => {
155+
const expectation = 'inventory(toy:"jack in the box") { id }';
156+
const childsToy = { toy: 'jack in the box', utils: { getState: () => { } } };
157+
158+
childsToy.utils.getState(); // for istanbul(coverage) to say all fn was called
159+
const itemQuery = new GraphqlQueryBuilder('inventory', childsToy);
160+
itemQuery.find('id');
161+
162+
expect(removeSpaces(itemQuery)).toEqual(removeSpaces(expectation));
163+
});
164+
165+
it('should skip empty objects in filter/args', () => {
166+
const expectation = 'inventory(toy:"jack in the box") { id }';
167+
const childsToy = { toy: 'jack in the box', utils: {} };
168+
169+
const itemQuery = new GraphqlQueryBuilder('inventory', childsToy);
170+
itemQuery.find('id');
171+
172+
expect(removeSpaces(itemQuery)).toEqual(removeSpaces(expectation));
173+
});
174+
175+
it('should throw Error if find input items have zero props', () => {
176+
expect(() => new GraphqlQueryBuilder('x').find({})).toThrow();
177+
});
178+
179+
it('should throw Error if find input items have multiple props', () => {
180+
expect(() => new GraphqlQueryBuilder('x').find({ a: 'z', b: 'y' })).toThrow();
181+
});
182+
183+
it('should throw Error if find is undefined', () => {
184+
expect(() => new GraphqlQueryBuilder('x').find()).toThrow();
185+
});
186+
187+
it('should throw Error if no find values have been set', () => {
188+
expect(() => `${new GraphqlQueryBuilder('x')}`).toThrow();
189+
});
190+
191+
it('should throw Error if find is not valid', () => {
192+
expect(() => new GraphqlQueryBuilder('x').find(123)).toThrow();
193+
});
194+
195+
it('should throw Error if you accidentally pass an undefined', () => {
196+
expect(() => new GraphqlQueryBuilder('x', undefined)).toThrow();
197+
});
198+
199+
it('should throw Error it is not an input object for alias', () => {
200+
// @ts-ignore: 2345
201+
expect(() => new GraphqlQueryBuilder('x', true)).toThrow();
202+
});
203+
});

src/app/modules/angular-slickgrid/services/graphqlQueryBuilder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ export default class GraphqlQueryBuilder {
1313

1414
/* Constructor, query/mutator you wish to use, and an alias or filter arguments. */
1515
constructor(private queryFnName: string, aliasOrFilter?: string | object) {
16-
if (typeof aliasOrFilter === 'function') {
16+
if (typeof aliasOrFilter === 'string') {
1717
this.alias = aliasOrFilter;
1818
} else if (typeof aliasOrFilter === 'object') {
1919
this.filter(aliasOrFilter);
20-
} else if (undefined === aliasOrFilter && 2 === arguments.length) {
20+
} else if (aliasOrFilter === undefined && arguments.length === 2) {
2121
throw new TypeError(`You have passed undefined as Second argument to "Query"`);
22-
} else if (undefined !== aliasOrFilter) {
23-
throw new TypeError(`Second argument to "Query" should be an alias name(String) or filter arguments(Object). was passed ${aliasOrFilter}`);
22+
} else if (aliasOrFilter !== undefined) {
23+
throw new TypeError(`Second argument to "Query" should be an alias name(String) or filter arguments(Object). What was passed is: ${aliasOrFilter}`);
2424
}
2525
}
2626

@@ -47,7 +47,7 @@ export default class GraphqlQueryBuilder {
4747
* @param properties representing each attribute you want Returned
4848
*/
4949
find(...searches: any[]) { // THIS NEED TO BE A "FUNCTION" to scope 'arguments'
50-
if (!searches) {
50+
if (!searches || !Array.isArray(searches) || searches.length === 0) {
5151
throw new TypeError(`find value can not be >>falsy<<`);
5252
}
5353
// if its a string.. it may have other values

0 commit comments

Comments
 (0)