Skip to content

Commit

Permalink
Uuid fix (#94)
Browse files Browse the repository at this point in the history
* used randomUUID to get uuid; added unit test to check uniqueness

* added separator symbol to element id
  • Loading branch information
asanovarthur authored Feb 22, 2022
1 parent 10c3f81 commit 565355a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
16 changes: 10 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "powerbi-visuals-utils-testutils",
"version": "2.5.0",
"version": "2.6.0",
"description": "powerbi-visuals-utils-testutils",
"main": "lib/index.js",
"module": "lib/index.js",
Expand Down
9 changes: 4 additions & 5 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@ function each(element: JQuery | HTMLElement, fn: (i: number, el: HTMLElement) =>
}
}

function uuid() {
const buf = new Uint8Array(1);
window.crypto.getRandomValues(buf);
return buf[0];
export function getUuid() {
const uuid = (window.crypto as any).randomUUID();
return uuid;
}


export function testDom(height: number | string, width: number | string): HTMLElement {
let element: HTMLElement = document.createElement("div"),
heightWithUnits: string = isFinite( Number(height) ) ? `${Number(height)}px` : String(height),
widthWithUnits: string = isFinite( Number(width) ) ? `${Number(width)}px` : String(width),
id = "item" + uuid();
id = "item_" + getUuid();

element.id = id;
element.style.height = heightWithUnits;
Expand Down
17 changes: 16 additions & 1 deletion test/helpers/helpersTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* THE SOFTWARE.
*/

import { testDom, getRandomNumber, getRandomNumbers } from "../../src/helpers/helpers";
import { testDom, getRandomNumber, getRandomNumbers, getUuid } from "../../src/helpers/helpers";

describe("testDom", () => {
it("should return an element", () => {
Expand All @@ -34,6 +34,21 @@ describe("testDom", () => {
});
});

describe("uuid", () => {
it("should be unique across multiple tests", () => {
const uuids = [];
const testsAmount = 1000;

for (let i = 0; i < testsAmount; i++) {
uuids.push(getUuid());
}

const uniqueUUids = [...new Set(uuids)];

expect(uuids.length).toEqual(uniqueUUids.length);
});
});

describe("getRandomNumber", () => {
it("should return a number between min and max", () => {
const min: number = 150,
Expand Down

0 comments on commit 565355a

Please sign in to comment.