forked from tsai/budibase
6 changed files with 147 additions and 20 deletions
@ -0,0 +1,38 @@ |
|||
|
|||
export const renderComponent = ({ |
|||
componentConstructor, uiFunctions, |
|||
htmlElement, anchor, parentContext, |
|||
componentProps}) => { |
|||
|
|||
const func = uiFunctions[componentProps._id]; |
|||
let component; |
|||
let componentContext; |
|||
const render = (context) => { |
|||
|
|||
if(context) { |
|||
componentContext = {...componentContext}; |
|||
componentContext.$parent = parentContext; |
|||
} else { |
|||
componentContext = parentContext; |
|||
} |
|||
|
|||
component = new componentConstructor({ |
|||
target: htmlElement, |
|||
props: componentProps, |
|||
hydrate:false, |
|||
anchor |
|||
}); |
|||
} |
|||
|
|||
if(func) { |
|||
func(render, parentContext); |
|||
} else { |
|||
render(); |
|||
} |
|||
|
|||
return ({ |
|||
context: componentContext, |
|||
component |
|||
}); |
|||
} |
|||
|
|||
@ -0,0 +1,52 @@ |
|||
import { load } from "./testAppDef"; |
|||
|
|||
describe("controlFlow", () => { |
|||
|
|||
it("should display simple div, with always true render function", async () => { |
|||
|
|||
const {dom} = await load({ |
|||
_component: "testlib/div", |
|||
className: "my-test-class", |
|||
_id: "always_render" |
|||
}); |
|||
|
|||
expect(dom.window.document.body.children.length).toBe(1); |
|||
const child = dom.window.document.body.children[0]; |
|||
expect(child.className).toBe("my-test-class"); |
|||
|
|||
}) |
|||
|
|||
it("should not display div, with always false render function", async () => { |
|||
|
|||
const {dom} = await load({ |
|||
_component: "testlib/div", |
|||
className: "my-test-class", |
|||
_id: "never_render" |
|||
}); |
|||
|
|||
expect(dom.window.document.body.children.length).toBe(0); |
|||
|
|||
}) |
|||
|
|||
it("should display 3 divs in a looped render function", async () => { |
|||
|
|||
const {dom} = await load({ |
|||
_component: "testlib/div", |
|||
className: "my-test-class", |
|||
_id: "three_clones" |
|||
}); |
|||
|
|||
expect(dom.window.document.body.children.length).toBe(3); |
|||
|
|||
const child0 = dom.window.document.body.children[0]; |
|||
expect(child0.className).toBe("my-test-class"); |
|||
|
|||
const child1 = dom.window.document.body.children[1]; |
|||
expect(child1.className).toBe("my-test-class"); |
|||
|
|||
const child2 = dom.window.document.body.children[2]; |
|||
expect(child2.className).toBe("my-test-class"); |
|||
|
|||
}) |
|||
|
|||
}); |
|||
Loading…
Reference in new issue