@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,126 @@
|
|
|
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
|
+
|
|
27
|
+
import { render, screen } from '@testing-library/react'
|
|
28
|
+
import '@testing-library/jest-dom'
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
withDeterministicId,
|
|
32
|
+
DeterministicIdContextProvider,
|
|
33
|
+
generateInstanceCounterMap
|
|
34
|
+
} from '../DeterministicIdContext'
|
|
35
|
+
import type { WithDeterministicIdProps } from '../DeterministicIdContext'
|
|
36
|
+
|
|
37
|
+
@withDeterministicId()
|
|
38
|
+
class TestComponent extends React.Component<
|
|
39
|
+
React.PropsWithChildren<WithDeterministicIdProps>
|
|
40
|
+
> {
|
|
41
|
+
render() {
|
|
42
|
+
return (
|
|
43
|
+
<div data-testid="test-component" id={this.props.deterministicId!()}>
|
|
44
|
+
{this.props.children}
|
|
45
|
+
</div>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class WrapperComponent extends React.Component {
|
|
51
|
+
render() {
|
|
52
|
+
return (
|
|
53
|
+
<div>
|
|
54
|
+
<TestComponent />
|
|
55
|
+
</div>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const uniqueIds = (el: Element) => {
|
|
61
|
+
const idList = Array.from(el.children).map((child) => child.id)
|
|
62
|
+
return new Set(idList).size === idList.length
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
describe('DeterministicIdContext', () => {
|
|
66
|
+
it('can be found and tested with ReactTestUtils', () => {
|
|
67
|
+
render(<WrapperComponent />)
|
|
68
|
+
const testComponent = screen.getByTestId('test-component')
|
|
69
|
+
|
|
70
|
+
expect(testComponent).toBeInTheDocument()
|
|
71
|
+
expect(testComponent.id).toBeDefined()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('should generate unique ids without Provider wrapper', () => {
|
|
75
|
+
render(
|
|
76
|
+
<div data-testid="test-components">
|
|
77
|
+
<TestComponent />
|
|
78
|
+
<TestComponent />
|
|
79
|
+
<TestComponent />
|
|
80
|
+
<TestComponent />
|
|
81
|
+
<TestComponent />
|
|
82
|
+
</div>
|
|
83
|
+
)
|
|
84
|
+
const el = screen.getByTestId('test-components')
|
|
85
|
+
|
|
86
|
+
expect(uniqueIds(el)).toBe(true)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('should generate unique ids when components are rendered both out and inside of provider', () => {
|
|
90
|
+
render(
|
|
91
|
+
<div data-testid="test-components">
|
|
92
|
+
<DeterministicIdContextProvider>
|
|
93
|
+
<TestComponent />
|
|
94
|
+
<TestComponent />
|
|
95
|
+
<TestComponent />
|
|
96
|
+
</DeterministicIdContextProvider>
|
|
97
|
+
<TestComponent />
|
|
98
|
+
<TestComponent />
|
|
99
|
+
</div>
|
|
100
|
+
)
|
|
101
|
+
const el = screen.getByTestId('test-components')
|
|
102
|
+
|
|
103
|
+
expect(uniqueIds(el)).toBe(true)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('should generate unique ids with provider only', () => {
|
|
107
|
+
const Wrapper = ({ children }: any) => {
|
|
108
|
+
return (
|
|
109
|
+
<DeterministicIdContextProvider
|
|
110
|
+
instanceCounterMap={generateInstanceCounterMap()}
|
|
111
|
+
>
|
|
112
|
+
<div data-testid="wrapper">{children}</div>
|
|
113
|
+
</DeterministicIdContextProvider>
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
const children = []
|
|
117
|
+
for (let i = 0; i < 10; i++) {
|
|
118
|
+
children.push(<TestComponent key={i} />)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
render(<Wrapper>{children}</Wrapper>)
|
|
122
|
+
const el = screen.getByTestId('wrapper')
|
|
123
|
+
|
|
124
|
+
expect(uniqueIds(el)).toBe(true)
|
|
125
|
+
})
|
|
126
|
+
})
|
|
@@ -0,0 +1,136 @@
|
|
|
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 PropTypes from 'prop-types'
|
|
27
|
+
|
|
28
|
+
import { render } from '@testing-library/react'
|
|
29
|
+
import '@testing-library/jest-dom'
|
|
30
|
+
|
|
31
|
+
import { callRenderProp } from '../callRenderProp'
|
|
32
|
+
|
|
33
|
+
describe('callRenderProp', () => {
|
|
34
|
+
it('strings', () => {
|
|
35
|
+
expect(callRenderProp('foo')).toEqual('foo')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('numbers', () => {
|
|
39
|
+
expect(callRenderProp(2)).toEqual(2)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('arrays', () => {
|
|
43
|
+
const prop = ['foo', 'bar', 'baz']
|
|
44
|
+
|
|
45
|
+
expect(callRenderProp(prop)).toStrictEqual(prop)
|
|
46
|
+
expect(callRenderProp([])).toStrictEqual([])
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('booleans', () => {
|
|
50
|
+
expect(callRenderProp(false)).toEqual(false)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('JSX literals', () => {
|
|
54
|
+
const Foo = () => <div>hello</div>
|
|
55
|
+
expect(callRenderProp(<Foo />)).toStrictEqual(<Foo />)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('React classes', () => {
|
|
59
|
+
class Foo extends React.Component {
|
|
60
|
+
render() {
|
|
61
|
+
return <div>hello</div>
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const Result = callRenderProp(Foo)
|
|
66
|
+
expect(Result).toStrictEqual(<Foo />)
|
|
67
|
+
|
|
68
|
+
const { getByText } = render(Result)
|
|
69
|
+
expect(getByText('hello')).toBeInTheDocument()
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('functions', () => {
|
|
73
|
+
const Baz = function () {
|
|
74
|
+
return 'some text'
|
|
75
|
+
}
|
|
76
|
+
const result = callRenderProp(Baz)
|
|
77
|
+
|
|
78
|
+
const { getByText } = render(<div>{result}</div>)
|
|
79
|
+
|
|
80
|
+
expect(getByText('some text')).toBeInTheDocument()
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('fat arrow functions', () => {
|
|
84
|
+
const Baz = () => 'some text'
|
|
85
|
+
|
|
86
|
+
// in this test we are trying to test that it works with fat arrow functions,
|
|
87
|
+
// but the babel config when we run these tests is currently configured
|
|
88
|
+
// to transpile fat arrow functions down to normal functions.
|
|
89
|
+
// Real, untranspiled, fat-arrow functions don't have a `prototype` but when
|
|
90
|
+
// they are transpiled down they do. So this next line is to make sure
|
|
91
|
+
// the thing we are testing really doesn't have a prototype like it would
|
|
92
|
+
// if it was a real untranspiled fat arrow function.
|
|
93
|
+
if (Baz.prototype) Baz.prototype! = undefined
|
|
94
|
+
|
|
95
|
+
const result = callRenderProp(Baz)
|
|
96
|
+
|
|
97
|
+
const { getByText } = render(<div>{result}</div>)
|
|
98
|
+
|
|
99
|
+
expect(getByText('some text')).toBeInTheDocument()
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe('passing props', () => {
|
|
103
|
+
it('should pass props correctly to functions', () => {
|
|
104
|
+
const someFunc = ({ shape }: { shape: string }) => <div>{shape}</div>
|
|
105
|
+
|
|
106
|
+
const result = callRenderProp(someFunc, { shape: 'rectangle' })
|
|
107
|
+
|
|
108
|
+
const { getByText } = render(<div>{result}</div>)
|
|
109
|
+
|
|
110
|
+
expect(getByText('rectangle')).toBeInTheDocument()
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('should pass props correctly to React classes', () => {
|
|
114
|
+
type FooProps = { shape?: string }
|
|
115
|
+
class Foo extends React.Component<FooProps> {
|
|
116
|
+
static propTypes = {
|
|
117
|
+
shape: PropTypes.oneOf(['circle', 'rectangle'])
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static defaultProps = {
|
|
121
|
+
shape: 'circle'
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
render() {
|
|
125
|
+
return <div>{this.props.shape}</div>
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const result = callRenderProp(Foo, { shape: 'rectangle' })
|
|
130
|
+
|
|
131
|
+
const { getByText } = render(<div>{result}</div>)
|
|
132
|
+
|
|
133
|
+
expect(getByText('rectangle')).toBeInTheDocument()
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
})
|
|
@@ -0,0 +1,357 @@
|
|
|
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 { deprecated } from '../deprecated'
|
|
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: null,
|
|
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('@deprecated', () => {
|
|
58
|
+
describe('deprecated props', () => {
|
|
59
|
+
const DeprecatedComponent = deprecated('2.1.0', {
|
|
60
|
+
foo: 'bar',
|
|
61
|
+
baz: true
|
|
62
|
+
})(TestComponent)
|
|
63
|
+
|
|
64
|
+
it('should warn when suggesting new prop when using old prop', () => {
|
|
65
|
+
const consoleWarningSpy = jest
|
|
66
|
+
.spyOn(console, 'warn')
|
|
67
|
+
.mockImplementation(() => {})
|
|
68
|
+
|
|
69
|
+
render(<DeprecatedComponent foo="Jane" />)
|
|
70
|
+
|
|
71
|
+
const expectedWarningMessage =
|
|
72
|
+
'Warning: [TestComponent] `foo` is deprecated and will be removed in version 2.1.0. Use `bar` instead. '
|
|
73
|
+
|
|
74
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
75
|
+
expect.stringContaining(expectedWarningMessage),
|
|
76
|
+
expect.any(String)
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
consoleWarningSpy.mockRestore()
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('should warn when using old prop with no new prop', () => {
|
|
83
|
+
const consoleWarningSpy = jest
|
|
84
|
+
.spyOn(console, 'warn')
|
|
85
|
+
.mockImplementation(() => {})
|
|
86
|
+
|
|
87
|
+
render(<DeprecatedComponent baz="Goodbye" />)
|
|
88
|
+
|
|
89
|
+
const expectedWarningMessage =
|
|
90
|
+
'Warning: [TestComponent] `baz` is deprecated and will be removed in version 2.1.0.'
|
|
91
|
+
|
|
92
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
93
|
+
expect.stringContaining(expectedWarningMessage),
|
|
94
|
+
expect.any(String)
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
consoleWarningSpy.mockRestore()
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('should not output a warning using new prop', () => {
|
|
101
|
+
const consoleWarningSpy = jest
|
|
102
|
+
.spyOn(console, 'warn')
|
|
103
|
+
.mockImplementation(() => {})
|
|
104
|
+
|
|
105
|
+
render(<DeprecatedComponent bar="Jane" />)
|
|
106
|
+
|
|
107
|
+
expect(consoleWarningSpy).not.toHaveBeenCalled()
|
|
108
|
+
|
|
109
|
+
consoleWarningSpy.mockRestore()
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
describe('deprecated component', () => {
|
|
114
|
+
const DeprecatedComponent = deprecated('3.4.0')(TestComponent)
|
|
115
|
+
|
|
116
|
+
it('should warn that the entire component is deprecated if no old props are supplied', () => {
|
|
117
|
+
const consoleWarningSpy = jest
|
|
118
|
+
.spyOn(console, 'warn')
|
|
119
|
+
.mockImplementation(() => {})
|
|
120
|
+
|
|
121
|
+
render(<DeprecatedComponent />)
|
|
122
|
+
|
|
123
|
+
const expectedWarningMessage =
|
|
124
|
+
'Warning: [TestComponent] is deprecated and will be removed in version 3.4.0.'
|
|
125
|
+
|
|
126
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
127
|
+
expect.stringContaining(expectedWarningMessage),
|
|
128
|
+
expect.any(String)
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
consoleWarningSpy.mockRestore()
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
describe('deprecated component with a changed package message', () => {
|
|
136
|
+
const DeprecatedComponent = deprecated(
|
|
137
|
+
'5.0.0',
|
|
138
|
+
null,
|
|
139
|
+
deprecated.changedPackageWarning('ui-forms', 'ui-number-input')
|
|
140
|
+
)(TestComponent)
|
|
141
|
+
|
|
142
|
+
it('should warn that the component is deprecated and output a warning that the package changed', () => {
|
|
143
|
+
const consoleWarningSpy = jest
|
|
144
|
+
.spyOn(console, 'warn')
|
|
145
|
+
.mockImplementation(() => {})
|
|
146
|
+
|
|
147
|
+
const expectedWarningMessage =
|
|
148
|
+
'Warning: [TestComponent] is deprecated and will be removed in version 5.0.0. It has been moved from @instructure/ui-forms to @instructure/ui-number-input.'
|
|
149
|
+
|
|
150
|
+
render(<DeprecatedComponent />)
|
|
151
|
+
|
|
152
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
153
|
+
expect.stringContaining(expectedWarningMessage),
|
|
154
|
+
expect.any(String)
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
consoleWarningSpy.mockRestore()
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
describe('component with deprecated prop values', () => {
|
|
162
|
+
it('should not warn when an allowed prop value is supplied', () => {
|
|
163
|
+
const consoleWarningSpy = jest
|
|
164
|
+
.spyOn(console, 'warn')
|
|
165
|
+
.mockImplementation(() => {})
|
|
166
|
+
type DeprecatedPropValueComponentProps = {
|
|
167
|
+
color: string
|
|
168
|
+
}
|
|
169
|
+
class DeprecatedPropValueComponent extends Component<DeprecatedPropValueComponentProps> {
|
|
170
|
+
static propTypes = {
|
|
171
|
+
color: deprecated.deprecatePropValues(
|
|
172
|
+
PropTypes.oneOf(['red', 'yellow', 'blue', 'orange', 'gold']),
|
|
173
|
+
['blue', 'orange', 'gold']
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static defaultProps = {
|
|
178
|
+
color: 'red'
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
render() {
|
|
182
|
+
return <div>{this.props.color}</div>
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
render(<DeprecatedPropValueComponent color="yellow" />)
|
|
187
|
+
|
|
188
|
+
expect(consoleWarningSpy).not.toHaveBeenCalled()
|
|
189
|
+
|
|
190
|
+
consoleWarningSpy.mockRestore()
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it('should warn when a forbidden prop value is supplied', () => {
|
|
194
|
+
const consoleWarningSpy = jest
|
|
195
|
+
.spyOn(console, 'warn')
|
|
196
|
+
.mockImplementation(() => {})
|
|
197
|
+
|
|
198
|
+
const color = 'orange'
|
|
199
|
+
type DeprecatedPropValueComponentProps = {
|
|
200
|
+
color: string
|
|
201
|
+
}
|
|
202
|
+
class DeprecatedPropValueComponent extends Component<DeprecatedPropValueComponentProps> {
|
|
203
|
+
static propTypes = {
|
|
204
|
+
color: deprecated.deprecatePropValues(
|
|
205
|
+
PropTypes.oneOf(['red', 'yellow', 'blue', 'orange', 'gold']),
|
|
206
|
+
['blue', 'orange', 'gold']
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
static defaultProps = {
|
|
211
|
+
color: 'red'
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
render() {
|
|
215
|
+
return <div>{this.props.color}</div>
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
render(<DeprecatedPropValueComponent color={color} />)
|
|
220
|
+
|
|
221
|
+
const expectedWarningMessage = `The '${color}' value for the \`color\` prop is deprecated.`
|
|
222
|
+
|
|
223
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
224
|
+
expect.stringContaining(expectedWarningMessage),
|
|
225
|
+
expect.any(String)
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
consoleWarningSpy.mockRestore()
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
it('should warn with additional message text when a forbidden prop value is supplied and has message text', () => {
|
|
232
|
+
const consoleWarningSpy = jest
|
|
233
|
+
.spyOn(console, 'warn')
|
|
234
|
+
.mockImplementation(() => {})
|
|
235
|
+
|
|
236
|
+
const color = 'gold'
|
|
237
|
+
const message = 'It will be removed in v8.0.0.'
|
|
238
|
+
type DeprecatedPropValueComponentProps = {
|
|
239
|
+
color: string
|
|
240
|
+
}
|
|
241
|
+
class DeprecatedPropValueComponent extends Component<DeprecatedPropValueComponentProps> {
|
|
242
|
+
static propTypes = {
|
|
243
|
+
color: deprecated.deprecatePropValues(
|
|
244
|
+
PropTypes.oneOf(['red', 'yellow', 'blue', 'orange', 'gold']),
|
|
245
|
+
['blue', 'orange', 'gold'],
|
|
246
|
+
'It will be removed in v8.0.0.'
|
|
247
|
+
)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
static defaultProps = {
|
|
251
|
+
color: 'red'
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
render() {
|
|
255
|
+
return <div>{this.props.color}</div>
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
render(<DeprecatedPropValueComponent color={color} />)
|
|
260
|
+
|
|
261
|
+
const expectedWarningMessage = `The '${color}' value for the \`color\` prop is deprecated. ${message}`
|
|
262
|
+
|
|
263
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
264
|
+
expect.stringContaining(expectedWarningMessage),
|
|
265
|
+
expect.any(String)
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
consoleWarningSpy.mockRestore()
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
it('should call functional message with the correct props', () => {
|
|
272
|
+
const consoleWarningSpy = jest
|
|
273
|
+
.spyOn(console, 'warn')
|
|
274
|
+
.mockImplementation(() => {})
|
|
275
|
+
const messageMock = jest.fn()
|
|
276
|
+
|
|
277
|
+
const color = 'gold'
|
|
278
|
+
type DeprecatedPropValueComponentProps = {
|
|
279
|
+
color: string
|
|
280
|
+
}
|
|
281
|
+
class DeprecatedPropValueComponent extends Component<DeprecatedPropValueComponentProps> {
|
|
282
|
+
static propTypes = {
|
|
283
|
+
color: deprecated.deprecatePropValues(
|
|
284
|
+
PropTypes.oneOf(['red', 'yellow', 'blue', 'orange', 'gold']),
|
|
285
|
+
['blue', 'orange', 'gold'],
|
|
286
|
+
messageMock
|
|
287
|
+
)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
static defaultProps = {
|
|
291
|
+
color: 'red'
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
render() {
|
|
295
|
+
return <div>{this.props.color}</div>
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
render(<DeprecatedPropValueComponent color={color} />)
|
|
300
|
+
|
|
301
|
+
const { props, propName, propValue } = messageMock.mock.calls[0][0]
|
|
302
|
+
|
|
303
|
+
expect(props).toEqual({ color })
|
|
304
|
+
expect(propName).toBe('color')
|
|
305
|
+
expect(propValue).toBe(color)
|
|
306
|
+
expect(consoleWarningSpy).toHaveBeenCalled()
|
|
307
|
+
|
|
308
|
+
consoleWarningSpy.mockRestore()
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
it('should warn with a completely custom message when provided message is functional and prop value is forbidden', () => {
|
|
312
|
+
const consoleWarningSpy = jest
|
|
313
|
+
.spyOn(console, 'warn')
|
|
314
|
+
.mockImplementation(() => {})
|
|
315
|
+
|
|
316
|
+
const color = 'gold'
|
|
317
|
+
type DeprecatedPropValueComponentProps = {
|
|
318
|
+
color: string
|
|
319
|
+
}
|
|
320
|
+
class DeprecatedPropValueComponent extends Component<DeprecatedPropValueComponentProps> {
|
|
321
|
+
static propTypes = {
|
|
322
|
+
color: deprecated.deprecatePropValues(
|
|
323
|
+
PropTypes.oneOf(['red', 'yellow', 'blue', 'orange', 'gold']),
|
|
324
|
+
['blue', 'orange', 'gold'],
|
|
325
|
+
({
|
|
326
|
+
propValue,
|
|
327
|
+
propName
|
|
328
|
+
}: {
|
|
329
|
+
propValue: string
|
|
330
|
+
propName: string
|
|
331
|
+
}) =>
|
|
332
|
+
`The ${propValue} value for ${propName} has been deprecated. Use the FooBar component with the 'baz' prop set instead.`
|
|
333
|
+
)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
static defaultProps = {
|
|
337
|
+
color: 'red'
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
render() {
|
|
341
|
+
return <div>{this.props.color}</div>
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
render(<DeprecatedPropValueComponent color={color} />)
|
|
346
|
+
|
|
347
|
+
const expectedWarningMessage = `The ${color} value for color has been deprecated. Use the FooBar component with the 'baz' prop set instead.`
|
|
348
|
+
|
|
349
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
350
|
+
expect.stringContaining(expectedWarningMessage),
|
|
351
|
+
expect.any(String)
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
consoleWarningSpy.mockRestore()
|
|
355
|
+
})
|
|
356
|
+
})
|
|
357
|
+
})
|