diff --git a/packages/client/package.json b/packages/client/package.json index 01b90a7bc5..acefff3770 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -15,7 +15,7 @@ "client": "web" } }, - "testURL": "http://jest-breaks-if-this-does-not-exist", + "testURL": "http://test.com", "moduleNameMapper": { "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "/internals/mocks/fileMock.js", "\\.(css|less|sass|scss)$": "identity-obj-proxy" diff --git a/packages/client/src/createApp.js b/packages/client/src/createApp.js index a8874960e0..9e1a9ea1b1 100644 --- a/packages/client/src/createApp.js +++ b/packages/client/src/createApp.js @@ -35,8 +35,12 @@ export const createApp = ({ routeTo = screenRouter({ screens: frontendDefinition.screens, onScreenSelected, + window, }) - const fallbackPath = window.location.pathname.replace(getAppId(), "") + const fallbackPath = window.location.pathname.replace( + getAppId(window.document.cookie), + "" + ) routeTo(currentUrl || fallbackPath) } diff --git a/packages/client/src/index.js b/packages/client/src/index.js index a54ad79420..87813a812d 100644 --- a/packages/client/src/index.js +++ b/packages/client/src/index.js @@ -9,7 +9,7 @@ import { getAppId } from "./render/getAppId" export const loadBudibase = async opts => { const _window = (opts && opts.window) || window // const _localStorage = (opts && opts.localStorage) || localStorage - const appId = getAppId() + const appId = getAppId(_window.document.cookie) const frontendDefinition = _window["##BUDIBASE_FRONTEND_DEFINITION##"] const user = {} @@ -37,7 +37,7 @@ export const loadBudibase = async opts => { componentLibraries: componentLibraryModules, frontendDefinition, user, - window, + window: _window, }) const route = _window.location diff --git a/packages/client/src/render/getAppId.js b/packages/client/src/render/getAppId.js index 09a1f1b54f..1a8d445920 100644 --- a/packages/client/src/render/getAppId.js +++ b/packages/client/src/render/getAppId.js @@ -1,5 +1,5 @@ -export const getAppId = () => - document.cookie +export const getAppId = cookie => + cookie .split(";") .find(c => c.trim().startsWith("budibase:appid")) .split("=")[1] diff --git a/packages/client/src/render/screenRouter.js b/packages/client/src/render/screenRouter.js index 8bff4950ca..ae11c10949 100644 --- a/packages/client/src/render/screenRouter.js +++ b/packages/client/src/render/screenRouter.js @@ -2,13 +2,14 @@ import regexparam from "regexparam" import { routerStore } from "../state/store" import { getAppId } from "./getAppId" -export const screenRouter = ({ screens, onScreenSelected }) => { +export const screenRouter = ({ screens, onScreenSelected, window }) => { const makeRootedPath = url => { if ( - window.location.hostname === "localhost" || - window.location.hostname === "127.0.0.1" + window.location && + (window.location.hostname === "localhost" || + window.location.hostname === "127.0.0.1") ) { - const appId = getAppId() + const appId = getAppId(window.document.cookie) if (url) { if (url.startsWith(appId)) return url return `/${appId}${url.startsWith("/") ? "" : "/"}${url}` diff --git a/packages/client/tests/screenRouting.spec.js b/packages/client/tests/screenRouting.spec.js index bb4380196f..d0b2c1b4d5 100644 --- a/packages/client/tests/screenRouting.spec.js +++ b/packages/client/tests/screenRouting.spec.js @@ -18,7 +18,7 @@ describe("screenRouting", () => { it("should load correct screen, for initial URL, when appRootPath is something", async () => { 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] expect(rootDiv.children.length).toBe(1) diff --git a/packages/client/tests/testAppDef.js b/packages/client/tests/testAppDef.js index e1e41c60c5..f75abb6556 100644 --- a/packages/client/tests/testAppDef.js +++ b/packages/client/tests/testAppDef.js @@ -1,12 +1,29 @@ -import { JSDOM } from "jsdom" +import jsdom, { JSDOM } from "jsdom" import { loadBudibase } from "../src/index" -export const load = async (page, screens, url) => { +export const load = async (page, screens, url, host = "test.com") => { screens = screens || [] 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("", { - url: `http://test${url}`, + url: fullUrl, + cookieJar, }) + /*const cookie = tough.Cookie + cookie.key = "budibase:appid" + cookie.value = "TEST_APPID"*/ + autoAssignIds(page.props) for (let s of screens) { autoAssignIds(s.props) @@ -27,6 +44,7 @@ export const load = async (page, screens, url) => { } const addWindowGlobals = (window, page, screens) => { + window.document.cookie = "budibase:appid=TEST_APP_ID" window["##BUDIBASE_FRONTEND_DEFINITION##"] = { page, screens,