-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.test.ts
134 lines (102 loc) · 3.73 KB
/
utils.test.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// @ts-nocheck
import { Environment } from '@standardnotes/snjs';
import {
isValidJsonString,
generateUuid,
environmentToString
} from './../lib/utils';
const uuidFormat = /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
describe("Utils", () => {
describe('generateUuid', () => {
test("length should be 36 characters", () => {
const uuid = generateUuid();
expect(uuid.length).toEqual(36);
});
it("should have a valid format", () => {
const uuid = generateUuid();
expect(uuid).toEqual(expect.stringMatching(uuidFormat));
});
});
describe('isValidJsonString', () => {
test("anything other than string should return false", () => {
let result = isValidJsonString(1);
expect(result).toBe(false);
result = isValidJsonString(false);
expect(result).toBe(false);
result = isValidJsonString(1000000000000);
expect(result).toBe(false);
result = isValidJsonString({});
expect(result).toBe(false);
result = isValidJsonString([]);
expect(result).toBe(false);
result = isValidJsonString(() => true);
expect(result).toBe(false);
result = isValidJsonString(undefined);
expect(result).toBe(false);
result = isValidJsonString(null);
expect(result).toBe(false);
});
test("an invalid JSON string should return false", () => {
let result = isValidJsonString("{???}");
expect(result).toBe(false);
result = isValidJsonString("");
expect(result).toBe(false);
result = isValidJsonString("{");
expect(result).toBe(false);
});
test("stringified objects should return true", () => {
let objToStr = JSON.stringify({})
let result = isValidJsonString(objToStr);
expect(result).toBe(true);
objToStr = JSON.stringify({ test: 1234, foo: "bar", testing: true })
result = isValidJsonString(objToStr);
expect(result).toBe(true);
});
test("stringified arrays should return true", () => {
let arrToStr = JSON.stringify([])
let result = isValidJsonString(arrToStr);
expect(result).toBe(true);
arrToStr = JSON.stringify([{ test: 1234, foo: "bar", testing: true }])
result = isValidJsonString(arrToStr);
expect(result).toBe(true);
});
});
describe('environmentToString', () => {
test('an invalid value should fallback to "web"', () => {
let result = environmentToString(10000000);
expect(result).toBe("web");
result = environmentToString(-1);
expect(result).toBe("web");
result = environmentToString(null);
expect(result).toBe("web");
result = environmentToString(undefined);
expect(result).toBe("web");
result = environmentToString('');
expect(result).toBe("web");
result = environmentToString(0.01);
expect(result).toBe("web");
result = environmentToString({});
expect(result).toBe("web");
result = environmentToString([]);
expect(result).toBe("web");
result = environmentToString(true);
expect(result).toBe("web");
result = environmentToString(false);
expect(result).toBe("web");
result = environmentToString(() => true);
expect(result).toBe("web");
});
test('Environment.Web should return "web"', () => {
const result = environmentToString(Environment.Web);
expect(result).toBe("web");
});
test('Environment.Desktop should return "desktop"', () => {
const result = environmentToString(Environment.Desktop);
expect(result).toBe("desktop");
});
test('Environment.Mobile should return "mobile"', () => {
const result = environmentToString(Environment.Mobile);
expect(result).toBe("mobile");
});
});
});