From 16735104841941467b1d43a1bc19024d41b72c73 Mon Sep 17 00:00:00 2001 From: Andre Miras Date: Tue, 8 Feb 2022 12:36:37 +0100 Subject: [PATCH] :recycle: refacto: Use fixtures --- src/components/Container.test.tsx | 31 +----------- src/components/ResultTable.test.tsx | 49 ++----------------- .../__snapshots__/ResultTable.test.tsx.snap | 8 +-- src/utils/fixtures.ts | 46 +++++++++++++++++ src/utils/search.test.ts | 14 +----- 5 files changed, 57 insertions(+), 91 deletions(-) create mode 100644 src/utils/fixtures.ts diff --git a/src/components/Container.test.tsx b/src/components/Container.test.tsx index 81cb95c..59a094f 100644 --- a/src/components/Container.test.tsx +++ b/src/components/Container.test.tsx @@ -6,6 +6,7 @@ import { fab } from "@fortawesome/free-brands-svg-icons"; import { fas } from "@fortawesome/free-solid-svg-icons"; import * as search from "../utils/search"; import { Node } from "../utils/types"; +import { origin, forks } from "../utils/fixtures"; import Container from "./Container"; library.add(fab, fas); @@ -19,35 +20,7 @@ test("search forks", (done) => { render(); const searchInput = screen.getByPlaceholderText(/github.com/); const submitButton = screen.getByRole("button"); - const forkId = "django-nonrel/django"; - const origin = { - nameWithOwner: "django/django", - stargazerCount: 54393, - forkCount: 23386, - defaultBranchRef: { - target: { - committedDate: "2020-12-18T08:23:22Z", - history: { - totalCount: 29060, - }, - }, - }, - }; - const forks = [ - { - nameWithOwner: forkId, - stargazerCount: 214, - forkCount: 84, - defaultBranchRef: { - target: { - committedDate: "2020-08-29T14:23:26Z", - history: { - totalCount: 13990, - }, - }, - }, - }, - ]; + const forkId = forks[0].nameWithOwner; const searchResult = [...[origin], ...forks]; const spy = jest .spyOn(search, "searchPopularForks") diff --git a/src/components/ResultTable.test.tsx b/src/components/ResultTable.test.tsx index df32dd3..9b690b8 100644 --- a/src/components/ResultTable.test.tsx +++ b/src/components/ResultTable.test.tsx @@ -4,55 +4,14 @@ import { library } from "@fortawesome/fontawesome-svg-core"; import { fab } from "@fortawesome/free-brands-svg-icons"; import { fas } from "@fortawesome/free-solid-svg-icons"; import ResultTable from "./ResultTable"; +import { originAndForks as forks } from "../utils/fixtures"; library.add(fab, fas); -const forks = [ - { - nameWithOwner: "django-nonrel/django", - stargazerCount: 214, - forkCount: 84, - defaultBranchRef: { - target: { - committedDate: "2020-08-29T14:23:26Z", - history: { - totalCount: 13990, - }, - }, - }, - }, - { - nameWithOwner: "django/django", - stargazerCount: 54330, - forkCount: 23377, - defaultBranchRef: { - target: { - committedDate: "2020-12-07:14:12Z", - history: { - totalCount: 29000, - }, - }, - }, - }, - { - nameWithOwner: "FlipperPA/django-mssql-backend", - stargazerCount: 18, - forkCount: 2, - defaultBranchRef: { - target: { - committedDate: "2020-01-13:11:12Z", - history: { - totalCount: 28017, - }, - }, - }, - }, -]; - test("renders", () => { const spy = jest .spyOn(Date, "now") - .mockImplementation(() => new Date("2020-12-08T19:18:03.135Z").valueOf()); + .mockImplementation(() => new Date("2020-12-18T19:18:03.135Z").valueOf()); const tree = renderer .create( { /> ); // sorted by stargazerCount default - expect(screen.getByText(forks[1].nameWithOwner)).toBeInTheDocument(); - expect(screen.queryByText(forks[0].nameWithOwner)).not.toBeInTheDocument(); + expect(screen.getByText(forks[0].nameWithOwner)).toBeInTheDocument(); + expect(screen.queryByText(forks[1].nameWithOwner)).not.toBeInTheDocument(); expect(screen.queryByText(forks[2].nameWithOwner)).not.toBeInTheDocument(); const repoTableHeader = screen.getByText("Repo"); // let's sort by repo name diff --git a/src/components/__snapshots__/ResultTable.test.tsx.snap b/src/components/__snapshots__/ResultTable.test.tsx.snap index 9755ad8..20b4546 100644 --- a/src/components/__snapshots__/ResultTable.test.tsx.snap +++ b/src/components/__snapshots__/ResultTable.test.tsx.snap @@ -223,16 +223,16 @@ Array [ - 54330 + 54393 - 23377 + 23386 - 29000 + 29060 - 1 days ago + 10 hours ago diff --git a/src/utils/fixtures.ts b/src/utils/fixtures.ts new file mode 100644 index 0000000..f73e6e6 --- /dev/null +++ b/src/utils/fixtures.ts @@ -0,0 +1,46 @@ +const origin = { + nameWithOwner: "django/django", + stargazerCount: 54393, + forkCount: 23386, + defaultBranchRef: { + target: { + committedDate: "2020-12-18T08:23:22Z", + history: { + totalCount: 29060, + }, + }, + }, +}; + +const forks = [ + { + nameWithOwner: "django-nonrel/django", + stargazerCount: 214, + forkCount: 84, + defaultBranchRef: { + target: { + committedDate: "2020-08-29T14:23:26Z", + history: { + totalCount: 13990, + }, + }, + }, + }, + { + nameWithOwner: "FlipperPA/django-mssql-backend", + stargazerCount: 18, + forkCount: 2, + defaultBranchRef: { + target: { + committedDate: "2020-01-13:11:12Z", + history: { + totalCount: 28017, + }, + }, + }, + }, +]; + +const originAndForks = [origin, ...forks]; + +export { origin, forks, originAndForks }; diff --git a/src/utils/search.test.ts b/src/utils/search.test.ts index 2baa686..d7b5cf9 100644 --- a/src/utils/search.test.ts +++ b/src/utils/search.test.ts @@ -2,25 +2,13 @@ import { ApolloQueryResult, ApolloError } from "@apollo/client"; import { searchPopularForks } from "./search"; import { Node } from "./types"; import { client } from "./graphql"; +import { origin } from "./fixtures"; test("basic case", (done) => { const url = "https://github.com/django/django"; const forks = { nodes: [], }; - const origin = { - nameWithOwner: "django/django", - stargazerCount: 54393, - forkCount: 23386, - defaultBranchRef: { - target: { - committedDate: "2020-12-18T08:23:22Z", - history: { - totalCount: 29060, - }, - }, - }, - }; const repository = { ...origin, forks,