forked from OpenStackweb/summit-admin
-
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
Open
tomrndom
wants to merge
2
commits into
master
Choose a base branch
from
feature/add-on-types-crud
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,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 | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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,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 | ||
| ) => | ||
| 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; | ||
| }; | ||
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
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.