Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/mock-addon/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: 'jsdom',
};
1 change: 1 addition & 0 deletions packages/mock-addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"boxen": "^5.0.1",
"concurrently": "^6.2.0",
"dedent": "^0.7.0",
"jest-environment-jsdom": "^29.7.0",
"rimraf": "^3.0.2",
"typescript": "^4.9.4",
"zx": "^1.14.1"
Expand Down
6 changes: 5 additions & 1 deletion packages/mock-addon/src/utils/request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { getBaseUrl } from './url';

export function Request(input, options = {}) {
if (typeof input === 'object') {
if (input instanceof URL) {
this.method = options.method || 'GET';
this.url = input.href;
this.body = options.body || null;
} else if (typeof input === 'object') {
this.method = options.method || input.method || 'GET';
this.url = input.url;
this.body = options.body || input.body || null;
Expand Down
6 changes: 1 addition & 5 deletions packages/mock-addon/src/utils/url.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export const getBaseUrl = (rawUrl = '') => {
const baseUrl =
rawUrl.indexOf('http') == 0 ? undefined : 'http://localhost';
const url = new URL(rawUrl, baseUrl);

return url;
return new URL(rawUrl, location.href);
};

export const getNormalizedUrl = (rawUrl = '') => {
Expand Down
14 changes: 14 additions & 0 deletions packages/mock-addon/src/utils/url.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { getNormalizedUrl } from './url';

describe('Url', () => {
let nativeLocation;
beforeAll(() => {
nativeLocation = window.location;
Object.defineProperty(window, 'location', {
value: {
href: 'http://localhost/?path=/story/examples-fetch--get',
},
});
});
afterAll(() => {
Object.defineProperty(window, 'location', {
value: nativeLocation,
});
});
describe('getNormalizedUrl', () => {
it('should remove protocol like http', () => {
const input = 'http://google.com/test';
Expand Down
Loading