1
0
Fork 0
mirror of synced 2024-10-03 19:43:32 +13:00

client: fixing broken tests

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

View file

@ -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)$": "<rootDir>/internals/mocks/fileMock.js",
"\\.(css|less|sass|scss)$": "identity-obj-proxy"

View file

@ -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)
}

View file

@ -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

View file

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

View file

@ -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}`

View file

@ -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)

View file

@ -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("<!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)
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,