Files
pyscript/pyscriptjs/tests/unit/utils.test.ts
Madhur Tandon 515858f313 implement proposal for fetching paths and retaining structure of dirs and packages (#914)
* implement proposal

* update docs and replace py-env

* more docs

* suggested proposal

* update docs

* add to_file parameter

* remove comment from Makefile

* suggested improvements

* move tests from basic to py_config

* retain leading slash from the first path
2022-11-08 17:26:45 +05:30

55 lines
1.6 KiB
TypeScript

import { ensureUniqueId, joinPaths } from '../../src/utils';
import { expect } from "@jest/globals";
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');
})
})