@instructure/ui-dom-utils 8.47.0 → 8.47.1-snapshot-1

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 (69) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/es/__new-tests__/addEventListener.test.js +42 -0
  3. package/es/__new-tests__/addInputModeListener.test.js +52 -0
  4. package/es/__new-tests__/addPositionChangeListener.test.js +64 -0
  5. package/es/__new-tests__/addResizeListener.test.js +64 -0
  6. package/es/__new-tests__/findTabbable.test.js +66 -0
  7. package/es/__new-tests__/getClassList.test.js +46 -0
  8. package/es/__new-tests__/getFontSize.test.js +40 -0
  9. package/es/__new-tests__/getOffsetParents.test.js +107 -0
  10. package/es/__new-tests__/getScrollParents.test.js +87 -0
  11. package/es/__new-tests__/isCustomElement.test.js +41 -0
  12. package/es/__new-tests__/isVisible.test.js +111 -0
  13. package/es/__new-tests__/requestAnimationFrame.test.js +36 -0
  14. package/es/__new-tests__/transformSelection.test.js +67 -0
  15. package/lib/__new-tests__/addEventListener.test.js +44 -0
  16. package/lib/__new-tests__/addInputModeListener.test.js +54 -0
  17. package/lib/__new-tests__/addPositionChangeListener.test.js +66 -0
  18. package/lib/__new-tests__/addResizeListener.test.js +66 -0
  19. package/lib/__new-tests__/findTabbable.test.js +68 -0
  20. package/lib/__new-tests__/getClassList.test.js +48 -0
  21. package/lib/__new-tests__/getFontSize.test.js +43 -0
  22. package/lib/__new-tests__/getOffsetParents.test.js +109 -0
  23. package/lib/__new-tests__/getScrollParents.test.js +89 -0
  24. package/lib/__new-tests__/isCustomElement.test.js +43 -0
  25. package/lib/__new-tests__/isVisible.test.js +113 -0
  26. package/lib/__new-tests__/requestAnimationFrame.test.js +38 -0
  27. package/lib/__new-tests__/transformSelection.test.js +69 -0
  28. package/package.json +6 -5
  29. package/src/__new-tests__/addEventListener.test.tsx +46 -0
  30. package/src/__new-tests__/addInputModeListener.test.tsx +60 -0
  31. package/src/__new-tests__/addPositionChangeListener.test.tsx +65 -0
  32. package/src/__new-tests__/addResizeListener.test.tsx +65 -0
  33. package/src/__new-tests__/findTabbable.test.tsx +71 -0
  34. package/src/__new-tests__/getClassList.test.tsx +47 -0
  35. package/src/__new-tests__/getFontSize.test.tsx +37 -0
  36. package/src/__new-tests__/getOffsetParents.test.tsx +99 -0
  37. package/src/__new-tests__/getScrollParents.test.tsx +84 -0
  38. package/src/__new-tests__/isCustomElement.test.tsx +45 -0
  39. package/src/__new-tests__/isVisible.test.tsx +114 -0
  40. package/src/__new-tests__/requestAnimationFrame.test.tsx +38 -0
  41. package/src/__new-tests__/transformSelection.test.tsx +80 -0
  42. package/tsconfig.build.json +3 -12
  43. package/tsconfig.build.tsbuildinfo +1 -1
  44. package/types/__new-tests__/addEventListener.test.d.ts +2 -0
  45. package/types/__new-tests__/addEventListener.test.d.ts.map +1 -0
  46. package/types/__new-tests__/addInputModeListener.test.d.ts +2 -0
  47. package/types/__new-tests__/addInputModeListener.test.d.ts.map +1 -0
  48. package/types/__new-tests__/addPositionChangeListener.test.d.ts +2 -0
  49. package/types/__new-tests__/addPositionChangeListener.test.d.ts.map +1 -0
  50. package/types/__new-tests__/addResizeListener.test.d.ts +2 -0
  51. package/types/__new-tests__/addResizeListener.test.d.ts.map +1 -0
  52. package/types/__new-tests__/findTabbable.test.d.ts +2 -0
  53. package/types/__new-tests__/findTabbable.test.d.ts.map +1 -0
  54. package/types/__new-tests__/getClassList.test.d.ts +2 -0
  55. package/types/__new-tests__/getClassList.test.d.ts.map +1 -0
  56. package/types/__new-tests__/getFontSize.test.d.ts +2 -0
  57. package/types/__new-tests__/getFontSize.test.d.ts.map +1 -0
  58. package/types/__new-tests__/getOffsetParents.test.d.ts +2 -0
  59. package/types/__new-tests__/getOffsetParents.test.d.ts.map +1 -0
  60. package/types/__new-tests__/getScrollParents.test.d.ts +2 -0
  61. package/types/__new-tests__/getScrollParents.test.d.ts.map +1 -0
  62. package/types/__new-tests__/isCustomElement.test.d.ts +2 -0
  63. package/types/__new-tests__/isCustomElement.test.d.ts.map +1 -0
  64. package/types/__new-tests__/isVisible.test.d.ts +2 -0
  65. package/types/__new-tests__/isVisible.test.d.ts.map +1 -0
  66. package/types/__new-tests__/requestAnimationFrame.test.d.ts +2 -0
  67. package/types/__new-tests__/requestAnimationFrame.test.d.ts.map +1 -0
  68. package/types/__new-tests__/transformSelection.test.d.ts +2 -0
  69. package/types/__new-tests__/transformSelection.test.d.ts.map +1 -0
@@ -0,0 +1,65 @@
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 React from 'react'
26
+ import { render, waitFor } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+ import { addResizeListener } from '../addResizeListener'
29
+
30
+ const mockRect = {
31
+ top: 0,
32
+ left: 0,
33
+ right: 0,
34
+ bottom: 0,
35
+ width: 0,
36
+ height: 0,
37
+ x: 0,
38
+ y: 0,
39
+ toJSON: jest.fn()
40
+ }
41
+
42
+ describe('addResizeListener', () => {
43
+ it('should provide a remove method', async () => {
44
+ const callback = jest.fn()
45
+ Element.prototype.getBoundingClientRect = jest.fn(() => mockRect as DOMRect)
46
+
47
+ const { container } = render(<div />)
48
+ const node = container.firstChild as HTMLDivElement
49
+
50
+ const listener = addResizeListener(node, callback)
51
+
52
+ // Manually trigger a size change (since JSDOM won't)
53
+ Element.prototype.getBoundingClientRect = jest.fn(() => {
54
+ return { ...mockRect, width: 200 } as DOMRect
55
+ })
56
+
57
+ await waitFor(() => {
58
+ expect(callback).toHaveBeenCalledTimes(1)
59
+ expect(typeof listener.remove).toBe('function')
60
+ })
61
+
62
+ listener.remove()
63
+ jest.restoreAllMocks()
64
+ })
65
+ })
@@ -0,0 +1,71 @@
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 React from 'react'
26
+ import { render } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+ import { findTabbable } from '../findTabbable'
29
+
30
+ describe('findTabbable', () => {
31
+ describe('tabbable content', () => {
32
+ it('should find tabbable descendants', () => {
33
+ /* eslint-disable jsx-a11y/tabindex-no-positive */
34
+ /* eslint-disable jsx-a11y/no-noninteractive-tabindex */
35
+ const { container } = render(
36
+ <div>
37
+ <a href="https://instructure-test">Tabbable</a>
38
+ <div>Not Tabbable</div>
39
+ <div tabIndex={1}>Tabbable</div>
40
+ <input type="text" value="Tabbable" readOnly />
41
+ <div>
42
+ <button>Tabbable</button>
43
+ <button style={{ display: 'none' }}>Not Tabbable</button>
44
+ </div>
45
+ </div>
46
+ )
47
+
48
+ const node = container.firstChild as HTMLElement
49
+ expect(findTabbable(node).length).toBe(4)
50
+ })
51
+ })
52
+
53
+ describe('tabbable root', () => {
54
+ it('should search the root node when shouldSearchRootNode is set', () => {
55
+ const shouldSearchRootNode = true
56
+ const { container } = render(
57
+ <button>
58
+ <span>hello</span>
59
+ </button>
60
+ )
61
+
62
+ const node = container.firstChild as HTMLElement
63
+ expect(findTabbable(node).length).toBe(0)
64
+ expect(findTabbable(node, shouldSearchRootNode).length).toBe(1)
65
+ })
66
+ })
67
+
68
+ it('should gracefully handle null', () => {
69
+ expect(findTabbable(null).length).toBe(0)
70
+ })
71
+ })
@@ -0,0 +1,47 @@
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 React from 'react'
26
+ import { render } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+ import { getClassList } from '../getClassList'
29
+
30
+ describe('getClassList', () => {
31
+ it('should provide classlist methods', () => {
32
+ const { container } = render(<span className="foo bar baz">Test</span>)
33
+ const node = container.firstChild as HTMLElement
34
+
35
+ const classes = getClassList(node)
36
+
37
+ expect(classes.toArray().length).toBe(3)
38
+ expect(classes.contains('foo')).toBeTruthy()
39
+ expect(classes.contains('lorem')).toBeFalsy()
40
+
41
+ classes.add('lorem')
42
+ expect(classes.contains('lorem')).toBeTruthy()
43
+
44
+ classes.remove('lorem')
45
+ expect(classes.contains('lorem')).toBeFalsy()
46
+ })
47
+ })
@@ -0,0 +1,37 @@
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 React from 'react'
26
+ import { render } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+ import { getFontSize } from '../getFontSize'
29
+
30
+ describe('getFontSize', () => {
31
+ it('should return font size as a number', () => {
32
+ const { container } = render(<span style={{ fontSize: '17px' }}>Test</span>)
33
+ const node = container.firstChild
34
+
35
+ expect(getFontSize(node)).toBe(17)
36
+ })
37
+ })
@@ -0,0 +1,99 @@
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 React from 'react'
26
+ import { render } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+ import { getScrollParents } from '../getScrollParents'
29
+
30
+ describe('getScrollParents', () => {
31
+ const node = (
32
+ <div>
33
+ <div id="item-1">
34
+ <div id="item-2">
35
+ <span id="item-3">Test</span>
36
+ <span id="sibling-1">Test</span>
37
+ <span id="sibling-2">Test</span>
38
+ <span id="sibling-3">Test</span>
39
+ </div>
40
+ </div>
41
+ <div id="ht" style={{ height: '50px' }} />
42
+ <div id="scroll-parent" style={{ height: '200px', overflow: 'scroll' }}>
43
+ <div>
44
+ <div id="scroll-child" style={{ height: '500px' }}>
45
+ Test
46
+ </div>
47
+ </div>
48
+ </div>
49
+
50
+ <div
51
+ id="scroll-parent-rel"
52
+ style={{ height: '200px', overflow: 'scroll', position: 'relative' }}
53
+ >
54
+ <div style={{ height: '200px', overflow: 'scroll' }}>
55
+ <div
56
+ id="scroll-child-rel"
57
+ style={{ height: '500px', position: 'absolute' }}
58
+ >
59
+ Test
60
+ </div>
61
+ </div>
62
+ </div>
63
+
64
+ <div style={{ height: '200px', overflow: 'scroll' }}>
65
+ <div
66
+ id="scroll-child-fixed"
67
+ style={{ height: '500px', position: 'fixed' }}
68
+ >
69
+ Test
70
+ </div>
71
+ </div>
72
+ </div>
73
+ )
74
+
75
+ beforeEach(() => {
76
+ render(node)
77
+ })
78
+
79
+ it('should find scroll parent for inline elements', () => {
80
+ const child = document.getElementById('scroll-child')
81
+ const parent = document.getElementById('scroll-parent')
82
+
83
+ expect(getScrollParents(child)[0]).toBe(parent)
84
+ })
85
+
86
+ it('should ignore static parents when absolute', () => {
87
+ const child = document.getElementById('scroll-child-rel')
88
+ const parent = document.getElementById('scroll-parent-rel')
89
+
90
+ expect(getScrollParents(child)[0]).toBe(parent)
91
+ })
92
+
93
+ it('should handle fixed', () => {
94
+ const child = document.getElementById('scroll-child-fixed')
95
+ const scrollParent = getScrollParents(child)[0]
96
+
97
+ expect(scrollParent).toBe(document)
98
+ })
99
+ })
@@ -0,0 +1,84 @@
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 React from 'react'
26
+ import { render } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+ import { getOffsetParents } from '../getOffsetParents'
29
+
30
+ describe('getOffsetParents', () => {
31
+ // At rendering, we receive the node_wrapper and the body as extra offsetParents
32
+ const node = (
33
+ <div data-testid="node">
34
+ <div
35
+ data-testid="child_1_wrapper_parent"
36
+ style={{ transform: '200px', position: 'relative' }}
37
+ >
38
+ <div data-testid="child_1_wrapper">
39
+ <div data-testid="child-1" style={{ height: '500px' }}>
40
+ Test
41
+ </div>
42
+ </div>
43
+ </div>
44
+
45
+ <div
46
+ data-testid="child_2_wrapper_parent"
47
+ style={{ transform: '200px', position: 'absolute' }}
48
+ >
49
+ <div data-testid="child_2_wrapper" style={{ position: 'absolute' }}>
50
+ <div data-testid="child-2">Test</div>
51
+ </div>
52
+ </div>
53
+
54
+ <div data-testid="child_3_wrapper">
55
+ <div data-testid="child-3">Test</div>
56
+ </div>
57
+ </div>
58
+ )
59
+
60
+ it('should find offset parent for inline elements', () => {
61
+ const { getByTestId } = render(node)
62
+ const child = getByTestId('child-1')
63
+ const parent = getByTestId('child_1_wrapper_parent')
64
+ const offsetParents = getOffsetParents(child)
65
+
66
+ expect(offsetParents[1]).toBe(parent)
67
+ })
68
+
69
+ it('should ignore static parents when absolute', () => {
70
+ const { getByTestId } = render(node)
71
+ const child = getByTestId('child-2')
72
+ const offsetParents = getOffsetParents(child)
73
+
74
+ expect(offsetParents.length).toBe(5)
75
+ })
76
+
77
+ it('should handle fixed', () => {
78
+ const { getByTestId } = render(node)
79
+ const child = getByTestId('child-3')
80
+ const offsetParents = getOffsetParents(child)
81
+
82
+ expect(offsetParents.length).toBe(4)
83
+ })
84
+ })
@@ -0,0 +1,45 @@
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 { isDefinedCustomElement } from '../isDefinedCustomElement'
27
+
28
+ class TestElement extends HTMLElement {}
29
+
30
+ describe('isDefinedCustomElement', () => {
31
+ beforeAll(() => {
32
+ customElements.define('test-element', TestElement)
33
+ })
34
+
35
+ it('should return true for defined elements', () => {
36
+ const el = document.createElement('test-element')
37
+ expect(isDefinedCustomElement(el)).toBe(true)
38
+ })
39
+
40
+ it('should return false for not defined elements', () => {
41
+ // it is possible to create a custom element that is not defined
42
+ const el = document.createElement('my-element')
43
+ expect(isDefinedCustomElement(el)).toBe(false)
44
+ })
45
+ })
@@ -0,0 +1,114 @@
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 React from 'react'
26
+ import { render, screen } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+ import { isVisible } from '../isVisible'
29
+
30
+ describe('isVisible', () => {
31
+ it('should recognize visible elements', () => {
32
+ render(<div data-testid="test">Hello world!</div>)
33
+ const element = screen.getByTestId('test')
34
+
35
+ expect(isVisible(element)).toBe(true)
36
+ })
37
+
38
+ it('should recognize elements with display: none', () => {
39
+ render(
40
+ <div data-testid="test">
41
+ <span style={{ display: 'none' }}>Hello world!</span>
42
+ </div>
43
+ )
44
+ const element = screen.getByTestId('test').firstChild
45
+
46
+ expect(isVisible(element)).toBe(false)
47
+ })
48
+
49
+ it('should recognize elements hidden with clip', () => {
50
+ const style: React.CSSProperties = {
51
+ position: 'absolute',
52
+ overflow: 'hidden',
53
+ clip: 'rect(0,0,0,0)'
54
+ }
55
+ render(
56
+ <div data-testid="test">
57
+ <span style={style}>Hello world!</span>
58
+ </div>
59
+ )
60
+ const element = screen.getByTestId('test').firstChild
61
+
62
+ expect(isVisible(element)).toBe(false)
63
+ })
64
+
65
+ it('should recognize clipped elements that are not hidden', () => {
66
+ const style: React.CSSProperties = {
67
+ position: 'absolute',
68
+ overflow: 'hidden',
69
+ clip: 'rect(0,0,10px,0)'
70
+ }
71
+ render(
72
+ <div data-testid="test">
73
+ <span style={style}>Hello world!</span>
74
+ </div>
75
+ )
76
+ const element = screen.getByTestId('test').firstChild
77
+
78
+ expect(isVisible(element)).toBe(true)
79
+ })
80
+
81
+ it('should recursively check parent visibility', () => {
82
+ const style: React.CSSProperties = { visibility: 'hidden' }
83
+ render(
84
+ <div data-testid="test" style={style}>
85
+ <span>
86
+ <span data-testid="test-2" style={{ visibility: 'visible' }}>
87
+ Hello world!
88
+ </span>
89
+ </span>
90
+ </div>
91
+ )
92
+ const element = screen.getByTestId('test-2')
93
+
94
+ expect(isVisible(element, false)).toBe(true)
95
+ expect(isVisible(element)).toBe(false)
96
+ })
97
+
98
+ it('should not recursively check text nodes', () => {
99
+ const style: React.CSSProperties = { visibility: 'hidden' }
100
+ render(
101
+ <div data-testid="test" style={style}>
102
+ <span>
103
+ <span data-testid="test-2" style={{ visibility: 'visible' }}>
104
+ Hello world!
105
+ </span>
106
+ </span>
107
+ </div>
108
+ )
109
+ const element = screen.getByTestId('test-2').firstChild
110
+
111
+ expect(isVisible(element, false)).toBe(true)
112
+ expect(isVisible(element)).toBe(false)
113
+ })
114
+ })
@@ -0,0 +1,38 @@
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 { waitFor } from '@testing-library/react'
26
+ import { requestAnimationFrame } from '../requestAnimationFrame'
27
+
28
+ describe('requestAnimationFrame', () => {
29
+ it('should provide a cancel method', async () => {
30
+ const callback = jest.fn()
31
+ const raf = requestAnimationFrame(callback)
32
+
33
+ await waitFor(() => {
34
+ expect(callback).toHaveBeenCalledTimes(1)
35
+ expect(typeof raf.cancel).toBe('function')
36
+ })
37
+ })
38
+ })
@@ -0,0 +1,80 @@
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 { transformSelection, transformCursor } from '../transformSelection'
26
+
27
+ describe('transformSelection', () => {
28
+ it('should work with transformCursor', () => {
29
+ const paramObj = {
30
+ selectionStart: 2,
31
+ selectionEnd: 3,
32
+ selectionDirection: 'none',
33
+ value: '1x2345'
34
+ }
35
+
36
+ const result = transformSelection(paramObj as HTMLTextAreaElement, '12345')
37
+
38
+ expect(result).toEqual({
39
+ selectionStart: 1,
40
+ selectionEnd: 2,
41
+ selectionDirection: 'none'
42
+ })
43
+ })
44
+ })
45
+
46
+ describe('transformCursor', () => {
47
+ it('should retain cursor at the end', () => {
48
+ const dirtyValue = '12_xxxx_'
49
+ const cleanedValue = '12'
50
+ const cursorIndex = dirtyValue.length
51
+
52
+ expect(transformCursor(cursorIndex, dirtyValue, cleanedValue)).toBe(
53
+ cleanedValue.length
54
+ )
55
+ })
56
+
57
+ it('should retain cursor at the start', () => {
58
+ const dirtyValue = '12_xxxx_67'
59
+ const cleanedValue = '1267'
60
+ const cursorIndex = 0
61
+
62
+ expect(transformCursor(cursorIndex, dirtyValue, cleanedValue)).toBe(0)
63
+ })
64
+
65
+ it('should retain cursor between cleaned values', () => {
66
+ const dirtyValue = '12_xxxx_67'
67
+ const cleanedValue = '1267'
68
+ const cursorIndex = 6
69
+
70
+ expect(transformCursor(cursorIndex, dirtyValue, cleanedValue)).toBe(2)
71
+ })
72
+
73
+ it('should retain cursor after cleaned values', () => {
74
+ const dirtyValue = '12_xxxx_67'
75
+ const cleanedValue = '1267'
76
+ const cursorIndex = dirtyValue.length - 1
77
+
78
+ expect(transformCursor(cursorIndex, dirtyValue, cleanedValue)).toBe(3)
79
+ })
80
+ })