|
5 | 5 | * Public License, v. 2.0 that can be found in the LICENSE file. |
6 | 6 | */ |
7 | 7 |
|
8 | | -import { describe, expect, it } from 'vitest'; |
9 | | -import { capitalize, groupBy, randInt, splitAt, toHex } from '.'; |
| 8 | +import { describe, expect, it, vi } from 'vitest'; |
| 9 | +import { assert, capitalize, debounce, groupBy, isClass, randInt, sleep, splitAt, toHex, uid } from '.'; |
10 | 10 |
|
11 | 11 | describe('The common utils', () => { |
12 | 12 | it.each([ |
@@ -83,4 +83,58 @@ describe('The common utils', () => { |
83 | 83 | expect(randInt(1, 5)).toBeLessThanOrEqual(5); |
84 | 84 | } |
85 | 85 | }); |
| 86 | + |
| 87 | + it('should assert a condition (assert)', () => { |
| 88 | + expect(() => assert(1)).not.toThrowError(); |
| 89 | + expect(() => assert(0)).toThrowError(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should debounce a method (debunce)', async () => { |
| 93 | + const fn = vi.fn(); |
| 94 | + |
| 95 | + const debouncedFn = debounce(() => fn(performance.now()), 10); |
| 96 | + |
| 97 | + debouncedFn(); |
| 98 | + debouncedFn(); |
| 99 | + |
| 100 | + await sleep(20); |
| 101 | + |
| 102 | + expect(fn).toBeCalledTimes(1); |
| 103 | + |
| 104 | + debouncedFn(); |
| 105 | + await sleep(11); |
| 106 | + debouncedFn(); |
| 107 | + |
| 108 | + await sleep(20); |
| 109 | + |
| 110 | + expect(fn).toBeCalledTimes(3); |
| 111 | + |
| 112 | + expect(fn.mock.calls[0][0]).toBeTypeOf('number'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should be able to detect classes (isClass)', () => { |
| 116 | + expect(isClass(5)).toBe(false); |
| 117 | + expect(isClass(() => false)).toBe(false); |
| 118 | + expect(isClass('')).toBe(false); |
| 119 | + expect(isClass(new class { }())).toBe(false); |
| 120 | + expect(isClass(class { })).toBe(true); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should be able to capitalize strings (capitalize)', () => { |
| 124 | + expect(capitalize('hello World')).toBe('Hello World'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should be able to generate short uids (uid)', () => { |
| 128 | + expect(uid()).toBeTypeOf('string'); |
| 129 | + expect(uid()?.length).toBe(8); |
| 130 | + expect(uid()).not.toBe(uid()); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should not sleep if the number is less than 0 (sleep)', async () => { |
| 134 | + const now = performance.now(); |
| 135 | + |
| 136 | + await sleep(-10); |
| 137 | + |
| 138 | + expect(performance.now() -now).toBeLessThan(1); |
| 139 | + }); |
86 | 140 | }); |
0 commit comments