forked from snico-dev/guid-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguid.spec.ts
83 lines (67 loc) · 3.37 KB
/
guid.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { expect } from "chai";
import "mocha";
import { Guid } from "../lib/guid";
describe("Guid", () => {
const example_hyphen: string = "0315642c-a069-9f3e-1852-9adf2d075b93";
const example_no_hyphen: string = "0315642ca0699f3e18529adf2d075b93";
it("should create & validate a random GUID", () => {
//generated guid using the static construction method
const static_guid: Guid = Guid.create();
expect(Guid.isValid(static_guid)).equal(true); //valid?
expect(static_guid.toString()).not.equal(Guid.EMPTY); //not null?
//generated guid using the constructor; same expectation here
const dynamic_guid: Guid = new Guid();
expect(Guid.isValid(dynamic_guid)).equal(true); //valid?
expect(dynamic_guid.toString()).not.equal(Guid.EMPTY); //not null?
});
it("should not instantiate itself if the input value is invalid", () => {
const text: string = "random test string";
//method 1: static instantiation
expect(() => Guid.create(text)).to.throw(TypeError);
//method 2: directly using the constructor
expect(() => new Guid(text)).to.throw(TypeError);
});
it("should parse & validate GUIDs", () => {
const wrong: string = "wrongguid";
expect(Guid.isValid(wrong)).equal(false); //must return false, no exception
expect(Guid.isValid(example_hyphen)).equal(true); //valid?
expect(Guid.isValid(example_no_hyphen)).equal(true); //non-hyphenated guid. also valid?
expect(Guid.isValid(example_no_hyphen + wrong)).equal(false); //valid guid plus one char. invalid?
//@ts-ignore
expect(Guid.isValid(undefined)).equal(false);
//@ts-ignore
expect(Guid.isValid(null)).equal(false);
//@ts-ignore
expect(Guid.isValid(123456789)).equal(false);
});
it("should create nulled GUIDs & return them as a string", () => {
expect(Guid.createEmpty().toString()).equal(Guid.EMPTY);
});
it("should parse non-hyphenated GUIDs & return them in the more readable RFC 4122 format", () => {
expect(Guid.create(example_no_hyphen).toString()).equal(example_hyphen);
expect(new Guid(example_no_hyphen).toString()).equal(example_hyphen); //same, but using the constructor
});
it("should convert GUIDs to non-hyphenated GUIDs", () => {
expect(Guid.create(example_hyphen).toShortString()).equal(example_no_hyphen);
});
it("should compare GUID instances to another", () => {
const wrong_guid: Guid = Guid.create();
expect(wrong_guid.equals(Guid.create())).equal(false);
const correct_guid: Guid = Guid.create(example_hyphen);
const duplicate_guid: Guid = Guid.create(example_hyphen);
expect(correct_guid.equals(duplicate_guid)).equal(true);
});
it("should generate unique GUIDs only", () => {
const guids: Array<Guid> = [];
for (let index: number = 0; index < 3000; index++) {
guids.push(Guid.create());
}
expect(guids.indexOf(guids[0]) < 0).equal(false);
expect(guids.indexOf(Guid.create()) < 0).equal(true);
});
it("should not care about GUID case at all", () => {
const upperCaseGuid: Guid = new Guid(example_hyphen.toUpperCase());
const lowerCaseGuid: Guid = Guid.create(example_hyphen);
expect(upperCaseGuid.equals(lowerCaseGuid)).equal(true);
});
});