Skip to content

Commit

Permalink
#8600: drop deprecated localStorage data binding
Browse files Browse the repository at this point in the history
  • Loading branch information
twschiller committed Jun 16, 2024
1 parent 0a7796d commit fe93dea
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 118 deletions.
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
106 changes: 80 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,56 @@ 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("error on storage.type localStorage", async () => {
// eslint-disable-next-line testing-library/render-result-naming-convention -- false positive
const renderPromise = brick.render(
unsafeAssumeValidArg({
storage: { type: "localStorage" },
autoSave: true,
recordId: "test",
schema: {
type: "object",
properties: {
name: { type: "string" },
},
},
}),
brickOptionsFactory(),
);

await expect(renderPromise).rejects.toThrow(
"localStorage data binding is no longer supported 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 +313,7 @@ describe("CustomFormRenderer", () => {
name: { type: "string" },
},
},
} as any,
}),
brickOptionsFactory({ runPipeline: () => runPipelineMock }),
);

Expand All @@ -278,23 +326,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 +355,8 @@ describe("CustomFormRenderer", () => {
name: { type: "string" },
},
},
} as any,
brickOptionsFactory({ runPipeline: () => runPipelineMock }),
}),
options,
);

render(<Component {...props} />);
Expand All @@ -322,7 +369,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

0 comments on commit fe93dea

Please sign in to comment.