Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support URL object for fetch and fix getBaseUrl #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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',
};
3 changes: 2 additions & 1 deletion packages/mock-addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@
"boxen": "^5.0.1",
"concurrently": "^6.2.0",
"dedent": "^0.7.0",
"jest-environment-jsdom": "^29.5.0",
"rimraf": "^3.0.2",
"typescript": "^4.9.4",
"zx": "^1.14.1"
},
"peerDependencies": {
"@storybook/addons": "^7.0.0",
"@storybook/blocks": "^7.0.0",
"@storybook/manager-api": "^7.0.0",
"@storybook/components": "^7.0.2",
"@storybook/manager-api": "^7.0.0",
"@storybook/theming": "^7.0.0",
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0",
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