2023-04-02 18:26:24 +08:00
|
|
|
import { describe, test, expect } from 'vitest';
|
2023-04-10 22:53:15 +08:00
|
|
|
import { dynamicURL } from '@/helpers/dynamicURL';
|
2023-04-02 18:26:24 +08:00
|
|
|
|
|
|
|
describe('src/helpers/dynamicURL.ts', () => {
|
2023-04-17 21:31:03 +08:00
|
|
|
test('应该支持空参数', () => {
|
|
|
|
expect(dynamicURL('http://api.com/test/:id')).toBe(
|
|
|
|
'http://api.com/test/undefined',
|
|
|
|
);
|
2023-04-03 21:03:33 +08:00
|
|
|
expect(dynamicURL('http://api.com/test/:id', {})).toBe(
|
|
|
|
'http://api.com/test/undefined',
|
2023-04-02 18:26:24 +08:00
|
|
|
);
|
2023-04-17 21:31:03 +08:00
|
|
|
expect(dynamicURL('http://api.com/test/:id', undefined, {})).toBe(
|
|
|
|
'http://api.com/test/undefined',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('应该支持 params', () => {
|
2023-04-03 21:03:33 +08:00
|
|
|
expect(dynamicURL('http://api.com/test/:id', { id: 1 })).toBe(
|
|
|
|
'http://api.com/test/1',
|
2023-04-02 18:26:24 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-04-17 21:31:03 +08:00
|
|
|
test('应该支持 data', () => {
|
|
|
|
expect(dynamicURL('http://api.com/test/:id', {}, { id: 1 })).toBe(
|
|
|
|
'http://api.com/test/1',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('应该优先从 params 取值', () => {
|
|
|
|
expect(dynamicURL('http://api.com/test/:id', { id: 1 }, { id: 2 })).toBe(
|
|
|
|
'http://api.com/test/1',
|
|
|
|
);
|
2023-04-02 18:26:24 +08:00
|
|
|
expect(
|
2023-04-17 21:31:03 +08:00
|
|
|
dynamicURL('http://api.com/test/:id1/:id2', { id1: 1 }, { id2: 2 }),
|
|
|
|
).toBe('http://api.com/test/1/2');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('应该从 params 删除对应的值', () => {
|
|
|
|
const p = { id1: 1 };
|
|
|
|
const d = { id2: 2 };
|
|
|
|
|
|
|
|
expect(dynamicURL('http://api.com/test/:id1/:id2', p, d)).toBe(
|
|
|
|
'http://api.com/test/1/2',
|
|
|
|
);
|
|
|
|
expect(p.id1).toBeUndefined();
|
|
|
|
expect(d.id2).toBe(2);
|
2023-04-02 18:26:24 +08:00
|
|
|
});
|
2023-04-05 12:21:26 +08:00
|
|
|
|
|
|
|
test('应该忽略端口号', () => {
|
|
|
|
expect(
|
|
|
|
dynamicURL(':8080/:id', {
|
|
|
|
id: 0,
|
|
|
|
}),
|
|
|
|
).toBe(':8080/0');
|
|
|
|
expect(
|
|
|
|
dynamicURL('http://api.com:8080/:id', {
|
|
|
|
id: 0,
|
|
|
|
}),
|
|
|
|
).toBe('http://api.com:8080/0');
|
|
|
|
});
|
2023-08-12 15:48:36 +08:00
|
|
|
|
|
|
|
test('应该支持带参数的链接', () => {
|
|
|
|
expect(
|
|
|
|
dynamicURL('http://api.com/test/:id?param=1', {
|
|
|
|
id: 0,
|
|
|
|
}),
|
|
|
|
).toBe('http://api.com/test/0?param=1');
|
|
|
|
});
|
2023-04-02 18:26:24 +08:00
|
|
|
});
|