code.gitea.io/gitea@v1.21.7/web_src/js/utils.test.js (about)

     1  import {
     2    basename, extname, isObject, stripTags, parseIssueHref,
     3    parseUrl, translateMonth, translateDay, blobToDataURI,
     4    toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64,
     5  } from './utils.js';
     6  
     7  test('basename', () => {
     8    expect(basename('/path/to/file.js')).toEqual('file.js');
     9    expect(basename('/path/to/file')).toEqual('file');
    10    expect(basename('file.js')).toEqual('file.js');
    11  });
    12  
    13  test('extname', () => {
    14    expect(extname('/path/to/file.js')).toEqual('.js');
    15    expect(extname('/path/')).toEqual('');
    16    expect(extname('/path')).toEqual('');
    17    expect(extname('file.js')).toEqual('.js');
    18  });
    19  
    20  test('isObject', () => {
    21    expect(isObject({})).toBeTruthy();
    22    expect(isObject([])).toBeFalsy();
    23  });
    24  
    25  test('stripTags', () => {
    26    expect(stripTags('<a>test</a>')).toEqual('test');
    27  });
    28  
    29  test('parseIssueHref', () => {
    30    expect(parseIssueHref('/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
    31    expect(parseIssueHref('/owner/repo/pulls/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
    32    expect(parseIssueHref('/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
    33    expect(parseIssueHref('/sub/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
    34    expect(parseIssueHref('/sub/sub2/owner/repo/pulls/1')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
    35    expect(parseIssueHref('/sub/sub2/owner/repo/issues/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
    36    expect(parseIssueHref('/sub/sub2/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
    37    expect(parseIssueHref('https://example.com/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
    38    expect(parseIssueHref('https://example.com/owner/repo/pulls/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
    39    expect(parseIssueHref('https://example.com/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
    40    expect(parseIssueHref('https://example.com/sub/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
    41    expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/pulls/1')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
    42    expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
    43    expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
    44    expect(parseIssueHref('')).toEqual({owner: undefined, repo: undefined, type: undefined, index: undefined});
    45  });
    46  
    47  test('parseUrl', () => {
    48    expect(parseUrl('').pathname).toEqual('/');
    49    expect(parseUrl('/path').pathname).toEqual('/path');
    50    expect(parseUrl('/path?search').pathname).toEqual('/path');
    51    expect(parseUrl('/path?search').search).toEqual('?search');
    52    expect(parseUrl('/path?search#hash').hash).toEqual('#hash');
    53    expect(parseUrl('https://localhost/path').pathname).toEqual('/path');
    54    expect(parseUrl('https://localhost/path?search').pathname).toEqual('/path');
    55    expect(parseUrl('https://localhost/path?search').search).toEqual('?search');
    56    expect(parseUrl('https://localhost/path?search#hash').hash).toEqual('#hash');
    57  });
    58  
    59  test('translateMonth', () => {
    60    const originalLang = document.documentElement.lang;
    61    document.documentElement.lang = 'en-US';
    62    expect(translateMonth(0)).toEqual('Jan');
    63    expect(translateMonth(4)).toEqual('May');
    64    document.documentElement.lang = 'es-ES';
    65    expect(translateMonth(5)).toEqual('jun');
    66    expect(translateMonth(6)).toEqual('jul');
    67    document.documentElement.lang = originalLang;
    68  });
    69  
    70  test('translateDay', () => {
    71    const originalLang = document.documentElement.lang;
    72    document.documentElement.lang = 'fr-FR';
    73    expect(translateDay(1)).toEqual('lun.');
    74    expect(translateDay(5)).toEqual('ven.');
    75    document.documentElement.lang = 'pl-PL';
    76    expect(translateDay(1)).toEqual('pon.');
    77    expect(translateDay(5)).toEqual('pt.');
    78    document.documentElement.lang = originalLang;
    79  });
    80  
    81  test('blobToDataURI', async () => {
    82    const blob = new Blob([JSON.stringify({test: true})], {type: 'application/json'});
    83    expect(await blobToDataURI(blob)).toEqual('data:application/json;base64,eyJ0ZXN0Ijp0cnVlfQ==');
    84  });
    85  
    86  test('toAbsoluteUrl', () => {
    87    expect(toAbsoluteUrl('//host/dir')).toEqual('http://host/dir');
    88    expect(toAbsoluteUrl('https://host/dir')).toEqual('https://host/dir');
    89  
    90    expect(toAbsoluteUrl('')).toEqual('http://localhost:3000');
    91    expect(toAbsoluteUrl('/user/repo')).toEqual('http://localhost:3000/user/repo');
    92  
    93    expect(() => toAbsoluteUrl('path')).toThrowError('unsupported');
    94  });
    95  
    96  test('encodeURLEncodedBase64, decodeURLEncodedBase64', () => {
    97    // TextEncoder is Node.js API while Uint8Array is jsdom API and their outputs are not
    98    // structurally comparable, so we convert to array to compare. The conversion can be
    99    // removed once https://github.com/jsdom/jsdom/issues/2524 is resolved.
   100    const encoder = new TextEncoder();
   101    const uint8array = encoder.encode.bind(encoder);
   102  
   103    expect(encodeURLEncodedBase64(uint8array('AA?'))).toEqual('QUE_'); // standard base64: "QUE/"
   104    expect(encodeURLEncodedBase64(uint8array('AA~'))).toEqual('QUF-'); // standard base64: "QUF+"
   105  
   106    expect(Array.from(decodeURLEncodedBase64('QUE/'))).toEqual(Array.from(uint8array('AA?')));
   107    expect(Array.from(decodeURLEncodedBase64('QUF+'))).toEqual(Array.from(uint8array('AA~')));
   108    expect(Array.from(decodeURLEncodedBase64('QUE_'))).toEqual(Array.from(uint8array('AA?')));
   109    expect(Array.from(decodeURLEncodedBase64('QUF-'))).toEqual(Array.from(uint8array('AA~')));
   110  
   111    expect(encodeURLEncodedBase64(uint8array('a'))).toEqual('YQ'); // standard base64: "YQ=="
   112    expect(Array.from(decodeURLEncodedBase64('YQ'))).toEqual(Array.from(uint8array('a')));
   113    expect(Array.from(decodeURLEncodedBase64('YQ=='))).toEqual(Array.from(uint8array('a')));
   114  });