Skip to content

Commit

Permalink
Merge pull request #54 from mbland/request-form-action-canonicalization
Browse files Browse the repository at this point in the history
Add resolvedUrl() helper, apply to form action
  • Loading branch information
mbland authored Dec 17, 2023
2 parents 5fb43ea + 2a439a9 commit bb5c82f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion strcalc/src/main/frontend/components/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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))

Expand Down
9 changes: 9 additions & 0 deletions strcalc/src/main/frontend/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit bb5c82f

Please sign in to comment.