Skip to content

Commit

Permalink
Test refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
phortx committed May 16, 2019
1 parent 11e455d commit 9d6654c
Show file tree
Hide file tree
Showing 18 changed files with 1,166 additions and 994 deletions.
138 changes: 138 additions & 0 deletions test/integration/actions/customMutation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { Post, setupMockData } from "../../support/mock-data";
import { Data } from "../../../src/support/interfaces";
import { recordGraphQLRequest } from "../../support/helpers";

let store: any;
let vuexOrmGraphQL;

describe("custom mutation", () => {
beforeEach(async () => {
[store, vuexOrmGraphQL] = await setupMockData();
});

describe("via Model.mutate", () => {
test("sends the correct query to the API", async () => {
// @ts-ignore
await Post.fetch(1);
const post: Data = Post.find(1)! as Data;

const request = await recordGraphQLRequest(async () => {
// @ts-ignore
await Post.mutate({ name: "upvotePost", args: { captchaToken: "15", id: post.id } });
});

expect(request!.variables.captchaToken).toEqual("15");
expect(request!.variables.id).toEqual(post.id);
expect(request!.query).toEqual(
`
mutation UpvotePost($captchaToken: String!, $id: ID!) {
upvotePost(captchaToken: $captchaToken, id: $id) {
id
content
title
otherId
published
author {
id
name
profile {
id
email
age
sex
}
}
comments {
nodes {
id
content
subjectId
subjectType
author {
id
name
profile {
id
email
age
sex
}
}
}
}
tags {
nodes {
id
name
}
}
}
}
`.trim() + "\n"
);
});
});

describe("via post.$mutate", () => {
test("sends the correct query to the API", async () => {
// @ts-ignore
await Post.fetch(1);
const post: Data = Post.find(1)! as Data;

const request = await recordGraphQLRequest(async () => {
// @ts-ignore
await post.$mutate({ name: "upvotePost", args: { captchaToken: "15" } });
});

expect(request!.variables.captchaToken).toEqual("15");
expect(request!.variables.id).toEqual(post.id);
expect(request!.query).toEqual(
`
mutation UpvotePost($captchaToken: String!, $id: ID!) {
upvotePost(captchaToken: $captchaToken, id: $id) {
id
content
title
otherId
published
author {
id
name
profile {
id
email
age
sex
}
}
comments {
nodes {
id
content
subjectId
subjectType
author {
id
name
profile {
id
email
age
sex
}
}
}
}
tags {
nodes {
id
name
}
}
}
}
`.trim() + "\n"
);
});
});
});
132 changes: 132 additions & 0 deletions test/integration/actions/customQuery.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { Post, setupMockData } from "../../support/mock-data";
import { Data } from "../../../src/support/interfaces";
import { recordGraphQLRequest } from "../../support/helpers";

let store: any;
let vuexOrmGraphQL;

describe("custom query", () => {
beforeEach(async () => {
[store, vuexOrmGraphQL] = await setupMockData();
});

test("via Model method sends the correct query to the API", async () => {
const request = await recordGraphQLRequest(async () => {
// @ts-ignore
await Post.customQuery({ name: "unpublishedPosts", filter: { authorId: 3 } });
});

expect(request!.variables.authorId).toEqual(3);
expect(request!.query).toEqual(
`
query UnpublishedPosts($authorId: ID!) {
unpublishedPosts(authorId: $authorId) {
nodes {
id
content
title
otherId
published
author {
id
name
profile {
id
email
age
sex
}
}
comments {
nodes {
id
content
subjectId
subjectType
author {
id
name
profile {
id
email
age
sex
}
}
}
}
tags {
nodes {
id
name
}
}
}
}
}
`.trim() + "\n"
);
});

test("via record method sends the correct query to the API", async () => {
// @ts-ignore
await Post.fetch(1);
const post: Data = Post.find(1)! as Data;

const request = await recordGraphQLRequest(async () => {
await post.$customQuery({ name: "unpublishedPosts", filter: { authorId: 2 } });
});

expect(request!.variables.authorId).toEqual(2);
expect(request!.variables.id).toEqual(1);
expect(request!.query).toEqual(
`
query UnpublishedPosts($authorId: ID!, $id: ID!) {
unpublishedPosts(authorId: $authorId, id: $id) {
nodes {
id
content
title
otherId
published
author {
id
name
profile {
id
email
age
sex
}
}
comments {
nodes {
id
content
subjectId
subjectType
author {
id
name
profile {
id
email
age
sex
}
}
}
}
tags {
nodes {
id
name
}
}
}
}
}
`.trim() + "\n"
);
});
});
73 changes: 73 additions & 0 deletions test/integration/actions/deleteAndDestroy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Post, setupMockData } from "../../support/mock-data";
import { Data } from "../../../src/support/interfaces";
import { recordGraphQLRequest } from "../../support/helpers";

let store: any;
let vuexOrmGraphQL;

describe("deleteAndDestroy", () => {
beforeEach(async () => {
[store, vuexOrmGraphQL] = await setupMockData();
});

test("sends the correct query to the API and deletes the record", async () => {
// @ts-ignore
await Post.fetch(1);
const post: Data = Post.find(1)! as Data;

const request = await recordGraphQLRequest(async () => {
await post.$deleteAndDestroy();
});

expect(request!.variables).toEqual({ id: 1 });
expect(request!.query).toEqual(
`
mutation DeletePost($id: ID!) {
deletePost(id: $id) {
id
content
title
otherId
published
author {
id
name
profile {
id
email
age
sex
}
}
comments {
nodes {
id
content
subjectId
subjectType
author {
id
name
profile {
id
email
age
sex
}
}
}
}
tags {
nodes {
id
name
}
}
}
}
`.trim() + "\n"
);

expect(await Post.find(1)).toEqual(null);
});
});
Loading

0 comments on commit 9d6654c

Please sign in to comment.