From 58d951bde3bd08dd0f01b355d81da94d57ac7914 Mon Sep 17 00:00:00 2001 From: Katie Gengler Date: Tue, 10 Dec 2024 13:59:09 -0500 Subject: [PATCH] Add support for query params to the TrailingHistory location --- app/locations/trailing-history.js | 15 +++++++------ tests/unit/locations/trailing-history-test.js | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 tests/unit/locations/trailing-history-test.js diff --git a/app/locations/trailing-history.js b/app/locations/trailing-history.js index 97c2a8f..0cb8dfb 100644 --- a/app/locations/trailing-history.js +++ b/app/locations/trailing-history.js @@ -4,11 +4,12 @@ import HistoryLocation from '@ember/routing/history-location'; export default HistoryLocation.extend({ formatURL() { let url = this._super(...arguments); - - if (url.includes('#')) { - return url.replace(/([^/])#(.*)/, '$1/#$2'); - } else { - return url.replace(/\/?$/, '/'); - } - } + return formatURL(url); + }, }); + +export function formatURL(url) { + let modifiedURL = new URL(url, 'http://example.com'); + modifiedURL.pathname += '/'; + return `${modifiedURL.pathname}${modifiedURL.search}${modifiedURL.hash}`; +} diff --git a/tests/unit/locations/trailing-history-test.js b/tests/unit/locations/trailing-history-test.js new file mode 100644 index 0000000..c4bb8bf --- /dev/null +++ b/tests/unit/locations/trailing-history-test.js @@ -0,0 +1,22 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import { formatURL } from 'dummy/locations/trailing-history'; + +module('Unit | Locations | trailing history', function (hooks) { + setupTest(hooks); + + test('supports query params', function (assert) { + assert.deepEqual(formatURL('/foo?bar=baz'), '/foo/?bar=baz'); + }); + + test('supports anchors', function (assert) { + assert.deepEqual(formatURL('/foo#placeholder'), '/foo/#placeholder'); + }); + + test('supports query params and anchors', function (assert) { + assert.deepEqual( + formatURL('/foo?bar=baz#placeholder'), + '/foo/?bar=baz#placeholder', + ); + }); +});