Files
pyscript/pyscriptjs/tests/unit/utils.test.ts
Hood Chatham 08f34f748b Apply prettier to css, html, js, md, ts, and yml (#1249)
* Apply prettier to css, js, html, md, ts, and yml

As a followup I will add prettier to the .pre-commit config.
This patch is 100% generated by prettier.
I used a forked version of prettier that understands the
py-script tag.
See https://github.com/hoodmane/pyscript-prettier-precommit
for more info.

* Apply old pre-commit

* Revert some problems

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Revert some changes

* More changes

* Fix pre-commit

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-03-06 14:20:21 +00:00

79 lines
3.2 KiB
TypeScript

import { beforeEach, expect, describe, it } from '@jest/globals';
import { ensureUniqueId, joinPaths, createSingularWarning } from '../../src/utils';
describe('Utils', () => {
let element: HTMLElement;
beforeEach(() => {
element = document.createElement('div');
});
it('ensureUniqueId sets unique id on element', async () => {
expect(element.id).toBe('');
ensureUniqueId(element);
expect(element.id).toBe('py-internal-0');
});
it('ensureUniqueId sets unique id with increasing counter', async () => {
const secondElement = document.createElement('div');
expect(element.id).toBe('');
expect(secondElement.id).toBe('');
ensureUniqueId(element);
ensureUniqueId(secondElement);
// The counter will have been incremented on
// the previous test, make sure it keeps increasing
expect(element.id).toBe('py-internal-1');
expect(secondElement.id).toBe('py-internal-2');
});
});
describe('JoinPaths', () => {
it('should remove trailing slashes from the beginning and the end', () => {
const paths: string[] = ['///abc/d/e///'];
const joinedPath = joinPaths(paths);
expect(joinedPath).toStrictEqual('/abc/d/e');
});
it('should not remove slashes from the middle to preserve protocols such as http', () => {
const paths: string[] = ['http://google.com', '///data.txt'];
const joinedPath = joinPaths(paths);
expect(joinedPath).toStrictEqual('http://google.com/data.txt');
});
it('should not join paths when they are empty strings', () => {
const paths: string[] = ['', '///hhh/ll/pp///', '', 'kkk'];
const joinedPath = joinPaths(paths);
expect(joinedPath).toStrictEqual('hhh/ll/pp/kkk');
});
describe('createSingularBanner', () => {
it('should create one and new banner containing the sentinel text, and not duplicate it', () => {
//One warning banner with the desired text should be created
createSingularWarning('A unique error message', 'unique');
expect(document.getElementsByClassName('alert-banner')?.length).toEqual(1);
expect(document.getElementsByClassName('alert-banner')[0].textContent).toEqual(
expect.stringContaining('A unique error message'),
);
//Should still only be one banner, since the second uses the existing sentinel value "unique"
createSingularWarning('This banner should not appear', 'unique');
expect(document.getElementsByClassName('alert-banner')?.length).toEqual(1);
expect(document.getElementsByClassName('alert-banner')[0].textContent).toEqual(
expect.stringContaining('A unique error message'),
);
//If the sentinel value is not provided, the entire msg is used as the sentinel
createSingularWarning('A unique error message', null);
expect(document.getElementsByClassName('alert-banner')?.length).toEqual(1);
expect(document.getElementsByClassName('alert-banner')[0].textContent).toEqual(
expect.stringContaining('A unique error message'),
);
});
});
});