Skip to content

Commit af2ec8d

Browse files
committed
Increase coverage
1 parent 279ce8d commit af2ec8d

File tree

7 files changed

+207
-27
lines changed

7 files changed

+207
-27
lines changed

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,16 @@
135135
],
136136
"coverageThreshold": {
137137
"global": {
138-
"branches": 50,
139-
"functions": 85,
140-
"lines": 85,
141-
"statements": 80
138+
"statements": 92,
139+
"branches": 80,
140+
"functions": 98,
141+
"lines": 93
142142
}
143143
},
144144
"collectCoverage": true,
145+
"coverageReporters": ["json", "lcov", "html"],
145146
"collectCoverageFrom": [
146-
"src/*.{js,ts}"
147+
"src/**/*.{js,ts}"
147148
]
148149
},
149150
"commitlint": {

src/support/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* istanbul ignore file */
12
import { Database, Model as ORMModel } from "@vuex-orm/core";
23
import RootState from "@vuex-orm/core/lib/modules/contracts/RootState";
34
import { ApolloLink } from "apollo-link";

src/test-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import VuexORMGraphQLPlugin from "./index";
66
let context: Context | null = null;
77

88
export function setupTestUtils(plugin: typeof VuexORMGraphQLPlugin): void {
9+
/* istanbul ignore next */
910
if (!plugin.instance) {
1011
throw new Error("Please call this function after setting up the store!");
1112
}
@@ -57,6 +58,7 @@ export class Mock {
5758
}
5859

5960
export async function clearORMStore() {
61+
/* istanbul ignore next */
6062
if (!context) {
6163
throw new Error("Please call setupTestUtils() before!");
6264
}
@@ -65,6 +67,7 @@ export async function clearORMStore() {
6567
}
6668

6769
export function mock(action: string, options?: MockOptions): Mock {
70+
/* istanbul ignore next */
6871
if (!context) {
6972
throw new Error("Please call setupTestUtils() before!");
7073
}

test/integration/plugin.spec.ts

Lines changed: 131 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,69 @@ mutation DeletePost($id: ID!) {
473473
});
474474
});
475475

476+
describe("deleteAndDestroy", () => {
477+
test("sends the correct query to the API and deletes the record", async () => {
478+
// @ts-ignore
479+
await Post.fetch(1);
480+
const post = Post.find(1)!;
481+
482+
const request = await recordGraphQLRequest(async () => {
483+
await post.$deleteAndDestroy();
484+
});
485+
486+
expect(request!.variables).toEqual({ id: 1 });
487+
expect(request!.query).toEqual(
488+
`
489+
mutation DeletePost($id: ID!) {
490+
deletePost(id: $id) {
491+
id
492+
content
493+
title
494+
otherId
495+
published
496+
author {
497+
id
498+
name
499+
profile {
500+
id
501+
email
502+
age
503+
sex
504+
}
505+
}
506+
comments {
507+
nodes {
508+
id
509+
content
510+
subjectId
511+
subjectType
512+
author {
513+
id
514+
name
515+
profile {
516+
id
517+
email
518+
age
519+
sex
520+
}
521+
}
522+
}
523+
}
524+
tags {
525+
nodes {
526+
id
527+
name
528+
}
529+
}
530+
}
531+
}
532+
`.trim() + "\n"
533+
);
534+
535+
expect(await Post.find(1)).toEqual(null);
536+
});
537+
});
538+
476539
describe("custom query", () => {
477540
test("via Model method sends the correct query to the API", async () => {
478541
const request = await recordGraphQLRequest(async () => {
@@ -595,23 +658,86 @@ query UnpublishedPosts($authorId: ID!, $id: ID!) {
595658
});
596659
});
597660

598-
describe("custom mutation", () => {
661+
describe("custom mutation via Model.mutate", () => {
662+
test("sends the correct query to the API", async () => {
663+
// @ts-ignore
664+
await Post.fetch(1);
665+
const post = Post.find(1)!;
666+
667+
const request = await recordGraphQLRequest(async () => {
668+
// @ts-ignore
669+
await Post.mutate({ name: "upvotePost", args: { captchaToken: "15", id: post.id } });
670+
});
671+
672+
expect(request!.variables.captchaToken).toEqual("15");
673+
expect(request!.variables.id).toEqual(post.id);
674+
expect(request!.query).toEqual(
675+
`
676+
mutation UpvotePost($captchaToken: String!, $id: ID!) {
677+
upvotePost(captchaToken: $captchaToken, id: $id) {
678+
id
679+
content
680+
title
681+
otherId
682+
published
683+
author {
684+
id
685+
name
686+
profile {
687+
id
688+
email
689+
age
690+
sex
691+
}
692+
}
693+
comments {
694+
nodes {
695+
id
696+
content
697+
subjectId
698+
subjectType
699+
author {
700+
id
701+
name
702+
profile {
703+
id
704+
email
705+
age
706+
sex
707+
}
708+
}
709+
}
710+
}
711+
tags {
712+
nodes {
713+
id
714+
name
715+
}
716+
}
717+
}
718+
}
719+
`.trim() + "\n"
720+
);
721+
});
722+
});
723+
724+
describe("custom mutation via post.$mutate", () => {
599725
test("sends the correct query to the API", async () => {
600726
// @ts-ignore
601727
await Post.fetch(1);
602728
const post = Post.find(1)!;
603729

604730
const request = await recordGraphQLRequest(async () => {
605731
// @ts-ignore
606-
await Post.mutate({ name: "upvotePost", args: { captchaToken: "15", postId: post.id } });
732+
await post.$mutate({ name: "upvotePost", args: { captchaToken: "15" } });
607733
});
608734

609735
expect(request!.variables.captchaToken).toEqual("15");
610-
expect(request!.variables.postId).toEqual(post.id);
736+
expect(request!.variables.id).toEqual(post.id);
611737
expect(request!.query).toEqual(
612738
`
613-
mutation UpvotePost($captchaToken: String!, $postId: ID!) {
614-
upvotePost(captchaToken: $captchaToken, postId: $postId) {
739+
mutation UpvotePost($captchaToken: String!, $id: ID!) {
740+
upvotePost(captchaToken: $captchaToken, id: $id) {
615741
id
616742
content
617743
title

test/integration/test-utils.spec.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { setupMockData, User, Post } from "../support/mock-data";
2-
import { mock } from "../../src/test-utils";
2+
import { clearORMStore, mock, setupTestUtils } from "../../src/test-utils";
33
import { recordGraphQLRequest } from "../support/helpers";
4+
import VuexORMGraphQLPlugin from "../../src";
45

56
// @ts-ignore
67
let store;
@@ -31,7 +32,7 @@ describe("TestUtils", () => {
3132
[store, vuexOrmGraphQL] = await setupMockData();
3233
});
3334

34-
test("allows to mock a fetch", async () => {
35+
it("allows to mock a fetch", async () => {
3536
mock("fetch", { filter: { id: 42 } })
3637
.for(User)
3738
.andReturn(userData);
@@ -46,7 +47,7 @@ describe("TestUtils", () => {
4647
expect(result).toEqual(userResult);
4748
});
4849

49-
test("allows to return multiple records", async () => {
50+
it("allows to return multiple records", async () => {
5051
const userData2 = JSON.parse(JSON.stringify(userData));
5152
userData2.id = 8;
5253
userData2.name = "Snoopy";
@@ -88,7 +89,7 @@ describe("TestUtils", () => {
8889
});
8990
});
9091

91-
test("only mocks matched options", async () => {
92+
it("only mocks matched options", async () => {
9293
mock("fetch", { filter: { id: 42 } })
9394
.for(User)
9495
.andReturn(userData);
@@ -103,7 +104,26 @@ describe("TestUtils", () => {
103104
expect(result).not.toEqual(userResult);
104105
});
105106

106-
test("allows to mock a action with a dynamic value", async () => {
107+
it("only mocks once", async () => {
108+
mock("fetch", { filter: { id: 42 } })
109+
.for(User)
110+
.andReturn(userData);
111+
112+
mock("fetch", { filter: { id: 42 } })
113+
.for(User)
114+
.andReturn(userData);
115+
116+
let result;
117+
const request = await recordGraphQLRequest(async () => {
118+
// @ts-ignore
119+
result = await User.fetch(1);
120+
}, true);
121+
122+
expect(request).not.toEqual(null);
123+
expect(result).not.toEqual(userResult);
124+
});
125+
126+
it("allows to mock a action with a dynamic value", async () => {
107127
mock("fetch", { filter: { id: 42 } })
108128
.for(User)
109129
.andReturn(() => userData);
@@ -118,7 +138,7 @@ describe("TestUtils", () => {
118138
expect(result).toEqual(userResult);
119139
});
120140

121-
test("allows to mock a action without options", async () => {
141+
it("allows to mock a action without options", async () => {
122142
mock("fetch")
123143
.for(User)
124144
.andReturn(() => userData);
@@ -133,7 +153,7 @@ describe("TestUtils", () => {
133153
expect(result).toEqual(userResult);
134154
});
135155

136-
test("allows to mock a action without partial matching options", async () => {
156+
it("allows to mock a action without partial matching options", async () => {
137157
mock("fetch", { filter: { id: 42 } })
138158
.for(User)
139159
.andReturn(() => userData);
@@ -148,7 +168,7 @@ describe("TestUtils", () => {
148168
expect(result).toEqual(userResult);
149169
});
150170

151-
test("allows to mock a destroy", async () => {
171+
it("allows to mock a destroy", async () => {
152172
mock("destroy", { id: 42 })
153173
.for(User)
154174
.andReturn(userData);
@@ -165,7 +185,7 @@ describe("TestUtils", () => {
165185
expect(result).toEqual(true);
166186
});
167187

168-
test("allows to mock a mutate", async () => {
188+
it("allows to mock a mutate", async () => {
169189
mock("mutate", { name: "upvote", args: { id: 4 } })
170190
.for(Post)
171191
.andReturn({
@@ -203,7 +223,7 @@ describe("TestUtils", () => {
203223
});
204224
});
205225

206-
test("allows to mock a persist", async () => {
226+
it("allows to mock a persist", async () => {
207227
mock("persist", { id: 42 })
208228
.for(User)
209229
.andReturn(userData);
@@ -220,7 +240,7 @@ describe("TestUtils", () => {
220240
expect(result).toEqual(userResult);
221241
});
222242

223-
test("allows to mock a push", async () => {
243+
it("allows to mock a push", async () => {
224244
mock("push")
225245
.for(User)
226246
.andReturn(userData);
@@ -237,7 +257,7 @@ describe("TestUtils", () => {
237257
expect(result).toEqual(userResult);
238258
});
239259

240-
test("allows to mock a custom query", async () => {
260+
it("allows to mock a custom query", async () => {
241261
mock("query", { name: "example" })
242262
.for(User)
243263
.andReturn(userData);
@@ -252,7 +272,7 @@ describe("TestUtils", () => {
252272
expect(result).not.toEqual(null);
253273
});
254274

255-
test("allows to mock a simple mutation", async () => {
275+
it("allows to mock a simple mutation", async () => {
256276
mock("simpleMutation", {
257277
name: "SendSms",
258278
variables: { to: "+4912345678", text: "GraphQL is awesome!" }
@@ -279,7 +299,7 @@ describe("TestUtils", () => {
279299
expect(result).toEqual({ sendSms: { delivered: true } });
280300
});
281301

282-
test("allows to mock a simple query", async () => {
302+
it("allows to mock a simple query", async () => {
283303
mock("simpleQuery", { name: "example" }).andReturn({ success: true });
284304

285305
let result;
@@ -297,4 +317,13 @@ describe("TestUtils", () => {
297317
expect(request).toEqual(null);
298318
expect(result).toEqual({ success: true });
299319
});
320+
321+
describe("clearORMStore", () => {
322+
it("cleans the store", async () => {
323+
await Post.create({ data: { name: "test" } });
324+
expect(Post.find(1)).not.toEqual(null);
325+
await clearORMStore();
326+
expect(Post.find(1)).toEqual(null);
327+
});
328+
});
300329
});

test/support/mock-schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const typeDefs = `
6464
updateTariffOption(id: ID!, tariffOption: TariffOptionInput!): TariffOption!
6565
updateTariff(id: ID!, tariff: TariffInput!): Tariff!
6666
67-
upvotePost(captchaToken: String!, postId: ID!): Post!
67+
upvotePost(captchaToken: String!, id: ID!): Post!
6868
sendSms(to: String!, text: String!): SmsStatus!
6969
reorderItems(id: ID!, itemIds: [ID]!): PostTypeConnection
7070
}
@@ -832,7 +832,7 @@ export const resolvers = {
832832
Mutation: {
833833
// Customs
834834

835-
upvotePost: (parent: any, { captchaToken, postId }: any) => findOne(Post, posts, postId),
835+
upvotePost: (parent: any, { captchaToken, id }: any) => findOne(Post, posts, id),
836836
sendSms: (parent: any, { to, text }: any) => ({ delivered: true }),
837837

838838
// Deletes

0 commit comments

Comments
 (0)