Skip to content

Commit

Permalink
Fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
twschiller committed Jul 6, 2024
1 parent b56aec9 commit 09b2a67
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 81 deletions.
23 changes: 9 additions & 14 deletions src/bricks/transformers/temporaryInfo/DisplayTemporaryInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import { uuidv4 } from "@/types/helpers";
import ConsoleLogger from "@/utils/ConsoleLogger";
import { tick } from "@/starterBricks/starterBrickTestUtils";
import pDefer from "p-defer";
import { registryIdFactory } from "@/testUtils/factories/stringFactories";
import { type RendererErrorPayload } from "@/types/rendererTypes";
import {
MergeStrategies,
Expand All @@ -60,6 +59,7 @@ import { unary } from "lodash";
import { toExpression } from "@/utils/expressionUtils";
import { showModal } from "@/contentScript/modalDom";
import { isLoadedInIframe } from "@/utils/iframeUtils";
import { modComponentRefFactory } from "@/testUtils/factories/modComponentFactories";

jest.mock("@/contentScript/modalDom");
jest.mock("@/contentScript/sidebarController");
Expand Down Expand Up @@ -92,8 +92,7 @@ describe("DisplayTemporaryInfo", () => {
});

test("it returns run payload for sidebar panel", async () => {
const extensionId = uuidv4();
const blueprintId = registryIdFactory();
const modComponentRef = modComponentRefFactory();

const config = getExampleBrickConfig(renderer.id);
const pipeline = {
Expand All @@ -106,13 +105,12 @@ describe("DisplayTemporaryInfo", () => {

await reducePipeline(pipeline, simpleInput({}), {
...testOptions("v3"),
logger: new ConsoleLogger({ extensionId, blueprintId }),
logger: new ConsoleLogger(modComponentRef),
});

// Show function will be called with a "loading" payload
expect(showTemporarySidebarPanel).toHaveBeenCalledExactlyOnceWith({
blueprintId,
extensionId,
modComponentRef,
nonce: expect.toBeString(),
heading: expect.toBeString(),
payload: expect.objectContaining({
Expand All @@ -122,8 +120,7 @@ describe("DisplayTemporaryInfo", () => {

// Panel will be updated when the real payload is ready
expect(updatePanelDefinition).toHaveBeenCalledExactlyOnceWith({
blueprintId,
extensionId,
modComponentRef,
nonce: expect.toBeString(),
heading: expect.toBeString(),
payload: expect.objectContaining({
Expand Down Expand Up @@ -179,13 +176,11 @@ describe("DisplayTemporaryInfo", () => {
},
};

const extensionId = uuidv4();
const modComponentRef = modComponentRefFactory();

const options = {
...testOptions("v3"),
logger: new ConsoleLogger({
extensionId,
}),
logger: new ConsoleLogger(modComponentRef),
};

await reducePipeline(pipeline, simpleInput({}), options);
Expand All @@ -195,10 +190,10 @@ describe("DisplayTemporaryInfo", () => {

expect(waitForTemporaryPanel).toHaveBeenCalledWith({
nonce: expect.toBeString(),
extensionId,
extensionId: modComponentRef.extensionId,
location: "modal",
entry: expect.objectContaining({
extensionId,
modComponentRef,
heading: "Test Temp Panel",
nonce: expect.toBeString(),
payload: expect.toBeObject(),
Expand Down
4 changes: 3 additions & 1 deletion src/starterBricks/sidebar/sidebarStarterBrick.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ describe("sidebarExtension", () => {
forms: [],
panels: [
expect.objectContaining({
extensionPointId: starterBrick.id,
modComponentRef: expect.objectContaining({
extensionPointId: starterBrick.id,
}),
}),
],
temporaryPanels: [],
Expand Down
24 changes: 17 additions & 7 deletions src/store/sidebar/eventKeyUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { uuidv4, validateRegistryId } from "@/types/helpers";
import { type SidebarEntries, type SidebarState } from "@/types/sidebarTypes";
import { sidebarEntryFactory } from "@/testUtils/factories/sidebarEntryFactories";
import { MOD_LAUNCHER } from "@/store/sidebar/constants";
import { modComponentRefFactory } from "@/testUtils/factories/modComponentFactories";

describe("defaultEventKey", () => {
it("returns null no content", () => {
Expand Down Expand Up @@ -147,9 +148,11 @@ describe("eventKeyForEntry", () => {
expect(eventKeyForEntry(value)).toBeNull();
});

it("uses recipeId for activateRecipe", () => {
const recipeId = validateRegistryId("@test/test-recipe");
const entry = sidebarEntryFactory("activateMods", { recipeId });
it("uses modId for activateRecipe", () => {
const modId = validateRegistryId("@test/test-recipe");
const entry = sidebarEntryFactory("activateMods", {
modComponentRef: modComponentRefFactory({ blueprintId: modId }),
});
// Main part is an object hash of the mod ids
expect(eventKeyForEntry(entry)).toStartWith("activate-");
});
Expand All @@ -158,8 +161,10 @@ describe("eventKeyForEntry", () => {
const extensionId = uuidv4();
const extensionPointId = validateRegistryId("@test/test-starter-brick");
const entry = sidebarEntryFactory("panel", {
extensionId,
extensionPointId,
modComponentRef: modComponentRefFactory({
extensionId,
extensionPointId,
}),
});
expect(eventKeyForEntry(entry)).toBe(`panel-${extensionId}`);
});
Expand All @@ -168,12 +173,17 @@ describe("eventKeyForEntry", () => {
const extensionId = uuidv4();
const nonce = uuidv4();

const formEntry = sidebarEntryFactory("form", { extensionId, nonce });
const formEntry = sidebarEntryFactory("form", {
nonce,
modComponentRef: modComponentRefFactory({
extensionId,
}),
});
expect(eventKeyForEntry(formEntry)).toBe(`form-${nonce}`);

const temporaryPanelEntry = sidebarEntryFactory("temporaryPanel", {
extensionId,
nonce,
modComponentRef: modComponentRefFactory({ extensionId }),
});
expect(eventKeyForEntry(temporaryPanelEntry)).toBe(
`temporaryPanel-${nonce}`,
Expand Down
94 changes: 49 additions & 45 deletions src/store/sidebar/sidebarSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import sidebarSlice, {
fixActiveTabOnRemove,
fixActiveTabOnRemoveInPlace,
} from "@/store/sidebar/sidebarSlice";
import { eventKeyForEntry } from "@/store/sidebar/eventKeyUtils";
import {
Expand All @@ -34,6 +34,7 @@ import { configureStore } from "@reduxjs/toolkit";
import addFormPanel from "@/store/sidebar/thunks/addFormPanel";
import addTemporaryPanel from "@/store/sidebar/thunks/addTemporaryPanel";
import removeTemporaryPanel from "@/store/sidebar/thunks/removeTemporaryPanel";
import { modComponentRefFactory } from "@/testUtils/factories/modComponentFactories";

jest.mock("@/sidebar/messenger/api");
jest.mock("@/contentScript/messenger/api");
Expand Down Expand Up @@ -101,7 +102,9 @@ describe("sidebarSlice.addTemporaryPanel", () => {
const existingPanel = sidebarEntryFactory("temporaryPanel");
const otherExistingPanel = sidebarEntryFactory("temporaryPanel");
const newPanel = sidebarEntryFactory("temporaryPanel", {
extensionId: existingPanel.modComponentRef.extensionId,
modComponentRef: modComponentRefFactory({
extensionId: existingPanel.modComponentRef.extensionId,
}),
});

const initialState: SidebarState = {
Expand Down Expand Up @@ -190,15 +193,13 @@ describe("removeTemporaryPanel", () => {
);
});

it("sets activeKey to a panel with the same extensionId if it exists", async () => {
const originalPanel = sidebarEntryFactory("panel", {
extensionId: uuidv4(),
});
const otherExistingPanel = sidebarEntryFactory("form", {
extensionId: uuidv4(),
});
it("sets activeKey to a panel with the same mod component id if it exists", async () => {
const originalPanel = sidebarEntryFactory("panel");
const otherExistingPanel = sidebarEntryFactory("form");
const newPanel = sidebarEntryFactory("temporaryPanel", {
extensionId: originalPanel.modComponentRef.extensionId,
modComponentRef: modComponentRefFactory({
extensionId: originalPanel.modComponentRef.extensionId,
}),
});

const initialState: SidebarState = {
Expand All @@ -210,7 +211,6 @@ describe("removeTemporaryPanel", () => {

const store = configureStore({
reducer: { sidebar: sidebarSlice.reducer },

preloadedState: { sidebar: initialState },
});

Expand Down Expand Up @@ -532,18 +532,20 @@ describe("sidebarSlice.fixActiveTabOnRemove", () => {
it("sets activeKey to the active key of any panel with the same extensionId as the removedEntry if it exists", () => {
const modId = validateRegistryId("test/123");
const originalPanel = sidebarEntryFactory("panel", {
componentRef: {
extensionId: uuidv4(),
modComponentRef: modComponentRefFactory({
blueprintId: modId,
},
}),
});
const otherExistingPanel = sidebarEntryFactory("form", {
extensionId: uuidv4(),
blueprintId: modId,
modComponentRef: modComponentRefFactory({
blueprintId: modId,
}),
});
const newPanel = sidebarEntryFactory("temporaryPanel", {
extensionId: originalPanel.modComponentRef.extensionId,
blueprintId: modId,
modComponentRef: modComponentRefFactory({
extensionId: originalPanel.modComponentRef.extensionId,
blueprintId: modId,
}),
});

const state = {
Expand All @@ -556,27 +558,27 @@ describe("sidebarSlice.fixActiveTabOnRemove", () => {

// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error -- Flaky error
// @ts-ignore-error "Type instantiation is excessively deep and possibly infinite"
fixActiveTabOnRemove(state, newPanel);
fixActiveTabOnRemoveInPlace(state, newPanel);

expect(state).toStrictEqual({
...state,
activeKey: eventKeyForEntry(originalPanel),
});
});

it("sets activeKey to the active key of any panel with the same modId as the removedEntry if it exists and there is no matching extensionId", () => {
it("sets activeKey to the active key of any panel with the same modId as the removedEntry if it exists and there is no matching component id", () => {
const modId = validateRegistryId("test/123");

const firstPanel = sidebarEntryFactory("panel", {
extensionId: uuidv4(),
});
const firstPanel = sidebarEntryFactory("panel");
const matchingPanel = sidebarEntryFactory("panel", {
extensionId: uuidv4(),
blueprintId: modId,
modComponentRef: modComponentRefFactory({
blueprintId: modId,
}),
});
const newPanel = sidebarEntryFactory("temporaryPanel", {
extensionId: uuidv4(),
blueprintId: modId,
modComponentRef: modComponentRefFactory({
blueprintId: modId,
}),
});

const state = {
Expand All @@ -586,7 +588,7 @@ describe("sidebarSlice.fixActiveTabOnRemove", () => {
temporaryPanels: [],
} as SidebarState;

fixActiveTabOnRemove(state, newPanel);
fixActiveTabOnRemoveInPlace(state, newPanel);

expect(state).toStrictEqual({
...state,
Expand All @@ -598,18 +600,26 @@ describe("sidebarSlice.fixActiveTabOnRemove", () => {
const extensionId = uuidv4();

const originalPanel = sidebarEntryFactory("panel", {
extensionId,
modComponentRef: modComponentRefFactory({
extensionId,
}),
});
const firstFormPanel = sidebarEntryFactory("form", {
extensionId,
modComponentRef: modComponentRefFactory({
extensionId,
}),
});
const nullModId = sidebarEntryFactory("form", {
extensionId: uuidv4(),
blueprintId: null,
modComponentRef: modComponentRefFactory({
extensionId,
blueprintId: null,
}),
});
const newPanel = sidebarEntryFactory("temporaryPanel", {
extensionId,
blueprintId: null,
modComponentRef: modComponentRefFactory({
extensionId,
blueprintId: null,
}),
});

const state = {
Expand All @@ -620,7 +630,7 @@ describe("sidebarSlice.fixActiveTabOnRemove", () => {
temporaryPanels: [],
} as SidebarState;

fixActiveTabOnRemove(state, newPanel);
fixActiveTabOnRemoveInPlace(state, newPanel);

expect(state).toStrictEqual({
...state,
Expand All @@ -629,15 +639,9 @@ describe("sidebarSlice.fixActiveTabOnRemove", () => {
});

it("sets activeKey to the defaultEventKey if no panel with the same extensionId as the removedEntry exists", () => {
const originalPanel = sidebarEntryFactory("panel", {
extensionId: uuidv4(),
});
const otherExistingPanel = sidebarEntryFactory("form", {
extensionId: uuidv4(),
});
const newPanel = sidebarEntryFactory("temporaryPanel", {
extensionId: uuidv4(),
});
const originalPanel = sidebarEntryFactory("panel");
const otherExistingPanel = sidebarEntryFactory("form");
const newPanel = sidebarEntryFactory("temporaryPanel");

const state = {
...sidebarSlice.getInitialState(),
Expand All @@ -647,7 +651,7 @@ describe("sidebarSlice.fixActiveTabOnRemove", () => {
temporaryPanels: [],
} as SidebarState;

fixActiveTabOnRemove(state, newPanel);
fixActiveTabOnRemoveInPlace(state, newPanel);

expect(state).toStrictEqual({
...state,
Expand Down
Loading

0 comments on commit 09b2a67

Please sign in to comment.