-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathgraphqlQueryBuilder.spec.ts
More file actions
201 lines (147 loc) · 7.8 KB
/
Copy pathgraphqlQueryBuilder.spec.ts
File metadata and controls
201 lines (147 loc) · 7.8 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { describe, expect, it } from 'vitest';
import GraphqlQueryBuilder from '../graphqlQueryBuilder.js';
function removeSpaces(text: string) {
return `${text}`.replace(/\s+/g, '');
}
describe('GraphqlQueryBuilder', () => {
it('should accept a single find value', () => {
const expectation = `user{age}`;
const user = new GraphqlQueryBuilder('user').find('age').toString();
expect(removeSpaces(expectation)).toBe(removeSpaces(user));
});
it('should create a Query with function name & alia', () => {
const expectation = `sam: user{name}`;
const user = new GraphqlQueryBuilder('user', 'sam').find('name').toString();
expect(removeSpaces(expectation)).toBe(removeSpaces(user));
});
it('should create a Query with function name & input', () => {
const expectation = `user(id:12345){name}`;
const user = new GraphqlQueryBuilder('user', { id: 12345 }).find('name').toString();
expect(removeSpaces(expectation)).toBe(removeSpaces(user));
});
it('should create a Query with function name & input(s)', () => {
const expectation = `user(id:12345, age:34){name}`;
const user = new GraphqlQueryBuilder('user', { id: 12345, age: 34 }).find('name').toString();
expect(removeSpaces(expectation)).toBe(removeSpaces(user));
});
it('should accept a single find value with alia', () => {
const expectation = `user{nickname:name}`;
const user = new GraphqlQueryBuilder('user').find({ nickname: 'name' }).toString();
expect(removeSpaces(expectation)).toBe(removeSpaces(user));
});
it('should accept a multiple find values', () => {
const expectation = `user{firstname, lastname}`;
const user = new GraphqlQueryBuilder('user').find('firstname', 'lastname').toString();
expect(removeSpaces(expectation)).toBe(removeSpaces(user));
});
it('should accept an array find values', () => {
const expectation = `user{firstname, lastname}`;
const user = new GraphqlQueryBuilder('user').find(['firstname', 'lastname']).toString();
expect(removeSpaces(expectation)).toBe(removeSpaces(user));
});
it('should work with nesting Querys', () => {
const expectation = `user( id:12345 ) {
id, nickname : name,isViewerFriend,
image : profilePicture( size:50 ) {
uri, width, height } }`;
const profilePicture = new GraphqlQueryBuilder('profilePicture', { size: 50 });
profilePicture.find('uri', 'width', 'height');
const user = new GraphqlQueryBuilder('user', { id: 12345 }).find(['id', { nickname: 'name' }, 'isViewerFriend', { image: profilePicture }]).toString();
expect(removeSpaces(expectation)).toBe(removeSpaces(user));
});
it('should work with simple nesting Querys', () => {
const expectation = `user { profilePicture { uri, width, height } }`;
const user = new GraphqlQueryBuilder('user').find({ profilePicture: ['uri', 'width', 'height'] }).toString();
expect(removeSpaces(expectation)).toBe(removeSpaces(user));
});
it('should be able to Query a Date field', () => {
const now = new Date();
const expectation = `FetchLeeAndSam { lee: user(modified: "${now.toISOString()}") { name, modified },
sam: user(modified: "${now.toISOString()}") { name, modified } }`;
const fetchLeeAndSam = new GraphqlQueryBuilder('FetchLeeAndSam');
const lee = new GraphqlQueryBuilder('user', { modified: now });
lee.setAlias('lee');
lee.find(['name', 'modified']);
const sam = new GraphqlQueryBuilder('user', 'sam');
sam.filter({ modified: now });
sam.find(['name', 'modified']);
const output = fetchLeeAndSam.find(lee, sam).toString();
expect(removeSpaces(output)).toBe(removeSpaces(expectation));
});
it('should be able to group Querys', () => {
const expectation = `FetchLeeAndSam { lee: user(id: "1") { name }, sam: user(id: "2") { name } }`;
const fetchLeeAndSam = new GraphqlQueryBuilder('FetchLeeAndSam');
const lee = new GraphqlQueryBuilder('user', { id: '1' });
lee.setAlias('lee');
lee.find(['name']);
const sam = new GraphqlQueryBuilder('user', 'sam');
sam.filter({ id: '2' });
sam.find('name');
const output = fetchLeeAndSam.find(lee, sam).toString();
expect(removeSpaces(output)).toBe(removeSpaces(expectation));
});
it('should work with nasted objects and lists', () => {
const expectation = `myPost:Message(type:"chat",message:"yoyo",
user:{name:"bob",screen:{ height:1080, width:1920}},
friends:[{id:1, name:"ann"},{id:2, name:"tom"}]) {
messageId: id, postedTime: createTime }`;
const messageRequest = {
type: 'chat',
message: 'yoyo',
user: {
name: 'bob',
screen: { height: 1080, width: 1920 },
},
friends: [
{ id: 1, name: 'ann' },
{ id: 2, name: 'tom' },
],
};
const messageQuery = new GraphqlQueryBuilder('Message', 'myPost').filter(messageRequest).find({ messageId: 'id' }, { postedTime: 'createTime' }).toString();
expect(removeSpaces(messageQuery)).toBe(removeSpaces(expectation));
});
it('should work with objects that have help functions(will skip function name)', () => {
const expectation = 'inventory(toy:"jack in the box") { id }';
const childsToy = { toy: 'jack in the box', getState: () => {} };
childsToy.getState(); // for v8 coverage to get all fn be called
const itemQuery = new GraphqlQueryBuilder('inventory', childsToy).find('id').toString();
expect(removeSpaces(itemQuery)).toBe(removeSpaces(expectation));
});
it('should work with nasted objects that have help functions(will skip function name)', () => {
const expectation = 'inventory(toy:"jack in the box") { id }';
const childsToy = { toy: 'jack in the box', utils: { getState: () => {} } };
childsToy.utils.getState(); // for v8 coverage to get all fn be called
const itemQuery = new GraphqlQueryBuilder('inventory', childsToy).find('id').toString();
expect(removeSpaces(itemQuery)).toBe(removeSpaces(expectation));
});
it('should skip empty objects in filter/args', () => {
const expectation = 'inventory(toy:"jack in the box") { id }';
const childsToy = { toy: 'jack in the box', utils: {} };
const itemQuery = new GraphqlQueryBuilder('inventory', childsToy).find('id').toString();
expect(removeSpaces(itemQuery)).toBe(removeSpaces(expectation));
});
it('should throw Error if find input items have zero props', () => {
expect(() => new GraphqlQueryBuilder('x').find({})).toThrow('Alias objects should only have one value. was passed: {}');
});
it('should throw Error if find input items have multiple props', () => {
expect(() => new GraphqlQueryBuilder('x').find({ a: 'z', b: 'y' })).toThrow('Alias objects should only have one value. was passed: {"a":"z","b":"y"}');
});
it('should throw Error if find is undefined', () => {
expect(() => new GraphqlQueryBuilder('x').find()).toThrow('find value can not be >>falsy<<');
});
it('should throw Error if no find values have been set', () => {
expect(() => `${new GraphqlQueryBuilder('x')}`).toThrow(`return properties are not defined. use the 'find' function to defined them`);
});
it('should throw Error if find is not valid', () => {
expect(() => new GraphqlQueryBuilder('x').find(123)).toThrow('cannot handle Find value of 123');
});
it('should throw Error if you accidentally pass an undefined', () => {
expect(() => new GraphqlQueryBuilder('x', undefined)).toThrow('You have passed undefined as Second argument to "Query"');
});
it('should throw Error it is not an input object for alias', () => {
// @ts-ignore: 2345
expect(() => new GraphqlQueryBuilder('x', true)).toThrow(
'Second argument to "Query" should be an alias name(String) or filter arguments(Object). What was passed is: true'
);
});
});