Skip to content

Commit

Permalink
Merge branch 'main' into chore/use-swc
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 14, 2022
2 parents fa198d3 + 48ccae4 commit 075053d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "msw",
"version": "0.48.1",
"version": "0.48.2",
"description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
31 changes: 24 additions & 7 deletions src/utils/url/getAbsoluteWorkerUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@
*/
import { getAbsoluteWorkerUrl } from './getAbsoluteWorkerUrl'

describe('getAbsoluteWorkerUrl', () => {
describe('given a relative worker URL', () => {
it('should return an absolute URL against the current location', () => {
expect(getAbsoluteWorkerUrl('./mockServiceWorker.js')).toEqual(
'http://localhost/mockServiceWorker.js',
)
})
const rawLocation = window.location

afterAll(() => {
Object.defineProperty(window, 'location', {
value: rawLocation,
})
})

it('returns absolute worker url relatively to the root', () => {
expect(getAbsoluteWorkerUrl('./worker.js')).toBe('http://localhost/worker.js')
})

it('returns an absolute worker url relatively to the current path', () => {
Object.defineProperty(window, 'location', {
value: {
href: 'http://localhost/path/to/page',
},
})

expect(getAbsoluteWorkerUrl('./worker.js')).toBe(
'http://localhost/path/to/worker.js',
)

// Leading slash must still resolve to the root.
expect(getAbsoluteWorkerUrl('/worker.js')).toBe('http://localhost/worker.js')
})
4 changes: 2 additions & 2 deletions src/utils/url/getAbsoluteWorkerUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
* Returns an absolute Service Worker URL based on the given
* relative URL (known during the registration).
*/
export function getAbsoluteWorkerUrl(relativeUrl: string): string {
return new URL(relativeUrl, location.origin).href
export function getAbsoluteWorkerUrl(workerUrl: string): string {
return new URL(workerUrl, location.href).href
}

0 comments on commit 075053d

Please sign in to comment.