Skip to content

Commit

Permalink
refactor: Add presentational component to Result (#3725)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Sep 30, 2024
1 parent 354d087 commit 1c3e77f
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 210 deletions.
2 changes: 1 addition & 1 deletion api.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@airbrake/node": "^2.1.8",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#c4a725f",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#79b6409",
"@types/isomorphic-fetch": "^0.0.36",
"adm-zip": "^0.5.10",
"aws-sdk": "^2.1467.0",
Expand Down
9 changes: 5 additions & 4 deletions api.planx.uk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/tests/api-driven/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"packageManager": "[email protected]",
"dependencies": {
"@cucumber/cucumber": "^9.3.0",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#c4a725f",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#79b6409",
"axios": "^1.7.4",
"dotenv": "^16.3.1",
"dotenv-expand": "^10.0.0",
Expand Down
8 changes: 4 additions & 4 deletions e2e/tests/api-driven/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/tests/ui-driven/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"postinstall": "./install-dependencies.sh"
},
"dependencies": {
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#c4a725f",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#79b6409",
"axios": "^1.7.4",
"dotenv": "^16.3.1",
"eslint": "^8.56.0",
Expand Down
8 changes: 4 additions & 4 deletions e2e/tests/ui-driven/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion editor.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@mui/material": "^5.15.10",
"@mui/utils": "^5.15.11",
"@opensystemslab/map": "1.0.0-alpha.3",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#c4a725f",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#79b6409",
"@tiptap/core": "^2.4.0",
"@tiptap/extension-bold": "^2.0.3",
"@tiptap/extension-bubble-menu": "^2.1.13",
Expand Down
20 changes: 9 additions & 11 deletions editor.planx.uk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 9 additions & 17 deletions editor.planx.uk/src/@planx/components/Result/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Box from "@mui/material/Box";
import Collapse from "@mui/material/Collapse";
import Typography from "@mui/material/Typography";
import { Flag, FlagSet, flatFlags } from "@opensystemslab/planx-core/types";
import { Flag, flatFlags } from "@opensystemslab/planx-core/types";
import { ComponentType as TYPES } from "@opensystemslab/planx-core/types";
import { useFormik } from "formik";
import groupBy from "lodash/groupBy";
Expand All @@ -11,8 +11,8 @@ import ModalSectionContent from "ui/editor/ModalSectionContent";
import Input from "ui/shared/Input";
import InputRow from "ui/shared/InputRow";

import { ICONS } from "../ui";
import type { Result } from "./model";
import { EditorProps, ICONS } from "../ui";
import { FlagDisplayText, Result } from "./model";

type FlagWithValue = Flag & { value: NonNullable<Flag["value"]> };

Expand All @@ -21,16 +21,6 @@ const flagsWithValues = flatFlags.filter((flag): flag is FlagWithValue =>
);
const flags = groupBy(flagsWithValues, (f) => f.category);

interface FormData {
flagSet: FlagSet;
overrides?: { [flagId: string]: FlagDisplayText };
}

interface FlagDisplayText {
heading?: string;
description?: string;
}

const FlagEditor: React.FC<{
flag: Flag;
existingOverrides?: FlagDisplayText;
Expand Down Expand Up @@ -82,8 +72,10 @@ const FlagEditor: React.FC<{
);
};

const ResultComponent: React.FC<Result> = (props) => {
const formik = useFormik<FormData>({
type Props = EditorProps<TYPES.Result, Result>;

const ResultComponent: React.FC<Props> = (props) => {
const formik = useFormik<Result>({
initialValues: {
flagSet: props.node?.data?.flagSet || Object.keys(flags)[0],
overrides: props.node?.data?.overrides || {},
Expand All @@ -93,7 +85,7 @@ const ResultComponent: React.FC<Result> = (props) => {
props.handleSubmit({ type: TYPES.Result, data: newValues });
}
},
validate: () => {},
validate: () => { },
});

const allFlagsForSet = flags[formik.values.flagSet];
Expand Down Expand Up @@ -157,4 +149,4 @@ const ResultComponent: React.FC<Result> = (props) => {
);
};

export default ResultComponent;
export default ResultComponent;
44 changes: 21 additions & 23 deletions editor.planx.uk/src/@planx/components/Result/Public.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { vi } from "vitest";
import { axe } from "vitest-axe";

import { useStore } from "../../../pages/FlowEditor/lib/store";
import Result from "./Public";
import { Presentational } from "./Public";

const { getState, setState } = useStore;

Expand All @@ -17,7 +17,7 @@ test("renders correctly", async () => {
const handleSubmit = vi.fn();

const { user } = setup(
<Result
<Presentational
headingColor={{ text: "#000", background: "#fff" }}
responses={[]}
handleSubmit={handleSubmit}
Expand All @@ -32,7 +32,7 @@ test("renders correctly", async () => {

it("should not have any accessibility violations", async () => {
const { container } = setup(
<Result
<Presentational
headingColor={{ text: "#000", background: "#fff" }}
responses={[
{
Expand All @@ -58,15 +58,15 @@ it("should not have any accessibility violations", async () => {
describe("showing and hiding change capabilities", () => {
it("hides the change button by default", () => {
setup(
<Result
<Presentational
responses={[
{
question: { data: { text: "How's the weather?" }, id: "a" },
hidden: false,
selections: [],
},
]}
handleSubmit={() => {}}
handleSubmit={() => { }}
headingColor={{ text: "pink", background: "white" }}
/>,
);
Expand Down Expand Up @@ -154,23 +154,21 @@ describe("showing and hiding change capabilities", () => {
];

scenarios.forEach(({ allowChanges, autoAnswered, shouldBeChangeable }) => {
it(`${
shouldBeChangeable ? "shows" : "hides"
} the change button when allowChanges is ${allowChanges} and question ${
autoAnswered ? "was" : "wasn't"
} auto-answered`, () => {
setup(
<Result
{...{ allowChanges, responses }}
handleSubmit={() => {}}
headingColor={{ text: "pink", background: "white" }}
/>,
);

expect(Boolean(screen.queryByText("Change"))).toEqual(
shouldBeChangeable,
);
});
it(`${shouldBeChangeable ? "shows" : "hides"
} the change button when allowChanges is ${allowChanges} and question ${autoAnswered ? "was" : "wasn't"
} auto-answered`, () => {
setup(
<Presentational
{...{ allowChanges, responses }}
handleSubmit={() => { }}
headingColor={{ text: "pink", background: "white" }}
/>,
);

expect(Boolean(screen.queryByText("Change"))).toEqual(
shouldBeChangeable,
);
});
});
});
});
});
Loading

0 comments on commit 1c3e77f

Please sign in to comment.