Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/actions/__tests__/sponsor-forms-actions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as OpenStackUiCoreActions from "openstack-uicore-foundation/lib/utils/actions";
import { deleteSponsorFormItem } from "../sponsor-forms-actions";
import * as UtilsMethods from "../../utils/methods";
import * as BaseActions from "../base-actions";

jest.mock("openstack-uicore-foundation/lib/utils/actions", () => {
const originalModule = jest.requireActual(
"openstack-uicore-foundation/lib/utils/actions"
);

return {
__esModule: true,
...originalModule,
deleteRequest: jest.fn(() => () => () => Promise.resolve())
};
});

describe("SponsorFormActions", () => {
describe("DeleteSponsorFormItem", () => {
afterEach(() => {
// restore the spy created with spyOn
jest.restoreAllMocks();
});

it("execute", async () => {
const mockedDispatch = jest.fn();
const mockedGetState = jest.fn(() => ({
currentSummitState: {
currentSummit: "SSS"
},
sponsorFormItemsListState: {
currentPage: 1,
perPage: 10,
order: "asc",
orderDir: 1,
hideArchived: false
}
}));

const params = {
formId: "AAA",
itemId: "III"
};

const spyOnGetAccessTokenSafely = jest
.spyOn(UtilsMethods, "getAccessTokenSafely")
.mockImplementation(() => "access _token");
const spyOnSnackbarSuccessHandler = jest.spyOn(
BaseActions,
"snackbarSuccessHandler"
);

await deleteSponsorFormItem(params.formId, params.itemId)(
mockedDispatch,
mockedGetState
);

// gets acces token safely
expect(spyOnGetAccessTokenSafely).toHaveBeenCalled();
// calls delete request
expect(OpenStackUiCoreActions.deleteRequest).toHaveBeenCalled();
// shows snackbar
expect(spyOnSnackbarSuccessHandler).toHaveBeenCalled();
});
Comment on lines +25 to +64
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix typo and consider more comprehensive assertions.

Minor typo on line 58: "acces" should be "access".

The test validates the observable side effects but could be more thorough. Consider adding assertions to verify:

  • The correct parameters are passed to deleteRequest (URL, action creator, error handler)
  • The SPONSOR_FORM_ITEM_DELETED action is dispatched with the correct itemId

Additionally, sponsorFormItemsListState in the mocked state (lines 31-37) is not used by deleteSponsorFormItem and can be removed for clarity.

📝 Proposed fix for typo
-      // gets acces token safely
+      // gets access token safely
       expect(spyOnGetAccessTokenSafely).toHaveBeenCalled();
♻️ Optional: More comprehensive assertions
       const mockedGetState = jest.fn(() => ({
         currentSummitState: {
           currentSummit: "SSS"
-        },
-        sponsorFormItemsListState: {
-          currentPage: 1,
-          perPage: 10,
-          order: "asc",
-          orderDir: 1,
-          hideArchived: false
         }
       }));

And add more specific assertions:

       // gets access token safely
       expect(spyOnGetAccessTokenSafely).toHaveBeenCalled();
       // calls delete request
       expect(OpenStackUiCoreActions.deleteRequest).toHaveBeenCalled();
+      // verify deleteRequest was called with correct action creator
+      const deleteRequestCall = OpenStackUiCoreActions.deleteRequest.mock.calls[0];
+      expect(deleteRequestCall[1]).toBeDefined(); // action creator
+      expect(deleteRequestCall[2]).toContain('/show-forms/AAA/items/III'); // URL
       // shows snackbar
       expect(spyOnSnackbarSuccessHandler).toHaveBeenCalled();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it("execute", async () => {
const mockedDispatch = jest.fn();
const mockedGetState = jest.fn(() => ({
currentSummitState: {
currentSummit: "SSS"
},
sponsorFormItemsListState: {
currentPage: 1,
perPage: 10,
order: "asc",
orderDir: 1,
hideArchived: false
}
}));
const params = {
formId: "AAA",
itemId: "III"
};
const spyOnGetAccessTokenSafely = jest
.spyOn(UtilsMethods, "getAccessTokenSafely")
.mockImplementation(() => "access _token");
const spyOnSnackbarSuccessHandler = jest.spyOn(
BaseActions,
"snackbarSuccessHandler"
);
await deleteSponsorFormItem(params.formId, params.itemId)(
mockedDispatch,
mockedGetState
);
// gets acces token safely
expect(spyOnGetAccessTokenSafely).toHaveBeenCalled();
// calls delete request
expect(OpenStackUiCoreActions.deleteRequest).toHaveBeenCalled();
// shows snackbar
expect(spyOnSnackbarSuccessHandler).toHaveBeenCalled();
});
it("execute", async () => {
const mockedDispatch = jest.fn();
const mockedGetState = jest.fn(() => ({
currentSummitState: {
currentSummit: "SSS"
}
}));
const params = {
formId: "AAA",
itemId: "III"
};
const spyOnGetAccessTokenSafely = jest
.spyOn(UtilsMethods, "getAccessTokenSafely")
.mockImplementation(() => "access _token");
const spyOnSnackbarSuccessHandler = jest.spyOn(
BaseActions,
"snackbarSuccessHandler"
);
await deleteSponsorFormItem(params.formId, params.itemId)(
mockedDispatch,
mockedGetState
);
// gets access token safely
expect(spyOnGetAccessTokenSafely).toHaveBeenCalled();
// calls delete request
expect(OpenStackUiCoreActions.deleteRequest).toHaveBeenCalled();
// verify deleteRequest was called with correct action creator
const deleteRequestCall = OpenStackUiCoreActions.deleteRequest.mock.calls[0];
expect(deleteRequestCall[1]).toBeDefined(); // action creator
expect(deleteRequestCall[2]).toContain('/show-forms/AAA/items/III'); // URL
// shows snackbar
expect(spyOnSnackbarSuccessHandler).toHaveBeenCalled();
});
🤖 Prompt for AI Agents
In @src/actions/__tests__/sponsor-forms-actions.test.js around lines 25 - 64,
Test has a typo in the expectation message and is missing stronger assertions:
fix the typo "acces" -> "access" in the comment/assertion, remove the unused
sponsorFormItemsListState from the mockedGetState return, and add assertions
inside the "execute" test to verify deleteRequest was called with the correct
URL and handlers and that the SPONSOR_FORM_ITEM_DELETED action (or the action
creator used by deleteSponsorFormItem) was dispatched with the expected itemId;
use the existing spies/mocks (spyOnGetAccessTokenSafely,
spyOnSnackbarSuccessHandler, OpenStackUiCoreActions.deleteRequest) and the
deleteSponsorFormItem invocation to assert the exact parameters and dispatched
action.

});
});
1 change: 0 additions & 1 deletion src/actions/sponsor-forms-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,6 @@ export const deleteSponsorFormItem =
const params = { access_token: accessToken };

dispatch(startLoading());

return deleteRequest(
null,
createAction(SPONSOR_FORM_ITEM_DELETED)({ itemId }),
Expand Down
11 changes: 10 additions & 1 deletion src/pages/sponsors/sponsor-form-item-list-page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,16 @@ const SponsorFormItemListPage = ({
: archiveSponsorFormItem(formId, item.id);

const handleRowDelete = (itemId) => {
deleteSponsorFormItem(formId, itemId);
deleteSponsorFormItem(formId, itemId).then(() => {
getSponsorFormItems(
formId,
currentPage,
perPage,
order,
orderDir,
hideArchived
);
});
};

const handleNewItem = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ describe("SponsorFormItemsListReducer", () => {
result = SponsorFormItemsListReducer(
{
...initialState,
totalCount: 2,
items: [
{
id: "A",
Expand Down Expand Up @@ -267,7 +266,6 @@ describe("SponsorFormItemsListReducer", () => {
);
expect(result).toStrictEqual({
...initialState,
totalCount: 1,
items: [
{
id: "B",
Expand Down
3 changes: 1 addition & 2 deletions src/reducers/sponsors/sponsor-form-items-list-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ const sponsorFormItemsListReducer = (state = DEFAULT_STATE, action) => {
case SPONSOR_FORM_ITEM_DELETED: {
const { itemId } = payload;
const items = state.items.filter((it) => it.id !== itemId);
const totalCount = items.length;

return { ...state, items, totalCount };
return { ...state, items };
}
case SPONSOR_FORM_ITEM_ARCHIVED: {
const { id: itemId } = payload.response;
Expand Down