@instructure/ui-utils 8.52.1-snapshot-5 → 8.52.1-snapshot-7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/es/__new-tests__/camelize.test.js +40 -0
  3. package/es/__new-tests__/cloneArray.test.js +52 -0
  4. package/es/__new-tests__/createChainedFunction.test.js +50 -0
  5. package/es/__new-tests__/generateId.test.js +60 -0
  6. package/es/__new-tests__/hash.test.js +415 -0
  7. package/es/__new-tests__/mergeDeep.test.js +196 -0
  8. package/es/__new-tests__/ms.test.js +40 -0
  9. package/es/__new-tests__/parseUnit.test.js +69 -0
  10. package/es/__new-tests__/pascalize.test.js +48 -0
  11. package/es/__new-tests__/px.test.js +51 -0
  12. package/es/__new-tests__/within.test.js +41 -0
  13. package/lib/__new-tests__/camelize.test.js +43 -0
  14. package/lib/__new-tests__/cloneArray.test.js +55 -0
  15. package/lib/__new-tests__/createChainedFunction.test.js +52 -0
  16. package/lib/__new-tests__/generateId.test.js +63 -0
  17. package/lib/__new-tests__/hash.test.js +417 -0
  18. package/lib/__new-tests__/mergeDeep.test.js +198 -0
  19. package/lib/__new-tests__/ms.test.js +42 -0
  20. package/lib/__new-tests__/parseUnit.test.js +71 -0
  21. package/lib/__new-tests__/pascalize.test.js +50 -0
  22. package/lib/__new-tests__/px.test.js +53 -0
  23. package/lib/__new-tests__/within.test.js +43 -0
  24. package/package.json +6 -5
  25. package/src/__new-tests__/camelize.test.tsx +42 -0
  26. package/src/__new-tests__/cloneArray.test.tsx +62 -0
  27. package/src/__new-tests__/createChainedFunction.test.tsx +54 -0
  28. package/src/__new-tests__/generateId.test.tsx +69 -0
  29. package/src/__new-tests__/hash.test.tsx +547 -0
  30. package/src/__new-tests__/mergeDeep.test.tsx +133 -0
  31. package/src/__new-tests__/ms.test.tsx +44 -0
  32. package/src/__new-tests__/parseUnit.test.tsx +80 -0
  33. package/src/__new-tests__/pascalize.test.tsx +51 -0
  34. package/src/__new-tests__/px.test.tsx +59 -0
  35. package/src/__new-tests__/within.test.tsx +44 -0
  36. package/tsconfig.build.json +0 -3
  37. package/tsconfig.build.tsbuildinfo +1 -1
  38. package/types/__new-tests__/camelize.test.d.ts +2 -0
  39. package/types/__new-tests__/camelize.test.d.ts.map +1 -0
  40. package/types/__new-tests__/cloneArray.test.d.ts +2 -0
  41. package/types/__new-tests__/cloneArray.test.d.ts.map +1 -0
  42. package/types/__new-tests__/createChainedFunction.test.d.ts +2 -0
  43. package/types/__new-tests__/createChainedFunction.test.d.ts.map +1 -0
  44. package/types/__new-tests__/generateId.test.d.ts +2 -0
  45. package/types/__new-tests__/generateId.test.d.ts.map +1 -0
  46. package/types/__new-tests__/hash.test.d.ts +2 -0
  47. package/types/__new-tests__/hash.test.d.ts.map +1 -0
  48. package/types/__new-tests__/mergeDeep.test.d.ts +2 -0
  49. package/types/__new-tests__/mergeDeep.test.d.ts.map +1 -0
  50. package/types/__new-tests__/ms.test.d.ts +2 -0
  51. package/types/__new-tests__/ms.test.d.ts.map +1 -0
  52. package/types/__new-tests__/parseUnit.test.d.ts +2 -0
  53. package/types/__new-tests__/parseUnit.test.d.ts.map +1 -0
  54. package/types/__new-tests__/pascalize.test.d.ts +2 -0
  55. package/types/__new-tests__/pascalize.test.d.ts.map +1 -0
  56. package/types/__new-tests__/px.test.d.ts +2 -0
  57. package/types/__new-tests__/px.test.d.ts.map +1 -0
  58. package/types/__new-tests__/within.test.d.ts +2 -0
  59. package/types/__new-tests__/within.test.d.ts.map +1 -0
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+
3
+ require("@testing-library/jest-dom");
4
+ var _parseUnit = require("../parseUnit");
5
+ /*
6
+ * The MIT License (MIT)
7
+ *
8
+ * Copyright (c) 2015 - present Instructure, Inc.
9
+ *
10
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ * of this software and associated documentation files (the "Software"), to deal
12
+ * in the Software without restriction, including without limitation the rights
13
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ * copies of the Software, and to permit persons to whom the Software is
15
+ * furnished to do so, subject to the following conditions:
16
+ *
17
+ * The above copyright notice and this permission notice shall be included in all
18
+ * copies or substantial portions of the Software.
19
+ *
20
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ * SOFTWARE.
27
+ */
28
+
29
+ describe('parseUnit', () => {
30
+ it('unitless', () => {
31
+ expect((0, _parseUnit.parseUnit)('50')[0]).toEqual(50);
32
+ });
33
+ it('integer', () => {
34
+ expect((0, _parseUnit.parseUnit)(50)[0]).toEqual(50);
35
+ });
36
+ it('decimal', () => {
37
+ expect((0, _parseUnit.parseUnit)(47.89101)[0]).toEqual(47.89101);
38
+ });
39
+ it('negative', () => {
40
+ expect((0, _parseUnit.parseUnit)('-20px')[0]).toEqual(-20);
41
+ expect((0, _parseUnit.parseUnit)('-20px')[1]).toEqual('px');
42
+ });
43
+ it('px', () => {
44
+ expect((0, _parseUnit.parseUnit)('100.0792px')[0]).toEqual(100.0792);
45
+ expect((0, _parseUnit.parseUnit)('100.0792px')[1]).toEqual('px');
46
+ });
47
+ it('rem', () => {
48
+ expect((0, _parseUnit.parseUnit)('4000rem')[0]).toEqual(4000);
49
+ expect((0, _parseUnit.parseUnit)('4000rem')[1]).toEqual('rem');
50
+ });
51
+ it('em', () => {
52
+ expect((0, _parseUnit.parseUnit)('300em')[0]).toEqual(300);
53
+ expect((0, _parseUnit.parseUnit)('300em')[1]).toEqual('em');
54
+ });
55
+ it('s', () => {
56
+ expect((0, _parseUnit.parseUnit)('5s')[0]).toEqual(5);
57
+ expect((0, _parseUnit.parseUnit)('5s')[1]).toEqual('s');
58
+ });
59
+ it('ms', () => {
60
+ expect((0, _parseUnit.parseUnit)('20ms')[0]).toEqual(20);
61
+ expect((0, _parseUnit.parseUnit)('20ms')[1]).toEqual('ms');
62
+ });
63
+ it('vh', () => {
64
+ expect((0, _parseUnit.parseUnit)('327vh')[0]).toEqual(327);
65
+ expect((0, _parseUnit.parseUnit)('327vh')[1]).toEqual('vh');
66
+ });
67
+ it('vmin', () => {
68
+ expect((0, _parseUnit.parseUnit)('70vmin')[0]).toEqual(70);
69
+ expect((0, _parseUnit.parseUnit)('70vmin')[1]).toEqual('vmin');
70
+ });
71
+ });
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+
3
+ require("@testing-library/jest-dom");
4
+ var _pascalize = require("../pascalize");
5
+ /*
6
+ * The MIT License (MIT)
7
+ *
8
+ * Copyright (c) 2015 - present Instructure, Inc.
9
+ *
10
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ * of this software and associated documentation files (the "Software"), to deal
12
+ * in the Software without restriction, including without limitation the rights
13
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ * copies of the Software, and to permit persons to whom the Software is
15
+ * furnished to do so, subject to the following conditions:
16
+ *
17
+ * The above copyright notice and this permission notice shall be included in all
18
+ * copies or substantial portions of the Software.
19
+ *
20
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ * SOFTWARE.
27
+ */
28
+
29
+ describe('convertCase', () => {
30
+ describe('pascalize', () => {
31
+ it('handles hyphenated strings', () => {
32
+ expect((0, _pascalize.pascalize)('foo-bar')).toEqual('FooBar');
33
+ expect((0, _pascalize.pascalize)('baz-qux-foo')).toEqual('BazQuxFoo');
34
+ expect((0, _pascalize.pascalize)('x-large')).toEqual('XLarge');
35
+ expect((0, _pascalize.pascalize)('x-x-small')).toEqual('XXSmall');
36
+ });
37
+ it('handles camel cased strings', () => {
38
+ expect((0, _pascalize.pascalize)('fooBar')).toEqual('FooBar');
39
+ expect((0, _pascalize.pascalize)('bazQuxFoo')).toEqual('BazQuxFoo');
40
+ expect((0, _pascalize.pascalize)('xLarge')).toEqual('XLarge');
41
+ expect((0, _pascalize.pascalize)('borderRadiusLarge')).toEqual('BorderRadiusLarge');
42
+ });
43
+ it('does not modify already pascal cased strings', () => {
44
+ expect((0, _pascalize.pascalize)('FooBar')).toEqual('FooBar');
45
+ expect((0, _pascalize.pascalize)('BazQuxFoo')).toEqual('BazQuxFoo');
46
+ expect((0, _pascalize.pascalize)('XLarge')).toEqual('XLarge');
47
+ expect((0, _pascalize.pascalize)('BorderRadiusLarge')).toEqual('BorderRadiusLarge');
48
+ });
49
+ });
50
+ });
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ require("@testing-library/jest-dom");
4
+ var _getFontSize = require("@instructure/ui-dom-utils/lib/getFontSize.js");
5
+ var _px = require("../px");
6
+ /*
7
+ * The MIT License (MIT)
8
+ *
9
+ * Copyright (c) 2015 - present Instructure, Inc.
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ * of this software and associated documentation files (the "Software"), to deal
13
+ * in the Software without restriction, including without limitation the rights
14
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ * copies of the Software, and to permit persons to whom the Software is
16
+ * furnished to do so, subject to the following conditions:
17
+ *
18
+ * The above copyright notice and this permission notice shall be included in all
19
+ * copies or substantial portions of the Software.
20
+ *
21
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ * SOFTWARE.
28
+ */
29
+
30
+ describe('px', () => {
31
+ let node;
32
+ beforeEach(() => {
33
+ node = document.createElement('div');
34
+ document.body.appendChild(node);
35
+ });
36
+ afterEach(() => {
37
+ node && node.parentNode && node.parentNode.removeChild(node);
38
+ node = null;
39
+ });
40
+ it('handles px units', () => {
41
+ expect((0, _px.px)('30px')).toEqual(30);
42
+ });
43
+ it('converts rem to px', () => {
44
+ expect((0, _px.px)('50rem')).toEqual(50 * (0, _getFontSize.getFontSize)());
45
+ });
46
+ it('converts em to px', () => {
47
+ node.style.fontSize = '24px';
48
+ expect((0, _px.px)('10em', node)).toEqual(10 * (0, _getFontSize.getFontSize)(node));
49
+ });
50
+ it('handles unitless input', () => {
51
+ expect((0, _px.px)('4')).toEqual(4);
52
+ });
53
+ });
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ require("@testing-library/jest-dom");
4
+ var _within = require("../within");
5
+ /*
6
+ * The MIT License (MIT)
7
+ *
8
+ * Copyright (c) 2015 - present Instructure, Inc.
9
+ *
10
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ * of this software and associated documentation files (the "Software"), to deal
12
+ * in the Software without restriction, including without limitation the rights
13
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ * copies of the Software, and to permit persons to whom the Software is
15
+ * furnished to do so, subject to the following conditions:
16
+ *
17
+ * The above copyright notice and this permission notice shall be included in all
18
+ * copies or substantial portions of the Software.
19
+ *
20
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ * SOFTWARE.
27
+ */
28
+
29
+ describe('within', () => {
30
+ it('returns true when values are within range', () => {
31
+ expect((0, _within.within)(10, 8, 2)).toBe(true);
32
+ expect((0, _within.within)(10, 11, 1)).toBe(true);
33
+ expect((0, _within.within)(10, 10, 0)).toBe(true);
34
+ expect((0, _within.within)(10, 9.999, 0.001)).toBe(true);
35
+ expect((0, _within.within)(-10, -15, 5)).toBe(true);
36
+ });
37
+ it('returns false when values are out of range', () => {
38
+ expect((0, _within.within)(10, 8, 1)).toBe(false);
39
+ expect((0, _within.within)(12, 10, 1.9999)).toBe(false);
40
+ expect((0, _within.within)(8.705, 8.7, 0.004)).toBe(false);
41
+ expect((0, _within.within)(-2, -1.5, 0.4)).toBe(false);
42
+ });
43
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instructure/ui-utils",
3
- "version": "8.52.1-snapshot-5",
3
+ "version": "8.52.1-snapshot-7",
4
4
  "description": "A collection of utilities for UI components",
5
5
  "author": "Instructure, Inc. Engineering and Product Design",
6
6
  "module": "./es/index.js",
@@ -22,14 +22,15 @@
22
22
  },
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
- "@instructure/ui-babel-preset": "8.52.1-snapshot-5",
26
- "@instructure/ui-test-utils": "8.52.1-snapshot-5",
25
+ "@instructure/ui-babel-preset": "8.52.1-snapshot-7",
26
+ "@testing-library/jest-dom": "^6.1.4",
27
+ "@testing-library/react": "^14.0.0",
27
28
  "@types/json-stable-stringify": "^1.0.34"
28
29
  },
29
30
  "dependencies": {
30
31
  "@babel/runtime": "^7.23.2",
31
- "@instructure/console": "8.52.1-snapshot-5",
32
- "@instructure/ui-dom-utils": "8.52.1-snapshot-5",
32
+ "@instructure/console": "8.52.1-snapshot-7",
33
+ "@instructure/ui-dom-utils": "8.52.1-snapshot-7",
33
34
  "@types/ua-parser-js": "^0.7.37",
34
35
  "bowser": "^2.11.0",
35
36
  "fast-deep-equal": "^3.1.3",
@@ -0,0 +1,42 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+ import '@testing-library/jest-dom'
25
+ import { camelize } from '../camelize'
26
+
27
+ describe('convertCase', () => {
28
+ describe('camelize', () => {
29
+ it('handles hyphenated strings', () => {
30
+ expect(camelize('foo-bar')).toEqual('fooBar')
31
+ expect(camelize('baz-qux-foo')).toEqual('bazQuxFoo')
32
+ expect(camelize('xx-small')).toEqual('xxSmall')
33
+ expect(camelize('border-radius-x-large')).toEqual('borderRadiusXLarge')
34
+ expect(camelize('margin-xxLarge')).toEqual('marginXxLarge')
35
+ })
36
+
37
+ it('does not modify already camel cased strings', () => {
38
+ expect(camelize('fooBar')).toEqual('fooBar')
39
+ expect(camelize('bazQuxFoo')).toEqual('bazQuxFoo')
40
+ })
41
+ })
42
+ })
@@ -0,0 +1,62 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+ import '@testing-library/jest-dom'
25
+
26
+ import { cloneArray } from '../cloneArray'
27
+ import { deepEqual } from '../deepEqual'
28
+
29
+ describe('cloneArray', () => {
30
+ it('should return an array', () => {
31
+ const arr = [['one', 'two'], ['three']]
32
+
33
+ const newArr = cloneArray(arr)
34
+
35
+ expect(Array.isArray(newArr)).toBeTruthy()
36
+ })
37
+
38
+ it('should preserve sub arrays', () => {
39
+ const arr = [['one', 'two'], ['three'], [4, 5, 6], [7, [8, 9, 10], 11, 12]]
40
+
41
+ const newArr = cloneArray(arr)
42
+
43
+ expect(newArr.length).toEqual(4)
44
+ expect(newArr[0].length).toEqual(2)
45
+ expect(newArr[2][1]).toEqual(5)
46
+ expect(Array.isArray(newArr[3][1])).toEqual(true)
47
+ expect((newArr[3][1] as number[])[2]).toEqual(10)
48
+ })
49
+
50
+ it('should return a new array', () => {
51
+ const arr = [['one', 'two'], ['three']]
52
+ const newArr = cloneArray(arr)
53
+
54
+ expect(deepEqual(arr, newArr)).toEqual(true)
55
+ newArr[0][1] = '2'
56
+ expect(deepEqual(arr, newArr)).toEqual(false)
57
+
58
+ const newArr2 = cloneArray(arr)
59
+ arr[0][0] = '1'
60
+ expect(deepEqual(arr, newArr2)).toEqual(false)
61
+ })
62
+ })
@@ -0,0 +1,54 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import '@testing-library/jest-dom'
26
+ import { createChainedFunction } from '../createChainedFunction'
27
+
28
+ describe('createChainedFunction', () => {
29
+ it('should return null if no function provided', () => {
30
+ expect(createChainedFunction(null, undefined)).toEqual(null)
31
+ })
32
+
33
+ it('should return a function', () => {
34
+ expect(typeof createChainedFunction(() => {})).toEqual('function')
35
+ })
36
+
37
+ it('should throw an error if something other than function, null, undefined provided', () => {
38
+ expect(() => {
39
+ // @ts-expect-error intentionally bad code
40
+ createChainedFunction(12345)
41
+ }).toThrow(Error)
42
+ })
43
+
44
+ it('should execute all the functions', () => {
45
+ const spies = Array.from({ length: 5 }, () => jest.fn())
46
+ const chain = createChainedFunction(...spies)
47
+
48
+ chain!()
49
+
50
+ spies.forEach((spy) => {
51
+ expect(spy).toHaveBeenCalledTimes(1)
52
+ })
53
+ })
54
+ })
@@ -0,0 +1,69 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+ import '@testing-library/jest-dom'
25
+ import { generateId } from '../generateId'
26
+
27
+ describe('generateId', () => {
28
+ it('should generate unique ids for the same instance', () => {
29
+ const seed = new Map<string, number>()
30
+ const component = 'TestComponent'
31
+
32
+ const inst1 = generateId(component, seed)
33
+ const inst2 = generateId(component, seed)
34
+
35
+ expect(inst1).not.toEqual(inst2)
36
+ })
37
+
38
+ it('should create ids deterministicly', () => {
39
+ const seed1 = new Map<string, number>()
40
+ const component = 'TestComponent'
41
+
42
+ // Simulate rendering component: TestComponent
43
+ const render1 = generateId(component, seed1)
44
+
45
+ // Suppose we have a refresh, meaning the instanceCounter will be reseted:
46
+ const seed2 = new Map<string, number>()
47
+ const render2 = generateId(component, seed2)
48
+
49
+ expect(render1).toEqual(render2)
50
+ })
51
+
52
+ it('should not create the same id for the same instance.', () => {
53
+ const counter = new Map<string, boolean>()
54
+ const seed = new Map<string, number>()
55
+ const component = 'TestComponent'
56
+
57
+ for (let i = 0; i <= 20000; i++) {
58
+ const id = generateId(component, seed)
59
+
60
+ if (!counter.has(id)) {
61
+ counter.set(id, false)
62
+ } else {
63
+ counter.set(id, true)
64
+ }
65
+
66
+ expect(counter.get(id)).toBe(false)
67
+ }
68
+ })
69
+ })