utils.test.ts 1.6 KB
Newer Older
何乐's avatar
何乐 committed
1 2
import { isUrl } from './utils';

陈帅's avatar
陈帅 committed
3 4
describe('isUrl tests', (): void => {
  it('should return false for invalid and corner case inputs', (): void => {
何乐's avatar
何乐 committed
5 6 7 8 9 10
    expect(isUrl([] as any)).toBeFalsy();
    expect(isUrl({} as any)).toBeFalsy();
    expect(isUrl(false as any)).toBeFalsy();
    expect(isUrl(true as any)).toBeFalsy();
    expect(isUrl(NaN as any)).toBeFalsy();
    expect(isUrl(null as any)).toBeFalsy();
陈帅's avatar
陈帅 committed
11
    expect(isUrl(undefined as any)).toBeFalsy();
何乐's avatar
何乐 committed
12 13 14
    expect(isUrl('')).toBeFalsy();
  });

陈帅's avatar
陈帅 committed
15
  it('should return false for invalid URLs', (): void => {
何乐's avatar
何乐 committed
16 17 18 19 20 21 22
    expect(isUrl('foo')).toBeFalsy();
    expect(isUrl('bar')).toBeFalsy();
    expect(isUrl('bar/test')).toBeFalsy();
    expect(isUrl('http:/example.com/')).toBeFalsy();
    expect(isUrl('ttp://example.com/')).toBeFalsy();
  });

陈帅's avatar
陈帅 committed
23
  it('should return true for valid URLs', (): void => {
何乐's avatar
何乐 committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37
    expect(isUrl('http://example.com/')).toBeTruthy();
    expect(isUrl('https://example.com/')).toBeTruthy();
    expect(isUrl('http://example.com/test/123')).toBeTruthy();
    expect(isUrl('https://example.com/test/123')).toBeTruthy();
    expect(isUrl('http://example.com/test/123?foo=bar')).toBeTruthy();
    expect(isUrl('https://example.com/test/123?foo=bar')).toBeTruthy();
    expect(isUrl('http://www.example.com/')).toBeTruthy();
    expect(isUrl('https://www.example.com/')).toBeTruthy();
    expect(isUrl('http://www.example.com/test/123')).toBeTruthy();
    expect(isUrl('https://www.example.com/test/123')).toBeTruthy();
    expect(isUrl('http://www.example.com/test/123?foo=bar')).toBeTruthy();
    expect(isUrl('https://www.example.com/test/123?foo=bar')).toBeTruthy();
  });
});