From 4c1f2e72cb142b1a145cf5e537ffb8df84d6d6a2 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Fri, 31 Aug 2018 17:24:42 +0200 Subject: [PATCH] Use createSinonStub --- .../framework/testHelpers/createSinonStub.ts | 20 +++++ src/core/client/stream/test/loadMore.spec.tsx | 87 ++++++++++--------- .../client/stream/test/permalinkView.spec.tsx | 19 ++-- .../permalinkViewCommentNotFound.spec.tsx | 10 +-- .../client/stream/test/renderReplies.spec.tsx | 14 +-- .../client/stream/test/renderStream.spec.tsx | 11 ++- .../stream/test/showAllReplies.spec.tsx | 87 ++++++++++--------- 7 files changed, 135 insertions(+), 113 deletions(-) diff --git a/src/core/client/framework/testHelpers/createSinonStub.ts b/src/core/client/framework/testHelpers/createSinonStub.ts index 5a7016d90..a1d92cddf 100644 --- a/src/core/client/framework/testHelpers/createSinonStub.ts +++ b/src/core/client/framework/testHelpers/createSinonStub.ts @@ -1,5 +1,25 @@ import sinon, { SinonStub } from "sinon"; +/** + * createSinonStub assist in setting up a stub with different + * return paths. + * + * e.g. + * ``` + * const s = sinon.stub(); + * s.throws(); + * s.withArgs("a").returns(0); + * s.withArgs("b").returns(1); + * ``` + * is equivalent to. + * ``` + * const s = createSinonStub( + * s => s.throws(), + * s => s.withArgs("a").returns(0), + * s => s.withArgs("b").returns(1), + * ); + * ``` + */ export default function createSinonStub( ...callbacks: Array<(s: SinonStub) => void> ): SinonStub { diff --git a/src/core/client/stream/test/loadMore.spec.tsx b/src/core/client/stream/test/loadMore.spec.tsx index 5341b39ee..f58a80004 100644 --- a/src/core/client/stream/test/loadMore.spec.tsx +++ b/src/core/client/stream/test/loadMore.spec.tsx @@ -1,12 +1,12 @@ import React from "react"; import TestRenderer, { ReactTestRenderer } from "react-test-renderer"; -import sinon from "sinon"; import { timeout } from "talk-common/utils"; import { TalkContext, TalkContextProvider } from "talk-framework/lib/bootstrap"; import { PostMessageService } from "talk-framework/lib/postMessage"; import { RestClient } from "talk-framework/lib/rest"; import { createInMemoryStorage } from "talk-framework/lib/storage"; +import { createSinonStub } from "talk-framework/testHelpers"; import AppContainer from "talk-stream/containers/AppContainer"; import createEnvironment from "./createEnvironment"; @@ -16,54 +16,55 @@ import { assets, comments } from "./fixtures"; let testRenderer: ReactTestRenderer; beforeEach(() => { - const connectionStub = sinon.stub().throws(); - connectionStub.withArgs({ first: 5, orderBy: "CREATED_AT_DESC" }).returns({ - edges: [ - { - node: comments[0], - cursor: comments[0].createdAt, - }, - { - node: comments[1], - cursor: comments[1].createdAt, - }, - ], - pageInfo: { - endCursor: comments[1].createdAt, - hasNextPage: true, - }, - }); - connectionStub - .withArgs({ - first: 10, - orderBy: "CREATED_AT_DESC", - after: comments[1].createdAt, - }) - .returns({ - edges: [ - { - node: comments[2], - cursor: comments[2].createdAt, - }, - ], - pageInfo: { - endCursor: comments[2].createdAt, - hasNextPage: false, - }, - }); - const assetStub = { ...assets[0], - comments: connectionStub, + comments: createSinonStub( + s => s.throws(), + s => + s.withArgs({ first: 5, orderBy: "CREATED_AT_DESC" }).returns({ + edges: [ + { + node: comments[0], + cursor: comments[0].createdAt, + }, + { + node: comments[1], + cursor: comments[1].createdAt, + }, + ], + pageInfo: { + endCursor: comments[1].createdAt, + hasNextPage: true, + }, + }), + s => + s + .withArgs({ + first: 10, + orderBy: "CREATED_AT_DESC", + after: comments[1].createdAt, + }) + .returns({ + edges: [ + { + node: comments[2], + cursor: comments[2].createdAt, + }, + ], + pageInfo: { + endCursor: comments[2].createdAt, + hasNextPage: false, + }, + }) + ), }; const resolvers = { Query: { - asset: sinon - .stub() - .throws() - .withArgs(undefined, { id: assetStub.id }) - .returns(assetStub), + asset: createSinonStub( + s => s.throws(), + s => s.withArgs(undefined, { id: assetStub.id }).returns(assetStub) + ), }, }; diff --git a/src/core/client/stream/test/permalinkView.spec.tsx b/src/core/client/stream/test/permalinkView.spec.tsx index 666f4af5e..c68d3fde3 100644 --- a/src/core/client/stream/test/permalinkView.spec.tsx +++ b/src/core/client/stream/test/permalinkView.spec.tsx @@ -8,6 +8,7 @@ import { TalkContext, TalkContextProvider } from "talk-framework/lib/bootstrap"; import { PostMessageService } from "talk-framework/lib/postMessage"; import { RestClient } from "talk-framework/lib/rest"; import { createInMemoryStorage } from "talk-framework/lib/storage"; +import { createSinonStub } from "talk-framework/testHelpers"; import AppContainer from "talk-stream/containers/AppContainer"; import createEnvironment from "./createEnvironment"; @@ -38,16 +39,14 @@ beforeEach(() => { const resolvers = { Query: { - comment: sinon - .stub() - .throws() - .withArgs(undefined, { id: commentStub.id }) - .returns(commentStub), - asset: sinon - .stub() - .throws() - .withArgs(undefined, { id: assetStub.id }) - .returns(assetStub), + comment: createSinonStub( + s => s.throws(), + s => s.withArgs(undefined, { id: commentStub.id }).returns(commentStub) + ), + asset: createSinonStub( + s => s.throws(), + s => s.withArgs(undefined, { id: assetStub.id }).returns(assetStub) + ), }, }; diff --git a/src/core/client/stream/test/permalinkViewCommentNotFound.spec.tsx b/src/core/client/stream/test/permalinkViewCommentNotFound.spec.tsx index 368e29bc3..f49dc0534 100644 --- a/src/core/client/stream/test/permalinkViewCommentNotFound.spec.tsx +++ b/src/core/client/stream/test/permalinkViewCommentNotFound.spec.tsx @@ -8,6 +8,7 @@ import { TalkContext, TalkContextProvider } from "talk-framework/lib/bootstrap"; import { PostMessageService } from "talk-framework/lib/postMessage"; import { RestClient } from "talk-framework/lib/rest"; import { createInMemoryStorage } from "talk-framework/lib/storage"; +import { createSinonStub } from "talk-framework/testHelpers"; import AppContainer from "talk-stream/containers/AppContainer"; import createEnvironment from "./createEnvironment"; @@ -39,11 +40,10 @@ beforeEach(() => { const resolvers = { Query: { comment: () => null, - asset: sinon - .stub() - .throws() - .withArgs(undefined, { id: assetStub.id }) - .returns(assetStub), + asset: createSinonStub( + s => s.throws(), + s => s.withArgs(undefined, { id: assetStub.id }).returns(assetStub) + ), }, }; diff --git a/src/core/client/stream/test/renderReplies.spec.tsx b/src/core/client/stream/test/renderReplies.spec.tsx index 673f6da60..970d1a31b 100644 --- a/src/core/client/stream/test/renderReplies.spec.tsx +++ b/src/core/client/stream/test/renderReplies.spec.tsx @@ -1,13 +1,13 @@ import React from "react"; import TestRenderer, { ReactTestRenderer } from "react-test-renderer"; import { RecordProxy } from "relay-runtime"; -import sinon from "sinon"; import { timeout } from "talk-common/utils"; import { TalkContext, TalkContextProvider } from "talk-framework/lib/bootstrap"; import { PostMessageService } from "talk-framework/lib/postMessage"; import { RestClient } from "talk-framework/lib/rest"; import { createInMemoryStorage } from "talk-framework/lib/storage"; +import { createSinonStub } from "talk-framework/testHelpers"; import AppContainer from "talk-stream/containers/AppContainer"; import createEnvironment from "./createEnvironment"; @@ -19,11 +19,13 @@ let testRenderer: ReactTestRenderer; beforeEach(() => { const resolvers = { Query: { - asset: sinon - .stub() - .throws() - .withArgs(undefined, { id: assetWithReplies.id }) - .returns(assetWithReplies), + asset: createSinonStub( + s => s.throws(), + s => + s + .withArgs(undefined, { id: assetWithReplies.id }) + .returns(assetWithReplies) + ), }, }; diff --git a/src/core/client/stream/test/renderStream.spec.tsx b/src/core/client/stream/test/renderStream.spec.tsx index 3e95fbc25..3ea0d74c6 100644 --- a/src/core/client/stream/test/renderStream.spec.tsx +++ b/src/core/client/stream/test/renderStream.spec.tsx @@ -1,13 +1,13 @@ import React from "react"; import TestRenderer, { ReactTestRenderer } from "react-test-renderer"; import { RecordProxy } from "relay-runtime"; -import sinon from "sinon"; import { timeout } from "talk-common/utils"; import { TalkContext, TalkContextProvider } from "talk-framework/lib/bootstrap"; import { PostMessageService } from "talk-framework/lib/postMessage"; import { RestClient } from "talk-framework/lib/rest"; import { createInMemoryStorage } from "talk-framework/lib/storage"; +import { createSinonStub } from "talk-framework/testHelpers"; import AppContainer from "talk-stream/containers/AppContainer"; import createEnvironment from "./createEnvironment"; @@ -19,11 +19,10 @@ let testRenderer: ReactTestRenderer; beforeEach(() => { const resolvers = { Query: { - asset: sinon - .stub() - .throws() - .withArgs(undefined, { id: assets[0].id }) - .returns(assets[0]), + asset: createSinonStub( + s => s.throws(), + s => s.withArgs(undefined, { id: assets[0].id }).returns(assets[0]) + ), }, }; diff --git a/src/core/client/stream/test/showAllReplies.spec.tsx b/src/core/client/stream/test/showAllReplies.spec.tsx index 052e38792..a76aab40d 100644 --- a/src/core/client/stream/test/showAllReplies.spec.tsx +++ b/src/core/client/stream/test/showAllReplies.spec.tsx @@ -8,6 +8,7 @@ import { TalkContext, TalkContextProvider } from "talk-framework/lib/bootstrap"; import { PostMessageService } from "talk-framework/lib/postMessage"; import { RestClient } from "talk-framework/lib/rest"; import { createInMemoryStorage } from "talk-framework/lib/storage"; +import { createSinonStub } from "talk-framework/testHelpers"; import AppContainer from "talk-stream/containers/AppContainer"; import createEnvironment from "./createEnvironment"; @@ -17,41 +18,43 @@ import { assets, comments } from "./fixtures"; let testRenderer: ReactTestRenderer; beforeEach(() => { - const connectionStub = sinon.stub().throws(); - connectionStub.withArgs({ first: 5, orderBy: "CREATED_AT_ASC" }).returns({ - edges: [ - { - node: comments[1], - cursor: comments[1].createdAt, - }, - ], - pageInfo: { - endCursor: comments[1].createdAt, - hasNextPage: true, - }, - }); - connectionStub - .withArgs({ - first: sinon.match(n => n > 10000), - orderBy: "CREATED_AT_ASC", - after: comments[1].createdAt, - }) - .returns({ - edges: [ - { - node: comments[2], - cursor: comments[2].createdAt, - }, - ], - pageInfo: { - endCursor: comments[2].createdAt, - hasNextPage: false, - }, - }); - const commentStub = { ...comments[0], - replies: connectionStub, + replies: createSinonStub( + s => s.throws(), + s => + s.withArgs({ first: 5, orderBy: "CREATED_AT_ASC" }).returns({ + edges: [ + { + node: comments[1], + cursor: comments[1].createdAt, + }, + ], + pageInfo: { + endCursor: comments[1].createdAt, + hasNextPage: true, + }, + }), + s => + s + .withArgs({ + first: sinon.match(n => n > 10000), + orderBy: "CREATED_AT_ASC", + after: comments[1].createdAt, + }) + .returns({ + edges: [ + { + node: comments[2], + cursor: comments[2].createdAt, + }, + ], + pageInfo: { + endCursor: comments[2].createdAt, + hasNextPage: false, + }, + }) + ), }; const assetStub = { @@ -71,16 +74,14 @@ beforeEach(() => { const resolvers = { Query: { - comment: sinon - .stub() - .throws() - .withArgs(undefined, { id: commentStub.id }) - .returns(commentStub), - asset: sinon - .stub() - .throws() - .withArgs(undefined, { id: assetStub.id }) - .returns(assetStub), + comment: createSinonStub( + s => s.throws(), + s => s.withArgs(undefined, { id: commentStub.id }).returns(commentStub) + ), + asset: createSinonStub( + s => s.throws(), + s => s.withArgs(undefined, { id: assetStub.id }).returns(assetStub) + ), }, };