Skip to content

Commit eab3cb2

Browse files
author
João Dias
committed
test(generateUUID): added more test coverage
1 parent 7475ebf commit eab3cb2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

cypress/test/functions/utlities/generate-uuid.cy.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,52 @@ describe("generateUUID", () => {
2323
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/
2424
);
2525
});
26+
27+
it("should generate unique UUIDs", () => {
28+
const uuids = new Set();
29+
const iterations = 1000;
30+
31+
for (let i = 0; i < iterations; i++) {
32+
const uuid = generateUUID();
33+
expect(uuids.has(uuid)).to.be.false;
34+
uuids.add(uuid);
35+
}
36+
37+
expect(uuids.size).to.equal(iterations);
38+
});
39+
40+
it("should generate UUIDs with correct version and variant bits", () => {
41+
const uuid = generateUUID();
42+
const parts = uuid.split("-");
43+
44+
// Version 4 UUID has version bits set to 0100
45+
const versionByte = parseInt(parts[2].substring(0, 2), 16);
46+
expect(versionByte & 0xf0).to.equal(0x40);
47+
48+
// Variant bits should be set to 10
49+
const variantByte = parseInt(parts[3].substring(0, 2), 16);
50+
expect(variantByte & 0xc0).to.equal(0x80);
51+
});
52+
53+
it("should generate UUIDs with consistent length", () => {
54+
const uuid = generateUUID();
55+
expect(uuid.length).to.equal(36); // 32 characters + 4 hyphens
56+
});
57+
58+
it("should generate UUIDs with correct character set", () => {
59+
const uuid = generateUUID();
60+
expect(uuid).to.match(/^[0-9a-fA-F-]+$/);
61+
});
62+
63+
it("should handle multiple consecutive calls", () => {
64+
const uuids = Array.from({ length: 10 }, () => generateUUID());
65+
const uniqueUuids = new Set(uuids);
66+
67+
expect(uuids.length).to.equal(uniqueUuids.size);
68+
uuids.forEach((uuid) => {
69+
expect(uuid).to.match(
70+
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/
71+
);
72+
});
73+
});
2674
});

0 commit comments

Comments
 (0)