From 3c98ef8cb1cd7fa0a7d970aeeae99ef020ac32f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Tue, 21 Dec 2021 16:26:29 +0100 Subject: [PATCH] [FIX] portal: do not crash in dev mode Before this commit, the props validation would fail in dev mode because it did not expect a slot prop. --- src/portal.ts | 1 + tests/misc/__snapshots__/portal.test.ts.snap | 21 +++++++++++++ tests/misc/portal.test.ts | 33 ++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/src/portal.ts b/src/portal.ts index fd5c7c8a..68f654e9 100644 --- a/src/portal.ts +++ b/src/portal.ts @@ -57,6 +57,7 @@ export class Portal extends Component { target: { type: String, }, + slots: true, }; constructor(props: any, env: any, node: ComponentNode) { diff --git a/tests/misc/__snapshots__/portal.test.ts.snap b/tests/misc/__snapshots__/portal.test.ts.snap index 2620141a..0c1a5f53 100644 --- a/tests/misc/__snapshots__/portal.test.ts.snap +++ b/tests/misc/__snapshots__/portal.test.ts.snap @@ -67,6 +67,27 @@ exports[`Portal basic use of portal 1`] = ` }" `; +exports[`Portal basic use of portal in dev mode 1`] = ` +"function anonymous(bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; + + let block1 = createBlock(\`
1
\`); + let block2 = createBlock(\`

2

\`); + + function slot1(ctx, node, key = \\"\\") { + return block2(); + } + + return function template(ctx, node, key = \\"\\") { + const props2 = {target: '#outside',slots: {'default': {__render: slot1, __ctx: ctx}}} + helpers.validateProps(\`Portal\`, props2, ctx) + let b3 = component(\`Portal\`, props2, key + \`__3\`, node, ctx); + return block1([], [b3]); + } +}" +`; + exports[`Portal conditional use of Portal (with sub Component) 1`] = ` "function anonymous(bdom, helpers ) { diff --git a/tests/misc/portal.test.ts b/tests/misc/portal.test.ts index e7ba2140..2168681f 100644 --- a/tests/misc/portal.test.ts +++ b/tests/misc/portal.test.ts @@ -10,10 +10,12 @@ import { } from "../../src"; import { Portal, xml } from "../../src/"; import { elem, makeTestFixture, nextTick, snapshotEverything } from "../helpers"; +import { DEV_MSG } from "../../src/app/app"; let fixture: HTMLElement; let originalconsoleWarn = console.warn; let mockConsoleWarn: any; +const info = console.info; function addOutsideDiv(fixture: HTMLElement): HTMLElement { let outside = document.createElement("div"); @@ -24,6 +26,19 @@ function addOutsideDiv(fixture: HTMLElement): HTMLElement { snapshotEverything(); +beforeAll(() => { + console.info = (message: any) => { + if (message === DEV_MSG) { + return; + } + info(message); + }; +}); + +afterAll(() => { + console.info = info; +}); + beforeEach(() => { fixture = makeTestFixture(); mockConsoleWarn = jest.fn(() => {}); @@ -53,6 +68,24 @@ describe("Portal", () => { expect(fixture.innerHTML).toBe('

2

1
'); }); + test("basic use of portal in dev mode", async () => { + class Parent extends Component { + static components = { Portal }; + static template = xml` +
+ 1 + +

2

+
+
`; + } + + addOutsideDiv(fixture); + await mount(Parent, fixture, { dev: true }); + + expect(fixture.innerHTML).toBe('

2

1
'); + }); + test("conditional use of Portal", async () => { class Parent extends Component { static components = { Portal };