Skip to content

Commit

Permalink
Refactor failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
podliashanyk committed Apr 20, 2023
1 parent 46b8836 commit 34c7437
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
13 changes: 5 additions & 8 deletions src/components/incident/test/IncidentDetails.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import {render, screen, waitFor, within} from "@testing-library/react";

import IncidentDetails from "../IncidentDetails";
import {Incident, IncidentTag, IncidentTicketUrlBody, SourceSystem} from "../../../api/types";
import * as utils from "../../../utils";
import userEvent from "@testing-library/user-event";
import client from "../../../api/client";

// Utils
const formatDurationSpy = jest.spyOn(utils, "formatDuration");
import {formatDuration} from "../../../utils";

// Mocks
const onIncidentChange = jest.fn();
Expand Down Expand Up @@ -134,8 +131,8 @@ describe('Incident Details: end time and duration are displayed correctly, depen
expect(durationItem).toBeInTheDocument()

// Duration value is displayed correctly
const durationValue = formatDurationSpy
.getMockImplementation()(resolvedIncidentMock.start_time, resolvedIncidentMock.end_time);
const durationValue = formatDuration(resolvedIncidentMock.start_time, resolvedIncidentMock.end_time);

// @ts-ignore
expect(within(durationItem).getByText(durationValue)).toBeInTheDocument();
});
Expand Down Expand Up @@ -166,8 +163,8 @@ describe('Incident Details: end time and duration are displayed correctly, depen


// Duration value is displayed correctly
const durationValue = formatDurationSpy
.getMockImplementation()(openIncidentMock.start_time, openIncidentMock.end_time);
const durationValue = formatDuration(openIncidentMock.start_time, openIncidentMock.end_time);

// @ts-ignore
expect(within(durationItem).getByText(durationValue)).toBeInTheDocument();
});
Expand Down
3 changes: 1 addition & 2 deletions src/components/login/Login.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const authTokenSpy = jest.spyOn(auth, 'token');
const authIsAuthenticatedSpy = jest.spyOn(auth, 'isAuthenticated');

const apiMock = new MockAdapter(api.api);
const flushPromises = () => new Promise(setImmediate);

describe("Render LoginForm", () => {
it("renders the Logo", () => {
Expand Down Expand Up @@ -118,7 +117,7 @@ describe("Functionality of LoginForm", () => {
await userEvent.click(screen.getByRole("button"));

// Waiting for all promises to resolve
await flushPromises();
await new Promise(process.nextTick);

expect(history.length).toBe(3);
expect(history.location.pathname).toBe("/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ describe("Functionality", () => {
it("displays error message when save button is clicked if no filters are selected", () => {
const saveButton = screen.getByRole("button", { name: /save/i });
const filterSelector = screen.getByRole("combobox", { name: /filters/i });
const filterClearButton = within(filterSelector).getByRole("button", { name: /clear/i });
const filterClearButton = within(filterSelector).getByTitle(/clear/i);

// Remove all filters and save
userEvent.click(filterClearButton);
Expand Down
15 changes: 9 additions & 6 deletions src/components/timeslotlist/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ const NEW_TIMESLOT: Timeslot = {
time_recurrences: [NEW_RECURRENCE],
};

// For avoiding authentication errors
beforeAll(() => {
authTokenSpy.mockImplementation(() => "token");
authIsAuthenticatedSpy.mockImplementation(() => true);
auth.login("token");
});

afterAll(() => {
authTokenSpy.mockReset();
authIsAuthenticatedSpy.mockReset();
auth.logout();
});

beforeEach(() => {
authTokenSpy.mockImplementation(() => "token");
authIsAuthenticatedSpy.mockImplementation(() => true);
apiMock
.onGet("/api/v1/notificationprofiles/timeslots/")
.reply(200, [EXISTING_TIMESLOT])
.reply(200, [EXISTING_TIMESLOT] as Timeslot[])
.onPost("/api/v1/token-auth/")
.reply(200, { token: "token" })
.onGet("/api/v1/auth/user/")
Expand All @@ -72,6 +72,9 @@ beforeEach(() => {

afterEach(() => {
apiMock.reset();
authTokenSpy.mockReset();
authIsAuthenticatedSpy.mockReset();
jest.clearAllMocks();
});

describe("TimeslotList: Initial render", () => {
Expand Down

0 comments on commit 34c7437

Please sign in to comment.