-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add add-on types pages, dialog, actions, reducers, tests #999
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /** | ||
| * @jest-environment jsdom | ||
| */ | ||
| import configureStore from "redux-mock-store"; | ||
| import thunk from "redux-thunk"; | ||
| import flushPromises from "flush-promises"; | ||
| import { | ||
| getRequest, | ||
| putRequest, | ||
| postRequest, | ||
| deleteRequest | ||
| } from "openstack-uicore-foundation/lib/utils/actions"; | ||
| import { | ||
| getAddOnTypes, | ||
| saveAddOnType, | ||
| deleteAddOnType, | ||
| resetAddOnTypeForm, | ||
| REQUEST_ADD_ON_TYPES, | ||
| RECEIVE_ADD_ON_TYPES, | ||
| ADD_ON_TYPE_UPDATED, | ||
| ADD_ON_TYPE_ADDED, | ||
| RESET_ADD_ON_TYPE_FORM | ||
| } from "../add-on-types-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"), | ||
| getRequest: jest.fn(), | ||
| putRequest: jest.fn(), | ||
| postRequest: jest.fn(), | ||
| deleteRequest: jest.fn() | ||
| })); | ||
|
|
||
| const requestMock = | ||
| (requestActionCreator, receiveActionCreator) => () => (dispatch) => { | ||
| if (requestActionCreator && typeof requestActionCreator === "function") { | ||
| dispatch(requestActionCreator({})); | ||
| } | ||
| return new Promise((resolve) => { | ||
| if (typeof receiveActionCreator === "function") { | ||
| dispatch(receiveActionCreator({ response: { id: 1 } })); | ||
| } else { | ||
| dispatch(receiveActionCreator); | ||
| } | ||
| resolve({ response: { id: 1 } }); | ||
| }); | ||
| }; | ||
|
|
||
| const rejectMock = () => () => () => Promise.reject(new Error("API error")); | ||
|
|
||
| const middlewares = [thunk]; | ||
| const mockStore = configureStore(middlewares); | ||
|
|
||
| describe("add-on-types actions", () => { | ||
| beforeEach(() => { | ||
| jest.spyOn(methods, "getAccessTokenSafely").mockResolvedValue("TOKEN"); | ||
| getRequest.mockImplementation(requestMock); | ||
| putRequest.mockImplementation(requestMock); | ||
| postRequest.mockImplementation(requestMock); | ||
| deleteRequest.mockImplementation(requestMock); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("getAddOnTypes", () => { | ||
| it("dispatches REQUEST and RECEIVE actions on success", async () => { | ||
| const store = mockStore({}); | ||
| store.dispatch(getAddOnTypes()); | ||
| await flushPromises(); | ||
|
|
||
| const types = store.getActions().map((a) => a.type); | ||
| expect(types).toContain(REQUEST_ADD_ON_TYPES); | ||
| expect(types).toContain(RECEIVE_ADD_ON_TYPES); | ||
| }); | ||
|
|
||
| it("passes page in the extra data payload so the reducer can track current page", async () => { | ||
| let capturedExtra; | ||
| getRequest.mockImplementation( | ||
| (req, res, _url, _err, extra) => () => (dispatch) => { | ||
| capturedExtra = extra; | ||
| return requestMock(req, res)()(dispatch); | ||
| } | ||
| ); | ||
|
|
||
| const store = mockStore({}); | ||
| await store.dispatch(getAddOnTypes("foo", 2, 20, "name", -1)); | ||
| await flushPromises(); | ||
|
|
||
| expect(capturedExtra).toMatchObject({ page: 2, term: "foo" }); | ||
| }); | ||
|
|
||
| it("still dispatches STOP_LOADING when getRequest rejects", async () => { | ||
| getRequest.mockImplementation(rejectMock); | ||
| const store = mockStore({}); | ||
| await store.dispatch(getAddOnTypes()).catch(() => {}); | ||
| await flushPromises(); | ||
|
|
||
| expect(store.getActions().map((a) => a.type)).toContain("STOP_LOADING"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("saveAddOnType", () => { | ||
| it("dispatches ADD_ON_TYPE_ADDED then STOP_LOADING when creating (no id)", async () => { | ||
| const store = mockStore({}); | ||
| store.dispatch(saveAddOnType({ name: "VIP" })); | ||
| await flushPromises(); | ||
|
|
||
| const types = store.getActions().map((a) => a.type); | ||
| expect(types).toContain(ADD_ON_TYPE_ADDED); | ||
| expect(types.indexOf("STOP_LOADING")).toBeGreaterThan( | ||
| types.indexOf(ADD_ON_TYPE_ADDED) | ||
| ); | ||
| }); | ||
|
|
||
| it("dispatches ADD_ON_TYPE_UPDATED then STOP_LOADING when updating (has id)", async () => { | ||
| const store = mockStore({}); | ||
| store.dispatch(saveAddOnType({ id: 5, name: "VIP" })); | ||
| await flushPromises(); | ||
|
|
||
| const types = store.getActions().map((a) => a.type); | ||
| expect(types).toContain(ADD_ON_TYPE_UPDATED); | ||
| expect(types.indexOf("STOP_LOADING")).toBeGreaterThan( | ||
| types.indexOf(ADD_ON_TYPE_UPDATED) | ||
| ); | ||
| }); | ||
|
|
||
| it("still dispatches STOP_LOADING when the request rejects", async () => { | ||
| postRequest.mockImplementation(rejectMock); | ||
| putRequest.mockImplementation(rejectMock); | ||
| const store = mockStore({}); | ||
| await store.dispatch(saveAddOnType({ name: "VIP" })).catch(() => {}); | ||
| await flushPromises(); | ||
|
|
||
| expect(store.getActions().map((a) => a.type)).toContain("STOP_LOADING"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("deleteAddOnType", () => { | ||
| it("dispatches STOP_LOADING on success", async () => { | ||
| const store = mockStore({}); | ||
| store.dispatch(deleteAddOnType(7)); | ||
| await flushPromises(); | ||
|
|
||
| expect(store.getActions().map((a) => a.type)).toContain("STOP_LOADING"); | ||
| }); | ||
|
|
||
| it("still dispatches STOP_LOADING when deleteRequest rejects", async () => { | ||
| deleteRequest.mockImplementation(rejectMock); | ||
| const store = mockStore({}); | ||
| await store.dispatch(deleteAddOnType(7)).catch(() => {}); | ||
| await flushPromises(); | ||
|
|
||
| expect(store.getActions().map((a) => a.type)).toContain("STOP_LOADING"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resetAddOnTypeForm", () => { | ||
| it("dispatches RESET_ADD_ON_TYPE_FORM", () => { | ||
| const store = mockStore({}); | ||
| store.dispatch(resetAddOnTypeForm()); | ||
|
|
||
| expect(store.getActions().map((a) => a.type)).toContain( | ||
| RESET_ADD_ON_TYPE_FORM | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| /** | ||
| * Copyright 2026 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * */ | ||
|
|
||
| import T from "i18n-react/dist/i18n-react"; | ||
| import { | ||
| getRequest, | ||
| putRequest, | ||
| postRequest, | ||
| deleteRequest, | ||
| createAction, | ||
| stopLoading, | ||
| startLoading, | ||
| escapeFilterValue | ||
| } from "openstack-uicore-foundation/lib/utils/actions"; | ||
| import { snackbarErrorHandler, snackbarSuccessHandler } from "./base-actions"; | ||
| import { getAccessTokenSafely } from "../utils/methods"; | ||
| import { DEFAULT_PER_PAGE } from "../utils/constants"; | ||
|
|
||
| export const REQUEST_ADD_ON_TYPES = "REQUEST_ADD_ON_TYPES"; | ||
| export const RECEIVE_ADD_ON_TYPES = "RECEIVE_ADD_ON_TYPES"; | ||
| export const RECEIVE_ADD_ON_TYPE = "RECEIVE_ADD_ON_TYPE"; | ||
| export const RESET_ADD_ON_TYPE_FORM = "RESET_ADD_ON_TYPE_FORM"; | ||
| export const UPDATE_ADD_ON_TYPE = "UPDATE_ADD_ON_TYPE"; | ||
| export const ADD_ON_TYPE_UPDATED = "ADD_ON_TYPE_UPDATED"; | ||
| export const ADD_ON_TYPE_ADDED = "ADD_ON_TYPE_ADDED"; | ||
| export const ADD_ON_TYPE_DELETED = "ADD_ON_TYPE_DELETED"; | ||
|
|
||
| export const getAddOnTypes = | ||
| ( | ||
| term = null, | ||
| page = 1, | ||
| perPage = DEFAULT_PER_PAGE, | ||
| order = "order", | ||
| orderDir = 1 | ||
|
Comment on lines
+40
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed - checked every real call site of |
||
| ) => | ||
| async (dispatch) => { | ||
| const accessToken = await getAccessTokenSafely(); | ||
| const filter = []; | ||
|
|
||
| dispatch(startLoading()); | ||
|
|
||
| const params = { | ||
| page, | ||
| per_page: perPage, | ||
| access_token: accessToken | ||
| }; | ||
|
|
||
| if (term) { | ||
| const escapedTerm = escapeFilterValue(term); | ||
| filter.push(`name=@${escapedTerm}`); | ||
| } | ||
|
|
||
| if (filter.length > 0) { | ||
| params["filter[]"] = filter; | ||
| } | ||
|
|
||
| // order | ||
| if (order != null && orderDir != null) { | ||
| const orderDirSign = orderDir === 1 ? "+" : "-"; | ||
| params.order = `${orderDirSign}${order}`; | ||
| } | ||
|
|
||
| return getRequest( | ||
| createAction(REQUEST_ADD_ON_TYPES), | ||
| createAction(RECEIVE_ADD_ON_TYPES), | ||
| `${window.API_BASE_URL}/api/v1/summits/all/add-on-types`, | ||
| snackbarErrorHandler, | ||
| { order, orderDir, perPage, page, term } | ||
| )(params)(dispatch).finally(() => { | ||
| dispatch(stopLoading()); | ||
| }); | ||
| }; | ||
|
|
||
| export const getAddOnType = (addOnTypeId) => async (dispatch) => { | ||
| const accessToken = await getAccessTokenSafely(); | ||
|
|
||
| dispatch(startLoading()); | ||
|
|
||
| const params = { | ||
| access_token: accessToken | ||
| }; | ||
|
|
||
| return getRequest( | ||
| null, | ||
| createAction(RECEIVE_ADD_ON_TYPE), | ||
| `${window.API_BASE_URL}/api/v1/summits/all/add-on-types/${addOnTypeId}`, | ||
| snackbarErrorHandler | ||
| )(params)(dispatch).finally(() => { | ||
| dispatch(stopLoading()); | ||
| }); | ||
| }; | ||
|
|
||
| export const saveAddOnType = (entity) => async (dispatch) => { | ||
| const accessToken = await getAccessTokenSafely(); | ||
| dispatch(startLoading()); | ||
|
|
||
| const params = { | ||
| access_token: accessToken | ||
| }; | ||
|
|
||
| const normalizedEntity = normalizeEntity(entity); | ||
|
|
||
| if (entity.id) { | ||
| return putRequest( | ||
| createAction(UPDATE_ADD_ON_TYPE), | ||
| createAction(ADD_ON_TYPE_UPDATED), | ||
| `${window.API_BASE_URL}/api/v1/summits/all/add-on-types/${entity.id}`, | ||
| normalizedEntity, | ||
| snackbarErrorHandler, | ||
| entity | ||
| )(params)(dispatch) | ||
| .then(() => { | ||
| dispatch( | ||
| snackbarSuccessHandler({ | ||
| title: T.translate("general.success"), | ||
| html: T.translate("add_on_types_list.add_on_type_saved") | ||
| }) | ||
| ); | ||
| }) | ||
| .finally(() => dispatch(stopLoading())); | ||
| } | ||
|
|
||
| return postRequest( | ||
| createAction(UPDATE_ADD_ON_TYPE), | ||
| createAction(ADD_ON_TYPE_ADDED), | ||
| `${window.API_BASE_URL}/api/v1/summits/all/add-on-types`, | ||
| normalizedEntity, | ||
| snackbarErrorHandler, | ||
| entity | ||
| )(params)(dispatch) | ||
| .then(() => { | ||
| dispatch( | ||
| snackbarSuccessHandler({ | ||
| title: T.translate("general.success"), | ||
| html: T.translate("add_on_types_list.add_on_type_created") | ||
| }) | ||
| ); | ||
| }) | ||
| .finally(() => dispatch(stopLoading())); | ||
| }; | ||
|
|
||
| export const deleteAddOnType = (addOnTypeId) => async (dispatch) => { | ||
| const accessToken = await getAccessTokenSafely(); | ||
|
|
||
| dispatch(startLoading()); | ||
|
|
||
| const params = { | ||
| access_token: accessToken | ||
| }; | ||
|
|
||
| return deleteRequest( | ||
| null, | ||
| createAction(ADD_ON_TYPE_DELETED)({ addOnTypeId }), | ||
| `${window.API_BASE_URL}/api/v1/summits/all/add-on-types/${addOnTypeId}`, | ||
| null, | ||
| snackbarErrorHandler | ||
| )(params)(dispatch).finally(() => { | ||
| dispatch(stopLoading()); | ||
| }); | ||
| }; | ||
|
|
||
| export const resetAddOnTypeForm = () => (dispatch) => { | ||
| dispatch(createAction(RESET_ADD_ON_TYPE_FORM)({})); | ||
| }; | ||
|
|
||
| const normalizeEntity = (entity) => { | ||
| const normalizedEntity = { ...entity }; | ||
|
|
||
| delete normalizedEntity.created; | ||
| delete normalizedEntity.last_edited; | ||
|
|
||
| return normalizedEntity; | ||
|
tomrndom marked this conversation as resolved.
|
||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomrndom this file is just testing standard redux procedures, what is the point of this ? Please remove this file entirely and next time ask AI for specific user cases that you want to test against, not just "make tests" because it will test everything, even nonsense