Skip to content

Commit

Permalink
update jest deps
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoMadera committed Aug 27, 2023
1 parent b8f5a5f commit 6d9cb21
Show file tree
Hide file tree
Showing 8 changed files with 3,413 additions and 2,353 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
browser: true,
es2021: true,
node: true,
jest: true,
},
extends: [
"eslint:recommended",
Expand Down
73 changes: 73 additions & 0 deletions components/CardSubTitle/__tests__/CardSubTitle.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React, { ComponentProps } from "react";

import { render, screen } from "@testing-library/react";
import { useRouter } from "next/router";

import { CardSubTitle } from "components";
import { CardType } from "components/CardContent";
import { AppContextProvider } from "context/AppContextProvider";
import { useOnScreen } from "hooks";
import { IUtilsMocks } from "types/mocks";
import { Language } from "utils";

jest.mock("hooks/useOnScreen");

const { getAllTranslations } = jest.requireActual<IUtilsMocks>(
"utils/__tests__/__mocks__/mocks.ts"
);

interface IMocks {
push?: () => void;
useOnScreen?: boolean;
}

interface ISetupProps {
props: ComponentProps<typeof CardSubTitle>;
mocks?: IMocks;
}

describe("cardSubTitle", () => {
function setup({ props, mocks }: ISetupProps) {
(useOnScreen as jest.Mock).mockImplementationOnce(
() => mocks?.useOnScreen ?? true
);
const push = jest.fn();
(useRouter as jest.Mock).mockImplementation(() => ({
asPath: "/",
push: mocks?.push ?? push,
query: {
country: "US",
},
}));

const translations = getAllTranslations(Language.EN);
render(
<AppContextProvider translations={translations}>
<CardSubTitle {...props} />
</AppContextProvider>
);
}

it("should receive correct props when type is ALBUM and compilation is in item", () => {
expect.assertions(1);
const item = {
album_type: "compilation",
artists: [
{
name: "artist1",
},
{
name: "artist2",
},
],
release_date: "2022-02-04",
} as unknown as ISetupProps["props"]["item"];
const push = jest.fn();
setup({ props: { type: CardType.ALBUM, item }, mocks: { push } });
expect(
screen.getAllByText((_, element) => {
return element?.textContent === "2022 · Compilation · artist1, artist2";
})
).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const customJestConfig = {
"^@/(.*)$": join(process.cwd(), "src", "$1"),
},
setupFiles: ["jest-canvas-mock"],
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
testMatch: ["<rootDir>/**/*.(spec|test).(js|jsx|ts|tsx)"],
moduleDirectories: ["<rootDir>", "node_modules"],
coverageThreshold: {
Expand Down
7 changes: 4 additions & 3 deletions jest.setup.js → jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "@testing-library/jest-dom/extend-expect";
import { beforeAll, afterAll, jest } from "@jest/globals";
import "@testing-library/jest-dom";
import { afterAll, beforeAll, jest } from "@jest/globals";
import { NextRouter } from "next/router";

process.env.NEXT_PUBLIC_SPOTIFY_CLIENT_ID = "clientId";
process.env.NEXT_PUBLIC_SPOTIFY_REDIRECT_URL =
Expand All @@ -10,7 +11,7 @@ process.env.SETLIST_FM_API_KEY = "setlistFmApiKey";
process.env.SPOTIFY_ACCESS_COOKIE = "accessCookie";

jest.mock("next/router", () => ({
...jest.requireActual("next/router"),
...jest.requireActual<NextRouter>("next/router"),
useRouter: jest.fn().mockImplementation(() => ({
asPath: "/",
push: jest.fn(),
Expand Down
28 changes: 14 additions & 14 deletions layouts/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ export default function MainLayout({
document.documentElement.style.setProperty("--vh", `${vh}px`);
}, []);

if (isLoginPage) {
return (
<>
<div>
<TopBar />
{children}
</div>
<Footer />
</>
);
}

return (
<>
{isLoginPage ? (
<>
<div>
<TopBar />
{children}
</div>
<Footer />
</>
) : (
<>
<AppContainer>{children}</AppContainer>
<SpotifyPlayer />
</>
)}
<AppContainer>{children}</AppContainer>
<SpotifyPlayer />
</>
);
}
Loading

0 comments on commit 6d9cb21

Please sign in to comment.