mirror of https://github.com/Budibase/budibase.git
6 changed files with 299 additions and 4 deletions
@ -0,0 +1,121 @@ |
|||||
|
import { load } from "./testAppDef"; |
||||
|
|
||||
|
describe("initialiseApp", () => { |
||||
|
|
||||
|
it("should populate root element prop from store value", async () => { |
||||
|
|
||||
|
const {dom} = await load({ |
||||
|
_component: "testlib/div", |
||||
|
className: { |
||||
|
"##bbstate": "divClassName", |
||||
|
"##bbsource": "store", |
||||
|
"##bbstatefallback":"default" |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
const rootDiv = dom.window.document.body.children[0]; |
||||
|
expect(rootDiv.className).toBe("default"); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
it("should update root element from store", async () => { |
||||
|
|
||||
|
const {dom, app} = await load({ |
||||
|
_component: "testlib/div", |
||||
|
className: { |
||||
|
"##bbstate": "divClassName", |
||||
|
"##bbsource": "store", |
||||
|
"##bbstatefallback":"default" |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
app.store.update(s => { |
||||
|
s.divClassName = "newvalue"; |
||||
|
return s; |
||||
|
}); |
||||
|
|
||||
|
const rootDiv = dom.window.document.body.children[0]; |
||||
|
expect(rootDiv.className).toBe("newvalue"); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
|
||||
|
it("should populate child component with store value", async () => { |
||||
|
const {dom} = await load({ |
||||
|
_component: "testlib/div", |
||||
|
_children: [ |
||||
|
{ |
||||
|
_component: "testlib/h1", |
||||
|
text: { |
||||
|
"##bbstate": "headerOneText", |
||||
|
"##bbsource": "store", |
||||
|
"##bbstatefallback":"header one" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
_component: "testlib/h1", |
||||
|
text: { |
||||
|
"##bbstate": "headerTwoText", |
||||
|
"##bbsource": "store", |
||||
|
"##bbstatefallback":"header two" |
||||
|
} |
||||
|
} |
||||
|
] |
||||
|
}); |
||||
|
|
||||
|
const rootDiv = dom.window.document.body.children[0]; |
||||
|
|
||||
|
expect(rootDiv.children.length).toBe(2); |
||||
|
expect(rootDiv.children[0].tagName).toBe("H1"); |
||||
|
expect(rootDiv.children[0].innerText).toBe("header one"); |
||||
|
expect(rootDiv.children[1].tagName).toBe("H1"); |
||||
|
expect(rootDiv.children[1].innerText).toBe("header two"); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
|
||||
|
it("should populate child component with store value", async () => { |
||||
|
const {dom, app} = await load({ |
||||
|
_component: "testlib/div", |
||||
|
_children: [ |
||||
|
{ |
||||
|
_component: "testlib/h1", |
||||
|
text: { |
||||
|
"##bbstate": "headerOneText", |
||||
|
"##bbsource": "store", |
||||
|
"##bbstatefallback":"header one" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
_component: "testlib/h1", |
||||
|
text: { |
||||
|
"##bbstate": "headerTwoText", |
||||
|
"##bbsource": "store", |
||||
|
"##bbstatefallback":"header two" |
||||
|
} |
||||
|
} |
||||
|
] |
||||
|
}); |
||||
|
|
||||
|
app.store.update(s => { |
||||
|
s.headerOneText = "header 1 - new val"; |
||||
|
s.headerTwoText = "header 2 - new val"; |
||||
|
return s; |
||||
|
}); |
||||
|
|
||||
|
const rootDiv = dom.window.document.body.children[0]; |
||||
|
|
||||
|
expect(rootDiv.children.length).toBe(2); |
||||
|
expect(rootDiv.children[0].tagName).toBe("H1"); |
||||
|
expect(rootDiv.children[0].innerText).toBe("header 1 - new val"); |
||||
|
expect(rootDiv.children[1].tagName).toBe("H1"); |
||||
|
expect(rootDiv.children[1].innerText).toBe("header 2 - new val"); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
|
||||
|
}); |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
@ -0,0 +1,73 @@ |
|||||
|
import { load } from "./testAppDef"; |
||||
|
|
||||
|
describe("initialiseApp", () => { |
||||
|
|
||||
|
it("should populate simple div with initial props", async () => { |
||||
|
|
||||
|
const {dom} = await load({ |
||||
|
_component: "testlib/div", |
||||
|
className: "my-test-class" |
||||
|
}); |
||||
|
|
||||
|
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 populate child component with props", async () => { |
||||
|
const {dom} = await load({ |
||||
|
_component: "testlib/div", |
||||
|
_children: [ |
||||
|
{ |
||||
|
_component: "testlib/h1", |
||||
|
text: "header one" |
||||
|
}, |
||||
|
{ |
||||
|
_component: "testlib/h1", |
||||
|
text: "header two" |
||||
|
} |
||||
|
] |
||||
|
}); |
||||
|
|
||||
|
const rootDiv = dom.window.document.body.children[0]; |
||||
|
|
||||
|
expect(rootDiv.children.length).toBe(2); |
||||
|
expect(rootDiv.children[0].tagName).toBe("H1"); |
||||
|
expect(rootDiv.children[0].innerText).toBe("header one"); |
||||
|
expect(rootDiv.children[1].tagName).toBe("H1"); |
||||
|
expect(rootDiv.children[1].innerText).toBe("header two"); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
it("should append children when told to do so", async () => { |
||||
|
const {dom} = await load({ |
||||
|
_component: "testlib/div", |
||||
|
_children: [ |
||||
|
{ |
||||
|
_component: "testlib/h1", |
||||
|
text: "header one" |
||||
|
}, |
||||
|
{ |
||||
|
_component: "testlib/h1", |
||||
|
text: "header two" |
||||
|
} |
||||
|
], |
||||
|
append: true |
||||
|
}); |
||||
|
|
||||
|
const rootDiv = dom.window.document.body.children[0]; |
||||
|
|
||||
|
expect(rootDiv.children.length).toBe(3); |
||||
|
expect(rootDiv.children[0].tagName).toBe("DIV"); |
||||
|
expect(rootDiv.children[0].className).toBe("default-child"); |
||||
|
expect(rootDiv.children[1].tagName).toBe("H1"); |
||||
|
expect(rootDiv.children[1].innerText).toBe("header one"); |
||||
|
expect(rootDiv.children[2].tagName).toBe("H1"); |
||||
|
expect(rootDiv.children[2].innerText).toBe("header two"); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
|
||||
|
|
||||
@ -0,0 +1,91 @@ |
|||||
|
import { JSDOM } from "jsdom"; |
||||
|
import { loadBudibase } from "../src/index"; |
||||
|
|
||||
|
export const load = async (props) => { |
||||
|
const dom = new JSDOM(`<!DOCTYPE html><html><body></body><html>`); |
||||
|
setAppDef(dom.window, props); |
||||
|
const app = await loadBudibase({ |
||||
|
componentLibraries: allLibs(dom.window), |
||||
|
window: dom.window, |
||||
|
localStorage: createLocalStorage(), |
||||
|
props |
||||
|
}); |
||||
|
return {dom, app}; |
||||
|
} |
||||
|
|
||||
|
const setAppDef = (window, props) => { |
||||
|
window["##BUDIBASE_APPDEFINITION##"] = ({ |
||||
|
componentLibraries: [], |
||||
|
props, |
||||
|
hierarchy: {}, |
||||
|
appRootPath: "" |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
const allLibs = (window) => ({ |
||||
|
testlib: maketestlib(window) |
||||
|
}); |
||||
|
|
||||
|
const createLocalStorage = () => { |
||||
|
const data = {}; |
||||
|
return ({ |
||||
|
getItem: key => data[key], |
||||
|
setItem: (key, value) => data[key] = value |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
const maketestlib = (window) => ({ |
||||
|
div: function(opts) { |
||||
|
|
||||
|
const node = window.document.createElement("DIV"); |
||||
|
const defaultChild = window.document.createElement("DIV"); |
||||
|
defaultChild.className = "default-child"; |
||||
|
node.appendChild(defaultChild); |
||||
|
|
||||
|
let currentProps = {...opts.props}; |
||||
|
let childNodes = []; |
||||
|
|
||||
|
const set = (props) => { |
||||
|
currentProps = Object.assign(currentProps, props); |
||||
|
node.className = currentProps.className || ""; |
||||
|
if(currentProps._children && currentProps._children.length > 0) { |
||||
|
if(currentProps.append) { |
||||
|
for(let c of childNodes) { |
||||
|
node.removeChild(c); |
||||
|
} |
||||
|
const components = currentProps._bb.appendChildren(currentProps._children, node); |
||||
|
childNodes = components.map(c => c._element); |
||||
|
} else { |
||||
|
currentProps._bb.hydrateChildren(currentProps._children, node); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
this.$set = set; |
||||
|
this._element = node; |
||||
|
set(opts.props); |
||||
|
opts.target.appendChild(node); |
||||
|
}, |
||||
|
|
||||
|
h1: function(opts) { |
||||
|
|
||||
|
const node = window.document.createElement("H1"); |
||||
|
|
||||
|
let currentProps = {...opts.props}; |
||||
|
|
||||
|
const set = (props) => { |
||||
|
currentProps = Object.assign(currentProps, props); |
||||
|
if(currentProps.text) { |
||||
|
node.innerText = currentProps.text; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
this.$set = set; |
||||
|
this._element = node; |
||||
|
set(opts.props); |
||||
|
opts.target.appendChild(node); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
|
||||
|
|
||||
Loading…
Reference in new issue