2020-02-03 10:24:25 +01:00
|
|
|
import { buildCodeForScreens } from "../src/builderStore/buildCodeForScreens"
|
2020-02-01 00:11:50 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
describe("buildCodeForScreen", () => {
|
2020-02-01 00:11:50 +01:00
|
|
|
it("should package _code into runnable function, for simple screen props", () => {
|
|
|
|
const screen = {
|
|
|
|
props: {
|
|
|
|
_id: "1234",
|
2020-02-03 10:24:25 +01:00
|
|
|
_code: "render('render argument');",
|
|
|
|
},
|
2020-02-01 00:11:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
let renderArg
|
|
|
|
const render = arg => {
|
|
|
|
renderArg = arg
|
2020-02-01 00:11:50 +01:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
const uiFunctions = getFunctions(screen)
|
2020-02-01 00:11:50 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const targetfunction = uiFunctions[screen.props._id]
|
|
|
|
expect(targetfunction).toBeDefined()
|
2020-02-01 00:11:50 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
targetfunction(render)
|
2020-02-01 00:11:50 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(renderArg).toBe("render argument")
|
|
|
|
})
|
2020-02-01 00:11:50 +01:00
|
|
|
|
|
|
|
it("should package _code into runnable function, for _children ", () => {
|
|
|
|
const screen = {
|
|
|
|
props: {
|
|
|
|
_id: "parent",
|
|
|
|
_code: "render('parent argument');",
|
|
|
|
_children: [
|
2020-02-03 10:24:25 +01:00
|
|
|
{
|
|
|
|
_id: "child1",
|
|
|
|
_code: "render('child 1 argument');",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
_id: "child2",
|
|
|
|
_code: "render('child 2 argument');",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-02-01 00:11:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
let renderArg
|
|
|
|
const render = arg => {
|
|
|
|
renderArg = arg
|
2020-02-01 00:11:50 +01:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
const uiFunctions = getFunctions(screen)
|
2020-02-01 00:11:50 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const targetfunction = uiFunctions["child2"]
|
|
|
|
expect(targetfunction).toBeDefined()
|
2020-02-01 00:11:50 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
targetfunction(render)
|
2020-02-01 00:11:50 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(renderArg).toBe("child 2 argument")
|
2020-02-01 00:11:50 +01:00
|
|
|
})
|
2020-02-03 10:24:25 +01:00
|
|
|
})
|
2020-02-01 00:11:50 +01:00
|
|
|
|
2020-02-25 17:27:52 +01:00
|
|
|
const getFunctions = screen => {
|
|
|
|
const code = buildCodeForScreens([screen])
|
|
|
|
const func = new Function(`return ${code}`)()
|
|
|
|
return func
|
|
|
|
}
|