forked from OpenStackweb/summit-admin
-
Notifications
You must be signed in to change notification settings - Fork 4
Fix: unable to delete speaker profile pic #1032
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
6 commits
Select commit
Hold shift + click to select a range
e11f7c7
chore: call delete speaker pic when pic removed
santipalenque 70b594d
chore: fix pre-existing lint errors in speaker reducer and edit page
smarcet c31ca58
fix: delete speaker photo from the remove button with a confirm
smarcet c84eb5e
fix: preserve unsaved speaker fields when a photo is removed
smarcet c44c1f9
fix: await saveSpeaker's request before resolving
smarcet f397042
fix: track pending picture-only updates with a counter
smarcet 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,146 @@ | ||
| /** | ||
| * @jest-environment jsdom | ||
| */ | ||
| import configureStore from "redux-mock-store"; | ||
| import thunk from "redux-thunk"; | ||
| import { | ||
| deleteRequest, | ||
| putRequest | ||
| } from "openstack-uicore-foundation/lib/utils/actions"; | ||
| import { removeAttachedPicture, saveSpeaker } from "../speaker-actions"; | ||
| import * as methods from "../../utils/methods"; | ||
|
|
||
| jest.mock("openstack-uicore-foundation/lib/utils/actions", () => ({ | ||
| __esModule: true, | ||
| ...jest.requireActual("openstack-uicore-foundation/lib/utils/actions"), | ||
| deleteRequest: jest.fn(), | ||
| putRequest: jest.fn() | ||
| })); | ||
|
|
||
| const SPEAKER_ID = 42; | ||
|
|
||
| describe("removeAttachedPicture", () => { | ||
| const mockStore = configureStore([thunk]); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| window.API_BASE_URL = "https://api.test"; | ||
| jest.spyOn(methods, "getAccessTokenSafely").mockResolvedValue("TOKEN"); | ||
| deleteRequest.mockImplementation( | ||
| (_requestAction, receiveAction) => () => (dispatch) => { | ||
| dispatch(receiveAction({ response: {} })); | ||
| return Promise.resolve({ response: {} }); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| delete window.API_BASE_URL; | ||
| }); | ||
|
|
||
| it("deletes the profile photo through the photo endpoint", async () => { | ||
| const store = mockStore({}); | ||
|
|
||
| await store.dispatch(removeAttachedPicture(SPEAKER_ID, "profile")); | ||
|
|
||
| expect(deleteRequest).toHaveBeenCalledTimes(1); | ||
| expect(deleteRequest.mock.calls[0][2]).toBe( | ||
| `https://api.test/api/v1/speakers/${SPEAKER_ID}/photo?access_token=TOKEN` | ||
| ); | ||
| expect(store.getActions().map((a) => a.type)).toContain("PIC_DELETED"); | ||
| }); | ||
|
|
||
| it("deletes the big photo through the big-photo endpoint", async () => { | ||
| const store = mockStore({}); | ||
|
|
||
| await store.dispatch(removeAttachedPicture(SPEAKER_ID, "big")); | ||
|
|
||
| expect(deleteRequest.mock.calls[0][2]).toBe( | ||
| `https://api.test/api/v1/speakers/${SPEAKER_ID}/big-photo?access_token=TOKEN` | ||
| ); | ||
| expect(store.getActions().map((a) => a.type)).toContain("BIG_PIC_DELETED"); | ||
| }); | ||
|
|
||
| it("does not emit a delete action when the request fails", async () => { | ||
| deleteRequest.mockImplementation( | ||
| () => () => () => Promise.reject(new Error("boom")) | ||
| ); | ||
| const store = mockStore({}); | ||
|
|
||
| await expect( | ||
| store.dispatch(removeAttachedPicture(SPEAKER_ID, "profile")) | ||
| ).resolves.toBeUndefined(); | ||
|
|
||
| const types = store.getActions().map((a) => a.type); | ||
| expect(types).not.toContain("PIC_DELETED"); | ||
| expect(types).not.toContain("STOP_LOADING"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("saveSpeaker", () => { | ||
| const mockStore = configureStore([thunk]); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| window.API_BASE_URL = "https://api.test"; | ||
| jest.spyOn(methods, "getAccessTokenSafely").mockResolvedValue("TOKEN"); | ||
| putRequest.mockImplementation( | ||
| (requestAction, receiveAction, _url, _body, _err, payload) => | ||
| () => | ||
| (dispatch) => { | ||
| dispatch(requestAction(payload)); | ||
| dispatch(receiveAction({ response: {} })); | ||
| return Promise.resolve({ response: {} }); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| delete window.API_BASE_URL; | ||
| }); | ||
|
|
||
| it("never deletes photos as a side effect of saving", async () => { | ||
| const store = mockStore({}); | ||
|
|
||
| await store.dispatch( | ||
| saveSpeaker({ id: SPEAKER_ID, title: "Dev", pic: "", big_pic: "" }) | ||
| ); | ||
|
|
||
| expect(deleteRequest).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("resolves only after the update request settles", async () => { | ||
| let resolveRequest; | ||
| putRequest.mockImplementation( | ||
| (requestAction, receiveAction) => () => (dispatch) => { | ||
| dispatch(requestAction()); | ||
| return new Promise((resolve) => { | ||
| resolveRequest = () => { | ||
| dispatch(receiveAction({ response: {} })); | ||
| resolve({ response: {} }); | ||
| }; | ||
| }); | ||
| } | ||
| ); | ||
| const store = mockStore({}); | ||
|
|
||
| let settled = false; | ||
| const dispatched = store | ||
| .dispatch( | ||
| saveSpeaker({ id: SPEAKER_ID, title: "Dev", pic: "", big_pic: "" }) | ||
| ) | ||
| .then(() => { | ||
| settled = true; | ||
| }); | ||
|
|
||
| await Promise.resolve(); | ||
| await Promise.resolve(); | ||
| expect(settled).toBe(false); | ||
|
|
||
| resolveRequest(); | ||
| await dispatched; | ||
| expect(settled).toBe(true); | ||
| }); | ||
| }); |
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.
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.