loading up the forgejo repo on tangled to test page performance
at forgejo 186 lines 8.7 kB view raw
1import { 2 basename, extname, isObject, stripTags, parseIssueHref, 3 parseUrl, translateMonth, translateDay, blobToDataURI, 4 toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64, 5 isDarkTheme, getCurrentLocale, parseDom, serializeXml, sleep, 6} from './utils.js'; 7 8afterEach(() => { 9 // Remove 'lang' and 'style' attributes of html tag 10 document.documentElement.removeAttribute('lang'); 11 document.documentElement.removeAttribute('style'); 12}); 13 14test('basename', () => { 15 expect(basename('/path/to/file.js')).toEqual('file.js'); 16 expect(basename('/path/to/file')).toEqual('file'); 17 expect(basename('file.js')).toEqual('file.js'); 18}); 19 20test('extname', () => { 21 expect(extname('/path/to/file.js')).toEqual('.js'); 22 expect(extname('/path/')).toEqual(''); 23 expect(extname('/path')).toEqual(''); 24 expect(extname('file.js')).toEqual('.js'); 25}); 26 27test('isObject', () => { 28 expect(isObject({})).toBeTruthy(); 29 expect(isObject([])).toBeFalsy(); 30}); 31 32test('should return true if dark theme is enabled', () => { 33 // When --is-dark-theme var is defined with value true 34 document.documentElement.style.setProperty('--is-dark-theme', 'true'); 35 expect(isDarkTheme()).toBeTruthy(); 36 37 // when --is-dark-theme var is defined with value TRUE 38 document.documentElement.style.setProperty('--is-dark-theme', 'TRUE'); 39 expect(isDarkTheme()).toBeTruthy(); 40}); 41 42test('should return false if dark theme is disabled', () => { 43 // when --is-dark-theme var is defined with value false 44 document.documentElement.style.setProperty('--is-dark-theme', 'false'); 45 expect(isDarkTheme()).toBeFalsy(); 46 47 // when --is-dark-theme var is defined with value FALSE 48 document.documentElement.style.setProperty('--is-dark-theme', 'FALSE'); 49 expect(isDarkTheme()).toBeFalsy(); 50}); 51 52test('should return false if dark theme is not defined', () => { 53 // when --is-dark-theme var is not exist 54 expect(isDarkTheme()).toBeFalsy(); 55}); 56 57test('stripTags', () => { 58 expect(stripTags('<a>test</a>')).toEqual('test'); 59}); 60 61test('parseIssueHref', () => { 62 expect(parseIssueHref('/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'}); 63 expect(parseIssueHref('/owner/repo/pulls/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'}); 64 expect(parseIssueHref('/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'}); 65 expect(parseIssueHref('/sub/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'}); 66 expect(parseIssueHref('/sub/sub2/owner/repo/pulls/1')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'}); 67 expect(parseIssueHref('/sub/sub2/owner/repo/issues/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'}); 68 expect(parseIssueHref('/sub/sub2/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'}); 69 expect(parseIssueHref('https://example.com/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'}); 70 expect(parseIssueHref('https://example.com/owner/repo/pulls/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'}); 71 expect(parseIssueHref('https://example.com/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'}); 72 expect(parseIssueHref('https://example.com/sub/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'}); 73 expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/pulls/1')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'}); 74 expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'}); 75 expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'}); 76 expect(parseIssueHref('')).toEqual({owner: undefined, repo: undefined, type: undefined, index: undefined}); 77}); 78 79test('parseUrl', () => { 80 expect(parseUrl('').pathname).toEqual('/'); 81 expect(parseUrl('/path').pathname).toEqual('/path'); 82 expect(parseUrl('/path?search').pathname).toEqual('/path'); 83 expect(parseUrl('/path?search').search).toEqual('?search'); 84 expect(parseUrl('/path?search#hash').hash).toEqual('#hash'); 85 expect(parseUrl('https://localhost/path').pathname).toEqual('/path'); 86 expect(parseUrl('https://localhost/path?search').pathname).toEqual('/path'); 87 expect(parseUrl('https://localhost/path?search').search).toEqual('?search'); 88 expect(parseUrl('https://localhost/path?search#hash').hash).toEqual('#hash'); 89}); 90 91test('getCurrentLocale', () => { 92 // HTML document without explicit lang 93 expect(getCurrentLocale()).toEqual(''); 94 95 // HTML document with explicit lang 96 document.documentElement.setAttribute('lang', 'en-US'); 97 expect(getCurrentLocale()).toEqual('en-US'); 98}); 99 100test('translateMonth', () => { 101 const originalLang = document.documentElement.lang; 102 document.documentElement.lang = 'en-US'; 103 expect(translateMonth(0)).toEqual('Jan'); 104 expect(translateMonth(4)).toEqual('May'); 105 document.documentElement.lang = 'es-ES'; 106 expect(translateMonth(5)).toEqual('jun'); 107 expect(translateMonth(6)).toEqual('jul'); 108 document.documentElement.lang = originalLang; 109}); 110 111test('translateDay', () => { 112 const originalLang = document.documentElement.lang; 113 document.documentElement.lang = 'fr-FR'; 114 expect(translateDay(1)).toEqual('lun.'); 115 expect(translateDay(5)).toEqual('ven.'); 116 document.documentElement.lang = 'pl-PL'; 117 expect(translateDay(1)).toEqual('pon.'); 118 expect(translateDay(5)).toEqual('pt.'); 119 document.documentElement.lang = originalLang; 120}); 121 122test('blobToDataURI', async () => { 123 const blob = new Blob([JSON.stringify({test: true})], {type: 'application/json'}); 124 expect(await blobToDataURI(blob)).toEqual('data:application/json;base64,eyJ0ZXN0Ijp0cnVlfQ=='); 125}); 126 127test('toAbsoluteUrl', () => { 128 expect(toAbsoluteUrl('//host/dir')).toEqual('http://host/dir'); 129 expect(toAbsoluteUrl('https://host/dir')).toEqual('https://host/dir'); 130 expect(toAbsoluteUrl('http://host/dir')).toEqual('http://host/dir'); 131 expect(toAbsoluteUrl('')).toEqual('http://localhost:3000'); 132 expect(toAbsoluteUrl('/user/repo')).toEqual('http://localhost:3000/user/repo'); 133 expect(() => toAbsoluteUrl('path')).toThrowError('unsupported'); 134}); 135 136test('encodeURLEncodedBase64, decodeURLEncodedBase64', () => { 137 // TextEncoder is Node.js API while Uint8Array is jsdom API and their outputs are not 138 // structurally comparable, so we convert to array to compare. The conversion can be 139 // removed once https://github.com/jsdom/jsdom/issues/2524 is resolved. 140 const encoder = new TextEncoder(); 141 const uint8array = encoder.encode.bind(encoder); 142 143 expect(encodeURLEncodedBase64(uint8array('AA?'))).toEqual('QUE_'); // standard base64: "QUE/" 144 expect(encodeURLEncodedBase64(uint8array('AA~'))).toEqual('QUF-'); // standard base64: "QUF+" 145 146 expect(Array.from(decodeURLEncodedBase64('QUE/'))).toEqual(Array.from(uint8array('AA?'))); 147 expect(Array.from(decodeURLEncodedBase64('QUF+'))).toEqual(Array.from(uint8array('AA~'))); 148 expect(Array.from(decodeURLEncodedBase64('QUE_'))).toEqual(Array.from(uint8array('AA?'))); 149 expect(Array.from(decodeURLEncodedBase64('QUF-'))).toEqual(Array.from(uint8array('AA~'))); 150 151 expect(encodeURLEncodedBase64(uint8array('a'))).toEqual('YQ'); // standard base64: "YQ==" 152 expect(Array.from(decodeURLEncodedBase64('YQ'))).toEqual(Array.from(uint8array('a'))); 153 expect(Array.from(decodeURLEncodedBase64('YQ=='))).toEqual(Array.from(uint8array('a'))); 154}); 155 156test('parseDom', () => { 157 const paragraphStr = 'This is sample paragraph'; 158 const paragraphTagStr = `<p>${paragraphStr}</p>`; 159 const content = parseDom(paragraphTagStr, 'text/html'); 160 expect(content.body.innerHTML).toEqual(paragraphTagStr); 161 162 // Content should have only one paragraph 163 const paragraphs = content.getElementsByTagName('p'); 164 expect(paragraphs.length).toEqual(1); 165 expect(paragraphs[0].textContent).toEqual(paragraphStr); 166}); 167 168test('serializeXml', () => { 169 const textStr = 'This is a sample text'; 170 const tagName = 'item'; 171 const node = document.createElement(tagName); 172 node.textContent = textStr; 173 expect(serializeXml(node)).toEqual(`<${tagName} xmlns="http://www.w3.org/1999/xhtml">${textStr}</${tagName}>`); 174}); 175 176test('sleep', async () => { 177 await testSleep(2000); 178}); 179 180async function testSleep(ms) { 181 const startTime = Date.now(); // Record the start time 182 await sleep(ms); 183 const endTime = Date.now(); // Record the end time 184 const actualSleepTime = endTime - startTime; 185 expect(actualSleepTime >= ms).toBeTruthy(); 186}