loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

test: add test coverage for web_src/js/utils.js (#4235)

[Added tests for methods]
---------------------------------
- isDarkTheme
- getCurrentLocale
- parseDom
- serializeXml
- sleep
- toAbsoluteUrl

[Pending tests for methods]
---------------------------------
- convertImage
- blobToDataURI

Co-authored-by: Vikas Choudhary <vikaschoudharycs097@gmail.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4235
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: vikaschoudhary <vikaschoudhary@noreply.codeberg.org>
Co-committed-by: vikaschoudhary <vikaschoudhary@noreply.codeberg.org>

authored by

vikaschoudhary
Vikas Choudhary
vikaschoudhary
and committed by
Earl Warren
2121a29f 8afdafeb

+81 -2
+81 -2
web_src/js/utils.test.js
··· 2 2 basename, extname, isObject, stripTags, parseIssueHref, 3 3 parseUrl, translateMonth, translateDay, blobToDataURI, 4 4 toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64, 5 + isDarkTheme, getCurrentLocale, parseDom, serializeXml, sleep, 5 6 } from './utils.js'; 7 + 8 + afterEach(() => { 9 + // Reset head and body sections of the document 10 + document.documentElement.innerHTML = '<head></head><body></body>'; 11 + 12 + // Remove 'lang' and 'style' attributes of html tag 13 + document.documentElement.removeAttribute('lang'); 14 + document.documentElement.removeAttribute('style'); 15 + }); 6 16 7 17 test('basename', () => { 8 18 expect(basename('/path/to/file.js')).toEqual('file.js'); ··· 22 32 expect(isObject([])).toBeFalsy(); 23 33 }); 24 34 35 + test('should return true if dark theme is enabled', () => { 36 + // When --is-dark-theme var is defined with value true 37 + document.documentElement.style.setProperty('--is-dark-theme', 'true'); 38 + expect(isDarkTheme()).toBeTruthy(); 39 + 40 + // when --is-dark-theme var is defined with value TRUE 41 + document.documentElement.style.setProperty('--is-dark-theme', 'TRUE'); 42 + expect(isDarkTheme()).toBeTruthy(); 43 + }); 44 + 45 + test('should return false if dark theme is disabled', () => { 46 + // when --is-dark-theme var is defined with value false 47 + document.documentElement.style.setProperty('--is-dark-theme', 'false'); 48 + expect(isDarkTheme()).toBeFalsy(); 49 + 50 + // when --is-dark-theme var is defined with value FALSE 51 + document.documentElement.style.setProperty('--is-dark-theme', 'FALSE'); 52 + expect(isDarkTheme()).toBeFalsy(); 53 + }); 54 + 55 + test('should return false if dark theme is not defined', () => { 56 + // when --is-dark-theme var is not exist 57 + expect(isDarkTheme()).toBeFalsy(); 58 + }); 59 + 25 60 test('stripTags', () => { 26 61 expect(stripTags('<a>test</a>')).toEqual('test'); 27 62 }); ··· 56 91 expect(parseUrl('https://localhost/path?search#hash').hash).toEqual('#hash'); 57 92 }); 58 93 94 + test('getCurrentLocale', () => { 95 + // HTML document without explicit lang 96 + expect(getCurrentLocale()).toEqual(''); 97 + 98 + // HTML document with explicit lang 99 + document.documentElement.setAttribute('lang', 'en-US'); 100 + expect(getCurrentLocale()).toEqual('en-US'); 101 + }); 102 + 59 103 test('translateMonth', () => { 60 104 const originalLang = document.documentElement.lang; 61 105 document.documentElement.lang = 'en-US'; ··· 86 130 test('toAbsoluteUrl', () => { 87 131 expect(toAbsoluteUrl('//host/dir')).toEqual('http://host/dir'); 88 132 expect(toAbsoluteUrl('https://host/dir')).toEqual('https://host/dir'); 89 - 133 + expect(toAbsoluteUrl('http://host/dir')).toEqual('http://host/dir'); 90 134 expect(toAbsoluteUrl('')).toEqual('http://localhost:3000'); 91 135 expect(toAbsoluteUrl('/user/repo')).toEqual('http://localhost:3000/user/repo'); 92 - 93 136 expect(() => toAbsoluteUrl('path')).toThrowError('unsupported'); 94 137 }); 95 138 ··· 112 155 expect(Array.from(decodeURLEncodedBase64('YQ'))).toEqual(Array.from(uint8array('a'))); 113 156 expect(Array.from(decodeURLEncodedBase64('YQ=='))).toEqual(Array.from(uint8array('a'))); 114 157 }); 158 + 159 + test('parseDom', () => { 160 + const paragraphStr = 'This is sample paragraph'; 161 + const paragraphTagStr = `<p>${paragraphStr}</p>`; 162 + const content = parseDom(paragraphTagStr, 'text/html'); 163 + expect(content.body.innerHTML).toEqual(paragraphTagStr); 164 + 165 + // Content should have only one paragraph 166 + const paragraphs = content.getElementsByTagName('p'); 167 + expect(paragraphs.length).toEqual(1); 168 + expect(paragraphs[0].textContent).toEqual(paragraphStr); 169 + }); 170 + 171 + test('serializeXml', () => { 172 + const textStr = 'This is a sample text'; 173 + const tagName = 'item'; 174 + const node = document.createElement(tagName); 175 + node.textContent = textStr; 176 + expect(serializeXml(node)).toEqual(`<${tagName}>${textStr}</${tagName}>`); 177 + }); 178 + 179 + test('sleep', async () => { 180 + // Test 500 ms sleep 181 + await testSleep(500); 182 + 183 + // Test 2000 ms sleep 184 + await testSleep(2000); 185 + }); 186 + 187 + async function testSleep(ms) { 188 + const startTime = Date.now(); // Record the start time 189 + await sleep(ms); 190 + const endTime = Date.now(); // Record the end time 191 + const actualSleepTime = endTime - startTime; 192 + expect(Math.abs(actualSleepTime - ms) <= 15).toBeTruthy(); 193 + }