mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-22 23:33:15 -05:00
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>
This commit is contained in:
parent
8afdafebf9
commit
2121a29f89
1 changed files with 81 additions and 2 deletions
|
@ -2,8 +2,18 @@ import {
|
|||
basename, extname, isObject, stripTags, parseIssueHref,
|
||||
parseUrl, translateMonth, translateDay, blobToDataURI,
|
||||
toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64,
|
||||
isDarkTheme, getCurrentLocale, parseDom, serializeXml, sleep,
|
||||
} from './utils.js';
|
||||
|
||||
afterEach(() => {
|
||||
// Reset head and body sections of the document
|
||||
document.documentElement.innerHTML = '<head></head><body></body>';
|
||||
|
||||
// Remove 'lang' and 'style' attributes of html tag
|
||||
document.documentElement.removeAttribute('lang');
|
||||
document.documentElement.removeAttribute('style');
|
||||
});
|
||||
|
||||
test('basename', () => {
|
||||
expect(basename('/path/to/file.js')).toEqual('file.js');
|
||||
expect(basename('/path/to/file')).toEqual('file');
|
||||
|
@ -22,6 +32,31 @@ test('isObject', () => {
|
|||
expect(isObject([])).toBeFalsy();
|
||||
});
|
||||
|
||||
test('should return true if dark theme is enabled', () => {
|
||||
// When --is-dark-theme var is defined with value true
|
||||
document.documentElement.style.setProperty('--is-dark-theme', 'true');
|
||||
expect(isDarkTheme()).toBeTruthy();
|
||||
|
||||
// when --is-dark-theme var is defined with value TRUE
|
||||
document.documentElement.style.setProperty('--is-dark-theme', 'TRUE');
|
||||
expect(isDarkTheme()).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should return false if dark theme is disabled', () => {
|
||||
// when --is-dark-theme var is defined with value false
|
||||
document.documentElement.style.setProperty('--is-dark-theme', 'false');
|
||||
expect(isDarkTheme()).toBeFalsy();
|
||||
|
||||
// when --is-dark-theme var is defined with value FALSE
|
||||
document.documentElement.style.setProperty('--is-dark-theme', 'FALSE');
|
||||
expect(isDarkTheme()).toBeFalsy();
|
||||
});
|
||||
|
||||
test('should return false if dark theme is not defined', () => {
|
||||
// when --is-dark-theme var is not exist
|
||||
expect(isDarkTheme()).toBeFalsy();
|
||||
});
|
||||
|
||||
test('stripTags', () => {
|
||||
expect(stripTags('<a>test</a>')).toEqual('test');
|
||||
});
|
||||
|
@ -56,6 +91,15 @@ test('parseUrl', () => {
|
|||
expect(parseUrl('https://localhost/path?search#hash').hash).toEqual('#hash');
|
||||
});
|
||||
|
||||
test('getCurrentLocale', () => {
|
||||
// HTML document without explicit lang
|
||||
expect(getCurrentLocale()).toEqual('');
|
||||
|
||||
// HTML document with explicit lang
|
||||
document.documentElement.setAttribute('lang', 'en-US');
|
||||
expect(getCurrentLocale()).toEqual('en-US');
|
||||
});
|
||||
|
||||
test('translateMonth', () => {
|
||||
const originalLang = document.documentElement.lang;
|
||||
document.documentElement.lang = 'en-US';
|
||||
|
@ -86,10 +130,9 @@ test('blobToDataURI', async () => {
|
|||
test('toAbsoluteUrl', () => {
|
||||
expect(toAbsoluteUrl('//host/dir')).toEqual('http://host/dir');
|
||||
expect(toAbsoluteUrl('https://host/dir')).toEqual('https://host/dir');
|
||||
|
||||
expect(toAbsoluteUrl('http://host/dir')).toEqual('http://host/dir');
|
||||
expect(toAbsoluteUrl('')).toEqual('http://localhost:3000');
|
||||
expect(toAbsoluteUrl('/user/repo')).toEqual('http://localhost:3000/user/repo');
|
||||
|
||||
expect(() => toAbsoluteUrl('path')).toThrowError('unsupported');
|
||||
});
|
||||
|
||||
|
@ -112,3 +155,39 @@ test('encodeURLEncodedBase64, decodeURLEncodedBase64', () => {
|
|||
expect(Array.from(decodeURLEncodedBase64('YQ'))).toEqual(Array.from(uint8array('a')));
|
||||
expect(Array.from(decodeURLEncodedBase64('YQ=='))).toEqual(Array.from(uint8array('a')));
|
||||
});
|
||||
|
||||
test('parseDom', () => {
|
||||
const paragraphStr = 'This is sample paragraph';
|
||||
const paragraphTagStr = `<p>${paragraphStr}</p>`;
|
||||
const content = parseDom(paragraphTagStr, 'text/html');
|
||||
expect(content.body.innerHTML).toEqual(paragraphTagStr);
|
||||
|
||||
// Content should have only one paragraph
|
||||
const paragraphs = content.getElementsByTagName('p');
|
||||
expect(paragraphs.length).toEqual(1);
|
||||
expect(paragraphs[0].textContent).toEqual(paragraphStr);
|
||||
});
|
||||
|
||||
test('serializeXml', () => {
|
||||
const textStr = 'This is a sample text';
|
||||
const tagName = 'item';
|
||||
const node = document.createElement(tagName);
|
||||
node.textContent = textStr;
|
||||
expect(serializeXml(node)).toEqual(`<${tagName}>${textStr}</${tagName}>`);
|
||||
});
|
||||
|
||||
test('sleep', async () => {
|
||||
// Test 500 ms sleep
|
||||
await testSleep(500);
|
||||
|
||||
// Test 2000 ms sleep
|
||||
await testSleep(2000);
|
||||
});
|
||||
|
||||
async function testSleep(ms) {
|
||||
const startTime = Date.now(); // Record the start time
|
||||
await sleep(ms);
|
||||
const endTime = Date.now(); // Record the end time
|
||||
const actualSleepTime = endTime - startTime;
|
||||
expect(Math.abs(actualSleepTime - ms) <= 15).toBeTruthy();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue