-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate changelog component to MUI. Partially realizes #9796.
- Loading branch information
Showing
3 changed files
with
72 additions
and
61 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,83 @@ | ||
import { act, render } from "@testing-library/react" | ||
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react" | ||
|
||
import * as changelog_api from "../api/changelog" | ||
import * as toast from "../widgets/toast" | ||
import { ChangeLog } from "./ChangeLog" | ||
|
||
jest.mock("../api/changelog.js") | ||
let result | ||
jest.mock("../widgets/toast.js") | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
function expectNrEventsToBe(nr) { | ||
// Assert that the change log contains nr events | ||
const rows = document.querySelectorAll(".MuiListItem-root") | ||
expect(rows.length).toBe(nr) | ||
} | ||
|
||
it("renders no changes", async () => { | ||
changelog_api.get_changelog.mockImplementation(() => Promise.resolve({ changelog: [] })) | ||
await act(async () => { | ||
result = render(<ChangeLog />) | ||
render(<ChangeLog />) | ||
}) | ||
const rows = result.container.querySelectorAll("div.event") | ||
expect(rows.length).toBe(0) | ||
expectNrEventsToBe(0) | ||
}) | ||
|
||
it("renders one report change", async () => { | ||
changelog_api.get_changelog.mockImplementation(() => Promise.resolve({ changelog: [{ timestamp: "2020-01-01" }] })) | ||
await act(async () => { | ||
result = render(<ChangeLog report_uuid="uuid" />) | ||
render(<ChangeLog report_uuid="uuid" />) | ||
}) | ||
const rows = result.container.querySelectorAll("div.event") | ||
expect(rows.length).toBe(1) | ||
expectNrEventsToBe(1) | ||
}) | ||
|
||
it("renders one subject change", async () => { | ||
changelog_api.get_changelog.mockImplementation(() => Promise.resolve({ changelog: [{ timestamp: "2020-01-01" }] })) | ||
await act(async () => { | ||
result = render(<ChangeLog subject_uuid="uuid" />) | ||
render(<ChangeLog subject_uuid="uuid" />) | ||
}) | ||
const rows = result.container.querySelectorAll("div.event") | ||
expect(rows.length).toBe(1) | ||
expectNrEventsToBe(1) | ||
}) | ||
|
||
it("renders one metric change", async () => { | ||
changelog_api.get_changelog.mockImplementation(() => Promise.resolve({ changelog: [{ timestamp: "2020-01-01" }] })) | ||
await act(async () => { | ||
result = render(<ChangeLog metric_uuid="uuid" />) | ||
render(<ChangeLog metric_uuid="uuid" />) | ||
}) | ||
const rows = result.container.querySelectorAll("div.event") | ||
expect(rows.length).toBe(1) | ||
expectNrEventsToBe(1) | ||
}) | ||
|
||
it("renders one source change", async () => { | ||
changelog_api.get_changelog.mockImplementation(() => Promise.resolve({ changelog: [{ timestamp: "2020-01-01" }] })) | ||
await act(async () => { | ||
result = render(<ChangeLog source_uuid="uuid" />) | ||
render(<ChangeLog source_uuid="uuid" />) | ||
}) | ||
expectNrEventsToBe(1) | ||
}) | ||
|
||
it("loads more changes", async () => { | ||
changelog_api.get_changelog.mockImplementation(() => Promise.resolve({ changelog: [{ timestamp: "2020-01-01" }] })) | ||
await act(async () => { | ||
render(<ChangeLog source_uuid="uuid" />) | ||
}) | ||
changelog_api.get_changelog.mockImplementation(() => | ||
Promise.resolve({ changelog: [{ timestamp: "2020-01-01" }, { timestamp: "2020-01-02" }] }), | ||
) | ||
await act(async () => fireEvent.click(screen.getByText(/Load more changes/))) | ||
expectNrEventsToBe(2) | ||
}) | ||
|
||
it("shows error when loading more changes fails", async () => { | ||
changelog_api.get_changelog.mockImplementation(() => Promise.resolve({ changelog: [] })) | ||
await act(async () => { | ||
render(<ChangeLog source_uuid="uuid" />) | ||
}) | ||
changelog_api.get_changelog.mockImplementation(() => Promise.reject(new Error("Couldn't retrieve changelog"))) | ||
await act(async () => fireEvent.click(screen.getByText(/Load more changes/))) | ||
await waitFor(() => { | ||
expect(toast.showMessage).toHaveBeenCalledTimes(1) | ||
}) | ||
const rows = result.container.querySelectorAll("div.event") | ||
expect(rows.length).toBe(1) | ||
expectNrEventsToBe(0) | ||
}) |