-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff5cf0b
commit b4d3757
Showing
4 changed files
with
216 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/brick-kit/src/core/CustomTemplates/replaceSlotWithSlottedBricks.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import type { BrickConf } from "@next-core/brick-types"; | ||
import { identity } from "lodash"; | ||
import { replaceSlotWithSlottedBricks } from "./replaceSlotWithSlottedBricks"; | ||
import type { ProxyContext } from "./expandCustomTemplate"; | ||
|
||
describe("replaceSlotWithSlottedBricks", () => { | ||
let mockHostContext: ProxyContext; | ||
|
||
beforeEach(() => { | ||
mockHostContext = { | ||
proxyBrick: { | ||
type: "tpl-test", | ||
}, | ||
usedSlots: new Set(), | ||
} as unknown as ProxyContext; | ||
}); | ||
|
||
it("should throw an error if 'if' is used in a slot", () => { | ||
const brickConf = { brick: "slot", if: false }; | ||
expect(() => | ||
replaceSlotWithSlottedBricks(brickConf, mockHostContext, identity) | ||
).toThrow( | ||
`Can not use "if" in a slot currently, check your template "tpl-test"` | ||
); | ||
const brickConf2 = { brick: "slot", if: "<% true %>" }; | ||
expect(() => | ||
replaceSlotWithSlottedBricks(brickConf2, mockHostContext, identity) | ||
).toThrow( | ||
`Can not use "if" in a slot currently, check your template "tpl-test"` | ||
); | ||
}); | ||
|
||
it("should throw an error if slot name is an expression", () => { | ||
const brickConf = { | ||
brick: "slot", | ||
properties: { name: "<% 'abc' %>" }, | ||
} as BrickConf; | ||
expect(() => | ||
replaceSlotWithSlottedBricks(brickConf, mockHostContext, identity) | ||
).toThrow( | ||
`Can not use an expression as slot name "<% 'abc' %>" currently, check your template "tpl-test"` | ||
); | ||
}); | ||
|
||
it("should throw an error if slot name is repeated", () => { | ||
const brickConf = { | ||
brick: "div", | ||
properties: { name: "repeated-slot" }, | ||
} as BrickConf; | ||
mockHostContext.usedSlots.add("repeated-slot"); | ||
expect(() => | ||
replaceSlotWithSlottedBricks(brickConf, mockHostContext, identity) | ||
).toThrow( | ||
`Can not have multiple slots with the same name "repeated-slot", check your template "tpl-test"` | ||
); | ||
}); | ||
|
||
it("should return external bricks if available", () => { | ||
const brickConf = { | ||
brick: "slot", | ||
properties: { name: "slot1" }, | ||
} as BrickConf; | ||
mockHostContext.externalSlots = { | ||
slot1: { type: "bricks", bricks: [{ brick: "h1" }, { brick: "h2" }] }, | ||
}; | ||
const result = replaceSlotWithSlottedBricks( | ||
brickConf, | ||
mockHostContext, | ||
identity | ||
); | ||
expect(result).toEqual([{ brick: "h1" }, { brick: "h2" }]); | ||
}); | ||
|
||
it("should return expanded default slots if no external bricks", () => { | ||
const brickConf = { | ||
brick: "slot", | ||
properties: { name: "slot1" }, | ||
slots: { | ||
"": { | ||
type: "bricks", | ||
bricks: [ | ||
{ | ||
brick: "p", | ||
}, | ||
], | ||
}, | ||
oops: { | ||
type: "bricks", | ||
bricks: [ | ||
{ | ||
brick: "hr", | ||
}, | ||
], | ||
}, | ||
}, | ||
} as BrickConf; | ||
const expandMock = jest.fn().mockImplementation((item) => [item]); | ||
const result = replaceSlotWithSlottedBricks( | ||
brickConf, | ||
mockHostContext, | ||
expandMock | ||
); | ||
expect(result).toEqual([{ brick: "p" }]); | ||
expect(expandMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("no default bricks nor external bricks", () => { | ||
const brickConf = { | ||
brick: "slot", | ||
properties: { name: "slot1" }, | ||
} as BrickConf; | ||
const result = replaceSlotWithSlottedBricks( | ||
brickConf, | ||
mockHostContext, | ||
identity | ||
); | ||
expect(result).toEqual([]); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
packages/brick-kit/src/core/CustomTemplates/replaceSlotWithSlottedBricks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { | ||
BrickConf, | ||
BrickConfInTemplate, | ||
UseSingleBrickConf, | ||
} from "@next-core/brick-types"; | ||
import { hasOwnProperty } from "@next-core/brick-utils"; | ||
import { isEvaluable } from "@next-core/cook"; | ||
import type { ProxyContext } from "./expandCustomTemplate"; | ||
|
||
export function replaceSlotWithSlottedBricks< | ||
T extends BrickConf | UseSingleBrickConf | ||
>( | ||
brickConf: T, | ||
proxyContext: ProxyContext, | ||
expand: (item: T, proxyContext: ProxyContext) => T | T[] | ||
): T[] { | ||
// Currently, no support for `if` in a slot. | ||
if ( | ||
(brickConf.if != null && !brickConf.if) || | ||
typeof brickConf.if === "string" | ||
) { | ||
throw new Error( | ||
`Can not use "if" in a slot currently, check your template "${proxyContext.proxyBrick.type}"` | ||
); | ||
} | ||
|
||
const slot = String(brickConf.properties?.name ?? ""); | ||
|
||
// Currently, no support for expression as slot name. | ||
if (isEvaluable(slot)) { | ||
throw new Error( | ||
`Can not use an expression as slot name "${slot}" currently, check your template "${proxyContext.proxyBrick.type}"` | ||
); | ||
} | ||
|
||
// Do not repeat the same slot name in a template. | ||
if (proxyContext.usedSlots.has(slot)) { | ||
throw new Error( | ||
`Can not have multiple slots with the same name "${slot}", check your template "${proxyContext.proxyBrick.type}"` | ||
); | ||
} | ||
proxyContext.usedSlots.add(slot); | ||
|
||
if ( | ||
proxyContext.externalSlots && | ||
hasOwnProperty(proxyContext.externalSlots, slot) | ||
) { | ||
const insertBricks = proxyContext.externalSlots[slot].bricks ?? []; | ||
if (insertBricks.length > 0) { | ||
return insertBricks as T[]; | ||
} | ||
} | ||
|
||
return ((brickConf as BrickConfInTemplate).slots?.[""]?.bricks ?? []).flatMap( | ||
(item) => expand(item as T, proxyContext) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters