forked from OpenStackweb/summit-admin
-
Notifications
You must be signed in to change notification settings - Fork 4
fix: add isSaving to state, guard submit and catch errors #1010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a473878
fix: add isSaving to state, guard submit and catch errors
tomrndom bd3b6d9
fix: adjust catch no not swallow errors, add tests
tomrndom 727eda1
fix: badge settings promise allSettled, avoid state change when unmou…
tomrndom dc08235
fix: add unit test to check saveBadgeSettings with new promise allSet…
tomrndom af7f977
fix: adjust test mock promises
tomrndom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * @jest-environment jsdom | ||
| */ | ||
| import configureStore from "redux-mock-store"; | ||
| import thunk from "redux-thunk"; | ||
| import flushPromises from "flush-promises"; | ||
| import { saveBadgeSettings } from "../badge-actions"; | ||
| import { saveMarketingSetting } from "../marketing-actions"; | ||
|
|
||
| jest.mock("../marketing-actions", () => ({ | ||
| __esModule: true, | ||
| saveMarketingSetting: jest.fn() | ||
| })); | ||
|
|
||
| const deferred = () => { | ||
| let resolve; | ||
| let reject; | ||
| const promise = new Promise((res, rej) => { | ||
| resolve = res; | ||
| reject = rej; | ||
| }); | ||
| return { promise, resolve, reject }; | ||
| }; | ||
|
|
||
| describe("saveBadgeSettings", () => { | ||
| const middlewares = [thunk]; | ||
| const mockStore = configureStore(middlewares); | ||
|
|
||
| afterEach(() => { | ||
| jest.resetAllMocks(); | ||
| }); | ||
|
|
||
| it("does not settle until every fanned-out setting request has settled, then rejects with the failure", async () => { | ||
| const early = deferred(); | ||
| const late = deferred(); | ||
|
|
||
| saveMarketingSetting.mockImplementation((entity) => () => { | ||
| if (entity.key === "A") return early.promise; | ||
| if (entity.key === "B") return late.promise; | ||
| return Promise.resolve(); | ||
| }); | ||
|
|
||
| const store = mockStore({}); | ||
| let settled = false; | ||
| const resultPromise = store.dispatch( | ||
| saveBadgeSettings({ | ||
| a: { id: 1, type: "TEXT", value: "x", updated: true }, | ||
| b: { id: 2, type: "TEXT", value: "y", updated: true } | ||
| }) | ||
| ); | ||
| resultPromise.then( | ||
| () => { | ||
| settled = true; | ||
| }, | ||
| () => { | ||
| settled = true; | ||
| } | ||
| ); | ||
|
|
||
| early.reject(new Error("early failure")); | ||
| await flushPromises(); | ||
|
|
||
| expect(settled).toBe(false); | ||
|
|
||
| late.resolve({ response: {} }); | ||
| await flushPromises(); | ||
|
|
||
| expect(settled).toBe(true); | ||
| await expect(resultPromise).rejects.toThrow("early failure"); | ||
| }); | ||
|
|
||
| it("resolves once every setting request resolves", async () => { | ||
| saveMarketingSetting | ||
| .mockImplementationOnce(() => () => Promise.resolve({ id: "first" })) | ||
| .mockImplementationOnce(() => () => Promise.resolve({ id: "second" })); | ||
| const store = mockStore({}); | ||
| await expect( | ||
| store.dispatch( | ||
| saveBadgeSettings({ | ||
| a: { id: 1, type: "TEXT", value: "x", updated: true }, | ||
| b: { id: 2, type: "TEXT", value: "y", updated: true } | ||
| }) | ||
| ) | ||
| ).resolves.toEqual([{ id: "first" }, { id: "second" }]); | ||
| }); | ||
| }); |
This file contains hidden or 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
109 changes: 109 additions & 0 deletions
109
src/components/forms/__tests__/badge-settings-form.test.js
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import React from "react"; | ||
| import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
| import "@testing-library/jest-dom"; | ||
| import Swal from "sweetalert2"; | ||
| import BadgeSettingsForm from "../badge-settings-form"; | ||
|
|
||
| jest.mock("i18n-react/dist/i18n-react", () => ({ | ||
| __esModule: true, | ||
| default: { translate: (key) => key } | ||
| })); | ||
|
|
||
| jest.mock("sweetalert2", () => ({ | ||
| __esModule: true, | ||
| default: { fire: jest.fn() } | ||
| })); | ||
|
|
||
| const mockSummit = { id: 1, badge_features_types: [], badge_types: [] }; | ||
|
|
||
| const renderForm = (onSubmit) => | ||
| render( | ||
| <BadgeSettingsForm | ||
| entity={{}} | ||
| currentSummit={mockSummit} | ||
| errors={{}} | ||
| onSubmit={onSubmit} | ||
| onDeleteImage={jest.fn()} | ||
| onDeleteBadgeTypeImage={jest.fn()} | ||
| /> | ||
| ); | ||
|
|
||
| it("should call onSubmit only once when Save is clicked twice while saving", async () => { | ||
| const pendingPromise = new Promise(() => {}); | ||
| const onSubmit = jest.fn(() => pendingPromise); | ||
| const { container } = renderForm(onSubmit); | ||
|
|
||
| fireEvent.change(container.querySelector("#BADGE_TEMPLATE_WIDTH"), { | ||
| target: { value: "100" } | ||
| }); | ||
|
|
||
| const saveButton = screen.getByRole("button", { name: "general.save" }); | ||
| fireEvent.click(saveButton); | ||
| fireEvent.click(saveButton); | ||
|
|
||
| expect(onSubmit).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("should re-enable Save and not throw an unhandled rejection when onSubmit rejects", async () => { | ||
| const onSubmit = jest.fn(() => Promise.reject(new Error("412"))); | ||
| const { container } = renderForm(onSubmit); | ||
|
|
||
| fireEvent.change(container.querySelector("#BADGE_TEMPLATE_WIDTH"), { | ||
| target: { value: "100" } | ||
| }); | ||
|
|
||
| fireEvent.click(screen.getByRole("button", { name: "general.save" })); | ||
|
|
||
| await waitFor(() => { | ||
| expect( | ||
| screen.getByRole("button", { name: "general.save" }) | ||
| ).not.toBeDisabled(); | ||
| }); | ||
| }); | ||
|
|
||
| it("should not let a success-handler error be swallowed by onSubmit's rejection handler", () => { | ||
| const then = jest.fn(() => ({ finally: jest.fn() })); | ||
| const onSubmit = jest.fn(() => ({ then })); | ||
| const { container } = renderForm(onSubmit); | ||
|
|
||
| fireEvent.change(container.querySelector("#BADGE_TEMPLATE_WIDTH"), { | ||
| target: { value: "100" } | ||
| }); | ||
| fireEvent.click(screen.getByRole("button", { name: "general.save" })); | ||
|
|
||
| // .then must receive two distinct handlers - a single-argument | ||
| // .then(success).catch(fail) would let fail also catch success's own errors. | ||
| expect(then).toHaveBeenCalledWith(expect.any(Function), expect.any(Function)); | ||
| const [onSuccess, onRejected] = then.mock.calls[0]; | ||
| expect(onSuccess).not.toBe(onRejected); | ||
|
|
||
| Swal.fire.mockImplementationOnce(() => { | ||
| throw new Error("Swal render error"); | ||
| }); | ||
|
|
||
| // invoking the success handler directly proves its own error is not | ||
| // pre-caught before it would reach onRejected | ||
| expect(onSuccess).toThrow("Swal render error"); | ||
| }); | ||
|
|
||
| it("should show the success message and re-enable Save when onSubmit resolves", async () => { | ||
| const onSubmit = jest.fn(() => Promise.resolve()); | ||
| const { container } = renderForm(onSubmit); | ||
|
|
||
| fireEvent.change(container.querySelector("#BADGE_TEMPLATE_WIDTH"), { | ||
| target: { value: "100" } | ||
| }); | ||
| fireEvent.click(screen.getByRole("button", { name: "general.save" })); | ||
|
|
||
| await waitFor(() => | ||
| expect(Swal.fire).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| html: "badge_settings.badge_template_settings_updated", | ||
| type: "success" | ||
| }) | ||
| ) | ||
| ); | ||
| expect( | ||
| screen.getByRole("button", { name: "general.save" }) | ||
| ).not.toBeDisabled(); | ||
| }); | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.