From 2a439a9beaa27826e80831bf97926f174fe7f633 Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Sat, 16 Dec 2023 20:58:08 -0500 Subject: [PATCH] Add resolvedUrl() helper, apply to form action This new resolvedUrl() helper should make resolution of application URLs more adaptable to different host document URLs, beyond prepending document.location.origin. --- strcalc/src/main/frontend/components/request.test.js | 3 ++- strcalc/src/main/frontend/test/helpers.js | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/strcalc/src/main/frontend/components/request.test.js b/strcalc/src/main/frontend/components/request.test.js index c8a06bc..5685d68 100644 --- a/strcalc/src/main/frontend/components/request.test.js +++ b/strcalc/src/main/frontend/components/request.test.js @@ -6,6 +6,7 @@ */ import { post, postForm, postOptions } from './request' import { afterEach, describe, expect, test, vi } from 'vitest' +import { resolvedUrl } from '../test/helpers' // @vitest-environment jsdom describe('Request', () => { @@ -70,7 +71,7 @@ describe('Request', () => { // causes form.action to become `#{document.location.origin}/fetch` in // every environment. const form = document.createElement('form') - const resolvedAction = `${document.location.origin}/fetch` + const resolvedAction = resolvedUrl('./fetch') const res = { foo: 'bar' } const fetchStub = setupFetchStub(JSON.stringify(res)) diff --git a/strcalc/src/main/frontend/test/helpers.js b/strcalc/src/main/frontend/test/helpers.js index 2c08ae3..b889c51 100644 --- a/strcalc/src/main/frontend/test/helpers.js +++ b/strcalc/src/main/frontend/test/helpers.js @@ -10,6 +10,15 @@ * @module test-helpers */ +/** + * Resolves URL path against the current document location + * @param {string} path - path to resolve + * @returns {string} - the result of resolving path against document.location + */ +export function resolvedUrl(path) { + return new URL(path, document.location.href).toString() +} + /** * Enables tests to load page URLs both in the browser and in Node using JSDom. */