Skip to content

Commit

Permalink
fix: fetch & flatten the latest published version of external portal …
Browse files Browse the repository at this point in the history
…data when publishing the parent flow (#2783)
  • Loading branch information
jessicamcinchak authored Mar 15, 2024
1 parent 0786cd5 commit ddd665f
Show file tree
Hide file tree
Showing 45 changed files with 1,299 additions and 319 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ AWS_SECRET_KEY=👻
FILE_API_KEY=👻
FILE_API_KEY_NEXUS=👻

# Editor/Preview
# Editor
EDITOR_URL_EXT=http://localhost:3000

# Hasura
Expand Down
163 changes: 76 additions & 87 deletions api.planx.uk/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import {
isLiveEnv,
} from "./helpers";
import { queryMock } from "./tests/graphqlQueryMock";
import { userContext } from "./modules/auth/middleware";
import { getJWT } from "./tests/mockJWT";
import {
childFlow,
draftParentFlow,
flattenedParentFlow,
} from "./tests/mocks/validateAndPublishMocks";

describe("getEnvironment function", () => {
describe("getFormattedEnvironment() function", () => {
const OLD_ENV = process.env;

beforeEach(() => {
Expand Down Expand Up @@ -69,121 +72,107 @@ describe("isLiveEnv() function", () => {
});

describe("dataMerged() function", () => {
const getStoreMock = jest.spyOn(userContext, "getStore");
getStoreMock.mockReturnValue({
user: {
sub: "123",
jwt: getJWT({ role: "teamEditor" }),
},
});

beforeEach(() => {
const unflattenedParent = {
_root: {
edges: ["Zj0ZKa0PwT", "Rur8iS88x3"],
},
"5yElH96W7I": {
data: {
text: "Option 2",
},
type: 200,
edges: ["aMlxwR7ONH"],
},
Rur8iS88x3: {
data: {
color: "#EFEFEF",
title: "End of the line",
resetButton: false,
},
type: 8,
},
SShTHaRo2k: {
data: {
flowId: "child-id",
},
type: 310,
},
Zj0ZKa0PwT: {
data: {
text: "This is a question with many options",
},
type: 100,
edges: ["c8hZwm0a9c", "5yElH96W7I", "UMsI68BuAy"],
},
c8hZwm0a9c: {
data: {
text: "Option 1",
},
type: 200,
edges: ["SShTHaRo2k"],
},
aMlxwR7ONH: {
type: 310,
data: {
flowId: "child-id",
},
queryMock.mockQuery({
name: "GetFlowData",
matchOnVariables: true,
variables: {
id: "parent-flow-with-external-portal",
},
UMsI68BuAy: {
type: 200,
data: {
text: "Option 3",
data: {
flow: {
data: draftParentFlow,
slug: "parent-flow",
team_id: 1,
team: {
slug: "testing",
},
publishedFlows: [{ data: flattenedParentFlow }],
},
},
};
});
});

const unflattenedChild = {
_root: {
edges: ["sbDyJVsyXg"],
it("flattens published external portal nodes by overwriting their type", async () => {
queryMock.mockQuery({
name: "GetFlowData",
matchOnVariables: true,
variables: {
id: "child-flow-id",
},
sbDyJVsyXg: {
type: 100,
data: {
description: "<p>Hello there 👋</p>",
text: "This is within the portal",
data: {
flow: {
data: childFlow,
slug: "child-flow",
team_id: 1,
team: {
slug: "testing",
},
publishedFlows: [{ data: childFlow }],
},
},
};
});

const result = await dataMerged("parent-flow-with-external-portal");
const nodeTypes = Object.values(result).map((node) =>
"type" in node ? node.type : undefined,
);

expect(nodeTypes.includes(ComponentType.ExternalPortal)).toBe(false);
});

it("throws an error when an external portal is not published", async () => {
queryMock.mockQuery({
name: "GetFlowData",
matchOnVariables: true,
variables: {
id: "child-id",
id: "child-flow-id",
},
data: {
flow: {
data: childFlow,
slug: "child-flow",
data: unflattenedChild,
team_id: 123,
team_id: 1,
team: {
slug: "testing",
},
publishedFlows: [],
},
},
});

await expect(
dataMerged("parent-flow-with-external-portal"),
).rejects.toThrow();
});

it("flattens any published or draft external portal nodes when isDraftData only is set to true", async () => {
queryMock.mockQuery({
name: "GetFlowData",
matchOnVariables: true,
variables: {
id: "parent-id",
id: "child-flow-id",
},
data: {
flow: {
slug: "parent-flow",
data: unflattenedParent,
team_id: 123,
data: childFlow,
slug: "child-flow",
team_id: 1,
team: {
slug: "testing",
},
publishedFlows: [],
},
},
});
});

it("handles multiple external portal nodes", async () => {
const result = await dataMerged("parent-id");
const nodeTypes = Object.values(result).map((node) =>
"type" in node ? node.type : undefined,
const result = await dataMerged(
"parent-flow-with-external-portal",
{},
false,
true,
);
const areAllPortalsFlattened = !nodeTypes.includes(
ComponentType.ExternalPortal,
);

// All external portals have been flattened / replaced
expect(areAllPortalsFlattened).toBe(true);
expect(result).toEqual(flattenedParentFlow);
});
});

Expand Down
Loading

0 comments on commit ddd665f

Please sign in to comment.