Skip to content

Commit 5e16355

Browse files
authored
test: Increase code coverage in package utils (#1093)
Closes #1089 Increase code coverage in package utils.
1 parent 14020f1 commit 5e16355

11 files changed

Lines changed: 422 additions & 10 deletions

packages/utils/src/Asserts.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { assertNotNull } from './Asserts';
2+
3+
it('throws an error when a value is null', () => {
4+
expect(() => assertNotNull(null)).toThrowError('Value is null or undefined');
5+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { copyToClipboard } from './ClipboardUtils';
2+
3+
document.execCommand = jest.fn();
4+
5+
describe('Clipboard', () => {
6+
describe('writeText', () => {
7+
beforeEach(() => jest.resetAllMocks());
8+
9+
it('should call clipboard.writeText', async () => {
10+
Object.assign(navigator, {
11+
clipboard: {
12+
// eslint-disable-next-line @typescript-eslint/no-empty-function
13+
writeText: () => {},
14+
},
15+
});
16+
jest.spyOn(navigator.clipboard, 'writeText');
17+
18+
await copyToClipboard('test');
19+
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('test');
20+
});
21+
22+
it('should call copyToClipboardExecCommand if writeText fails', async () => {
23+
Object.assign(navigator, {
24+
clipboard: {
25+
writeText: () => {
26+
throw new Error('Could not write text');
27+
},
28+
},
29+
});
30+
document.execCommand = jest.fn(() => true);
31+
jest.spyOn(navigator.clipboard, 'writeText');
32+
33+
await expect(copyToClipboard('test')).resolves.toBeUndefined();
34+
expect(document.execCommand).toHaveBeenCalledWith('copy');
35+
});
36+
37+
it('calls copyToClipboardExecCommand if clipboard is undefined and throws', async () => {
38+
Object.assign(navigator, {
39+
clipboard: undefined,
40+
});
41+
document.execCommand = jest.fn(() => false);
42+
43+
await expect(copyToClipboard('test')).rejects.toThrowError(
44+
'Unable to execute copy command'
45+
);
46+
});
47+
48+
it('calls copyToClipboardExecCommand if clipboard is undefined but does not throw', async () => {
49+
Object.assign(navigator, {
50+
clipboard: undefined,
51+
});
52+
document.execCommand = jest.fn(() => true);
53+
54+
await expect(copyToClipboard('test')).resolves.toBeUndefined();
55+
await expect(document.execCommand).toHaveBeenCalledWith('copy');
56+
});
57+
});
58+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import ColorUtils from './ColorUtils';
2+
3+
describe('isDark', () => {
4+
it('returns true if the background is dark', () => {
5+
expect(ColorUtils.isDark('#000000')).toBe(true);
6+
expect(ColorUtils.isDark('#000')).toBe(true);
7+
expect(ColorUtils.isDark('rgb(0,0,0)')).toBe(true);
8+
expect(ColorUtils.isDark('rgba(0,0,0,1)')).toBe(true);
9+
expect(ColorUtils.isDark('hsl(0,0%,0%)')).toBe(true);
10+
});
11+
12+
it('returns false if the background is bright', () => {
13+
expect(ColorUtils.isDark('#ffffff')).toBe(false);
14+
expect(ColorUtils.isDark('#fff')).toBe(false);
15+
expect(ColorUtils.isDark('rgb(255,255,255)')).toBe(false);
16+
expect(ColorUtils.isDark('rgba(255,255,255,1)')).toBe(false);
17+
expect(ColorUtils.isDark('hsl(0,100%,100%)')).toBe(false);
18+
});
19+
20+
it('throws an error if the color is not a valid value', () => {
21+
expect(() => ColorUtils.isDark('')).toThrowError(/Invalid color received/);
22+
});
23+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { EventShimCustomEvent } from './EventTargetShimUtils';
2+
3+
it('should create an EventShimCustomEvent', () => {
4+
const event = new EventShimCustomEvent('test');
5+
expect(event.type).toBe('test');
6+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import InvalidMetadataError from './InvalidMetadataError';
2+
3+
it('should create an InvalidMetadataError object', () => {
4+
const error = new InvalidMetadataError();
5+
expect(error.isInvalidMetadata).toBe(true);
6+
});
Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
1+
import CanceledPromiseError from './CanceledPromiseError';
12
import { PromiseUtils } from './PromiseUtils';
3+
import TimeoutError from './TimeoutError';
24

3-
it('wraps promises properly', () => {
4-
const makeCancelable = () => {
5-
// Need to add a catch block or the tests crash.
6-
// https://github.com/facebook/jest/issues/5311
7-
const cancelable = PromiseUtils.makeCancelable(null);
8-
return cancelable;
9-
};
5+
describe('makeCancelable', () => {
6+
it('wraps promises properly', () => {
7+
const makeCancelable = () => {
8+
// Need to add a catch block or the tests crash.
9+
// https://github.com/facebook/jest/issues/5311
10+
const cancelable = PromiseUtils.makeCancelable(null);
11+
return cancelable;
12+
};
1013

11-
expect(typeof makeCancelable().cancel).toEqual('function');
14+
expect(typeof makeCancelable().cancel).toEqual('function');
15+
});
16+
});
17+
18+
describe('isCanceled', () => {
19+
it('returns true if the error is a CanceledPromiseError', () => {
20+
const error = new CanceledPromiseError();
21+
expect(PromiseUtils.isCanceled(error)).toBe(true);
22+
});
23+
24+
it('returns false if the error is not a CanceledPromiseError', () => {
25+
const error = new Error();
26+
expect(PromiseUtils.isCanceled(error)).toBe(false);
27+
});
28+
});
29+
30+
describe('isTimedOut', () => {
31+
it('returns true if the error is a TimeoutError', () => {
32+
const error = new TimeoutError();
33+
expect(PromiseUtils.isTimedOut(error)).toBe(true);
34+
});
35+
36+
it('returns false if the error is not a TimeoutError', () => {
37+
const error = new Error();
38+
expect(PromiseUtils.isTimedOut(error)).toBe(false);
39+
});
1240
});

packages/utils/src/RangeUtils.test.ts

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,164 @@ describe('getItemsInRanges', () => {
2323
).toEqual([1, 3, 4]);
2424
});
2525
});
26+
27+
describe('isValidRange', () => {
28+
it('returns true if the range is valid', () => {
29+
expect(RangeUtils.isValidRange([0, 10])).toBe(true);
30+
});
31+
32+
it('returns false if the range is invalid', () => {
33+
expect(RangeUtils.isValidRange([10, 0])).toBe(false);
34+
});
35+
});
36+
37+
describe('validateRange', () => {
38+
it('throws an error if a range is invalid', () => {
39+
expect(() => RangeUtils.validateRange([10, 0])).toThrowError(
40+
'Invalid range! 10,0'
41+
);
42+
});
43+
});
44+
45+
describe('isSelected', () => {
46+
it('returns true if the index is within the selected ranges', () => {
47+
expect(
48+
RangeUtils.isSelected(
49+
[
50+
[0, 2],
51+
[3, 4],
52+
],
53+
1
54+
)
55+
).toBe(true);
56+
});
57+
58+
it('returns false if the index is not within one of the selected ranges', () => {
59+
expect(
60+
RangeUtils.isSelected(
61+
[
62+
[0, 2],
63+
[3, 4],
64+
],
65+
5
66+
)
67+
).toBe(false);
68+
});
69+
});
70+
71+
describe('selectRange', () => {
72+
it('returns the same selected ranges if the given range is already within a range', () => {
73+
expect(
74+
RangeUtils.selectRange(
75+
[
76+
[0, 2],
77+
[3, 4],
78+
[5, 7],
79+
],
80+
[0, 1]
81+
)
82+
).toEqual([
83+
[0, 2],
84+
[3, 4],
85+
[5, 7],
86+
]);
87+
});
88+
89+
it('consolidates the previous ranges if they overlap', () => {
90+
expect(
91+
RangeUtils.selectRange(
92+
[
93+
[50, 60],
94+
[10, 20],
95+
[30, 40],
96+
],
97+
[15, 35]
98+
)
99+
).toEqual([
100+
[50, 60],
101+
[10, 40],
102+
]);
103+
});
104+
});
105+
106+
describe('deselectRanges', () => {
107+
it('returns the same selected ranges if the given range is not selected', () => {
108+
expect(
109+
RangeUtils.deselectRange(
110+
[
111+
[50, 60],
112+
[10, 20],
113+
[30, 40],
114+
],
115+
[0, 1]
116+
)
117+
).toEqual([
118+
[50, 60],
119+
[10, 20],
120+
[30, 40],
121+
]);
122+
});
123+
124+
it('splits a range if the given range is fully contained within another range', () => {
125+
expect(
126+
RangeUtils.deselectRange(
127+
[
128+
[50, 60],
129+
[10, 20],
130+
[30, 40],
131+
],
132+
[55, 57]
133+
)
134+
).toEqual([
135+
[50, 54],
136+
[58, 60],
137+
[10, 20],
138+
[30, 40],
139+
]);
140+
});
141+
142+
it('removes a range if the given range fully engulfs the range', () => {
143+
expect(
144+
RangeUtils.deselectRange(
145+
[
146+
[50, 60],
147+
[10, 20],
148+
[30, 40],
149+
],
150+
[5, 25]
151+
)
152+
).toEqual([
153+
[50, 60],
154+
[30, 40],
155+
]);
156+
});
157+
158+
it('trims a range if the given range overlaps the start or end of a range', () => {
159+
expect(
160+
RangeUtils.deselectRange(
161+
[
162+
[50, 60],
163+
[10, 20],
164+
[30, 40],
165+
],
166+
[15, 35]
167+
)
168+
).toEqual([
169+
[50, 60],
170+
[10, 14],
171+
[36, 40],
172+
]);
173+
});
174+
});
175+
176+
describe('count', () => {
177+
it('should return the total number of items in the given ranges', () => {
178+
expect(
179+
RangeUtils.count([
180+
[50, 60],
181+
[10, 20],
182+
[30, 40],
183+
])
184+
).toEqual(33);
185+
});
186+
});

0 commit comments

Comments
 (0)