Skip to content
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

#8600: drop deprecated localStorage data binding for embedded forms #8631

Merged
merged 6 commits into from
Jun 17, 2024
Merged
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
5 changes: 0 additions & 5 deletions src/__mocks__/@/background/messenger/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,4 @@ export const performConfiguredRequestInBackground = getMethod(
requestConfig: NetworkRequestConfig,
) => Promise<RemoteResponse<TData>>;

export const dataStore = {
get: jest.fn().mockRejectedValue(new Error("Not implemented in mock")),
set: getMethod("SET_DATA_STORE", bg),
};

export const clearServiceCache = jest.fn();
38 changes: 0 additions & 38 deletions src/background/dataStore.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/background/messenger/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ export const getAvailableVersion = getMethod("GET_AVAILABLE_VERSION", bg);
export const showMySidePanel = getMethod("SHOW_MY_SIDE_PANEL", bg);
export const waitForContentScript = getMethod("WAIT_FOR_CONTENT_SCRIPT", bg);

export const dataStore = {
get: getMethod("GET_DATA_STORE", bg),
set: getMethod("SET_DATA_STORE", bg),
};
export const activateTheme = getMethod("ACTIVATE_THEME", bg);

export const traces = {
Expand Down
5 changes: 0 additions & 5 deletions src/background/messenger/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { registerMethods } from "webext-messenger";
import { expectContext } from "@/utils/expectContext";
import { showMySidePanel } from "@/background/sidePanel";
import { waitForContentScript } from "@/background/contentScript";
import { getRecord, setRecord } from "@/background/dataStore";
import initTheme from "@/background/initTheme";
import {
addTraceEntry,
Expand Down Expand Up @@ -84,8 +83,6 @@ declare global {
interface MessengerMethods {
SHOW_MY_SIDE_PANEL: typeof showMySidePanel;
WAIT_FOR_CONTENT_SCRIPT: typeof waitForContentScript;
GET_DATA_STORE: typeof getRecord;
SET_DATA_STORE: typeof setRecord;
ACTIVATE_THEME: typeof initTheme;
ADD_TRACE_ENTRY: typeof addTraceEntry;
ADD_TRACE_EXIT: typeof addTraceExit;
Expand Down Expand Up @@ -156,8 +153,6 @@ export default function registerMessenger(): void {
registerMethods({
SHOW_MY_SIDE_PANEL: showMySidePanel,
WAIT_FOR_CONTENT_SCRIPT: waitForContentScript,
GET_DATA_STORE: getRecord,
SET_DATA_STORE: setRecord,
ACTIVATE_THEME: initTheme,
ADD_TRACE_ENTRY: addTraceEntry,
ADD_TRACE_EXIT: addTraceExit,
Expand Down
84 changes: 58 additions & 26 deletions src/bricks/renderers/customForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ import {
normalizeOutgoingFormData,
} from "./customForm";
import userEvent from "@testing-library/user-event";

import { dataStore } from "@/background/messenger/api";
import { type Schema } from "@/types/schemaTypes";
import { brickOptionsFactory } from "@/testUtils/factories/runtimeFactories";
import { templates } from "@/components/formBuilder/RjsfTemplates";
import { toExpression } from "@/utils/expressionUtils";

const dataStoreGetMock = jest.mocked(dataStore.get);
const dataStoreSetSpy = jest.spyOn(dataStore, "set");
import { unsafeAssumeValidArg } from "@/runtime/runtimeTypes";
import {
TEST_resetState,
getState,
setState,
} from "@/platform/state/stateController";
import type { Target } from "@/types/messengerTypes";

const brick = new CustomFormRenderer();

// CustomForm uses @/contentScript/messenger/api instead of the platform API
jest.mock("@/contentScript/messenger/api", () => ({
getPageState: jest.fn((_: Target, args: any) => getState(args)),
setPageState: jest.fn((_: Target, args: any) => setState(args)),
}));

// I couldn't get shadow-dom-testing-library working
jest.mock("react-shadow/emotion", () => ({
Expand Down Expand Up @@ -214,17 +224,14 @@ describe("form data normalization", () => {

describe("CustomFormRenderer", () => {
beforeEach(() => {
TEST_resetState();
jest.clearAllMocks();
});

test("Render auto-saved form", async () => {
const brick = new CustomFormRenderer();

dataStoreGetMock.mockResolvedValue({});

const { Component, props } = await brick.render(
{
storage: { type: "localStorage" },
unsafeAssumeValidArg({
storage: { type: "state" },
autoSave: true,
recordId: "test",
schema: {
Expand All @@ -233,7 +240,7 @@ describe("CustomFormRenderer", () => {
name: { type: "string" },
},
},
} as any,
}),
brickOptionsFactory(),
);

Expand All @@ -243,15 +250,34 @@ describe("CustomFormRenderer", () => {
await expect(screen.findByRole("textbox")).resolves.toBeInTheDocument();
});

test("error on no storage defined", async () => {
// eslint-disable-next-line testing-library/render-result-naming-convention -- false positive
const renderPromise = brick.render(
unsafeAssumeValidArg({
autoSave: true,
recordId: "test",
schema: {
type: "object",
properties: {
name: { type: "string" },
},
},
}),
brickOptionsFactory(),
);

await expect(renderPromise).rejects.toThrow(
"storage is required since extension version 2.0.3",
);
});

test("Supports postSubmitAction reset", async () => {
const brick = new CustomFormRenderer();
const runPipelineMock = jest.fn();

dataStoreGetMock.mockResolvedValue({});

const { Component, props } = await brick.render(
{
storage: { type: "localStorage" },
unsafeAssumeValidArg({
storage: { type: "state" },
recordId: "test",
onSubmit: toExpression("pipeline", []),
postSubmitAction: "reset",
Expand All @@ -265,7 +291,7 @@ describe("CustomFormRenderer", () => {
name: { type: "string" },
},
},
} as any,
}),
brickOptionsFactory({ runPipeline: () => runPipelineMock }),
);

Expand All @@ -278,23 +304,22 @@ describe("CustomFormRenderer", () => {

expect(runPipelineMock).toHaveBeenCalledOnce();

expect(dataStoreSetSpy).not.toHaveBeenCalled();

// Need to get new textbox reference, because the old one is removed when the key changes
expect(screen.getByRole("textbox")).toHaveValue("");
});

test.each([undefined, "save"])(
"postSubmitAction: %s doesn't reset",
async (postSubmitAction) => {
const brick = new CustomFormRenderer();
const runPipelineMock = jest.fn();

dataStoreGetMock.mockResolvedValue({});
const options = brickOptionsFactory({
runPipeline: () => runPipelineMock,
});

const { Component, props } = await brick.render(
{
storage: { type: "localStorage" },
unsafeAssumeValidArg({
storage: { type: "state" },
recordId: "test",
onSubmit: toExpression("pipeline", []),
postSubmitAction,
Expand All @@ -308,8 +333,8 @@ describe("CustomFormRenderer", () => {
name: { type: "string" },
},
},
} as any,
brickOptionsFactory({ runPipeline: () => runPipelineMock }),
}),
options,
);

render(<Component {...props} />);
Expand All @@ -322,7 +347,14 @@ describe("CustomFormRenderer", () => {
await userEvent.click(screen.getByRole("button", { name: "Submit" }));

expect(runPipelineMock).toHaveBeenCalledOnce();
expect(dataStoreSetSpy).toHaveBeenCalledExactlyOnceWith("test", {

expect(
getState({
namespace: "blueprint",
extensionId: options.logger.context.extensionId,
blueprintId: options.logger.context.blueprintId,
}),
).toStrictEqual({
name: value,
});

Expand Down
Loading
Loading