Extract key from props in h() for UElement, keep key in props for URoot
This commit is contained in:
@@ -100,6 +100,34 @@ describe("type guards", () => {
|
||||
});
|
||||
|
||||
describe("UElement key field", () => {
|
||||
it("h('div', { key: 'a' }) produces UElement with key: 'a' and no key in props", () => {
|
||||
const el = h("div", { key: "a" });
|
||||
expect(isUElement(el)).toBe(true);
|
||||
if (isUElement(el)) {
|
||||
expect(el.key).toBe("a");
|
||||
expect(el.props.key).toBeUndefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("h('div', { key: 'b', class: 'x' }) — key promoted, class remains in props", () => {
|
||||
const el = h("div", { key: "b", class: "x" });
|
||||
expect(isUElement(el)).toBe(true);
|
||||
if (isUElement(el)) {
|
||||
expect(el.key).toBe("b");
|
||||
expect(el.props.key).toBeUndefined();
|
||||
expect(el.props.class).toBe("x");
|
||||
}
|
||||
});
|
||||
|
||||
it("h('div', null) — key is undefined, no key in props", () => {
|
||||
const el = h("div", null);
|
||||
expect(isUElement(el)).toBe(true);
|
||||
if (isUElement(el)) {
|
||||
expect(el.key).toBeUndefined();
|
||||
expect(el.props).toEqual({});
|
||||
}
|
||||
});
|
||||
|
||||
it("h() extracts key from props and promotes to element level", () => {
|
||||
const el = h("div", { key: "item-1", class: "test" }, "hello");
|
||||
expect(isUElement(el)).toBe(true);
|
||||
@@ -144,11 +172,11 @@ describe("UElement key field", () => {
|
||||
});
|
||||
|
||||
it("h() with root type does not promote key to URoot", () => {
|
||||
const root = h("root", { key: "should-not-appear", id: "test" }, "child");
|
||||
const root = h("root", { key: "x", id: "test" }, "child");
|
||||
expect(isURoot(root)).toBe(true);
|
||||
if (isURoot(root)) {
|
||||
expect("key" in root).toBe(false);
|
||||
expect(root.props.key).toBeUndefined();
|
||||
expect(root.props.key).toBe("x");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user