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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@mui/icons-material": "^6.4.3",
"@mui/material": "^6.4.3",
"@mui/x-date-pickers": "^7.26.0",
"@mui/x-tree-view": "^7.26.0",
Comment thread
santipalenque marked this conversation as resolved.
"@react-pdf/renderer": "^4.4.1",
"@sentry/react": "^8.32.0",
"@sentry/webpack-plugin": "^2.22.4",
Expand Down Expand Up @@ -101,7 +102,6 @@
"prop-types": "^15.8.1",
"qr-scanner": "^1.4.2",
"react": "^16.13.1",
"react-accessible-treeview": "^2.6.2",
"react-beautiful-dnd": "^13.1.1",
"react-bootstrap": "^0.31.5",
"react-breadcrumbs": "^2.1.6",
Expand Down
53 changes: 35 additions & 18 deletions src/actions/email-flows-events-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* 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 {
Expand All @@ -18,25 +18,25 @@ import {
createAction,
stopLoading,
startLoading,
showSuccessMessage,
setSnackbarMessage,
authErrorHandler,
escapeFilterValue
} from "openstack-uicore-foundation/lib/utils/actions";
import { getAccessTokenSafely } from "../utils/methods";
import { DEFAULT_PER_PAGE } from "../utils/constants";

export const REQUEST_EMAIL_FLOW_EVENTS = "REQUEST_EMAIL_FLOW_EVENTS";
export const RECEIVE_EMAIL_FLOW_EVENTS = "RECEIVE_EMAIL_FLOW_EVENTS";
export const RECEIVE_EMAIL_FLOW_EVENT = "RECEIVE_EMAIL_FLOW_EVENT";
export const RESET_EMAIL_FLOW_EVENT_FORM = "RESET_EMAIL_FLOW_EVENT_FORM";
export const UPDATE_EMAIL_FLOW_EVENT = "UPDATE_EMAIL_FLOW_EVENT";
export const EMAIL_FLOW_EVENT_UPDATED = "EMAIL_FLOW_EVENT_UPDATED";
export const EMAIL_FLOW_EVENT_DELETED = "EMAIL_FLOW_EVENT_DELETED";

export const getEmailFlowEvents =
(
term = null,
term = "",
page = 1,
perPage = 10,
perPage = DEFAULT_PER_PAGE,
order = "email_template_identifier",
orderDir = 1
) =>
Expand All @@ -49,7 +49,7 @@ export const getEmailFlowEvents =
const filter = [];

const params = {
page: page,
page,
per_page: perPage,
access_token: accessToken
};
Expand All @@ -64,7 +64,7 @@ export const getEmailFlowEvents =
// order
if (order != null && orderDir != null) {
const orderDirSign = orderDir === 1 ? "+" : "-";
params["order"] = `${orderDirSign}${order}`;
params.order = `${orderDirSign}${order}`;
}

if (filter.length > 0) {
Expand All @@ -75,11 +75,14 @@ export const getEmailFlowEvents =
createAction(REQUEST_EMAIL_FLOW_EVENTS),
createAction(RECEIVE_EMAIL_FLOW_EVENTS),
`${window.API_BASE_URL}/api/v1/summits/${currentSummit.id}/email-flows-events`,
// TODO: replace with snackbarErrorHandler once it handles 401's (re-login redirect)
authErrorHandler,
{ order, orderDir, term }
)(params)(dispatch).then(() => {
dispatch(stopLoading());
});
)(params)(dispatch)
.finally(() => {
dispatch(stopLoading());
})
.catch(() => {});
};

export const getEmailFlowEvent = (eventId) => async (dispatch, getState) => {
Expand All @@ -98,13 +101,16 @@ export const getEmailFlowEvent = (eventId) => async (dispatch, getState) => {
null,
createAction(RECEIVE_EMAIL_FLOW_EVENT),
`${window.API_BASE_URL}/api/v1/summits/${currentSummit.id}/email-flows-events/${eventId}`,
// TODO: replace with snackbarErrorHandler once it handles 401's (re-login redirect)
authErrorHandler
)(params)(dispatch).then(() => {
dispatch(stopLoading());
});
)(params)(dispatch)
.finally(() => {
dispatch(stopLoading());
})
.catch(() => {});
};

export const resetEmailFlowEventForm = () => (dispatch, getState) => {
export const resetEmailFlowEventForm = () => (dispatch) => {
dispatch(createAction(RESET_EMAIL_FLOW_EVENT_FORM)({}));
};

Expand All @@ -119,14 +125,25 @@ export const saveEmailFlowEvent = (entity) => async (dispatch, getState) => {

dispatch(startLoading());

putRequest(
return putRequest(
createAction(UPDATE_EMAIL_FLOW_EVENT),
createAction(EMAIL_FLOW_EVENT_UPDATED),
`${window.API_BASE_URL}/api/v1/summits/${currentSummit.id}/email-flows-events/${entity.id}`,
entity,
// TODO: replace with snackbarErrorHandler once it handles 401's (re-login redirect)
authErrorHandler,
entity
)(params)(dispatch).then((payload) => {
dispatch(showSuccessMessage(T.translate("edit_email_flow_event.saved")));
});
)(params)(dispatch)
.then(() => {
dispatch(
setSnackbarMessage({
html: T.translate("edit_email_flow_event.saved"),
type: "success"
})
);
})
.finally(() => {
dispatch(stopLoading());
})
.catch(() => {});
};
56 changes: 36 additions & 20 deletions src/components/CustomTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ import { createTheme } from "@mui/material/styles";
import { MuiBaseCustomTheme } from "openstack-uicore-foundation/lib/utils/theme";
import PropTypes from "prop-types";

const theme = createTheme(MuiBaseCustomTheme, {
palette: {
primary: {
main: "#2196F3",
dark: "#1E88E5",
contrast: "#FFFFFF"
},
background: {
light: "#F7F7F9",
light_gray: "#eaeaea"
},
text: {
primary: "#000000DE",
secondary: "#00000099",
link: "#2196f3",
disabled: "#00000061"
}
// theme.typography.<variant> functions are silently ignored by MUI (it only
// applies plain-object entries), so a palette-derived typography color can't
// read the theme at runtime - build the palette first and reference it directly.
const palette = {
primary: {
main: "#2196F3",
dark: "#1E88E5",
contrast: "#FFFFFF"
},
background: {
light: "#F7F7F9",
light_gray: "#eaeaea"
},
text: {
primary: "#000000DE",
secondary: "#00000099",
link: "#2196f3",
disabled: "#00000061"
}
};

const theme = createTheme(MuiBaseCustomTheme, {
palette,
typography: {
fontFamily: ["Roboto", "sans-serif"].join(","),
body1: {
Expand All @@ -36,11 +41,17 @@ const theme = createTheme(MuiBaseCustomTheme, {
fontSize: "12px",
fontWeight: 400
},
subtitle2: ({ theme: t }) => ({
subtitle2: {
fontSize: "14px",
fontWeight: 500,
color: t.palette.text.primary
}),
color: palette.text.primary
},
label: {
fontSize: "14px",
fontWeight: 400,
lineHeight: "1.4375em",
color: palette.text.secondary
},
h4: {
fontSize: "34px",
fontWeight: 500,
Expand All @@ -65,6 +76,11 @@ const theme = createTheme(MuiBaseCustomTheme, {
}
},
components: {
MuiTypography: {
variantMapping: {
label: "p"
}
},
MuiFormHelperText: {
styleOverrides: {
root: {
Expand Down
174 changes: 174 additions & 0 deletions src/components/forms/email-flow-event-form/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// jsdom does not implement scrollIntoView; polyfill so the errors effect
// (which calls scrollToError -> firstNode.scrollIntoView) does not throw.
window.HTMLElement.prototype.scrollIntoView = jest.fn();

jest.mock("i18n-react/dist/i18n-react", () => ({
__esModule: true,
default: { translate: (key) => key }
}));

// Stands in for the real react-select AsyncSelect based input: exposes a
// button that fires the same {target: {id, value}} shape the real component
// dispatches on selection.
jest.mock("../../../inputs/email-template-input", () => ({
__esModule: true,
default: ({ id, onChange }) => (
<button
type="button"
onClick={() =>
onChange({
target: { id, value: "NEW_TEMPLATE", type: "emailtemplateinput" }
})
}
>
pick-template
</button>
)
}));

// Mirrors the mocking convention used for marketing-setting-form.test.js:
// read/write real Formik state via useField, letting the wrapper's own
// error/FormHelperText rendering stay untested here (covered upstream).
jest.mock(
"openstack-uicore-foundation/lib/components/mui/formik-inputs/textfield",
() => {
const React = require("react");
const { useField } = require("formik");
return {
__esModule: true,
default: function MockMuiFormikTextField({ name }) {
const [field] = useField(name);
return <input data-testid={`textfield-${name}`} {...field} />;
}
};
}
);

/* eslint-disable import/first */
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import "@testing-library/jest-dom";
import { FormikProvider, useFormik } from "formik";
import EmailFlowEventForm from "../index";
import {
buildValues,
validationSchema
} from "../../../../pages/email_flow_events/edit-email-flow-event-page";
/* eslint-enable import/first */

const BASE_ENTITY = {
id: 5,
flow_name: "REGISTRATION",
event_type_name: "Attendee Registered",
email_template_identifier: "",
recipients: [],
template_schema: null
};

// Mirrors the formik wiring edit-email-flow-event-page.js provides in
// production, so EmailFlowEventForm's useFormikContext() has a real context.
const Harness = ({ entity, onSubmit = jest.fn() }) => {
const formik = useFormik({
initialValues: buildValues(entity),
validationSchema,
onSubmit
});

return (
<FormikProvider value={formik}>
<EmailFlowEventForm entity={entity} />
<button type="button" onClick={() => formik.handleSubmit()}>
general.save
</button>
<pre data-testid="debug-values">{JSON.stringify(formik.values)}</pre>
<pre data-testid="debug-errors">{JSON.stringify(formik.errors)}</pre>
</FormikProvider>
);
};

const readFormikValues = () =>
JSON.parse(screen.getByTestId("debug-values").textContent);
const readFormikErrors = () =>
JSON.parse(screen.getByTestId("debug-errors").textContent);

const clickSave = () =>
userEvent.click(screen.getByRole("button", { name: "general.save" }));

describe("EmailFlowEventForm", () => {
it("renders the read-only flow name and event type", () => {
render(<Harness entity={BASE_ENTITY} />);

expect(screen.getByText("REGISTRATION")).toBeInTheDocument();
expect(screen.getByText("Attendee Registered")).toBeInTheDocument();
});

it("round-trips typed recipients into formik values", () => {
render(<Harness entity={BASE_ENTITY} />);

fireEvent.change(screen.getByTestId("textfield-recipients"), {
target: { value: "a@example.com,b@example.com" }
});

expect(readFormikValues().recipients).toBe("a@example.com,b@example.com");
});

it("updates the email template identifier via EmailTemplateInput", async () => {
render(<Harness entity={BASE_ENTITY} />);

await userEvent.click(
screen.getByRole("button", { name: "pick-template" })
);

expect(readFormikValues().email_template_identifier).toBe("NEW_TEMPLATE");
});

it("shows the see-template link and CopyClipboard when a template is set", () => {
render(
<Harness
entity={{ ...BASE_ENTITY, email_template_identifier: "MY_TEMPLATE" }}
/>
);

expect(screen.getByRole("link", { name: "see template" })).toHaveAttribute(
"href",
"/app/emails/templates/MY_TEMPLATE"
);
});

it("blocks submit and surfaces an error for an invalid recipient email", async () => {
const onSubmit = jest.fn();
render(<Harness entity={BASE_ENTITY} onSubmit={onSubmit} />);

fireEvent.change(screen.getByTestId("textfield-recipients"), {
target: { value: "not-an-email" }
});
await clickSave();

expect(readFormikErrors().recipients).toBeTruthy();
expect(onSubmit).not.toHaveBeenCalled();
});

it("allows submit with valid comma-separated recipient emails", async () => {
const onSubmit = jest.fn();
render(<Harness entity={BASE_ENTITY} onSubmit={onSubmit} />);

fireEvent.change(screen.getByTestId("textfield-recipients"), {
target: { value: "a@example.com, b@example.com" }
});
await clickSave();

expect(readFormikErrors().recipients).toBeUndefined();
expect(onSubmit).toHaveBeenCalled();
});

it("allows submit with empty recipients (optional field)", async () => {
const onSubmit = jest.fn();
render(<Harness entity={BASE_ENTITY} onSubmit={onSubmit} />);

await clickSave();

expect(readFormikErrors().recipients).toBeUndefined();
expect(onSubmit).toHaveBeenCalled();
});
});
Loading
Loading