@instructure/ui-react-utils 8.53.3-snapshot-6 → 8.53.3-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 (59) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/es/__new-tests__/ComponentIdentifier.test.js +77 -0
  3. package/es/__new-tests__/DeterministicIdContext.test.js +89 -0
  4. package/es/__new-tests__/callRenderProp.test.js +121 -0
  5. package/es/__new-tests__/deprecated.test.js +223 -0
  6. package/es/__new-tests__/experimental.test.js +94 -0
  7. package/es/__new-tests__/getInteraction.test.js +117 -0
  8. package/es/__new-tests__/hack.test.js +66 -0
  9. package/es/__new-tests__/omitProps.test.js +91 -0
  10. package/es/__new-tests__/passthroughProps.test.js +100 -0
  11. package/es/__new-tests__/pickProps.test.js +95 -0
  12. package/es/__new-tests__/safeCloneElement.test.js +86 -0
  13. package/lib/__new-tests__/ComponentIdentifier.test.js +80 -0
  14. package/lib/__new-tests__/DeterministicIdContext.test.js +91 -0
  15. package/lib/__new-tests__/callRenderProp.test.js +123 -0
  16. package/lib/__new-tests__/deprecated.test.js +227 -0
  17. package/lib/__new-tests__/experimental.test.js +98 -0
  18. package/lib/__new-tests__/getInteraction.test.js +119 -0
  19. package/lib/__new-tests__/hack.test.js +70 -0
  20. package/lib/__new-tests__/omitProps.test.js +94 -0
  21. package/lib/__new-tests__/passthroughProps.test.js +102 -0
  22. package/lib/__new-tests__/pickProps.test.js +98 -0
  23. package/lib/__new-tests__/safeCloneElement.test.js +89 -0
  24. package/package.json +9 -8
  25. package/src/__new-tests__/ComponentIdentifier.test.tsx +105 -0
  26. package/src/__new-tests__/DeterministicIdContext.test.tsx +126 -0
  27. package/src/__new-tests__/callRenderProp.test.tsx +136 -0
  28. package/src/__new-tests__/deprecated.test.tsx +357 -0
  29. package/src/__new-tests__/experimental.test.tsx +138 -0
  30. package/src/__new-tests__/getInteraction.test.tsx +119 -0
  31. package/src/__new-tests__/hack.test.tsx +90 -0
  32. package/src/__new-tests__/omitProps.test.tsx +113 -0
  33. package/src/__new-tests__/passthroughProps.test.tsx +100 -0
  34. package/src/__new-tests__/pickProps.test.tsx +111 -0
  35. package/src/__new-tests__/safeCloneElement.test.tsx +95 -0
  36. package/tsconfig.build.json +0 -3
  37. package/tsconfig.build.tsbuildinfo +1 -1
  38. package/types/__new-tests__/ComponentIdentifier.test.d.ts +2 -0
  39. package/types/__new-tests__/ComponentIdentifier.test.d.ts.map +1 -0
  40. package/types/__new-tests__/DeterministicIdContext.test.d.ts +2 -0
  41. package/types/__new-tests__/DeterministicIdContext.test.d.ts.map +1 -0
  42. package/types/__new-tests__/callRenderProp.test.d.ts +2 -0
  43. package/types/__new-tests__/callRenderProp.test.d.ts.map +1 -0
  44. package/types/__new-tests__/deprecated.test.d.ts +2 -0
  45. package/types/__new-tests__/deprecated.test.d.ts.map +1 -0
  46. package/types/__new-tests__/experimental.test.d.ts +2 -0
  47. package/types/__new-tests__/experimental.test.d.ts.map +1 -0
  48. package/types/__new-tests__/getInteraction.test.d.ts +2 -0
  49. package/types/__new-tests__/getInteraction.test.d.ts.map +1 -0
  50. package/types/__new-tests__/hack.test.d.ts +2 -0
  51. package/types/__new-tests__/hack.test.d.ts.map +1 -0
  52. package/types/__new-tests__/omitProps.test.d.ts +2 -0
  53. package/types/__new-tests__/omitProps.test.d.ts.map +1 -0
  54. package/types/__new-tests__/passthroughProps.test.d.ts +2 -0
  55. package/types/__new-tests__/passthroughProps.test.d.ts.map +1 -0
  56. package/types/__new-tests__/pickProps.test.d.ts +2 -0
  57. package/types/__new-tests__/pickProps.test.d.ts.map +1 -0
  58. package/types/__new-tests__/safeCloneElement.test.d.ts +2 -0
  59. package/types/__new-tests__/safeCloneElement.test.d.ts.map +1 -0
package/CHANGELOG.md CHANGED
@@ -3,7 +3,7 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ## [8.53.3-snapshot-6](https://github.com/instructure/instructure-ui/compare/v8.53.2...v8.53.3-snapshot-6) (2024-02-20)
6
+ ## [8.53.3-snapshot-1](https://github.com/instructure/instructure-ui/compare/v8.53.2...v8.53.3-snapshot-1) (2024-02-20)
7
7
 
8
8
  **Note:** Version bump only for package @instructure/ui-react-utils
9
9
 
@@ -0,0 +1,77 @@
1
+ var _h;
2
+ /*
3
+ * The MIT License (MIT)
4
+ *
5
+ * Copyright (c) 2015 - present Instructure, Inc.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ import React, { Component } from 'react';
27
+ import PropTypes from 'prop-types';
28
+ import { render } from '@testing-library/react';
29
+ import '@testing-library/jest-dom';
30
+ import { ComponentIdentifier } from '../ComponentIdentifier';
31
+ describe('ComponentIdentifier', () => {
32
+ var _App;
33
+ beforeAll(() => {
34
+ // Mocking console warnings to prevent test output pollution
35
+ jest.spyOn(console, 'warn').mockImplementation(() => {});
36
+ });
37
+ afterAll(() => {
38
+ jest.restoreAllMocks();
39
+ });
40
+ class Trigger extends ComponentIdentifier {}
41
+ Trigger.displayName = 'Trigger';
42
+ class App extends Component {
43
+ render() {
44
+ const trigger = ComponentIdentifier.pick(Trigger, this.props.children);
45
+ return /*#__PURE__*/React.createElement("div", null, _h || (_h = /*#__PURE__*/React.createElement("h2", null, "Trigger")), trigger);
46
+ }
47
+ }
48
+ App.displayName = "App";
49
+ App.propTypes = {
50
+ children: PropTypes.node
51
+ };
52
+ App.defaultProps = {
53
+ children: null
54
+ };
55
+ it('should render only child', () => {
56
+ let buttonRef;
57
+ render( /*#__PURE__*/React.createElement(App, null, /*#__PURE__*/React.createElement(Trigger, null, /*#__PURE__*/React.createElement("button", {
58
+ ref: el => buttonRef = el
59
+ }, "Click Me"))));
60
+ expect(buttonRef.textContent).toEqual('Click Me');
61
+ });
62
+ it('should not error when no children provided', () => {
63
+ const renderApp = () => render(_App || (_App = /*#__PURE__*/React.createElement(App, null, /*#__PURE__*/React.createElement(Trigger, null))));
64
+ expect(renderApp).not.toThrow();
65
+ });
66
+ it('should pass props', () => {
67
+ let buttonRef;
68
+ const onClick = jest.fn();
69
+ render( /*#__PURE__*/React.createElement(App, null, /*#__PURE__*/React.createElement(Trigger, {
70
+ onClick: onClick
71
+ }, /*#__PURE__*/React.createElement("button", {
72
+ ref: el => buttonRef = el
73
+ }, "Click Me"))));
74
+ buttonRef.click();
75
+ expect(onClick).toHaveBeenCalled();
76
+ });
77
+ });
@@ -0,0 +1,89 @@
1
+ var _dec, _class, _class2, _div, _WrapperComponent, _div2, _div3;
2
+ /*
3
+ * The MIT License (MIT)
4
+ *
5
+ * Copyright (c) 2015 - present Instructure, Inc.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ import React from 'react';
27
+ import { render, screen } from '@testing-library/react';
28
+ import '@testing-library/jest-dom';
29
+ import { withDeterministicId, DeterministicIdContextProvider, generateInstanceCounterMap } from '../DeterministicIdContext';
30
+ let TestComponent = (_dec = withDeterministicId(), _dec(_class = (_class2 = class TestComponent extends React.Component {
31
+ render() {
32
+ return /*#__PURE__*/React.createElement("div", {
33
+ "data-testid": "test-component",
34
+ id: this.props.deterministicId()
35
+ }, this.props.children);
36
+ }
37
+ }, _class2.displayName = "TestComponent", _class2)) || _class);
38
+ class WrapperComponent extends React.Component {
39
+ render() {
40
+ return _div || (_div = /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(TestComponent, null)));
41
+ }
42
+ }
43
+ WrapperComponent.displayName = "WrapperComponent";
44
+ const uniqueIds = el => {
45
+ const idList = Array.from(el.children).map(child => child.id);
46
+ return new Set(idList).size === idList.length;
47
+ };
48
+ describe('DeterministicIdContext', () => {
49
+ it('can be found and tested with ReactTestUtils', () => {
50
+ render(_WrapperComponent || (_WrapperComponent = /*#__PURE__*/React.createElement(WrapperComponent, null)));
51
+ const testComponent = screen.getByTestId('test-component');
52
+ expect(testComponent).toBeInTheDocument();
53
+ expect(testComponent.id).toBeDefined();
54
+ });
55
+ it('should generate unique ids without Provider wrapper', () => {
56
+ render(_div2 || (_div2 = /*#__PURE__*/React.createElement("div", {
57
+ "data-testid": "test-components"
58
+ }, /*#__PURE__*/React.createElement(TestComponent, null), /*#__PURE__*/React.createElement(TestComponent, null), /*#__PURE__*/React.createElement(TestComponent, null), /*#__PURE__*/React.createElement(TestComponent, null), /*#__PURE__*/React.createElement(TestComponent, null))));
59
+ const el = screen.getByTestId('test-components');
60
+ expect(uniqueIds(el)).toBe(true);
61
+ });
62
+ it('should generate unique ids when components are rendered both out and inside of provider', () => {
63
+ render(_div3 || (_div3 = /*#__PURE__*/React.createElement("div", {
64
+ "data-testid": "test-components"
65
+ }, /*#__PURE__*/React.createElement(DeterministicIdContextProvider, null, /*#__PURE__*/React.createElement(TestComponent, null), /*#__PURE__*/React.createElement(TestComponent, null), /*#__PURE__*/React.createElement(TestComponent, null)), /*#__PURE__*/React.createElement(TestComponent, null), /*#__PURE__*/React.createElement(TestComponent, null))));
66
+ const el = screen.getByTestId('test-components');
67
+ expect(uniqueIds(el)).toBe(true);
68
+ });
69
+ it('should generate unique ids with provider only', () => {
70
+ const Wrapper = ({
71
+ children
72
+ }) => {
73
+ return /*#__PURE__*/React.createElement(DeterministicIdContextProvider, {
74
+ instanceCounterMap: generateInstanceCounterMap()
75
+ }, /*#__PURE__*/React.createElement("div", {
76
+ "data-testid": "wrapper"
77
+ }, children));
78
+ };
79
+ const children = [];
80
+ for (let i = 0; i < 10; i++) {
81
+ children.push( /*#__PURE__*/React.createElement(TestComponent, {
82
+ key: i
83
+ }));
84
+ }
85
+ render( /*#__PURE__*/React.createElement(Wrapper, null, children));
86
+ const el = screen.getByTestId('wrapper');
87
+ expect(uniqueIds(el)).toBe(true);
88
+ });
89
+ });
@@ -0,0 +1,121 @@
1
+ var _div, _div2;
2
+ /*
3
+ * The MIT License (MIT)
4
+ *
5
+ * Copyright (c) 2015 - present Instructure, Inc.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ import React from 'react';
27
+ import PropTypes from 'prop-types';
28
+ import { render } from '@testing-library/react';
29
+ import '@testing-library/jest-dom';
30
+ import { callRenderProp } from '../callRenderProp';
31
+ describe('callRenderProp', () => {
32
+ it('strings', () => {
33
+ expect(callRenderProp('foo')).toEqual('foo');
34
+ });
35
+ it('numbers', () => {
36
+ expect(callRenderProp(2)).toEqual(2);
37
+ });
38
+ it('arrays', () => {
39
+ const prop = ['foo', 'bar', 'baz'];
40
+ expect(callRenderProp(prop)).toStrictEqual(prop);
41
+ expect(callRenderProp([])).toStrictEqual([]);
42
+ });
43
+ it('booleans', () => {
44
+ expect(callRenderProp(false)).toEqual(false);
45
+ });
46
+ it('JSX literals', () => {
47
+ const Foo = () => _div || (_div = /*#__PURE__*/React.createElement("div", null, "hello"));
48
+ expect(callRenderProp( /*#__PURE__*/React.createElement(Foo, null))).toStrictEqual( /*#__PURE__*/React.createElement(Foo, null));
49
+ });
50
+ it('React classes', () => {
51
+ class Foo extends React.Component {
52
+ render() {
53
+ return _div2 || (_div2 = /*#__PURE__*/React.createElement("div", null, "hello"));
54
+ }
55
+ }
56
+ Foo.displayName = "Foo";
57
+ const Result = callRenderProp(Foo);
58
+ expect(Result).toStrictEqual( /*#__PURE__*/React.createElement(Foo, null));
59
+ const _render = render(Result),
60
+ getByText = _render.getByText;
61
+ expect(getByText('hello')).toBeInTheDocument();
62
+ });
63
+ it('functions', () => {
64
+ const Baz = function () {
65
+ return 'some text';
66
+ };
67
+ const result = callRenderProp(Baz);
68
+ const _render2 = render( /*#__PURE__*/React.createElement("div", null, result)),
69
+ getByText = _render2.getByText;
70
+ expect(getByText('some text')).toBeInTheDocument();
71
+ });
72
+ it('fat arrow functions', () => {
73
+ const Baz = () => 'some text';
74
+
75
+ // in this test we are trying to test that it works with fat arrow functions,
76
+ // but the babel config when we run these tests is currently configured
77
+ // to transpile fat arrow functions down to normal functions.
78
+ // Real, untranspiled, fat-arrow functions don't have a `prototype` but when
79
+ // they are transpiled down they do. So this next line is to make sure
80
+ // the thing we are testing really doesn't have a prototype like it would
81
+ // if it was a real untranspiled fat arrow function.
82
+ if (Baz.prototype) Baz.prototype = void 0;
83
+ const result = callRenderProp(Baz);
84
+ const _render3 = render( /*#__PURE__*/React.createElement("div", null, result)),
85
+ getByText = _render3.getByText;
86
+ expect(getByText('some text')).toBeInTheDocument();
87
+ });
88
+ describe('passing props', () => {
89
+ it('should pass props correctly to functions', () => {
90
+ const someFunc = ({
91
+ shape
92
+ }) => /*#__PURE__*/React.createElement("div", null, shape);
93
+ const result = callRenderProp(someFunc, {
94
+ shape: 'rectangle'
95
+ });
96
+ const _render4 = render( /*#__PURE__*/React.createElement("div", null, result)),
97
+ getByText = _render4.getByText;
98
+ expect(getByText('rectangle')).toBeInTheDocument();
99
+ });
100
+ it('should pass props correctly to React classes', () => {
101
+ class Foo extends React.Component {
102
+ render() {
103
+ return /*#__PURE__*/React.createElement("div", null, this.props.shape);
104
+ }
105
+ }
106
+ Foo.displayName = "Foo";
107
+ Foo.propTypes = {
108
+ shape: PropTypes.oneOf(['circle', 'rectangle'])
109
+ };
110
+ Foo.defaultProps = {
111
+ shape: 'circle'
112
+ };
113
+ const result = callRenderProp(Foo, {
114
+ shape: 'rectangle'
115
+ });
116
+ const _render5 = render( /*#__PURE__*/React.createElement("div", null, result)),
117
+ getByText = _render5.getByText;
118
+ expect(getByText('rectangle')).toBeInTheDocument();
119
+ });
120
+ });
121
+ });
@@ -0,0 +1,223 @@
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 { deprecated } from '../deprecated';
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: null,
42
+ qux: 'Hello'
43
+ };
44
+ describe('@deprecated', () => {
45
+ describe('deprecated props', () => {
46
+ var _DeprecatedComponent, _DeprecatedComponent2, _DeprecatedComponent3;
47
+ const DeprecatedComponent = deprecated('2.1.0', {
48
+ foo: 'bar',
49
+ baz: true
50
+ })(TestComponent);
51
+ it('should warn when suggesting new prop when using old prop', () => {
52
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
53
+ render(_DeprecatedComponent || (_DeprecatedComponent = /*#__PURE__*/React.createElement(DeprecatedComponent, {
54
+ foo: "Jane"
55
+ })));
56
+ const expectedWarningMessage = 'Warning: [TestComponent] `foo` is deprecated and will be removed in version 2.1.0. Use `bar` instead. ';
57
+ expect(consoleWarningSpy).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
58
+ consoleWarningSpy.mockRestore();
59
+ });
60
+ it('should warn when using old prop with no new prop', () => {
61
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
62
+ render(_DeprecatedComponent2 || (_DeprecatedComponent2 = /*#__PURE__*/React.createElement(DeprecatedComponent, {
63
+ baz: "Goodbye"
64
+ })));
65
+ const expectedWarningMessage = 'Warning: [TestComponent] `baz` is deprecated and will be removed in version 2.1.0.';
66
+ expect(consoleWarningSpy).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
67
+ consoleWarningSpy.mockRestore();
68
+ });
69
+ it('should not output a warning using new prop', () => {
70
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
71
+ render(_DeprecatedComponent3 || (_DeprecatedComponent3 = /*#__PURE__*/React.createElement(DeprecatedComponent, {
72
+ bar: "Jane"
73
+ })));
74
+ expect(consoleWarningSpy).not.toHaveBeenCalled();
75
+ consoleWarningSpy.mockRestore();
76
+ });
77
+ });
78
+ describe('deprecated component', () => {
79
+ var _DeprecatedComponent4;
80
+ const DeprecatedComponent = deprecated('3.4.0')(TestComponent);
81
+ it('should warn that the entire component is deprecated if no old props are supplied', () => {
82
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
83
+ render(_DeprecatedComponent4 || (_DeprecatedComponent4 = /*#__PURE__*/React.createElement(DeprecatedComponent, null)));
84
+ const expectedWarningMessage = 'Warning: [TestComponent] is deprecated and will be removed in version 3.4.0.';
85
+ expect(consoleWarningSpy).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
86
+ consoleWarningSpy.mockRestore();
87
+ });
88
+ });
89
+ describe('deprecated component with a changed package message', () => {
90
+ var _DeprecatedComponent5;
91
+ const DeprecatedComponent = deprecated('5.0.0', null, deprecated.changedPackageWarning('ui-forms', 'ui-number-input'))(TestComponent);
92
+ it('should warn that the component is deprecated and output a warning that the package changed', () => {
93
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
94
+ const expectedWarningMessage = '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.';
95
+ render(_DeprecatedComponent5 || (_DeprecatedComponent5 = /*#__PURE__*/React.createElement(DeprecatedComponent, null)));
96
+ expect(consoleWarningSpy).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
97
+ consoleWarningSpy.mockRestore();
98
+ });
99
+ });
100
+ describe('component with deprecated prop values', () => {
101
+ it('should not warn when an allowed prop value is supplied', () => {
102
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
103
+ class DeprecatedPropValueComponent extends Component {
104
+ render() {
105
+ return /*#__PURE__*/React.createElement("div", null, this.props.color);
106
+ }
107
+ }
108
+ DeprecatedPropValueComponent.displayName = "DeprecatedPropValueComponent";
109
+ DeprecatedPropValueComponent.propTypes = {
110
+ color: deprecated.deprecatePropValues(PropTypes.oneOf(['red', 'yellow', 'blue', 'orange', 'gold']), ['blue', 'orange', 'gold'])
111
+ };
112
+ DeprecatedPropValueComponent.defaultProps = {
113
+ color: 'red'
114
+ };
115
+ render( /*#__PURE__*/React.createElement(DeprecatedPropValueComponent, {
116
+ color: "yellow"
117
+ }));
118
+ expect(consoleWarningSpy).not.toHaveBeenCalled();
119
+ consoleWarningSpy.mockRestore();
120
+ });
121
+ it('should warn when a forbidden prop value is supplied', () => {
122
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
123
+ const color = 'orange';
124
+ class DeprecatedPropValueComponent extends Component {
125
+ render() {
126
+ return /*#__PURE__*/React.createElement("div", null, this.props.color);
127
+ }
128
+ }
129
+ DeprecatedPropValueComponent.displayName = "DeprecatedPropValueComponent";
130
+ DeprecatedPropValueComponent.propTypes = {
131
+ color: deprecated.deprecatePropValues(PropTypes.oneOf(['red', 'yellow', 'blue', 'orange', 'gold']), ['blue', 'orange', 'gold'])
132
+ };
133
+ DeprecatedPropValueComponent.defaultProps = {
134
+ color: 'red'
135
+ };
136
+ render( /*#__PURE__*/React.createElement(DeprecatedPropValueComponent, {
137
+ color: color
138
+ }));
139
+ const expectedWarningMessage = `The '${color}' value for the \`color\` prop is deprecated.`;
140
+ expect(consoleWarningSpy).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
141
+ consoleWarningSpy.mockRestore();
142
+ });
143
+ it('should warn with additional message text when a forbidden prop value is supplied and has message text', () => {
144
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
145
+ const color = 'gold';
146
+ const message = 'It will be removed in v8.0.0.';
147
+ class DeprecatedPropValueComponent extends Component {
148
+ render() {
149
+ return /*#__PURE__*/React.createElement("div", null, this.props.color);
150
+ }
151
+ }
152
+ DeprecatedPropValueComponent.displayName = "DeprecatedPropValueComponent";
153
+ DeprecatedPropValueComponent.propTypes = {
154
+ color: deprecated.deprecatePropValues(PropTypes.oneOf(['red', 'yellow', 'blue', 'orange', 'gold']), ['blue', 'orange', 'gold'], 'It will be removed in v8.0.0.')
155
+ };
156
+ DeprecatedPropValueComponent.defaultProps = {
157
+ color: 'red'
158
+ };
159
+ render( /*#__PURE__*/React.createElement(DeprecatedPropValueComponent, {
160
+ color: color
161
+ }));
162
+ const expectedWarningMessage = `The '${color}' value for the \`color\` prop is deprecated. ${message}`;
163
+ expect(consoleWarningSpy).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
164
+ consoleWarningSpy.mockRestore();
165
+ });
166
+ it('should call functional message with the correct props', () => {
167
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
168
+ const messageMock = jest.fn();
169
+ const color = 'gold';
170
+ class DeprecatedPropValueComponent extends Component {
171
+ render() {
172
+ return /*#__PURE__*/React.createElement("div", null, this.props.color);
173
+ }
174
+ }
175
+ DeprecatedPropValueComponent.displayName = "DeprecatedPropValueComponent";
176
+ DeprecatedPropValueComponent.propTypes = {
177
+ color: deprecated.deprecatePropValues(PropTypes.oneOf(['red', 'yellow', 'blue', 'orange', 'gold']), ['blue', 'orange', 'gold'], messageMock)
178
+ };
179
+ DeprecatedPropValueComponent.defaultProps = {
180
+ color: 'red'
181
+ };
182
+ render( /*#__PURE__*/React.createElement(DeprecatedPropValueComponent, {
183
+ color: color
184
+ }));
185
+ const _messageMock$mock$cal = messageMock.mock.calls[0][0],
186
+ props = _messageMock$mock$cal.props,
187
+ propName = _messageMock$mock$cal.propName,
188
+ propValue = _messageMock$mock$cal.propValue;
189
+ expect(props).toEqual({
190
+ color
191
+ });
192
+ expect(propName).toBe('color');
193
+ expect(propValue).toBe(color);
194
+ expect(consoleWarningSpy).toHaveBeenCalled();
195
+ consoleWarningSpy.mockRestore();
196
+ });
197
+ it('should warn with a completely custom message when provided message is functional and prop value is forbidden', () => {
198
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
199
+ const color = 'gold';
200
+ class DeprecatedPropValueComponent extends Component {
201
+ render() {
202
+ return /*#__PURE__*/React.createElement("div", null, this.props.color);
203
+ }
204
+ }
205
+ DeprecatedPropValueComponent.displayName = "DeprecatedPropValueComponent";
206
+ DeprecatedPropValueComponent.propTypes = {
207
+ color: deprecated.deprecatePropValues(PropTypes.oneOf(['red', 'yellow', 'blue', 'orange', 'gold']), ['blue', 'orange', 'gold'], ({
208
+ propValue,
209
+ propName
210
+ }) => `The ${propValue} value for ${propName} has been deprecated. Use the FooBar component with the 'baz' prop set instead.`)
211
+ };
212
+ DeprecatedPropValueComponent.defaultProps = {
213
+ color: 'red'
214
+ };
215
+ render( /*#__PURE__*/React.createElement(DeprecatedPropValueComponent, {
216
+ color: color
217
+ }));
218
+ const expectedWarningMessage = `The ${color} value for color has been deprecated. Use the FooBar component with the 'baz' prop set instead.`;
219
+ expect(consoleWarningSpy).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
220
+ consoleWarningSpy.mockRestore();
221
+ });
222
+ });
223
+ });
@@ -0,0 +1,94 @@
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 { experimental } from '../experimental';
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('@experimental', () => {
45
+ describe('experimental props', () => {
46
+ var _ExperimentalComponen, _ExperimentalComponen2, _ExperimentalComponen3;
47
+ const ExperimentalComponent = experimental(['bar'])(TestComponent);
48
+ it('should warn when using an experimental prop', () => {
49
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
50
+ render(_ExperimentalComponen || (_ExperimentalComponen = /*#__PURE__*/React.createElement(ExperimentalComponent, {
51
+ bar: "Jane"
52
+ })));
53
+ const expectedWarningMessage = 'Warning: [TestComponent] The `bar` prop is experimental and its API could change significantly 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-experimental prop', () => {
58
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
59
+ render(_ExperimentalComponen2 || (_ExperimentalComponen2 = /*#__PURE__*/React.createElement(ExperimentalComponent, {
60
+ qux: "Jane"
61
+ })));
62
+ expect(consoleWarningSpy).not.toHaveBeenCalled();
63
+ consoleWarningSpy.mockRestore();
64
+ });
65
+ it('should not output a warning for an experimental prop when dangerously ignored', () => {
66
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
67
+ render(_ExperimentalComponen3 || (_ExperimentalComponen3 = /*#__PURE__*/React.createElement(ExperimentalComponent, {
68
+ qux: "Jane",
69
+ __dangerouslyIgnoreExperimentalWarnings: true
70
+ })));
71
+ expect(consoleWarningSpy).not.toHaveBeenCalled();
72
+ consoleWarningSpy.mockRestore();
73
+ });
74
+ });
75
+ describe('experimental component', () => {
76
+ var _ExperimentalComponen4, _ExperimentalComponen5;
77
+ const ExperimentalComponent = experimental()(TestComponent);
78
+ it('should warn that the entire component is experimental if no props are supplied', () => {
79
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
80
+ render(_ExperimentalComponen4 || (_ExperimentalComponen4 = /*#__PURE__*/React.createElement(ExperimentalComponent, null)));
81
+ const expectedWarningMessage = 'Warning: [TestComponent] is experimental and its API could change significantly in a future release.';
82
+ expect(consoleWarningSpy).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
83
+ consoleWarningSpy.mockRestore();
84
+ });
85
+ it('should not output a warning for a component when dangerously ignored', () => {
86
+ const consoleWarningSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
87
+ render(_ExperimentalComponen5 || (_ExperimentalComponen5 = /*#__PURE__*/React.createElement(ExperimentalComponent, {
88
+ __dangerouslyIgnoreExperimentalWarnings: true
89
+ })));
90
+ expect(consoleWarningSpy).not.toHaveBeenCalled();
91
+ consoleWarningSpy.mockRestore();
92
+ });
93
+ });
94
+ });