Skip to content

Commit

Permalink
fix: add default GlobalFetch type to mock adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
JOU-amjs committed Dec 16, 2023
1 parent a3b3f81 commit 9bc6b51
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 74 deletions.
7 changes: 4 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"editor.suggestSelection": "recentlyUsedByPrefix", // 自动补全
"editor.codeActionsOnSave": {
"source.organizeImports": true,
"source.fixAll.eslint": true // 修复所有代码
}
"source.organizeImports": "explicit",
"source.fixAll.eslint": "explicit"
},
"jest.autoRun": "off"
}
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@types/jest": "^29.4.0",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"alova": "^2.12.0",
"alova": "^2.16.1",
"babel-jest": "^29.5.0",
"commitizen": "^4.3.0",
"commitlint": "^17.5.1",
Expand Down
2 changes: 1 addition & 1 deletion src/MockRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default function MockRequest<RC, RE, RH>(
? mockDataRaw({
query,
params,
data,
data: isString(data) || !data ? {} : data,
headers: requestHeaders
})
: mockDataRaw;
Expand Down
2 changes: 1 addition & 1 deletion src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MockResponse } from '../typings';
/**
* 默认的响应数据拦截器,并返回Response数据
*/
export const defaultMockResponse: MockResponse = ({ status = 200, statusText = 'ok', body }) => ({
export const defaultMockResponse: MockResponse<any, any, any> = ({ status = 200, statusText = 'ok', body }) => ({
response: new Response(JSON.stringify(body), {
status,
statusText
Expand Down
114 changes: 57 additions & 57 deletions test/parseUrl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
import { parseUrl } from '../src/helper';

describe('parseUrl', () => {
test('parse full url', () => {
let parsed = parseUrl('http://xx.com/aa/bb');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {},
hash: ''
});

parsed = parseUrl('//xx.com/aa/bb#555');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {},
hash: '#555'
});

parsed = parseUrl('//xx.com/aa/bb?a=1&b=2&c=#555');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {
a: '1',
b: '2',
c: ''
},
hash: '#555'
});
});

test('parse relative url', () => {
let parsed = parseUrl('/aa/bb');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {},
hash: ''
});

parsed = parseUrl('/aa/bb#555');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {},
hash: '#555'
});

parsed = parseUrl('/aa/bb?a=1&b=2&c=#555');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {
a: '1',
b: '2',
c: ''
},
hash: '#555'
});
});
});
import { parseUrl } from '../src/helper';

describe('parseUrl', () => {
test('parse full url', () => {
let parsed = parseUrl('http://xx.com/aa/bb');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {},
hash: ''
});

parsed = parseUrl('//xx.com/aa/bb#555');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {},
hash: '#555'
});

parsed = parseUrl('//xx.com/aa/bb?a=1&b=2&c=#555');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {
a: '1',
b: '2',
c: ''
},
hash: '#555'
});
});

test('parse relative url', () => {
let parsed = parseUrl('/aa/bb');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {},
hash: ''
});

parsed = parseUrl('/aa/bb#555');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {},
hash: '#555'
});

parsed = parseUrl('/aa/bb?a=1&b=2&c=#555');
expect(parsed).toStrictEqual({
pathname: '/aa/bb',
query: {
a: '1',
b: '2',
c: ''
},
hash: '#555'
});
});
});
9 changes: 5 additions & 4 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AlovaRequestAdapter, Method } from 'alova';
import { FetchRequestInit } from 'alova/GlobalFetch';

interface MockServerRequest {
headers: Record<string, any>;
Expand All @@ -23,7 +24,7 @@ interface MockRequestLoggerAdapter {
/**
* 模拟响应函数
*/
interface MockResponse<RC = any, RE = any, RH = any> {
interface MockResponse<RC, RE, RH> {
(
response: {
status: number;
Expand Down Expand Up @@ -87,9 +88,9 @@ interface MockWrapper {
data: Mock;
}

export declare function createAlovaMockAdapter<R = any, T = any, RC = any, RE = any, RH = any>(
export declare function createAlovaMockAdapter<RC = FetchRequestInit, RE = Response, RH = Headers>(
mockWrapper: MockWrapper[],
options?: MockRequestInit<R, T, RC, RE, RH>
): AlovaRequestAdapter<R, T, RC, RE, RH>;
options?: MockRequestInit<any, any, RC, RE, RH>
): AlovaRequestAdapter<any, any, RC, RE, RH>;

export declare function defineMock(mock: Mock, enable?: boolean): MockWrapper;

0 comments on commit 9bc6b51

Please sign in to comment.