Skip to content

Commit 9d6654c

Browse files
committed
Test refactorings
1 parent 11e455d commit 9d6654c

18 files changed

+1166
-994
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { Post, setupMockData } from "../../support/mock-data";
2+
import { Data } from "../../../src/support/interfaces";
3+
import { recordGraphQLRequest } from "../../support/helpers";
4+
5+
let store: any;
6+
let vuexOrmGraphQL;
7+
8+
describe("custom mutation", () => {
9+
beforeEach(async () => {
10+
[store, vuexOrmGraphQL] = await setupMockData();
11+
});
12+
13+
describe("via Model.mutate", () => {
14+
test("sends the correct query to the API", async () => {
15+
// @ts-ignore
16+
await Post.fetch(1);
17+
const post: Data = Post.find(1)! as Data;
18+
19+
const request = await recordGraphQLRequest(async () => {
20+
// @ts-ignore
21+
await Post.mutate({ name: "upvotePost", args: { captchaToken: "15", id: post.id } });
22+
});
23+
24+
expect(request!.variables.captchaToken).toEqual("15");
25+
expect(request!.variables.id).toEqual(post.id);
26+
expect(request!.query).toEqual(
27+
`
28+
mutation UpvotePost($captchaToken: String!, $id: ID!) {
29+
upvotePost(captchaToken: $captchaToken, id: $id) {
30+
id
31+
content
32+
title
33+
otherId
34+
published
35+
author {
36+
id
37+
name
38+
profile {
39+
id
40+
email
41+
age
42+
sex
43+
}
44+
}
45+
comments {
46+
nodes {
47+
id
48+
content
49+
subjectId
50+
subjectType
51+
author {
52+
id
53+
name
54+
profile {
55+
id
56+
email
57+
age
58+
sex
59+
}
60+
}
61+
}
62+
}
63+
tags {
64+
nodes {
65+
id
66+
name
67+
}
68+
}
69+
}
70+
}
71+
`.trim() + "\n"
72+
);
73+
});
74+
});
75+
76+
describe("via post.$mutate", () => {
77+
test("sends the correct query to the API", async () => {
78+
// @ts-ignore
79+
await Post.fetch(1);
80+
const post: Data = Post.find(1)! as Data;
81+
82+
const request = await recordGraphQLRequest(async () => {
83+
// @ts-ignore
84+
await post.$mutate({ name: "upvotePost", args: { captchaToken: "15" } });
85+
});
86+
87+
expect(request!.variables.captchaToken).toEqual("15");
88+
expect(request!.variables.id).toEqual(post.id);
89+
expect(request!.query).toEqual(
90+
`
91+
mutation UpvotePost($captchaToken: String!, $id: ID!) {
92+
upvotePost(captchaToken: $captchaToken, id: $id) {
93+
id
94+
content
95+
title
96+
otherId
97+
published
98+
author {
99+
id
100+
name
101+
profile {
102+
id
103+
email
104+
age
105+
sex
106+
}
107+
}
108+
comments {
109+
nodes {
110+
id
111+
content
112+
subjectId
113+
subjectType
114+
author {
115+
id
116+
name
117+
profile {
118+
id
119+
email
120+
age
121+
sex
122+
}
123+
}
124+
}
125+
}
126+
tags {
127+
nodes {
128+
id
129+
name
130+
}
131+
}
132+
}
133+
}
134+
`.trim() + "\n"
135+
);
136+
});
137+
});
138+
});
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { Post, setupMockData } from "../../support/mock-data";
2+
import { Data } from "../../../src/support/interfaces";
3+
import { recordGraphQLRequest } from "../../support/helpers";
4+
5+
let store: any;
6+
let vuexOrmGraphQL;
7+
8+
describe("custom query", () => {
9+
beforeEach(async () => {
10+
[store, vuexOrmGraphQL] = await setupMockData();
11+
});
12+
13+
test("via Model method sends the correct query to the API", async () => {
14+
const request = await recordGraphQLRequest(async () => {
15+
// @ts-ignore
16+
await Post.customQuery({ name: "unpublishedPosts", filter: { authorId: 3 } });
17+
});
18+
19+
expect(request!.variables.authorId).toEqual(3);
20+
expect(request!.query).toEqual(
21+
`
22+
query UnpublishedPosts($authorId: ID!) {
23+
unpublishedPosts(authorId: $authorId) {
24+
nodes {
25+
id
26+
content
27+
title
28+
otherId
29+
published
30+
author {
31+
id
32+
name
33+
profile {
34+
id
35+
email
36+
age
37+
sex
38+
}
39+
}
40+
comments {
41+
nodes {
42+
id
43+
content
44+
subjectId
45+
subjectType
46+
author {
47+
id
48+
name
49+
profile {
50+
id
51+
email
52+
age
53+
sex
54+
}
55+
}
56+
}
57+
}
58+
tags {
59+
nodes {
60+
id
61+
name
62+
}
63+
}
64+
}
65+
}
66+
}
67+
`.trim() + "\n"
68+
);
69+
});
70+
71+
test("via record method sends the correct query to the API", async () => {
72+
// @ts-ignore
73+
await Post.fetch(1);
74+
const post: Data = Post.find(1)! as Data;
75+
76+
const request = await recordGraphQLRequest(async () => {
77+
await post.$customQuery({ name: "unpublishedPosts", filter: { authorId: 2 } });
78+
});
79+
80+
expect(request!.variables.authorId).toEqual(2);
81+
expect(request!.variables.id).toEqual(1);
82+
expect(request!.query).toEqual(
83+
`
84+
query UnpublishedPosts($authorId: ID!, $id: ID!) {
85+
unpublishedPosts(authorId: $authorId, id: $id) {
86+
nodes {
87+
id
88+
content
89+
title
90+
otherId
91+
published
92+
author {
93+
id
94+
name
95+
profile {
96+
id
97+
email
98+
age
99+
sex
100+
}
101+
}
102+
comments {
103+
nodes {
104+
id
105+
content
106+
subjectId
107+
subjectType
108+
author {
109+
id
110+
name
111+
profile {
112+
id
113+
email
114+
age
115+
sex
116+
}
117+
}
118+
}
119+
}
120+
tags {
121+
nodes {
122+
id
123+
name
124+
}
125+
}
126+
}
127+
}
128+
}
129+
`.trim() + "\n"
130+
);
131+
});
132+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { Post, setupMockData } from "../../support/mock-data";
2+
import { Data } from "../../../src/support/interfaces";
3+
import { recordGraphQLRequest } from "../../support/helpers";
4+
5+
let store: any;
6+
let vuexOrmGraphQL;
7+
8+
describe("deleteAndDestroy", () => {
9+
beforeEach(async () => {
10+
[store, vuexOrmGraphQL] = await setupMockData();
11+
});
12+
13+
test("sends the correct query to the API and deletes the record", async () => {
14+
// @ts-ignore
15+
await Post.fetch(1);
16+
const post: Data = Post.find(1)! as Data;
17+
18+
const request = await recordGraphQLRequest(async () => {
19+
await post.$deleteAndDestroy();
20+
});
21+
22+
expect(request!.variables).toEqual({ id: 1 });
23+
expect(request!.query).toEqual(
24+
`
25+
mutation DeletePost($id: ID!) {
26+
deletePost(id: $id) {
27+
id
28+
content
29+
title
30+
otherId
31+
published
32+
author {
33+
id
34+
name
35+
profile {
36+
id
37+
email
38+
age
39+
sex
40+
}
41+
}
42+
comments {
43+
nodes {
44+
id
45+
content
46+
subjectId
47+
subjectType
48+
author {
49+
id
50+
name
51+
profile {
52+
id
53+
email
54+
age
55+
sex
56+
}
57+
}
58+
}
59+
}
60+
tags {
61+
nodes {
62+
id
63+
name
64+
}
65+
}
66+
}
67+
}
68+
`.trim() + "\n"
69+
);
70+
71+
expect(await Post.find(1)).toEqual(null);
72+
});
73+
});

0 commit comments

Comments
 (0)