Skip to content

Commit

Permalink
Ignore variable expression in get variable schema (#9442)
Browse files Browse the repository at this point in the history
  • Loading branch information
twschiller authored Nov 8, 2024
1 parent 0352cfe commit 52e435e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
63 changes: 58 additions & 5 deletions src/bricks/effects/pageState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,31 @@ import { toExpression } from "@/utils/expressionUtils";
import { GetPageState, SetPageState } from "@/bricks/effects/pageState";
import { TEST_resetStateController } from "@/contentScript/stateController/stateController";
import { MergeStrategies, StateNamespaces } from "@/platform/state/stateTypes";
import { getPlatform } from "@/platform/platformContext";

beforeEach(async () => {
await TEST_resetStateController();
});

describe("@pixiebrix/state/get", () => {
test("default to mod namespace", async () => {
it("defaults to mod namespace", async () => {
const data = { foo: 42 };
const options = brickOptionsFactory();

await getPlatform().state.setState({
namespace: StateNamespaces.MOD,
modComponentRef: options.meta.modComponentRef,
data,
mergeStrategy: MergeStrategies.REPLACE,
});

const brick = new GetPageState();
await brick.transform(unsafeAssumeValidArg({}), brickOptionsFactory());
await expect(
brick.transform(unsafeAssumeValidArg({}), options),
).resolves.toStrictEqual(data);
});

test("is page state aware", async () => {
it("is page state aware", async () => {
const brick = new GetPageState();
await expect(brick.isPageStateAware()).resolves.toBe(true);
});
Expand Down Expand Up @@ -140,7 +153,7 @@ describe("@pixiebrix/state/set", () => {
});

describe("set and get", () => {
test("default to blueprint state", async () => {
it("defaults to mod state", async () => {
const setState = new SetPageState();
const getState = new GetPageState();
const brickOptions = brickOptionsFactory();
Expand Down Expand Up @@ -169,8 +182,48 @@ describe("set and get", () => {
expect(result).toStrictEqual({});
});

test("is page state aware", async () => {
it("is page state aware", async () => {
const brick = new SetPageState();
await expect(brick.isPageStateAware()).resolves.toBe(true);
});
});

describe("getModVariableSchema", () => {
it("includes object literal", async () => {
const brick = new SetPageState();
await expect(
brick.getModVariableSchema({
id: SetPageState.BRICK_ID,
config: {
namespace: StateNamespaces.MOD,
data: {
foo: toExpression("var", "@hello"),
},
},
}),
).resolves.toStrictEqual({
type: "object",
properties: {
foo: true,
},
required: ["foo"],
additionalProperties: false,
});
});

it("ignores variable data", async () => {
const brick = new SetPageState();
await expect(
brick.getModVariableSchema({
id: SetPageState.BRICK_ID,
config: {
namespace: StateNamespaces.MOD,
data: toExpression("var", "@hello"),
},
}),
).resolves.toStrictEqual({
type: "object",
additionalProperties: true,
});
});
});
4 changes: 2 additions & 2 deletions src/bricks/effects/pageState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { validateRegistryId } from "@/types/helpers";
import { type BrickConfig } from "@/bricks/types";
import { isObject } from "@/utils/objectUtils";
import { mapValues } from "lodash";
import { castTextLiteralOrThrow } from "@/utils/expressionUtils";
import { castTextLiteralOrThrow, isExpression } from "@/utils/expressionUtils";
import { propertiesToSchema } from "@/utils/schemaUtils";
import {
MergeStrategies,
Expand Down Expand Up @@ -122,7 +122,7 @@ export class SetPageState extends TransformerABC {
return;
}

if (isObject(data)) {
if (isObject(data) && !isExpression(data)) {
return {
type: "object",
// Only track the existence of the properties, not their values
Expand Down

0 comments on commit 52e435e

Please sign in to comment.