Skip to content

Commit

Permalink
Refactor records mocks to avoid duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Alambea committed Dec 27, 2023
1 parent 7e36b00 commit 4696f73
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 147 deletions.
10 changes: 5 additions & 5 deletions src/components/App/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import App from "./App";
import { paths } from "../../routers/paths";
import { setupStore } from "../../store";
import {
recordGetByIdMock,
radioheadRecordApiMock,
recordMock,
recordsMock,
} from "../../mocks/recordsMock";
Expand Down Expand Up @@ -302,10 +302,10 @@ describe("Given an App component", () => {
});
});

describe(`And the path is '/records/modify/${recordGetByIdMock._id}'`, () => {
test(`Then it should show a heading "Modify ${recordGetByIdMock.artist}'s ${recordGetByIdMock.record} record"`, async () => {
const expectedHeading = `Modify ${recordGetByIdMock.artist}'s ${recordGetByIdMock.record} record`;
const recordId = recordGetByIdMock._id;
describe(`And the path is '/records/modify/${radioheadRecordApiMock._id}'`, () => {
test(`Then it should show a heading "Modify ${radioheadRecordApiMock.artist}'s ${radioheadRecordApiMock.record} record"`, async () => {
const expectedHeading = `Modify ${radioheadRecordApiMock.artist}'s ${radioheadRecordApiMock.record} record`;
const recordId = radioheadRecordApiMock._id;
const initialPath = `/records/modify/${recordId}`;

const user: Partial<User> = {
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/__tests__/addRecord.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PropsWithChildren } from "react";
import { errorHandlers } from "../../mocks/handlers";
import {
recordMock,
recordToAddMock,
fkaRecordMock,
recordsMock,
} from "../../mocks/recordsMock";
import { server } from "../../mocks/server";
Expand Down Expand Up @@ -46,7 +46,7 @@ describe("Given an addRecord function", () => {
},
} = renderHook(() => useRecordsApi(), { wrapper });

const newRecordCreated = await addRecord(recordToAddMock);
const newRecordCreated = await addRecord(fkaRecordMock);

expect(recordMock).toStrictEqual(newRecordCreated);
});
Expand Down Expand Up @@ -79,7 +79,7 @@ describe("Given an addRecord function", () => {

server.resetHandlers(...errorHandlers);

await addRecord(recordToAddMock);
await addRecord(fkaRecordMock);

expect(spyShowFeedback).toHaveBeenCalledWith(
expectedErrorMessage,
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/__tests__/addRecordUserError.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { renderHook } from "@testing-library/react";
import useRecordsApi from "../useRecordsApi";
import { Provider } from "react-redux";
import { PropsWithChildren } from "react";
import { recordToAddMock, recordsMock } from "../../mocks/recordsMock";
import { fkaRecordMock, recordsMock } from "../../mocks/recordsMock";
import { setupStore } from "../../store";
import { BrowserRouter } from "react-router-dom";
import * as utils from "../../utils/showFeedback";
Expand Down Expand Up @@ -31,7 +31,7 @@ describe("Given a addRecord function", () => {
},
} = renderHook(() => useRecordsApi(), { wrapper });

await addRecord(recordToAddMock);
await addRecord(fkaRecordMock);

expect(spyShowFeedback).toHaveBeenCalledWith(
expectedErrorMessage,
Expand Down
13 changes: 8 additions & 5 deletions src/mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { rest } from "msw";
import {
recordApiMock,
recordGetByIdMock,
fkaRecordApiMock,
radioheadRecordApiMock,
recordsApiMock,
} from "./recordsMock";

Expand All @@ -17,15 +17,18 @@ export const handlers = [
),

rest.post(`${urlApi}/records`, (_req, res, ctx) =>
res(ctx.status(201), ctx.json({ record: recordApiMock })),
res(ctx.status(201), ctx.json({ record: fkaRecordApiMock })),
),

rest.get(`${urlApi}/records/:id`, (_req, res, ctx) =>
res(ctx.status(200), ctx.json({ record: recordGetByIdMock })),
res(ctx.status(200), ctx.json({ record: radioheadRecordApiMock })),
),

rest.patch(`${urlApi}/records/:id`, (_req, res, ctx) =>
res(ctx.status(200), ctx.json({ record: { ...recordApiMock, rating: 5 } })),
res(
ctx.status(200),
ctx.json({ record: { ...fkaRecordApiMock, rating: 5 } }),
),
),
];

Expand Down
192 changes: 60 additions & 132 deletions src/mocks/recordsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Record, RecordApi, RecordsApi } from "../types";
export const recordIdMock: string = "64fc9c9a5c621a08508d556c";
export const wrongIdMock: string = "75698c6a5c987a08508d58jy";

export const recordToAddMock: Omit<Record, "id"> = {
export const fkaRecordMock: Omit<Record, "id"> = {
record: "LP1",
artist: "FKA Twigs",
cover: "http://example.com/image.png",
Expand All @@ -16,36 +16,7 @@ export const recordToAddMock: Omit<Record, "id"> = {
releaseDate: 2014,
};

export const recordMock: Record = {
id: "4o099c6a5c987a08508d58jy",
record: "LP1",
artist: "FKA Twigs",
cover: "http://example.com/image.png",
description:
"LP1 is the debut studio album by English singer-songwriter FKA Twigs",
genres: "Avant-pop, electronic, art pop R&B, trip hop",
label: "Young Turks",
length: "40:46",
rating: 3,
releaseDate: 2014,
};

export const recordApiMock: RecordApi = {
_id: "4o099c6a5c987a08508d58jy",
record: "LP1",
artist: "FKA Twigs",
cover: "http://example.com/image.png",
description:
"LP1 is the debut studio album by English singer-songwriter FKA Twigs",
genres: "Avant-pop, electronic, art pop R&B, trip hop",
label: "Young Turks",
length: "40:46",
rating: 3,
releaseDate: 2014,
};

export const recordGetByIdMock = {
_id: recordIdMock,
export const radioheadRecordMock: Omit<Record, "id"> = {
record: "In Rainbows",
artist: "Radiohead",
cover: "http://example.com/image.png",
Expand All @@ -58,130 +29,87 @@ export const recordGetByIdMock = {
releaseDate: 2007,
};

export const expectedRecordApiMock: Record = {
id: "4o099c6a5c987a08508d58jy",
record: "LP1",
artist: "FKA Twigs",
export const portisheadRecordMock: Omit<Record, "id"> = {
record: "Third",
artist: "Portishead",
cover: "http://example.com/image.png",
description:
"LP1 is the debut studio album by English singer-songwriter FKA Twigs",
genres: "Avant-pop, electronic, art pop R&B, trip hop",
label: "Young Turks",
length: "40:46",
rating: 3,
releaseDate: 2014,
"Third is the third and most recent studio album by the English band Portishead",
genres: "Experimental rock, electronica, psychedelic rock",
label: "Island - Mercury",
length: "49:17",
rating: 4,
releaseDate: 2008,
};

export const massiveRecordMock: Omit<Record, "id"> = {
record: "Mezzanine",
artist: "Massive Attack",
cover: "http://example.com/image.png",
description:
"Mezzanine is the third studio album by English electronic music group Massive Attack",
genres: "Trip-hop, electronica",
label: "Virgin - Circa",
length: "63:29",
rating: 4,
releaseDate: 1998,
};

export const oceanRecordMock: Omit<Record, "id"> = {
record: "Heliocentric",
artist: "The Ocean",
cover: "http://example.com/image.png",
description:
"Heliocentric is the fourth studio album by German progressive metal band The Ocean",
genres: "Post-metal, post-rock, sludge metal, progressive metal",
label: "Pelagic, Metal Blade",
length: " 50:58",
rating: 2,
releaseDate: 2010,
};

export const recordMock: Record = {
...fkaRecordMock,
id: "4o099c6a5c987a08508d58jy",
};

export const fkaRecordApiMock: RecordApi = {
...fkaRecordMock,
_id: "4o099c6a5c987a08508d58jy",
};

export const radioheadRecordApiMock = {
...radioheadRecordMock,
_id: recordIdMock,
};

export const recordsApiMock: RecordsApi = {
records: [
{ ...radioheadRecordMock, _id: recordIdMock },
{
_id: recordIdMock,
record: "In Rainbows",
artist: "Radiohead",
cover: "http://example.com/image.png",
description:
"In Rainbows is the seventh studio album by the English rock band Radiohead.",
genres:
"Art rock, alternative rock, experimental rock, art pop, electronica",
label: "Self-released",
length: "42:39",
rating: 5,
releaseDate: 2007,
},
{
...portisheadRecordMock,
_id: "1o8c9c6a5c621a08508d598y",
record: "Third",
artist: "Portishead",
cover: "http://example.com/image.png",
description:
"Third is the third and most recent studio album by the English band Portishead",
genres: "Experimental rock, electronica, psychedelic rock",
label: "Island - Mercury",
length: "49:17",
rating: 4,
releaseDate: 2008,
},
{
_id: "12hj7c6a5c621a08508d678e",
record: "Mezzanine",
artist: "Massive Attack",
cover: "http://example.com/image.png",
description:
"Mezzanine is the third studio album by English electronic music group Massive Attack",
genres: "Trip-hop, electronica",
label: "Virgin - Circa",
length: "63:29",
rating: 4,
releaseDate: 1998,
},
{ ...massiveRecordMock, _id: "12hj7c6a5c621a08508d678e" },
{
...oceanRecordMock,
_id: "73fc9c6a5c621a08508d534j",
record: "Heliocentric",
artist: "The Ocean",
cover: "http://example.com/image.png",
description:
"Heliocentric is the fourth studio album by German progressive metal band The Ocean",
genres: "Post-metal, post-rock, sludge metal, progressive metal",
label: "Pelagic, Metal Blade",
length: " 50:58",
rating: 2,
releaseDate: 2010,
},
],
};

export const recordsMock: Record[] = [
{
...radioheadRecordMock,
id: recordIdMock,
record: "In Rainbows",
artist: "Radiohead",
cover: "http://example.com/image.png",
description:
"In Rainbows is the seventh studio album by the English rock band Radiohead.",
genres:
"Art rock, alternative rock, experimental rock, art pop, electronica",
label: "Self-released",
length: "42:39",
rating: 5,
releaseDate: 2007,
},
{
...portisheadRecordMock,
id: "1o8c9c6a5c621a08508d598y",
record: "Third",
artist: "Portishead",
cover: "http://example.com/image.png",
description:
"Third is the third and most recent studio album by the English band Portishead",
genres: "Experimental rock, electronica, psychedelic rock",
label: "Island - Mercury",
length: "49:17",
rating: 4,
releaseDate: 2008,
},
{
id: "12hj7c6a5c621a08508d678e",
record: "Mezzanine",
artist: "Massive Attack",
cover: "http://example.com/image.png",
description:
"Mezzanine is the third studio album by English electronic music group Massive Attack",
genres: "Trip-hop, electronica",
label: "Virgin - Circa",
length: "63:29",
rating: 4,
releaseDate: 1998,
},
{ ...massiveRecordMock, id: "12hj7c6a5c621a08508d678e" },
{
...oceanRecordMock,
id: "73fc9c6a5c621a08508d534j",
record: "Heliocentric",
artist: "The Ocean",
cover: "http://example.com/image.png",
description:
"Heliocentric is the fourth studio album by German progressive metal band The Ocean",
genres: "Post-metal, post-rock, sludge metal, progressive metal",
label: "Pelagic, Metal Blade",
length: " 50:58",
rating: 2,
releaseDate: 2010,
},
];

0 comments on commit 4696f73

Please sign in to comment.