-
Notifications
You must be signed in to change notification settings - Fork 5
/
schema.test.js
52 lines (48 loc) · 1.23 KB
/
schema.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
const schema = require("./schema");
const { graphql } = require("graphql");
const PostModel = require("./PostModel");
const UserModel = require("./UserModel");
class MockJsonPlaceholder {
constructor(data) {
this.data = data;
}
async query(resource) {
const [uri, querystring] = resource.split("?");
const [type, param] = uri.split("/");
if (querystring) {
const ids = querystring.split("&");
const idValues = ids.map(id => id.split("=")[1]);
const idName = ids[0].split("=")[0];
return idValues.map(idValue =>
this.data[type].filter(obj => obj[idName] === idValue)
);
} else {
return this.data[type].find(obj => obj.id === param);
}
return null;
}
}
const mockPlaceholder = new MockJsonPlaceholder(require("./data2"));
it("returns post", async () => {
const result = await graphql({
schema: schema,
source: `
{
postById(id:"81") {
title
}
}
`,
contextValue: {
postModel: new PostModel(mockPlaceholder),
userModel: new UserModel(mockPlaceholder)
}
});
expect(result).toEqual({
data: {
postById: {
title: "tempora rem veritatis voluptas quo dolores vero"
}
}
});
});