@instructure/ui-react-utils 8.53.2 → 8.53.3-pr-snapshot-8
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.
- package/CHANGELOG.md +8 -0
- package/es/__new-tests__/ComponentIdentifier.test.js +77 -0
- package/es/__new-tests__/DeterministicIdContext.test.js +89 -0
- package/es/__new-tests__/callRenderProp.test.js +121 -0
- package/es/__new-tests__/deprecated.test.js +223 -0
- package/es/__new-tests__/experimental.test.js +94 -0
- package/es/__new-tests__/getInteraction.test.js +117 -0
- package/es/__new-tests__/hack.test.js +66 -0
- package/es/__new-tests__/omitProps.test.js +91 -0
- package/es/__new-tests__/passthroughProps.test.js +100 -0
- package/es/__new-tests__/pickProps.test.js +95 -0
- package/es/__new-tests__/safeCloneElement.test.js +86 -0
- package/lib/__new-tests__/ComponentIdentifier.test.js +80 -0
- package/lib/__new-tests__/DeterministicIdContext.test.js +91 -0
- package/lib/__new-tests__/callRenderProp.test.js +123 -0
- package/lib/__new-tests__/deprecated.test.js +227 -0
- package/lib/__new-tests__/experimental.test.js +98 -0
- package/lib/__new-tests__/getInteraction.test.js +119 -0
- package/lib/__new-tests__/hack.test.js +70 -0
- package/lib/__new-tests__/omitProps.test.js +94 -0
- package/lib/__new-tests__/passthroughProps.test.js +102 -0
- package/lib/__new-tests__/pickProps.test.js +98 -0
- package/lib/__new-tests__/safeCloneElement.test.js +89 -0
- package/package.json +9 -8
- package/src/__new-tests__/ComponentIdentifier.test.tsx +105 -0
- package/src/__new-tests__/DeterministicIdContext.test.tsx +126 -0
- package/src/__new-tests__/callRenderProp.test.tsx +136 -0
- package/src/__new-tests__/deprecated.test.tsx +357 -0
- package/src/__new-tests__/experimental.test.tsx +138 -0
- package/src/__new-tests__/getInteraction.test.tsx +119 -0
- package/src/__new-tests__/hack.test.tsx +90 -0
- package/src/__new-tests__/omitProps.test.tsx +113 -0
- package/src/__new-tests__/passthroughProps.test.tsx +100 -0
- package/src/__new-tests__/pickProps.test.tsx +111 -0
- package/src/__new-tests__/safeCloneElement.test.tsx +95 -0
- package/tsconfig.build.json +0 -3
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/__new-tests__/ComponentIdentifier.test.d.ts +2 -0
- package/types/__new-tests__/ComponentIdentifier.test.d.ts.map +1 -0
- package/types/__new-tests__/DeterministicIdContext.test.d.ts +2 -0
- package/types/__new-tests__/DeterministicIdContext.test.d.ts.map +1 -0
- package/types/__new-tests__/callRenderProp.test.d.ts +2 -0
- package/types/__new-tests__/callRenderProp.test.d.ts.map +1 -0
- package/types/__new-tests__/deprecated.test.d.ts +2 -0
- package/types/__new-tests__/deprecated.test.d.ts.map +1 -0
- package/types/__new-tests__/experimental.test.d.ts +2 -0
- package/types/__new-tests__/experimental.test.d.ts.map +1 -0
- package/types/__new-tests__/getInteraction.test.d.ts +2 -0
- package/types/__new-tests__/getInteraction.test.d.ts.map +1 -0
- package/types/__new-tests__/hack.test.d.ts +2 -0
- package/types/__new-tests__/hack.test.d.ts.map +1 -0
- package/types/__new-tests__/omitProps.test.d.ts +2 -0
- package/types/__new-tests__/omitProps.test.d.ts.map +1 -0
- package/types/__new-tests__/passthroughProps.test.d.ts +2 -0
- package/types/__new-tests__/passthroughProps.test.d.ts.map +1 -0
- package/types/__new-tests__/pickProps.test.d.ts +2 -0
- package/types/__new-tests__/pickProps.test.d.ts.map +1 -0
- package/types/__new-tests__/safeCloneElement.test.d.ts +2 -0
- package/types/__new-tests__/safeCloneElement.test.d.ts.map +1 -0
|
@@ -0,0 +1,138 @@
|
|
|
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, { Component } from 'react'
|
|
26
|
+
import PropTypes from 'prop-types'
|
|
27
|
+
|
|
28
|
+
import { render } from '@testing-library/react'
|
|
29
|
+
import '@testing-library/jest-dom'
|
|
30
|
+
|
|
31
|
+
import { experimental } from '../experimental'
|
|
32
|
+
|
|
33
|
+
type TestComponentProps = {
|
|
34
|
+
bar: string
|
|
35
|
+
qux: string
|
|
36
|
+
}
|
|
37
|
+
class TestComponent extends Component<TestComponentProps> {
|
|
38
|
+
static propTypes = {
|
|
39
|
+
bar: PropTypes.string,
|
|
40
|
+
qux: PropTypes.string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static defaultProps = {
|
|
44
|
+
bar: undefined,
|
|
45
|
+
qux: 'Hello'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
render() {
|
|
49
|
+
return (
|
|
50
|
+
<div>
|
|
51
|
+
{this.props.qux} {this.props.bar}
|
|
52
|
+
</div>
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
describe('@experimental', () => {
|
|
58
|
+
describe('experimental props', () => {
|
|
59
|
+
const ExperimentalComponent = experimental(['bar'])(TestComponent)
|
|
60
|
+
|
|
61
|
+
it('should warn when using an experimental prop', () => {
|
|
62
|
+
const consoleWarningSpy = jest
|
|
63
|
+
.spyOn(console, 'warn')
|
|
64
|
+
.mockImplementation(() => {})
|
|
65
|
+
render(<ExperimentalComponent bar="Jane" />)
|
|
66
|
+
|
|
67
|
+
const expectedWarningMessage =
|
|
68
|
+
'Warning: [TestComponent] The `bar` prop is experimental and its API could change significantly in a future release.'
|
|
69
|
+
|
|
70
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
71
|
+
expect.stringContaining(expectedWarningMessage),
|
|
72
|
+
expect.any(String)
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
consoleWarningSpy.mockRestore()
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('should not output a warning using a non-experimental prop', () => {
|
|
79
|
+
const consoleWarningSpy = jest
|
|
80
|
+
.spyOn(console, 'warn')
|
|
81
|
+
.mockImplementation(() => {})
|
|
82
|
+
render(<ExperimentalComponent qux="Jane" />)
|
|
83
|
+
|
|
84
|
+
expect(consoleWarningSpy).not.toHaveBeenCalled()
|
|
85
|
+
|
|
86
|
+
consoleWarningSpy.mockRestore()
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('should not output a warning for an experimental prop when dangerously ignored', () => {
|
|
90
|
+
const consoleWarningSpy = jest
|
|
91
|
+
.spyOn(console, 'warn')
|
|
92
|
+
.mockImplementation(() => {})
|
|
93
|
+
|
|
94
|
+
render(
|
|
95
|
+
<ExperimentalComponent
|
|
96
|
+
qux="Jane"
|
|
97
|
+
__dangerouslyIgnoreExperimentalWarnings
|
|
98
|
+
/>
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
expect(consoleWarningSpy).not.toHaveBeenCalled()
|
|
102
|
+
|
|
103
|
+
consoleWarningSpy.mockRestore()
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
describe('experimental component', () => {
|
|
108
|
+
const ExperimentalComponent = experimental()(TestComponent)
|
|
109
|
+
|
|
110
|
+
it('should warn that the entire component is experimental if no props are supplied', () => {
|
|
111
|
+
const consoleWarningSpy = jest
|
|
112
|
+
.spyOn(console, 'warn')
|
|
113
|
+
.mockImplementation(() => {})
|
|
114
|
+
render(<ExperimentalComponent />)
|
|
115
|
+
|
|
116
|
+
const expectedWarningMessage =
|
|
117
|
+
'Warning: [TestComponent] is experimental and its API could change significantly in a future release.'
|
|
118
|
+
|
|
119
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
120
|
+
expect.stringContaining(expectedWarningMessage),
|
|
121
|
+
expect.any(String)
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
consoleWarningSpy.mockRestore()
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('should not output a warning for a component when dangerously ignored', () => {
|
|
128
|
+
const consoleWarningSpy = jest
|
|
129
|
+
.spyOn(console, 'warn')
|
|
130
|
+
.mockImplementation(() => {})
|
|
131
|
+
render(<ExperimentalComponent __dangerouslyIgnoreExperimentalWarnings />)
|
|
132
|
+
|
|
133
|
+
expect(consoleWarningSpy).not.toHaveBeenCalled()
|
|
134
|
+
|
|
135
|
+
consoleWarningSpy.mockRestore()
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
})
|
|
@@ -0,0 +1,119 @@
|
|
|
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 { getInteraction, InteractionType } from '../getInteraction'
|
|
27
|
+
|
|
28
|
+
describe('getInteraction', () => {
|
|
29
|
+
it("should return 'enabled' when `interaction`, `disabled`, and `readOnly` are not provided", () => {
|
|
30
|
+
const props = {
|
|
31
|
+
foo: 'foo',
|
|
32
|
+
bar: 'bar'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
expect(getInteraction({ props })).toEqual('enabled')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should return `interaction` value when `interaction` is specified', () => {
|
|
39
|
+
const props: { interaction: InteractionType } = {
|
|
40
|
+
interaction: 'enabled'
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
expect(getInteraction({ props })).toEqual('enabled')
|
|
44
|
+
|
|
45
|
+
props.interaction = 'disabled'
|
|
46
|
+
|
|
47
|
+
expect(getInteraction({ props })).toEqual('disabled')
|
|
48
|
+
|
|
49
|
+
props.interaction = 'readonly'
|
|
50
|
+
|
|
51
|
+
expect(getInteraction({ props })).toEqual('readonly')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('should give preference to interaction even when disabled and readonly are also specified', () => {
|
|
55
|
+
const props: { interaction: InteractionType; [key: string]: any } = {
|
|
56
|
+
interaction: 'enabled',
|
|
57
|
+
disabled: true,
|
|
58
|
+
readOnly: true
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
expect(getInteraction({ props })).toEqual('enabled')
|
|
62
|
+
|
|
63
|
+
props.disabled = false
|
|
64
|
+
props.interaction = 'disabled'
|
|
65
|
+
|
|
66
|
+
expect(getInteraction({ props })).toEqual('disabled')
|
|
67
|
+
|
|
68
|
+
props.readOnly = false
|
|
69
|
+
props.interaction = 'readonly'
|
|
70
|
+
|
|
71
|
+
expect(getInteraction({ props })).toEqual('readonly')
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it("should return 'disabled' when `disabled` prop is set and `interaction` is not specified", () => {
|
|
75
|
+
const props = {
|
|
76
|
+
disabled: true
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
expect(getInteraction({ props })).toEqual('disabled')
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it("should return 'disabled' when both `disabled` and `readonly` props are set and `interaction` is not specified", () => {
|
|
83
|
+
const props = {
|
|
84
|
+
disabled: true,
|
|
85
|
+
readOnly: true
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
expect(getInteraction({ props })).toEqual('disabled')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it("should return 'readonly' when `readonly` prop is set and `interaction` and `disabled` are not specified", () => {
|
|
92
|
+
const props = {
|
|
93
|
+
disabled: true
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
expect(getInteraction({ props })).toEqual('disabled')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('should not include `disabled` if it is not listed in the interactionTypes', () => {
|
|
100
|
+
const props = {
|
|
101
|
+
disabled: true,
|
|
102
|
+
readOnly: true
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
expect(getInteraction({ props, interactionTypes: ['readonly'] })).toEqual(
|
|
106
|
+
'readonly'
|
|
107
|
+
)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('should not include `readonly` if it is not listed in the interactionTypes', () => {
|
|
111
|
+
const props = {
|
|
112
|
+
readOnly: true
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
expect(getInteraction({ props, interactionTypes: ['disabled'] })).toEqual(
|
|
116
|
+
'enabled'
|
|
117
|
+
)
|
|
118
|
+
})
|
|
119
|
+
})
|
|
@@ -0,0 +1,90 @@
|
|
|
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, { Component } from 'react'
|
|
26
|
+
import PropTypes from 'prop-types'
|
|
27
|
+
|
|
28
|
+
import { render } from '@testing-library/react'
|
|
29
|
+
import '@testing-library/jest-dom'
|
|
30
|
+
|
|
31
|
+
import { hack } from '../hack'
|
|
32
|
+
|
|
33
|
+
type TestComponentProps = {
|
|
34
|
+
bar: string
|
|
35
|
+
qux: string
|
|
36
|
+
}
|
|
37
|
+
class TestComponent extends Component<TestComponentProps> {
|
|
38
|
+
static propTypes = {
|
|
39
|
+
bar: PropTypes.string,
|
|
40
|
+
qux: PropTypes.string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static defaultProps = {
|
|
44
|
+
bar: undefined,
|
|
45
|
+
qux: 'Hello'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
render() {
|
|
49
|
+
return (
|
|
50
|
+
<div>
|
|
51
|
+
{this.props.qux} {this.props.bar}
|
|
52
|
+
</div>
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
describe('@hack', () => {
|
|
58
|
+
describe('hack props', () => {
|
|
59
|
+
const HackComponent = hack(['bar'])(TestComponent)
|
|
60
|
+
|
|
61
|
+
it('should warn when using an hack prop', () => {
|
|
62
|
+
const consoleWarningSpy = jest
|
|
63
|
+
.spyOn(console, 'warn')
|
|
64
|
+
.mockImplementation(() => {})
|
|
65
|
+
render(<HackComponent bar="Jane" />)
|
|
66
|
+
|
|
67
|
+
const expectedWarningMessage =
|
|
68
|
+
'Warning: [TestComponent] The `bar` prop is a temporary hack and will be removed in a future release.'
|
|
69
|
+
|
|
70
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
71
|
+
expect.stringContaining(expectedWarningMessage),
|
|
72
|
+
expect.any(String)
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
consoleWarningSpy.mockRestore()
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('should not output a warning using a non-hack prop', () => {
|
|
79
|
+
const consoleWarningSpy = jest
|
|
80
|
+
.spyOn(console, 'warn')
|
|
81
|
+
.mockImplementation(() => {})
|
|
82
|
+
|
|
83
|
+
render(<HackComponent qux="Jane" />)
|
|
84
|
+
|
|
85
|
+
expect(consoleWarningSpy).not.toHaveBeenCalled()
|
|
86
|
+
|
|
87
|
+
consoleWarningSpy.mockRestore()
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
})
|
|
@@ -0,0 +1,113 @@
|
|
|
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 PropTypes from 'prop-types'
|
|
26
|
+
import '@testing-library/jest-dom'
|
|
27
|
+
import { omitProps } from '../omitProps'
|
|
28
|
+
|
|
29
|
+
describe('omitProps', () => {
|
|
30
|
+
it('should work with PropTypes', () => {
|
|
31
|
+
const propTypes = {
|
|
32
|
+
prop1: PropTypes.string.isRequired,
|
|
33
|
+
prop2: PropTypes.number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const inputProps = {
|
|
37
|
+
prop1: 'hello',
|
|
38
|
+
prop2: 42,
|
|
39
|
+
excessiveProp: 'excessiveValue'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const expectedResult = {
|
|
43
|
+
excessiveProp: 'excessiveValue'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const actualResult = omitProps(inputProps, propTypes)
|
|
47
|
+
|
|
48
|
+
expect(actualResult).toEqual(expectedResult)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should work with PropTypes and exclude the given keys', () => {
|
|
52
|
+
const propTypes = {
|
|
53
|
+
prop1: PropTypes.string.isRequired,
|
|
54
|
+
prop2: PropTypes.number
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const inputProps = {
|
|
58
|
+
prop1: 'hello',
|
|
59
|
+
prop2: 42,
|
|
60
|
+
excessiveProp1: 'excessiveValue1',
|
|
61
|
+
excessiveProp2: 'excessiveValue2'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const expectedResult = {
|
|
65
|
+
excessiveProp2: 'excessiveValue2'
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const actualResult = omitProps(inputProps, propTypes, ['excessiveProp1'])
|
|
69
|
+
|
|
70
|
+
expect(actualResult).toEqual(expectedResult)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('should work with an input of a list allowed prop names', () => {
|
|
74
|
+
const allowedPropKeys = ['prop1', 'prop2']
|
|
75
|
+
|
|
76
|
+
const inputProps = {
|
|
77
|
+
prop1: 'hello',
|
|
78
|
+
prop2: 42,
|
|
79
|
+
excessiveProp1: 'excessiveValue1',
|
|
80
|
+
excessiveProp2: 'excessiveValue2'
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const expectedResult = {
|
|
84
|
+
excessiveProp1: 'excessiveValue1',
|
|
85
|
+
excessiveProp2: 'excessiveValue2'
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const actualResult = omitProps(inputProps, allowedPropKeys)
|
|
89
|
+
|
|
90
|
+
expect(actualResult).toEqual(expectedResult)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('should work with an input of a list allowed prop names and exclude the given keys', () => {
|
|
94
|
+
const allowedPropKeys = ['prop1', 'prop2']
|
|
95
|
+
|
|
96
|
+
const inputProps = {
|
|
97
|
+
prop1: 'hello',
|
|
98
|
+
prop2: 42,
|
|
99
|
+
excessiveProp1: 'excessiveValue1',
|
|
100
|
+
excessiveProp2: 'excessiveValue2'
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const expectedResult = {
|
|
104
|
+
excessiveProp2: 'excessiveValue2'
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const actualResult = omitProps(inputProps, allowedPropKeys, [
|
|
108
|
+
'excessiveProp1'
|
|
109
|
+
])
|
|
110
|
+
|
|
111
|
+
expect(actualResult).toEqual(expectedResult)
|
|
112
|
+
})
|
|
113
|
+
})
|
|
@@ -0,0 +1,100 @@
|
|
|
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 { passthroughProps } from '../passthroughProps'
|
|
27
|
+
|
|
28
|
+
describe('passthroughProps', () => {
|
|
29
|
+
it('should passthrough standard dom attributes', () => {
|
|
30
|
+
const props = {
|
|
31
|
+
id: 'myId',
|
|
32
|
+
title: 'My title'
|
|
33
|
+
}
|
|
34
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(2)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should passthrough data attributes', () => {
|
|
38
|
+
const props = {
|
|
39
|
+
'data-test': true
|
|
40
|
+
}
|
|
41
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(1)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('should passthrough valid react props', () => {
|
|
45
|
+
const props = {
|
|
46
|
+
ref: () => {},
|
|
47
|
+
innerHTML: '<span>hello world</span>'
|
|
48
|
+
}
|
|
49
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(2)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('should passthrough props prefixed with "on"', () => {
|
|
53
|
+
const props = {
|
|
54
|
+
onClick: () => {},
|
|
55
|
+
onCustomCallback: () => {},
|
|
56
|
+
onChange: () => {}
|
|
57
|
+
}
|
|
58
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(3)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('should omit invalid props', () => {
|
|
62
|
+
const props = {
|
|
63
|
+
myCustomProp: 'hello'
|
|
64
|
+
}
|
|
65
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(0)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('should omit certain react props', () => {
|
|
69
|
+
const props = {
|
|
70
|
+
style: { color: 'blue' },
|
|
71
|
+
className: 'myClass',
|
|
72
|
+
children: 'hello world',
|
|
73
|
+
theme: { themeVar: 'myColor' }
|
|
74
|
+
}
|
|
75
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(0)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('should passthrough and omit correct props', () => {
|
|
79
|
+
const props = {
|
|
80
|
+
id: 'myId',
|
|
81
|
+
title: 'My title',
|
|
82
|
+
'data-test': true,
|
|
83
|
+
className: 'myClassName',
|
|
84
|
+
children: 'hello world',
|
|
85
|
+
style: { color: 'blue' },
|
|
86
|
+
theme: { themeVar: 'myColor' },
|
|
87
|
+
withSomething: false,
|
|
88
|
+
onCustomCallback: null,
|
|
89
|
+
onChange: null
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
expect(passthroughProps(props)).toEqual({
|
|
93
|
+
id: 'myId',
|
|
94
|
+
title: 'My title',
|
|
95
|
+
'data-test': true,
|
|
96
|
+
onCustomCallback: null,
|
|
97
|
+
onChange: null
|
|
98
|
+
})
|
|
99
|
+
})
|
|
100
|
+
})
|
|
@@ -0,0 +1,111 @@
|
|
|
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 PropTypes from 'prop-types'
|
|
26
|
+
import '@testing-library/jest-dom'
|
|
27
|
+
import { pickProps } from '../pickProps'
|
|
28
|
+
|
|
29
|
+
describe('pickProps', () => {
|
|
30
|
+
it('should work with propTypes', () => {
|
|
31
|
+
const propTypes = {
|
|
32
|
+
prop1: PropTypes.string.isRequired,
|
|
33
|
+
prop2: PropTypes.number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const inputProps = {
|
|
37
|
+
prop1: 'hello',
|
|
38
|
+
prop2: 42,
|
|
39
|
+
excessiveProp: 'excessiveValue'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const expectedResult = {
|
|
43
|
+
prop1: 'hello',
|
|
44
|
+
prop2: 42
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const actualResult = pickProps(inputProps, propTypes)
|
|
48
|
+
|
|
49
|
+
expect(actualResult).toEqual(expectedResult)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('should add the `include` keys to the result with PropTypes', () => {
|
|
53
|
+
const propTypes = {
|
|
54
|
+
prop1: PropTypes.string.isRequired,
|
|
55
|
+
prop2: PropTypes.number
|
|
56
|
+
}
|
|
57
|
+
const inputProps = {
|
|
58
|
+
prop1: 'hello',
|
|
59
|
+
prop2: 42,
|
|
60
|
+
excessiveProp1: 'excessiveValue1',
|
|
61
|
+
excessiveProp2: 'excessiveValue2'
|
|
62
|
+
}
|
|
63
|
+
const expectedResult = {
|
|
64
|
+
prop1: 'hello',
|
|
65
|
+
prop2: 42,
|
|
66
|
+
excessiveProp2: 'excessiveValue2'
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const actualResult = pickProps(inputProps, propTypes, ['excessiveProp2'])
|
|
70
|
+
|
|
71
|
+
expect(actualResult).toEqual(expectedResult)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('should work with an input of a list allowed prop names', () => {
|
|
75
|
+
const allowedPropKeys = ['prop1', 'prop2']
|
|
76
|
+
const inputProps = {
|
|
77
|
+
prop1: 'hello',
|
|
78
|
+
prop2: 42,
|
|
79
|
+
excessiveProp1: 'excessiveValue1'
|
|
80
|
+
}
|
|
81
|
+
const expectedResult = {
|
|
82
|
+
prop1: 'hello',
|
|
83
|
+
prop2: 42
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const actualResult = pickProps(inputProps, allowedPropKeys)
|
|
87
|
+
|
|
88
|
+
expect(actualResult).toEqual(expectedResult)
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('should add the `include` keys to the result with a list allowed prop names', () => {
|
|
92
|
+
const allowedPropKeys = ['prop1', 'prop2']
|
|
93
|
+
const inputProps = {
|
|
94
|
+
prop1: 'hello',
|
|
95
|
+
prop2: 42,
|
|
96
|
+
excessiveProp1: 'excessiveValue1',
|
|
97
|
+
excessiveProp2: 'excessiveValue2'
|
|
98
|
+
}
|
|
99
|
+
const expectedResult = {
|
|
100
|
+
prop1: 'hello',
|
|
101
|
+
prop2: 42,
|
|
102
|
+
excessiveProp2: 'excessiveValue2'
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const actualResult = pickProps(inputProps, allowedPropKeys, [
|
|
106
|
+
'excessiveProp2'
|
|
107
|
+
])
|
|
108
|
+
|
|
109
|
+
expect(actualResult).toEqual(expectedResult)
|
|
110
|
+
})
|
|
111
|
+
})
|