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
170 changes: 170 additions & 0 deletions src/actions/__tests__/add-on-types-actions.test.js
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
);
});
});
});
183 changes: 183 additions & 0 deletions src/actions/add-on-types-actions.js
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;
Comment thread
tomrndom marked this conversation as resolved.
};
4 changes: 4 additions & 0 deletions src/components/menu/menu-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export const getGlobalItems = () => [
{
name: "page_templates",
linkUrl: "page-templates"
},
{
name: "add_on_types",
linkUrl: "add-on-types"
Comment thread
tomrndom marked this conversation as resolved.
}
]
},
Expand Down
17 changes: 16 additions & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@
"sponsors_inventory": "Sponsors",
"form_templates": "Form Templates",
"inventory": "Inventory",
"page_templates": "Pages"
"page_templates": "Pages",
"add_on_types": "Add-On Types"
},
"schedule": {
"schedule": "Schedule",
Expand Down Expand Up @@ -4223,6 +4224,20 @@
},
"clone_success": "Page template cloned successfully."
},
"add_on_types_list": {
"add_on_types": "Add-On Types",
"add_on_type": "Add-On Type",
"id_column_label": "Id",
"name_column_label": "Name",
"add_add_on_type": "Add Add-On Type",
"add_on_type_created": "Add-On Type Created",
"add_on_type_saved": "Add-On Type Saved",
"delete_add_on_type_warning": "Are you sure you want to delete Add-On Type {name}",
"no_results": "No items found for this search criteria.",
"placeholders": {
"search_add_on_types": "Search by name"
}
},
"dropbox_sync": {
"panel_title": "Synchronization Settings",
"toggle_label": "Enable location-based Dropbox file synchronization",
Expand Down
Loading
Loading