@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,117 @@
|
|
|
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 } from '../getInteraction';
|
|
27
|
+
describe('getInteraction', () => {
|
|
28
|
+
it("should return 'enabled' when `interaction`, `disabled`, and `readOnly` are not provided", () => {
|
|
29
|
+
const props = {
|
|
30
|
+
foo: 'foo',
|
|
31
|
+
bar: 'bar'
|
|
32
|
+
};
|
|
33
|
+
expect(getInteraction({
|
|
34
|
+
props
|
|
35
|
+
})).toEqual('enabled');
|
|
36
|
+
});
|
|
37
|
+
it('should return `interaction` value when `interaction` is specified', () => {
|
|
38
|
+
const props = {
|
|
39
|
+
interaction: 'enabled'
|
|
40
|
+
};
|
|
41
|
+
expect(getInteraction({
|
|
42
|
+
props
|
|
43
|
+
})).toEqual('enabled');
|
|
44
|
+
props.interaction = 'disabled';
|
|
45
|
+
expect(getInteraction({
|
|
46
|
+
props
|
|
47
|
+
})).toEqual('disabled');
|
|
48
|
+
props.interaction = 'readonly';
|
|
49
|
+
expect(getInteraction({
|
|
50
|
+
props
|
|
51
|
+
})).toEqual('readonly');
|
|
52
|
+
});
|
|
53
|
+
it('should give preference to interaction even when disabled and readonly are also specified', () => {
|
|
54
|
+
const props = {
|
|
55
|
+
interaction: 'enabled',
|
|
56
|
+
disabled: true,
|
|
57
|
+
readOnly: true
|
|
58
|
+
};
|
|
59
|
+
expect(getInteraction({
|
|
60
|
+
props
|
|
61
|
+
})).toEqual('enabled');
|
|
62
|
+
props.disabled = false;
|
|
63
|
+
props.interaction = 'disabled';
|
|
64
|
+
expect(getInteraction({
|
|
65
|
+
props
|
|
66
|
+
})).toEqual('disabled');
|
|
67
|
+
props.readOnly = false;
|
|
68
|
+
props.interaction = 'readonly';
|
|
69
|
+
expect(getInteraction({
|
|
70
|
+
props
|
|
71
|
+
})).toEqual('readonly');
|
|
72
|
+
});
|
|
73
|
+
it("should return 'disabled' when `disabled` prop is set and `interaction` is not specified", () => {
|
|
74
|
+
const props = {
|
|
75
|
+
disabled: true
|
|
76
|
+
};
|
|
77
|
+
expect(getInteraction({
|
|
78
|
+
props
|
|
79
|
+
})).toEqual('disabled');
|
|
80
|
+
});
|
|
81
|
+
it("should return 'disabled' when both `disabled` and `readonly` props are set and `interaction` is not specified", () => {
|
|
82
|
+
const props = {
|
|
83
|
+
disabled: true,
|
|
84
|
+
readOnly: true
|
|
85
|
+
};
|
|
86
|
+
expect(getInteraction({
|
|
87
|
+
props
|
|
88
|
+
})).toEqual('disabled');
|
|
89
|
+
});
|
|
90
|
+
it("should return 'readonly' when `readonly` prop is set and `interaction` and `disabled` are not specified", () => {
|
|
91
|
+
const props = {
|
|
92
|
+
disabled: true
|
|
93
|
+
};
|
|
94
|
+
expect(getInteraction({
|
|
95
|
+
props
|
|
96
|
+
})).toEqual('disabled');
|
|
97
|
+
});
|
|
98
|
+
it('should not include `disabled` if it is not listed in the interactionTypes', () => {
|
|
99
|
+
const props = {
|
|
100
|
+
disabled: true,
|
|
101
|
+
readOnly: true
|
|
102
|
+
};
|
|
103
|
+
expect(getInteraction({
|
|
104
|
+
props,
|
|
105
|
+
interactionTypes: ['readonly']
|
|
106
|
+
})).toEqual('readonly');
|
|
107
|
+
});
|
|
108
|
+
it('should not include `readonly` if it is not listed in the interactionTypes', () => {
|
|
109
|
+
const props = {
|
|
110
|
+
readOnly: true
|
|
111
|
+
};
|
|
112
|
+
expect(getInteraction({
|
|
113
|
+
props,
|
|
114
|
+
interactionTypes: ['disabled']
|
|
115
|
+
})).toEqual('enabled');
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
import { render } from '@testing-library/react';
|
|
28
|
+
import '@testing-library/jest-dom';
|
|
29
|
+
import { hack } from '../hack';
|
|
30
|
+
class TestComponent extends Component {
|
|
31
|
+
render() {
|
|
32
|
+
return /*#__PURE__*/React.createElement("div", null, this.props.qux, " ", this.props.bar);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
TestComponent.displayName = "TestComponent";
|
|
36
|
+
TestComponent.propTypes = {
|
|
37
|
+
bar: PropTypes.string,
|
|
38
|
+
qux: PropTypes.string
|
|
39
|
+
};
|
|
40
|
+
TestComponent.defaultProps = {
|
|
41
|
+
bar: void 0,
|
|
42
|
+
qux: 'Hello'
|
|
43
|
+
};
|
|
44
|
+
describe('@hack', () => {
|
|
45
|
+
describe('hack props', () => {
|
|
46
|
+
var _HackComponent, _HackComponent2;
|
|
47
|
+
const HackComponent = hack(['bar'])(TestComponent);
|
|
48
|
+
it('should warn when using an hack prop', () => {
|
|
49
|
+
const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
50
|
+
render(_HackComponent || (_HackComponent = /*#__PURE__*/React.createElement(HackComponent, {
|
|
51
|
+
bar: "Jane"
|
|
52
|
+
})));
|
|
53
|
+
const expectedWarningMessage = 'Warning: [TestComponent] The `bar` prop is a temporary hack and will be removed in a future release.';
|
|
54
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
|
|
55
|
+
consoleWarningSpy.mockRestore();
|
|
56
|
+
});
|
|
57
|
+
it('should not output a warning using a non-hack prop', () => {
|
|
58
|
+
const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
59
|
+
render(_HackComponent2 || (_HackComponent2 = /*#__PURE__*/React.createElement(HackComponent, {
|
|
60
|
+
qux: "Jane"
|
|
61
|
+
})));
|
|
62
|
+
expect(consoleWarningSpy).not.toHaveBeenCalled();
|
|
63
|
+
consoleWarningSpy.mockRestore();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
describe('omitProps', () => {
|
|
29
|
+
it('should work with PropTypes', () => {
|
|
30
|
+
const propTypes = {
|
|
31
|
+
prop1: PropTypes.string.isRequired,
|
|
32
|
+
prop2: PropTypes.number
|
|
33
|
+
};
|
|
34
|
+
const inputProps = {
|
|
35
|
+
prop1: 'hello',
|
|
36
|
+
prop2: 42,
|
|
37
|
+
excessiveProp: 'excessiveValue'
|
|
38
|
+
};
|
|
39
|
+
const expectedResult = {
|
|
40
|
+
excessiveProp: 'excessiveValue'
|
|
41
|
+
};
|
|
42
|
+
const actualResult = omitProps(inputProps, propTypes);
|
|
43
|
+
expect(actualResult).toEqual(expectedResult);
|
|
44
|
+
});
|
|
45
|
+
it('should work with PropTypes and exclude the given keys', () => {
|
|
46
|
+
const propTypes = {
|
|
47
|
+
prop1: PropTypes.string.isRequired,
|
|
48
|
+
prop2: PropTypes.number
|
|
49
|
+
};
|
|
50
|
+
const inputProps = {
|
|
51
|
+
prop1: 'hello',
|
|
52
|
+
prop2: 42,
|
|
53
|
+
excessiveProp1: 'excessiveValue1',
|
|
54
|
+
excessiveProp2: 'excessiveValue2'
|
|
55
|
+
};
|
|
56
|
+
const expectedResult = {
|
|
57
|
+
excessiveProp2: 'excessiveValue2'
|
|
58
|
+
};
|
|
59
|
+
const actualResult = omitProps(inputProps, propTypes, ['excessiveProp1']);
|
|
60
|
+
expect(actualResult).toEqual(expectedResult);
|
|
61
|
+
});
|
|
62
|
+
it('should work with an input of a list allowed prop names', () => {
|
|
63
|
+
const allowedPropKeys = ['prop1', 'prop2'];
|
|
64
|
+
const inputProps = {
|
|
65
|
+
prop1: 'hello',
|
|
66
|
+
prop2: 42,
|
|
67
|
+
excessiveProp1: 'excessiveValue1',
|
|
68
|
+
excessiveProp2: 'excessiveValue2'
|
|
69
|
+
};
|
|
70
|
+
const expectedResult = {
|
|
71
|
+
excessiveProp1: 'excessiveValue1',
|
|
72
|
+
excessiveProp2: 'excessiveValue2'
|
|
73
|
+
};
|
|
74
|
+
const actualResult = omitProps(inputProps, allowedPropKeys);
|
|
75
|
+
expect(actualResult).toEqual(expectedResult);
|
|
76
|
+
});
|
|
77
|
+
it('should work with an input of a list allowed prop names and exclude the given keys', () => {
|
|
78
|
+
const allowedPropKeys = ['prop1', 'prop2'];
|
|
79
|
+
const inputProps = {
|
|
80
|
+
prop1: 'hello',
|
|
81
|
+
prop2: 42,
|
|
82
|
+
excessiveProp1: 'excessiveValue1',
|
|
83
|
+
excessiveProp2: 'excessiveValue2'
|
|
84
|
+
};
|
|
85
|
+
const expectedResult = {
|
|
86
|
+
excessiveProp2: 'excessiveValue2'
|
|
87
|
+
};
|
|
88
|
+
const actualResult = omitProps(inputProps, allowedPropKeys, ['excessiveProp1']);
|
|
89
|
+
expect(actualResult).toEqual(expectedResult);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -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
|
+
describe('passthroughProps', () => {
|
|
28
|
+
it('should passthrough standard dom attributes', () => {
|
|
29
|
+
const props = {
|
|
30
|
+
id: 'myId',
|
|
31
|
+
title: 'My title'
|
|
32
|
+
};
|
|
33
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(2);
|
|
34
|
+
});
|
|
35
|
+
it('should passthrough data attributes', () => {
|
|
36
|
+
const props = {
|
|
37
|
+
'data-test': true
|
|
38
|
+
};
|
|
39
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(1);
|
|
40
|
+
});
|
|
41
|
+
it('should passthrough valid react props', () => {
|
|
42
|
+
const props = {
|
|
43
|
+
ref: () => {},
|
|
44
|
+
innerHTML: '<span>hello world</span>'
|
|
45
|
+
};
|
|
46
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(2);
|
|
47
|
+
});
|
|
48
|
+
it('should passthrough props prefixed with "on"', () => {
|
|
49
|
+
const props = {
|
|
50
|
+
onClick: () => {},
|
|
51
|
+
onCustomCallback: () => {},
|
|
52
|
+
onChange: () => {}
|
|
53
|
+
};
|
|
54
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(3);
|
|
55
|
+
});
|
|
56
|
+
it('should omit invalid props', () => {
|
|
57
|
+
const props = {
|
|
58
|
+
myCustomProp: 'hello'
|
|
59
|
+
};
|
|
60
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(0);
|
|
61
|
+
});
|
|
62
|
+
it('should omit certain react props', () => {
|
|
63
|
+
const props = {
|
|
64
|
+
style: {
|
|
65
|
+
color: 'blue'
|
|
66
|
+
},
|
|
67
|
+
className: 'myClass',
|
|
68
|
+
children: 'hello world',
|
|
69
|
+
theme: {
|
|
70
|
+
themeVar: 'myColor'
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
expect(Object.keys(passthroughProps(props)).length).toEqual(0);
|
|
74
|
+
});
|
|
75
|
+
it('should passthrough and omit correct props', () => {
|
|
76
|
+
const props = {
|
|
77
|
+
id: 'myId',
|
|
78
|
+
title: 'My title',
|
|
79
|
+
'data-test': true,
|
|
80
|
+
className: 'myClassName',
|
|
81
|
+
children: 'hello world',
|
|
82
|
+
style: {
|
|
83
|
+
color: 'blue'
|
|
84
|
+
},
|
|
85
|
+
theme: {
|
|
86
|
+
themeVar: 'myColor'
|
|
87
|
+
},
|
|
88
|
+
withSomething: false,
|
|
89
|
+
onCustomCallback: null,
|
|
90
|
+
onChange: null
|
|
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,95 @@
|
|
|
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
|
+
describe('pickProps', () => {
|
|
29
|
+
it('should work with propTypes', () => {
|
|
30
|
+
const propTypes = {
|
|
31
|
+
prop1: PropTypes.string.isRequired,
|
|
32
|
+
prop2: PropTypes.number
|
|
33
|
+
};
|
|
34
|
+
const inputProps = {
|
|
35
|
+
prop1: 'hello',
|
|
36
|
+
prop2: 42,
|
|
37
|
+
excessiveProp: 'excessiveValue'
|
|
38
|
+
};
|
|
39
|
+
const expectedResult = {
|
|
40
|
+
prop1: 'hello',
|
|
41
|
+
prop2: 42
|
|
42
|
+
};
|
|
43
|
+
const actualResult = pickProps(inputProps, propTypes);
|
|
44
|
+
expect(actualResult).toEqual(expectedResult);
|
|
45
|
+
});
|
|
46
|
+
it('should add the `include` keys to the result with PropTypes', () => {
|
|
47
|
+
const propTypes = {
|
|
48
|
+
prop1: PropTypes.string.isRequired,
|
|
49
|
+
prop2: PropTypes.number
|
|
50
|
+
};
|
|
51
|
+
const inputProps = {
|
|
52
|
+
prop1: 'hello',
|
|
53
|
+
prop2: 42,
|
|
54
|
+
excessiveProp1: 'excessiveValue1',
|
|
55
|
+
excessiveProp2: 'excessiveValue2'
|
|
56
|
+
};
|
|
57
|
+
const expectedResult = {
|
|
58
|
+
prop1: 'hello',
|
|
59
|
+
prop2: 42,
|
|
60
|
+
excessiveProp2: 'excessiveValue2'
|
|
61
|
+
};
|
|
62
|
+
const actualResult = pickProps(inputProps, propTypes, ['excessiveProp2']);
|
|
63
|
+
expect(actualResult).toEqual(expectedResult);
|
|
64
|
+
});
|
|
65
|
+
it('should work with an input of a list allowed prop names', () => {
|
|
66
|
+
const allowedPropKeys = ['prop1', 'prop2'];
|
|
67
|
+
const inputProps = {
|
|
68
|
+
prop1: 'hello',
|
|
69
|
+
prop2: 42,
|
|
70
|
+
excessiveProp1: 'excessiveValue1'
|
|
71
|
+
};
|
|
72
|
+
const expectedResult = {
|
|
73
|
+
prop1: 'hello',
|
|
74
|
+
prop2: 42
|
|
75
|
+
};
|
|
76
|
+
const actualResult = pickProps(inputProps, allowedPropKeys);
|
|
77
|
+
expect(actualResult).toEqual(expectedResult);
|
|
78
|
+
});
|
|
79
|
+
it('should add the `include` keys to the result with a list allowed prop names', () => {
|
|
80
|
+
const allowedPropKeys = ['prop1', 'prop2'];
|
|
81
|
+
const inputProps = {
|
|
82
|
+
prop1: 'hello',
|
|
83
|
+
prop2: 42,
|
|
84
|
+
excessiveProp1: 'excessiveValue1',
|
|
85
|
+
excessiveProp2: 'excessiveValue2'
|
|
86
|
+
};
|
|
87
|
+
const expectedResult = {
|
|
88
|
+
prop1: 'hello',
|
|
89
|
+
prop2: 42,
|
|
90
|
+
excessiveProp2: 'excessiveValue2'
|
|
91
|
+
};
|
|
92
|
+
const actualResult = pickProps(inputProps, allowedPropKeys, ['excessiveProp2']);
|
|
93
|
+
expect(actualResult).toEqual(expectedResult);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
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 { createChainedFunction } from '@instructure/ui-utils';
|
|
27
|
+
import { render, screen } from '@testing-library/react';
|
|
28
|
+
import '@testing-library/jest-dom';
|
|
29
|
+
import { safeCloneElement } from '../safeCloneElement';
|
|
30
|
+
describe('safeCloneElement', () => {
|
|
31
|
+
const SafeClone = function ({
|
|
32
|
+
element,
|
|
33
|
+
props,
|
|
34
|
+
children
|
|
35
|
+
}) {
|
|
36
|
+
return safeCloneElement(element, props, children);
|
|
37
|
+
};
|
|
38
|
+
it('should preserve refs', () => {
|
|
39
|
+
const origRef = jest.fn();
|
|
40
|
+
const cloneRef = jest.fn();
|
|
41
|
+
render( /*#__PURE__*/React.createElement(SafeClone, {
|
|
42
|
+
element: /*#__PURE__*/React.createElement("div", {
|
|
43
|
+
ref: origRef
|
|
44
|
+
}),
|
|
45
|
+
props: {
|
|
46
|
+
ref: cloneRef
|
|
47
|
+
}
|
|
48
|
+
}));
|
|
49
|
+
expect(origRef).toHaveBeenCalled();
|
|
50
|
+
expect(cloneRef).toHaveBeenCalled();
|
|
51
|
+
});
|
|
52
|
+
it('should preserve event handlers', () => {
|
|
53
|
+
const onClickA = jest.fn();
|
|
54
|
+
const onClickB = jest.fn();
|
|
55
|
+
render( /*#__PURE__*/React.createElement(SafeClone, {
|
|
56
|
+
element: /*#__PURE__*/React.createElement("button", {
|
|
57
|
+
onClick: onClickA
|
|
58
|
+
}),
|
|
59
|
+
props: {
|
|
60
|
+
onClick: onClickB
|
|
61
|
+
}
|
|
62
|
+
}));
|
|
63
|
+
const button = screen.getByRole('button');
|
|
64
|
+
button.click();
|
|
65
|
+
expect(onClickA).toHaveBeenCalled();
|
|
66
|
+
expect(onClickB).toHaveBeenCalled();
|
|
67
|
+
});
|
|
68
|
+
it('should preserve already chained functions', () => {
|
|
69
|
+
const onClickA = jest.fn();
|
|
70
|
+
const onClickB = jest.fn();
|
|
71
|
+
const onClickC = jest.fn();
|
|
72
|
+
render( /*#__PURE__*/React.createElement(SafeClone, {
|
|
73
|
+
element: /*#__PURE__*/React.createElement("button", {
|
|
74
|
+
onClick: onClickA
|
|
75
|
+
}),
|
|
76
|
+
props: {
|
|
77
|
+
onClick: createChainedFunction(onClickB, onClickC)
|
|
78
|
+
}
|
|
79
|
+
}));
|
|
80
|
+
const button = screen.getByRole('button');
|
|
81
|
+
button.click();
|
|
82
|
+
expect(onClickA).toHaveBeenCalled();
|
|
83
|
+
expect(onClickB).toHaveBeenCalled();
|
|
84
|
+
expect(onClickC).toHaveBeenCalled();
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
5
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
6
|
+
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
7
|
+
var _react2 = require("@testing-library/react");
|
|
8
|
+
require("@testing-library/jest-dom");
|
|
9
|
+
var _ComponentIdentifier = require("../ComponentIdentifier");
|
|
10
|
+
var _h;
|
|
11
|
+
/*
|
|
12
|
+
* The MIT License (MIT)
|
|
13
|
+
*
|
|
14
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
15
|
+
*
|
|
16
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
17
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
18
|
+
* in the Software without restriction, including without limitation the rights
|
|
19
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
20
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
21
|
+
* furnished to do so, subject to the following conditions:
|
|
22
|
+
*
|
|
23
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
24
|
+
* copies or substantial portions of the Software.
|
|
25
|
+
*
|
|
26
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
27
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
28
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
29
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
30
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
31
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
32
|
+
* SOFTWARE.
|
|
33
|
+
*/
|
|
34
|
+
describe('ComponentIdentifier', () => {
|
|
35
|
+
var _App;
|
|
36
|
+
beforeAll(() => {
|
|
37
|
+
// Mocking console warnings to prevent test output pollution
|
|
38
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
39
|
+
});
|
|
40
|
+
afterAll(() => {
|
|
41
|
+
jest.restoreAllMocks();
|
|
42
|
+
});
|
|
43
|
+
class Trigger extends _ComponentIdentifier.ComponentIdentifier {}
|
|
44
|
+
Trigger.displayName = 'Trigger';
|
|
45
|
+
class App extends _react.Component {
|
|
46
|
+
render() {
|
|
47
|
+
const trigger = _ComponentIdentifier.ComponentIdentifier.pick(Trigger, this.props.children);
|
|
48
|
+
return /*#__PURE__*/_react.default.createElement("div", null, _h || (_h = /*#__PURE__*/_react.default.createElement("h2", null, "Trigger")), trigger);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
App.displayName = "App";
|
|
52
|
+
App.propTypes = {
|
|
53
|
+
children: _propTypes.default.node
|
|
54
|
+
};
|
|
55
|
+
App.defaultProps = {
|
|
56
|
+
children: null
|
|
57
|
+
};
|
|
58
|
+
it('should render only child', () => {
|
|
59
|
+
let buttonRef;
|
|
60
|
+
(0, _react2.render)( /*#__PURE__*/_react.default.createElement(App, null, /*#__PURE__*/_react.default.createElement(Trigger, null, /*#__PURE__*/_react.default.createElement("button", {
|
|
61
|
+
ref: el => buttonRef = el
|
|
62
|
+
}, "Click Me"))));
|
|
63
|
+
expect(buttonRef.textContent).toEqual('Click Me');
|
|
64
|
+
});
|
|
65
|
+
it('should not error when no children provided', () => {
|
|
66
|
+
const renderApp = () => (0, _react2.render)(_App || (_App = /*#__PURE__*/_react.default.createElement(App, null, /*#__PURE__*/_react.default.createElement(Trigger, null))));
|
|
67
|
+
expect(renderApp).not.toThrow();
|
|
68
|
+
});
|
|
69
|
+
it('should pass props', () => {
|
|
70
|
+
let buttonRef;
|
|
71
|
+
const onClick = jest.fn();
|
|
72
|
+
(0, _react2.render)( /*#__PURE__*/_react.default.createElement(App, null, /*#__PURE__*/_react.default.createElement(Trigger, {
|
|
73
|
+
onClick: onClick
|
|
74
|
+
}, /*#__PURE__*/_react.default.createElement("button", {
|
|
75
|
+
ref: el => buttonRef = el
|
|
76
|
+
}, "Click Me"))));
|
|
77
|
+
buttonRef.click();
|
|
78
|
+
expect(onClick).toHaveBeenCalled();
|
|
79
|
+
});
|
|
80
|
+
});
|