2023-04-03 21:03:33 +08:00
|
|
|
import { describe, test, expect, vi } from 'vitest';
|
2023-04-09 15:20:10 +08:00
|
|
|
import { ignore } from '@/helpers/ignore';
|
2023-04-21 18:09:32 +08:00
|
|
|
import { CancelToken } from '@/request/cancel';
|
2023-04-09 15:20:10 +08:00
|
|
|
import { mergeConfig } from '@/core/mergeConfig';
|
2023-04-03 21:03:33 +08:00
|
|
|
|
|
|
|
describe('src/core/mergeConfig.ts', () => {
|
|
|
|
test('应该支持空参数', () => {
|
|
|
|
expect(mergeConfig()).toEqual({});
|
2023-04-10 22:53:15 +08:00
|
|
|
expect(mergeConfig({})).toEqual({});
|
|
|
|
expect(mergeConfig(undefined, {})).toEqual({});
|
2023-04-03 21:03:33 +08:00
|
|
|
expect(mergeConfig({ baseURL: '/api' })).toEqual({ baseURL: '/api' });
|
2023-04-10 22:53:15 +08:00
|
|
|
expect(mergeConfig(undefined, { baseURL: '/api' })).toEqual({
|
|
|
|
baseURL: '/api',
|
|
|
|
});
|
2023-04-03 21:03:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('应该只取 config2', () => {
|
|
|
|
const c1 = {
|
|
|
|
url: 'a',
|
2023-04-16 13:39:10 +08:00
|
|
|
data: {
|
|
|
|
v1: '1',
|
|
|
|
},
|
2023-04-03 21:03:33 +08:00
|
|
|
upload: true,
|
|
|
|
download: true,
|
|
|
|
};
|
|
|
|
const c2 = {
|
|
|
|
url: 'b',
|
2023-04-16 13:39:10 +08:00
|
|
|
data: {
|
|
|
|
v1: '2',
|
|
|
|
},
|
2023-04-03 21:03:33 +08:00
|
|
|
upload: false,
|
|
|
|
download: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(mergeConfig(c1, {})).toEqual({});
|
|
|
|
expect(mergeConfig({}, c2)).toEqual(c2);
|
|
|
|
expect(mergeConfig(c1, c2)).toEqual(c2);
|
|
|
|
|
|
|
|
Object.keys(c2).forEach((_) => {
|
|
|
|
const key = _ as keyof typeof c2;
|
|
|
|
expect(mergeConfig(ignore(c1, key), c2)).toEqual(c2);
|
|
|
|
expect(mergeConfig(c1, ignore(c2, key))).toEqual(ignore(c2, key));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('应该深度合并', () => {
|
2023-04-08 22:37:50 +08:00
|
|
|
const o1 = {
|
2023-04-10 22:53:15 +08:00
|
|
|
v1: {},
|
2023-04-08 22:37:50 +08:00
|
|
|
v2: 1,
|
|
|
|
v3: {
|
|
|
|
v1: 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const o2 = {
|
|
|
|
v2: 2,
|
|
|
|
v3: {
|
|
|
|
v2: 2,
|
|
|
|
},
|
2023-04-10 22:53:15 +08:00
|
|
|
v4: {},
|
2023-04-08 22:37:50 +08:00
|
|
|
};
|
|
|
|
const o3 = {
|
2023-04-10 22:53:15 +08:00
|
|
|
v1: {},
|
2023-04-08 22:37:50 +08:00
|
|
|
v2: 2,
|
|
|
|
v3: {
|
|
|
|
v1: 1,
|
|
|
|
v2: 2,
|
|
|
|
},
|
2023-04-10 22:53:15 +08:00
|
|
|
v4: {},
|
2023-04-08 22:37:50 +08:00
|
|
|
};
|
2023-04-03 21:03:33 +08:00
|
|
|
const c1 = {
|
|
|
|
headers: {
|
|
|
|
common: {
|
|
|
|
v1: 1,
|
|
|
|
},
|
|
|
|
v1: 1,
|
|
|
|
},
|
2023-04-08 22:37:50 +08:00
|
|
|
params: o1,
|
2023-04-03 21:03:33 +08:00
|
|
|
};
|
|
|
|
const c2 = {
|
|
|
|
headers: {
|
|
|
|
common: {
|
|
|
|
v2: 2,
|
|
|
|
},
|
|
|
|
v2: 2,
|
|
|
|
},
|
2023-04-08 22:37:50 +08:00
|
|
|
params: o2,
|
2023-04-03 21:03:33 +08:00
|
|
|
};
|
|
|
|
const mc = {
|
|
|
|
headers: {
|
|
|
|
common: {
|
|
|
|
v1: 1,
|
|
|
|
v2: 2,
|
|
|
|
},
|
|
|
|
v1: 1,
|
|
|
|
v2: 2,
|
|
|
|
},
|
2023-04-08 22:37:50 +08:00
|
|
|
params: o3,
|
2023-04-03 21:03:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
expect(mergeConfig(c1, {})).toEqual(c1);
|
|
|
|
expect(mergeConfig({}, c2)).toEqual(c2);
|
|
|
|
expect(mergeConfig(c1, c2)).toEqual(mc);
|
|
|
|
|
|
|
|
Object.keys(c2).forEach((_) => {
|
|
|
|
const key = _ as keyof typeof c2;
|
|
|
|
expect(mergeConfig(ignore(c1, key), c2)).toEqual({
|
|
|
|
...mc,
|
|
|
|
[key]: c2[key],
|
|
|
|
});
|
|
|
|
expect(mergeConfig(c1, ignore(c2, key))).toEqual({
|
|
|
|
...mc,
|
|
|
|
[key]: c1[key],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-04-10 22:53:15 +08:00
|
|
|
test('深度合并应该丢弃非普通对象值', () => {
|
|
|
|
const c1 = {
|
|
|
|
headers: 1,
|
|
|
|
params: '1',
|
|
|
|
};
|
|
|
|
const c2 = {
|
|
|
|
headers: () => null,
|
|
|
|
params: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(mergeConfig(c1 as any, c2 as any)).toEqual({});
|
|
|
|
});
|
|
|
|
|
2024-01-10 19:33:33 +08:00
|
|
|
test('深度合并应该返回新的对象', () => {
|
|
|
|
const c1 = {
|
|
|
|
headers: { t: 1 },
|
|
|
|
params: {},
|
|
|
|
};
|
|
|
|
const c2 = {
|
|
|
|
headers: {},
|
|
|
|
params: { t: 1 },
|
|
|
|
};
|
|
|
|
const c3 = mergeConfig(c1 as any, c2 as any);
|
|
|
|
|
|
|
|
expect(c3.headers === c1.headers).toBeFalsy();
|
|
|
|
expect(c3.params === c1.params).toBeFalsy();
|
|
|
|
expect(c3.headers === c2.headers).toBeFalsy();
|
|
|
|
expect(c3.params === c2.params).toBeFalsy();
|
|
|
|
expect(c3.headers).toEqual(c1.headers);
|
|
|
|
expect(c3.params).toEqual(c2.params);
|
|
|
|
});
|
|
|
|
|
2023-04-03 21:03:33 +08:00
|
|
|
test('应该优先取 config2', () => {
|
|
|
|
const c1 = {
|
|
|
|
adapter: vi.fn(),
|
|
|
|
baseURL: 'https://c1.com',
|
2023-04-27 09:45:48 +08:00
|
|
|
method: 'post' as const,
|
2023-04-03 21:03:33 +08:00
|
|
|
paramsSerializer: vi.fn(),
|
|
|
|
transformRequest: vi.fn(),
|
|
|
|
transformResponse: vi.fn(),
|
|
|
|
errorHandler: vi.fn(),
|
|
|
|
cancelToken: CancelToken.source().token,
|
|
|
|
dataType: 'json',
|
2023-08-15 15:43:24 +08:00
|
|
|
responseType: 'text',
|
2023-04-03 21:03:33 +08:00
|
|
|
timeout: 1000,
|
|
|
|
validateStatus: vi.fn(),
|
|
|
|
onUploadProgress: vi.fn(),
|
|
|
|
onDownloadProgress: vi.fn(),
|
|
|
|
};
|
|
|
|
const c2 = {
|
|
|
|
adapter: vi.fn(),
|
|
|
|
baseURL: 'https://c2.com',
|
2023-04-27 09:45:48 +08:00
|
|
|
method: 'put' as const,
|
2023-04-03 21:03:33 +08:00
|
|
|
paramsSerializer: vi.fn(),
|
|
|
|
transformRequest: vi.fn(),
|
|
|
|
transformResponse: vi.fn(),
|
|
|
|
errorHandler: vi.fn(),
|
|
|
|
cancelToken: CancelToken.source().token,
|
|
|
|
dataType: 'json',
|
2023-08-15 15:43:24 +08:00
|
|
|
responseType: 'text',
|
2023-04-03 21:03:33 +08:00
|
|
|
timeout: 1000,
|
|
|
|
validateStatus: vi.fn(),
|
|
|
|
onUploadProgress: vi.fn(),
|
|
|
|
onDownloadProgress: vi.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(mergeConfig(c1, {})).toEqual(c1);
|
|
|
|
expect(mergeConfig({}, c2)).toEqual(c2);
|
|
|
|
expect(mergeConfig(c1, c2)).toEqual(c2);
|
|
|
|
|
|
|
|
Object.keys(c2).forEach((_) => {
|
|
|
|
const key = _ as keyof typeof c2;
|
|
|
|
expect(mergeConfig(ignore(c1, key), c2)).toEqual(c2);
|
|
|
|
expect(mergeConfig(c1, ignore(c2, key))).toEqual({
|
|
|
|
...c2,
|
|
|
|
[key]: c1[key],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-04-15 16:21:54 +08:00
|
|
|
test('应该支持请求配置', () => {
|
2023-04-03 21:03:33 +08:00
|
|
|
const c1 = {
|
|
|
|
custom1: 1,
|
|
|
|
custom2: 'c1',
|
|
|
|
custom3: vi.fn(),
|
|
|
|
custom4: { c1: 1 },
|
|
|
|
custom5: ['c1'],
|
|
|
|
custom6: new Date(),
|
|
|
|
custom7: () => 1,
|
|
|
|
};
|
|
|
|
const c2 = {
|
|
|
|
custom1: 2,
|
|
|
|
custom2: 'c2',
|
|
|
|
custom3: vi.fn(),
|
|
|
|
custom4: { c2: 2 },
|
|
|
|
custom5: ['c2'],
|
|
|
|
custom6: new Date(),
|
|
|
|
custom7: () => 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(mergeConfig(c1, {})).toEqual(c1);
|
|
|
|
expect(mergeConfig({}, c2)).toEqual(c2);
|
|
|
|
expect(mergeConfig(c1, c2)).toEqual(c2);
|
|
|
|
|
|
|
|
Object.keys(c2).forEach((_) => {
|
|
|
|
const key = _ as keyof typeof c2;
|
|
|
|
expect(mergeConfig(ignore(c1, key), c2)).toEqual(c2);
|
|
|
|
expect(mergeConfig(c1, ignore(c2, key))).toEqual({
|
|
|
|
...c2,
|
|
|
|
[key]: c1[key],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|