client: fixing broken tests

This commit is contained in:
Michael Shanks 2020-06-14 20:30:23 +01:00
parent ac00a47d23
commit f3ba6552f0
7 changed files with 37 additions and 14 deletions

View File

@ -15,7 +15,7 @@
"client": "web" "client": "web"
} }
}, },
"testURL": "http://jest-breaks-if-this-does-not-exist", "testURL": "http://test.com",
"moduleNameMapper": { "moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/internals/mocks/fileMock.js", "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/internals/mocks/fileMock.js",
"\\.(css|less|sass|scss)$": "identity-obj-proxy" "\\.(css|less|sass|scss)$": "identity-obj-proxy"

View File

@ -35,8 +35,12 @@ export const createApp = ({
routeTo = screenRouter({ routeTo = screenRouter({
screens: frontendDefinition.screens, screens: frontendDefinition.screens,
onScreenSelected, onScreenSelected,
window,
}) })
const fallbackPath = window.location.pathname.replace(getAppId(), "") const fallbackPath = window.location.pathname.replace(
getAppId(window.document.cookie),
""
)
routeTo(currentUrl || fallbackPath) routeTo(currentUrl || fallbackPath)
} }

View File

@ -9,7 +9,7 @@ import { getAppId } from "./render/getAppId"
export const loadBudibase = async opts => { export const loadBudibase = async opts => {
const _window = (opts && opts.window) || window const _window = (opts && opts.window) || window
// const _localStorage = (opts && opts.localStorage) || localStorage // const _localStorage = (opts && opts.localStorage) || localStorage
const appId = getAppId() const appId = getAppId(_window.document.cookie)
const frontendDefinition = _window["##BUDIBASE_FRONTEND_DEFINITION##"] const frontendDefinition = _window["##BUDIBASE_FRONTEND_DEFINITION##"]
const user = {} const user = {}
@ -37,7 +37,7 @@ export const loadBudibase = async opts => {
componentLibraries: componentLibraryModules, componentLibraries: componentLibraryModules,
frontendDefinition, frontendDefinition,
user, user,
window, window: _window,
}) })
const route = _window.location const route = _window.location

View File

@ -1,5 +1,5 @@
export const getAppId = () => export const getAppId = cookie =>
document.cookie cookie
.split(";") .split(";")
.find(c => c.trim().startsWith("budibase:appid")) .find(c => c.trim().startsWith("budibase:appid"))
.split("=")[1] .split("=")[1]

View File

@ -2,13 +2,14 @@ import regexparam from "regexparam"
import { routerStore } from "../state/store" import { routerStore } from "../state/store"
import { getAppId } from "./getAppId" import { getAppId } from "./getAppId"
export const screenRouter = ({ screens, onScreenSelected }) => { export const screenRouter = ({ screens, onScreenSelected, window }) => {
const makeRootedPath = url => { const makeRootedPath = url => {
if ( if (
window.location.hostname === "localhost" || window.location &&
window.location.hostname === "127.0.0.1" (window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1")
) { ) {
const appId = getAppId() const appId = getAppId(window.document.cookie)
if (url) { if (url) {
if (url.startsWith(appId)) return url if (url.startsWith(appId)) return url
return `/${appId}${url.startsWith("/") ? "" : "/"}${url}` return `/${appId}${url.startsWith("/") ? "" : "/"}${url}`

View File

@ -18,7 +18,7 @@ describe("screenRouting", () => {
it("should load correct screen, for initial URL, when appRootPath is something", async () => { it("should load correct screen, for initial URL, when appRootPath is something", async () => {
const { page, screens } = pageWith3Screens() const { page, screens } = pageWith3Screens()
const { dom } = await load(page, screens, "/testApp/screen2", "/testApp") const { dom } = await load(page, screens, "/TEST_APP_ID/screen2", "localhost")
const rootDiv = dom.window.document.body.children[0] const rootDiv = dom.window.document.body.children[0]
expect(rootDiv.children.length).toBe(1) expect(rootDiv.children.length).toBe(1)

View File

@ -1,12 +1,29 @@
import { JSDOM } from "jsdom" import jsdom, { JSDOM } from "jsdom"
import { loadBudibase } from "../src/index" import { loadBudibase } from "../src/index"
export const load = async (page, screens, url) => { export const load = async (page, screens, url, host = "test.com") => {
screens = screens || [] screens = screens || []
url = url || "/" url = url || "/"
const fullUrl = `http://${host}${url}`
const cookieJar = new jsdom.CookieJar()
cookieJar.setCookie(
`budibase:appid=TEST_APP_ID;domain=${host};path=/`,
fullUrl,
{
looseMode: false,
},
() => {}
)
const dom = new JSDOM("<!DOCTYPE html><html><body></body><html>", { const dom = new JSDOM("<!DOCTYPE html><html><body></body><html>", {
url: `http://test${url}`, url: fullUrl,
cookieJar,
}) })
/*const cookie = tough.Cookie
cookie.key = "budibase:appid"
cookie.value = "TEST_APPID"*/
autoAssignIds(page.props) autoAssignIds(page.props)
for (let s of screens) { for (let s of screens) {
autoAssignIds(s.props) autoAssignIds(s.props)
@ -27,6 +44,7 @@ export const load = async (page, screens, url) => {
} }
const addWindowGlobals = (window, page, screens) => { const addWindowGlobals = (window, page, screens) => {
window.document.cookie = "budibase:appid=TEST_APP_ID"
window["##BUDIBASE_FRONTEND_DEFINITION##"] = { window["##BUDIBASE_FRONTEND_DEFINITION##"] = {
page, page,
screens, screens,