Files
docx-js/src/util/convenience-functions.spec.ts

102 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-06-05 00:33:43 +01:00
import { describe, expect, it } from "vitest";
2021-03-12 03:58:05 +00:00
import {
abstractNumUniqueNumericIdGen,
bookmarkUniqueNumericIdGen,
concreteNumUniqueNumericIdGen,
convertInchesToTwip,
convertMillimetersToTwip,
docPropertiesUniqueNumericIdGen,
uniqueId,
hashedId,
uniqueNumericIdCreator,
uniqueUuid,
} from "./convenience-functions";
2020-12-24 03:37:43 +00:00
describe("Utility", () => {
describe("#convertMillimetersToTwip", () => {
2022-07-12 17:10:37 +01:00
it("should convert millimeters to TWIP", () => {
2020-12-24 03:37:43 +00:00
expect(convertMillimetersToTwip(1000)).to.equal(56692);
});
});
describe("#convertInchesToTwip", () => {
2022-07-12 17:10:37 +01:00
it("should convert inches to TWIP", () => {
2020-12-24 03:37:43 +00:00
expect(convertInchesToTwip(1)).to.equal(1440);
expect(convertInchesToTwip(0.5)).to.equal(720);
expect(convertInchesToTwip(0.25)).to.equal(360);
});
});
2021-03-12 03:58:05 +00:00
2023-04-19 16:51:03 +03:00
describe("#uniqueNumericIdCreator", () => {
2021-05-22 03:32:37 +03:00
it("should generate a unique incrementing ID", () => {
2023-04-19 16:51:03 +03:00
const uniqueNumericId = uniqueNumericIdCreator();
2021-03-24 03:09:01 +00:00
expect(uniqueNumericId()).to.not.be.undefined;
2021-03-12 03:58:05 +00:00
});
});
describe("#abstractNumUniqueNumericIdGen", () => {
it("should generate a unique incrementing ID", () => {
const uniqueNumericId = abstractNumUniqueNumericIdGen();
expect(uniqueNumericId()).to.equal(1);
expect(uniqueNumericId()).to.equal(2);
});
});
describe("#concreteNumUniqueNumericIdGen", () => {
it("should generate a unique incrementing ID", () => {
const uniqueNumericId = concreteNumUniqueNumericIdGen();
expect(uniqueNumericId()).to.equal(2);
expect(uniqueNumericId()).to.equal(3);
});
});
describe("#docPropertiesUniqueNumericIdGen", () => {
it("should generate a unique incrementing ID", () => {
const uniqueNumericId = docPropertiesUniqueNumericIdGen();
expect(uniqueNumericId()).to.equal(1);
expect(uniqueNumericId()).to.equal(2);
});
});
describe("#bookmarkUniqueNumericIdGen", () => {
it("should generate a unique incrementing ID", () => {
const uniqueNumericId = bookmarkUniqueNumericIdGen();
expect(uniqueNumericId()).to.equal(1);
expect(uniqueNumericId()).to.equal(2);
});
});
2021-03-12 03:58:05 +00:00
describe("#uniqueId", () => {
2021-05-22 03:32:37 +03:00
it("should generate a unique pseudorandom ID", () => {
2021-03-12 03:58:05 +00:00
expect(uniqueId()).to.not.be.empty;
});
});
describe("#hashedId", () => {
it("should generate a hex string", () => {
expect(hashedId("")).to.equal("da39a3ee5e6b4b0d3255bfef95601890afd80709");
});
it("should work with string, Uint8Array, Buffer and ArrayBuffer", () => {
const stringInput = "DATA";
const uint8ArrayInput = new Uint8Array(new TextEncoder().encode(stringInput));
const bufferInput = Buffer.from(uint8ArrayInput);
const arrayBufferInput = uint8ArrayInput.buffer;
const expectedHash = "580393f5a94fb469585f5dd2a6859a4aab899f37";
expect(hashedId(stringInput)).to.equal(expectedHash);
expect(hashedId(uint8ArrayInput)).to.equal(expectedHash);
expect(hashedId(bufferInput)).to.equal(expectedHash);
expect(hashedId(arrayBufferInput)).to.equal(expectedHash);
});
});
describe("#uniqueUuid", () => {
it("should generate a unique pseudorandom ID", () => {
expect(uniqueUuid()).to.not.be.empty;
});
});
2020-12-24 03:37:43 +00:00
});