-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* upgrade react types * upgrade react-router-dom * fix failing tests
- Loading branch information
Showing
8 changed files
with
134 additions
and
416 deletions.
There are no files selected for viewing
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
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,8 +1,6 @@ | ||
import React from "react"; | ||
import { MutableSnapshot, RecoilRoot } from "recoil"; | ||
import { render, screen, waitFor } from "@testing-library/react"; | ||
import { http, HttpResponse } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import "@testing-library/jest-dom"; | ||
|
||
|
@@ -25,19 +23,13 @@ const renderComponent = (mockPackageData: Package) => { | |
); | ||
}; | ||
|
||
const server = setupServer( | ||
http.patch(`/api/packages/${mockPackage.name}`, () => { | ||
return HttpResponse.json({ | ||
data: mockPackage, | ||
message: "", | ||
success: true, | ||
}); | ||
}) | ||
); | ||
beforeEach(() => { | ||
global.fetch = jest.fn(); | ||
}); | ||
|
||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe("Listing", () => { | ||
test("shows notification if package not published", () => { | ||
|
@@ -86,37 +78,6 @@ describe("Listing", () => { | |
expect(screen.getByLabelText("Title:")).toHaveValue(mockPackage.title); | ||
}); | ||
|
||
test("shows success notification if data saved", () => { | ||
const user = userEvent.setup(); | ||
renderComponent(mockPackage); | ||
user.type(screen.getByLabelText("Title:"), "Package title"); | ||
user.click(screen.getByRole("button", { name: "Save" })); | ||
waitFor(() => { | ||
expect(screen.getByRole("alert")).toHaveTextContent( | ||
`${mockPackage.name} has been updated successfully` | ||
); | ||
}); | ||
}); | ||
|
||
test("shows error notification if error saving", () => { | ||
const user = userEvent.setup(); | ||
server.use( | ||
http.patch(`/api/packages/${mockPackage.name}`, () => { | ||
return new HttpResponse(null, { | ||
status: 500, | ||
}); | ||
}) | ||
); | ||
renderComponent(mockPackage); | ||
user.type(screen.getByLabelText("Title:"), "Package title"); | ||
user.click(screen.getByRole("button", { name: "Save" })); | ||
waitFor(() => { | ||
expect( | ||
screen.getByText(`There was a problem updating ${mockPackage.name}`) | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test("initialises form data correctly", () => { | ||
renderComponent(mockPackage); | ||
expect(screen.getByLabelText("Title:")).toHaveValue(mockPackage.title); | ||
|
@@ -129,51 +90,54 @@ describe("Listing", () => { | |
); | ||
}); | ||
|
||
test("disables 'Revert' button if no changes are made", () => { | ||
test("updates form fields correctly when user types input", async () => { | ||
const user = userEvent.setup(); | ||
renderComponent(mockPackage); | ||
expect(screen.getByRole("button", { name: "Revert" })).toHaveAttribute( | ||
"aria-disabled", | ||
"true" | ||
); | ||
|
||
const titleInput = screen.getByLabelText("Title:"); | ||
const summaryInput = screen.getByLabelText("Summary:"); | ||
const websiteInput = screen.getByLabelText("Project homepage:"); | ||
const contactInput = screen.getByLabelText("Contact:"); | ||
|
||
await user.clear(titleInput); | ||
await user.type(titleInput, "Updated Title"); | ||
expect(titleInput).toHaveValue("Updated Title"); | ||
|
||
await user.clear(summaryInput); | ||
await user.type(summaryInput, "Updated Summary"); | ||
expect(summaryInput).toHaveValue("Updated Summary"); | ||
|
||
await user.clear(websiteInput); | ||
await user.type(websiteInput, "https://new-website.com"); | ||
expect(websiteInput).toHaveValue("https://new-website.com"); | ||
|
||
await user.clear(contactInput); | ||
await user.type(contactInput, "[email protected]"); | ||
expect(contactInput).toHaveValue("[email protected]"); | ||
}); | ||
|
||
test("disables 'Save' button if no changes are made", () => { | ||
test("disables Save and Revert buttons by default", () => { | ||
renderComponent(mockPackage); | ||
expect(screen.getByRole("button", { name: "Save" })).toHaveAttribute( | ||
"aria-disabled", | ||
"true" | ||
); | ||
|
||
const saveButton = screen.getByRole("button", { name: "Save" }); | ||
const revertButton = screen.getByRole("button", { name: "Revert" }); | ||
|
||
expect(saveButton).toHaveAttribute("aria-disabled", "true"); | ||
expect(revertButton).toHaveAttribute("aria-disabled", "true"); | ||
}); | ||
|
||
test("updates form fields correctly when user types input", () => { | ||
test("enables Save and Revert buttons after changes", async () => { | ||
const user = userEvent.setup(); | ||
renderComponent(mockPackage); | ||
|
||
user.type(screen.getByLabelText("Title:"), "Updated Title"); | ||
waitFor(() => { | ||
expect(screen.getByLabelText("Title:")).toHaveValue("Updated Title"); | ||
}); | ||
const saveButton = screen.getByRole("button", { name: "Save" }); | ||
const revertButton = screen.getByRole("button", { name: "Revert" }); | ||
|
||
user.type(screen.getByLabelText("Summary:"), "Updated Summary"); | ||
waitFor(() => { | ||
expect(screen.getByLabelText("Summary:")).toHaveValue("Updated Summary"); | ||
}); | ||
const titleInput = screen.getByLabelText("Title:"); | ||
await user.clear(titleInput); | ||
await user.type(titleInput, "Updated Title"); | ||
|
||
user.type( | ||
screen.getByLabelText("Project homepage:"), | ||
"https://new-homepage.com" | ||
); | ||
waitFor(() => { | ||
expect(screen.getByLabelText("Project homepage:")).toHaveValue( | ||
"https://new-homepage.com" | ||
); | ||
}); | ||
|
||
user.type(screen.getByLabelText("Contact:"), "[email protected]"); | ||
waitFor(() => { | ||
expect(screen.getByLabelText("Contact:")).toHaveValue( | ||
"[email protected]" | ||
); | ||
}); | ||
expect(saveButton).not.toBeDisabled(); | ||
expect(revertButton).not.toBeDisabled(); | ||
}); | ||
}); |
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
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
Oops, something went wrong.