-
Notifications
You must be signed in to change notification settings - Fork 33
/
test.js
200 lines (133 loc) · 6.36 KB
/
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
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
"use strict";
var expect = require('chai').expect;
var Query = require('./index');
function removeSpaces(textS) {
return `${textS}`.replace(/\s+/g, '');
}
describe("graphql query builder", function() { //log the function
it('should accept a single find value', function(){
let expeted = `user{age}`;
let user = new Query("user").find("age");
expect(removeSpaces(expeted)).to.equal(removeSpaces(user));
});
it('should create a Query with function name & alia', function(){
let expeted = `sam : user{name}`;
let user = new Query("user","sam").find("name");
expect(removeSpaces(expeted)).to.equal(removeSpaces(user));
});
it('should create a Query with function name & input', function(){
let expeted = `user(id:12345){name}`;
let user = new Query("user",{id : 12345}).find("name");
expect(removeSpaces(expeted)).to.equal(removeSpaces(user));
});
it('should create a Query with function name & input(s)', function(){
let expeted = `user(id:12345, age:34){name}`;
let user = new Query("user",{id : 12345, age:34}).find("name");
expect(removeSpaces(expeted)).to.equal(removeSpaces(user));
});
it('should accept a single find value with alia', function(){
let expeted = `user{nickname:name}`;
let user = new Query("user").find({nickname:"name"});
expect(removeSpaces(expeted)).to.equal(removeSpaces(user));
});
it('should accept a multiple find values', function(){
let expeted = `user{firstname, lastname}`;
let user = new Query("user").find("firstname","lastname")
expect(removeSpaces(expeted)).to.equal(removeSpaces(user));
});
it('should accept an array find values', function(){
let expeted = `user{firstname, lastname}`;
let user = new Query("user").find(["firstname","lastname"]);
expect(removeSpaces(expeted)).to.equal(removeSpaces(user));
});
it('should work with nesting Querys', function(){
let expeted = `user( id:12345 ) {
id, nickname : name, isViewerFriend,
image : profilePicture( size:50 ) {
uri, width, height } }`;
let profilePicture = new Query("profilePicture",{size : 50});
profilePicture.find( "uri", "width", "height");
let user = new Query("user",{id : 12345});
user.find(["id", {"nickname":"name"}, "isViewerFriend", {"image":profilePicture}]);
expect(removeSpaces(expeted)).to.equal(removeSpaces(user));
});
it('should work with simple nesting Querys', function(){
let expeted = `user { profilePicture { uri, width, height } }`
let user = new Query("user");
user.find({"profilePicture": ['uri', 'width', 'height']});
expect(removeSpaces(expeted)).to.equal(removeSpaces(user));
});
it('should be able to group Querys', function(){
let expeted = `FetchLeeAndSam { lee: user(id: "1") { name },
sam: user(id: "2") { name } }`;
let FetchLeeAndSam = new Query("FetchLeeAndSam");
let lee = new Query("user",{id : '1'});
lee.setAlias('lee');
lee.find(["name"]);
let sam = new Query("user","sam");
sam.filter({id : '2'});
sam.find("name");
FetchLeeAndSam.find(lee,sam);
expect(removeSpaces(expeted)).to.equal(removeSpaces(FetchLeeAndSam));
});
it('should work with nasted objects and lists', function(){
let expeted =`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 }`;
let MessageRequest = { type:"chat",
message:"yoyo",
user:{ name:"bob",
screen:{ height:1080, width:1920 } },
friends:[ {id:1,name:"ann"}, {id:2,name:"tom"} ]
};
let MessageQuery = new Query("Message","myPost");
MessageQuery.filter(MessageRequest);
MessageQuery.find({ messageId : "id"}, {postedTime : "createTime" });
expect(removeSpaces(expeted)).to.equal(removeSpaces(MessageQuery));
});
it('should work with objects that have help functions(will skip function name)', function(){
let expeted ='inventory(toy:"jack in the box") { id }';
let ChildsToy = { toy:"jack in the box", getState:function(){ } };
ChildsToy.getState();//for istanbul(coverage) to say all fn was called
let ItemQuery = new Query("inventory",ChildsToy);
ItemQuery.find("id");
expect(removeSpaces(expeted)).to.equal(removeSpaces(ItemQuery));
});
it('should work with nasted objects that have help functions(will skip function name)', function(){
let expeted ='inventory(toy:"jack in the box") { id }';
let ChildsToy = { toy:"jack in the box", utils: { getState:function(){ } } };
ChildsToy.utils.getState();//for istanbul(coverage) to say all fn was called
let ItemQuery = new Query("inventory",ChildsToy);
ItemQuery.find("id");
expect(removeSpaces(expeted)).to.equal(removeSpaces(ItemQuery));
});
it('should skip empty objects in filter/args', function(){
let expeted ='inventory(toy:"jack in the box") { id }';
let ChildsToy = { toy:"jack in the box", utils: { } };
let ItemQuery = new Query("inventory",ChildsToy);
ItemQuery.find("id");
expect(removeSpaces(expeted)).to.equal(removeSpaces(ItemQuery));
});
it('should throw Error if find input items have zero props', function(){
expect(() => new Query("x").find({})).to.throw(Error);
});
it('should throw Error if find input items have multiple props', function(){
expect(() => new Query("x").find({a:"z",b:"y"})).to.throw(Error);
});
it('should throw Error if find is undefined', function(){
expect(() => new Query("x").find()).to.throw(Error);
});
it('should throw Error if no find values have been set', function(){
expect(() => `${new Query("x")}`).to.throw(Error);
});
it('should throw Error if find is not valid', function(){
expect(() => new Query("x").find(123)).to.throw(Error);
});
it('should throw Error if you accidentally pass an undefined', function(){
expect(() => new Query("x",undefined)).to.throw(Error);
});
it('should throw Error it is not an input object for alias', function(){
expect(() => new Query("x",true)).to.throw(Error);
});
});