File size: 2,165 Bytes
bf48b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { describe, expect, it } from 'vitest';

import { collapseWhitespace, convertDateToISO8601, getLocalhostAddress, getSubPath, toTitleCase } from '@/utils/common-utils';

describe('common-utils', () => {
    it('toTitleCase', () => {
        expect(toTitleCase('RSSHub IS AS aweSOme aS henry')).toBe('Rsshub Is as Awesome as Henry');
    });

    it('convertDateToISO8601', () => {
        expect(convertDateToISO8601('')).toBe('');
        expect(convertDateToISO8601(null)).toBe(null);
        expect(convertDateToISO8601(undefined)).toBe(undefined);

        const date = new Date('2019-01-01');
        const expected = date.toISOString();
        expect(convertDateToISO8601(date)).toBe(expected);
        expect(convertDateToISO8601(date.toISOString())).toBe(expected);
        expect(convertDateToISO8601(date.toUTCString())).toBe(expected);
        expect(convertDateToISO8601(date.toLocaleString())).toBe(expected);
        expect(convertDateToISO8601('Tue, 01 Jan 2019 08:00:00 UTC+8')).toBe(expected);

        expect(convertDateToISO8601('Tue, 01 Jan 2019 00:00:00')).toBe(new Date(date.getTime() + new Date().getTimezoneOffset() * 60 * 1000).toISOString());
        // need to pass a function in order to use `toThrow`
        expect(() => {
            convertDateToISO8601('something invalid');
        }).toThrow(RangeError);
    });

    it('collapseWhitespace', () => {
        expect(collapseWhitespace('')).toBe('');
        expect(collapseWhitespace(null)).toBe(null);
        expect(collapseWhitespace(undefined)).toBe(undefined);
        expect(collapseWhitespace('   \n\n\n    ')).toBe('');
        expect(collapseWhitespace('a string already collapsed')).toBe('a string already collapsed');
        expect(collapseWhitespace(' \n  a lot of     whitespaces   and \n\n\n\n linebreaks   \n\n ')).toBe('a lot of whitespaces and linebreaks');
    });

    it('getLocalhostAddress', () => {
        expect(getLocalhostAddress()).toBeInstanceOf(Array);
    });

    it('getSubPath', () => {
        expect(getSubPath({ req: { path: '/test/abc' } })).toBe('/abc');
        expect(getSubPath({ req: { path: '/test' } })).toBe('/');
    });
});