react-resizable 1.7.5 → 1.11.0

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 (50) hide show
  1. package/.babelrc +10 -4
  2. package/.browserslistrc +3 -0
  3. package/.eslintrc +10 -6
  4. package/.flowconfig +9 -11
  5. package/CHANGELOG.md +59 -29
  6. package/LICENSE +21 -0
  7. package/README.md +18 -5
  8. package/__tests__/Resizable.test.js +245 -0
  9. package/__tests__/ResizableBox.test.js +99 -0
  10. package/__tests__/__snapshots__/Resizable.test.js.snap +29 -0
  11. package/__tests__/__snapshots__/ResizableBox.test.js.snap +23 -0
  12. package/build/Resizable.js +183 -177
  13. package/build/Resizable.js.flow +115 -152
  14. package/build/ResizableBox.js +95 -75
  15. package/build/ResizableBox.js.flow +61 -34
  16. package/build/propTypes.js +112 -0
  17. package/build/propTypes.js.flow +135 -0
  18. package/build/utils.js +27 -0
  19. package/build/{cloneElement.js.flow → utils.js.flow} +2 -2
  20. package/coverage/clover.xml +107 -0
  21. package/coverage/coverage-final.json +5 -0
  22. package/coverage/lcov-report/Resizable.js.html +665 -0
  23. package/coverage/lcov-report/ResizableBox.js.html +374 -0
  24. package/coverage/lcov-report/base.css +224 -0
  25. package/coverage/lcov-report/block-navigation.js +79 -0
  26. package/coverage/lcov-report/favicon.png +0 -0
  27. package/coverage/lcov-report/flow-typed/npm/index.html +111 -0
  28. package/coverage/lcov-report/flow-typed/npm/jest_v26.x.x.js.html +3734 -0
  29. package/coverage/lcov-report/index.html +156 -0
  30. package/coverage/lcov-report/prettify.css +1 -0
  31. package/coverage/lcov-report/prettify.js +2 -0
  32. package/coverage/lcov-report/propTypes.js.html +485 -0
  33. package/coverage/lcov-report/react-resizable/dist/bundle.js.html +95 -0
  34. package/coverage/lcov-report/react-resizable/dist/index.html +111 -0
  35. package/coverage/lcov-report/react-resizable/flow-typed/npm/index.html +111 -0
  36. package/coverage/lcov-report/react-resizable/flow-typed/npm/jest_v26.x.x.js.html +3734 -0
  37. package/coverage/lcov-report/react-resizable/index.html +111 -0
  38. package/coverage/lcov-report/react-resizable/index.js.html +101 -0
  39. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  40. package/coverage/lcov-report/sorter.js +170 -0
  41. package/coverage/lcov-report/utils.js.html +122 -0
  42. package/coverage/lcov.info +233 -0
  43. package/css/styles.css +53 -5
  44. package/dist/bundle.js +6 -0
  45. package/flow-typed/npm/jest_v26.x.x.js +1218 -0
  46. package/package.json +33 -25
  47. package/setupTests/enzyme.js +4 -0
  48. package/.github/ISSUE_TEMPLATE.md +0 -23
  49. package/.github/PULL_REQUEST_TEMPLATE.md +0 -6
  50. package/build/cloneElement.js +0 -20
package/.babelrc CHANGED
@@ -1,10 +1,16 @@
1
1
  {
2
2
  "presets": [
3
- ["es2015", {"loose": true}],
4
- "react"
3
+ [
4
+ "@babel/preset-env",
5
+ {
6
+ "loose": true
7
+ }
8
+ ],
9
+ "@babel/preset-react",
10
+ "@babel/preset-flow"
5
11
  ],
6
12
  "plugins": [
7
- "transform-class-properties",
8
- "transform-object-rest-spread",
13
+ "@babel/plugin-proposal-class-properties",
14
+ "@babel/plugin-proposal-object-rest-spread"
9
15
  ]
10
16
  }
@@ -0,0 +1,3 @@
1
+ > 0.25%
2
+ ie 11
3
+ not dead
package/.eslintrc CHANGED
@@ -4,7 +4,9 @@
4
4
  "plugins": [
5
5
  "react"
6
6
  ],
7
- "extends": "eslint:recommended",
7
+ "extends": [
8
+ "eslint:recommended"
9
+ ],
8
10
  "rules": {
9
11
  "strict": 0,
10
12
  "quotes": [0, "single"],
@@ -20,14 +22,16 @@
20
22
  "react/jsx-uses-vars": 1,
21
23
  "semi": [1, "always"]
22
24
  },
23
- env: {
25
+ "env": {
24
26
  "browser": true,
25
- "node": true
27
+ "node": true,
28
+ "jest": true
26
29
  },
27
30
  "globals": {
28
31
  // For Flow
29
- "ReactElement",
30
- "ReactClass",
31
- "SyntheticEvent"
32
+ "ReactElement": false,
33
+ "ReactClass": false,
34
+ "SyntheticEvent": false,
35
+ "ClientRect": false
32
36
  }
33
37
  }
package/.flowconfig CHANGED
@@ -1,9 +1,8 @@
1
1
  [version]
2
- 0.55.0
2
+ ^0.133.0
3
3
 
4
4
  [ignore]
5
- .*/node_modules/babel.*
6
- .*/node_modules/fbjs/.*
5
+ .*/node_modules/@babel.*
7
6
  .*/node_modules/express/.*
8
7
  .*/node_modules/serve-index/.*
9
8
  <PROJECT_ROOT>/build/.*
@@ -11,21 +10,20 @@
11
10
  [include]
12
11
 
13
12
  [libs]
13
+ flow-typed
14
+
15
+ [lints]
16
+ all=warn
17
+ implicit-inexact-object=error
14
18
 
15
19
  [options]
16
- suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowFixMe.*
17
- suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowBug.*
18
- suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowIgnore.*
19
- suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowNewLine.*
20
- suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowIssue
21
20
  suppress_type=$FlowFixMe
22
21
  esproposal.class_static_fields=enable
23
22
  esproposal.class_instance_fields=enable
24
23
  esproposal.decorators=ignore
25
24
  esproposal.export_star_as=enable
26
25
  experimental.strict_call_arity=true
27
- module.system.node.resolve_dirname=node_modules
28
- module.system.node.resolve_dirname=.
26
+ module.system.node.allow_root_relative=true
29
27
  module.use_strict=true
30
28
  server.max_workers=6
31
- unsafe.enable_getters_and_setters=true
29
+ exact_by_default=true
package/CHANGELOG.md CHANGED
@@ -1,94 +1,124 @@
1
1
  # Changelog
2
2
 
3
+ ### 1.11.0 (Sep 3, 2020)
4
+
5
+ - ⚠ Important Notice!
6
+ - React-Resizable 2.0.0 was published due to a breaking change in `props` handling. This change ended up actually completely breaking certain workflows, for the dubious benefit of making the code slightly simpler to add to. The breaking change has been reverted, 2.0.0 is now deprecated, and we will continue on the 1.x branch. Future breaking changes to `react-resizable` will upgrade the major version to `3`.
7
+ - ➕ Feature: `<ResizableBox>` now takes a `style` prop which will be applied on the wrapping `<div>`. `width` and `height` in this prop are ignored.
8
+ - 🐛 Bugfix: remove unknown Prop `handle` from div children in Resizable `React.cloneElement`. [#124](https://github.com/STRML/react-resizable/issues/124)
9
+ - 🐛 Bugfix: Fix top and left resizing jerkiness. Thanks @conor-kelleher. [#136](https://github.com/STRML/react-resizable/pull/136)
10
+ - ✏ Chore: Improved test suite. Please contribute tests for your use cases if you have the time, I would really appreciate it! Thanks very much, @Danielecina
11
+ - ✏ Chore: Minor internal refactors and additional tests.
12
+ - ✏ Chore: Additional examples.
13
+
14
+ ### 1.10.1 (Nov 25, 2019)
15
+
16
+ > Note: 1.10.0 was a mis-publish.
17
+
18
+ - ➕ Feature: Add `transformScale` prop [#115](https://github.com/STRML/react-resizable/pull/115)
19
+ - 🐛 Bugfix: Resolve `getDerivedStateFromProps` dev warning [#117](https://github.com/STRML/react-resizable/pull/117)
20
+
21
+ ### 1.9.0 (Oct 24, 2019)
22
+
23
+ - 🐛 Bugfix: Fix resize with north/south handles when `lockAspectRatio=true` [#106](https://github.com/STRML/react-resizable/pull/106)
24
+ - ✏ Chore: Remove deprecated React 16.9 lifecycle methods (`componentWillReceiveProps`) (https://github.com/STRML/react-resizable/pull/112/commits/541dee69b8e45d91a533855609472b481634edee)
25
+ - ✏ Chore: Upgrade to babel 7
26
+ - ✏ Chore: [Remove unused state inside `<Draggable>`](https://github.com/STRML/react-resizable/pull/112/commits/05693f63d6d221ad652f0f28af024cfb46a5f2df). This has not been needed for quite some time, fixes [some bugs](https://github.com/STRML/react-resizable/issues/99) and improves performance.
27
+
28
+ ### 1.8.0 (May 15, 2019)
29
+
30
+ - ➕ Feature: Added support for custom resize handles [#79](https://github.com/STRML/react-resizable/pull/79)
31
+ - ➕ Feature: Added support for resize handles on all corners [#191](https://github.com/STRML/react-resizable/pull/191)
32
+
3
33
  ### 1.7.5 (Sep 26, 2017)
4
34
 
5
- - Support for React 16 (no changes required, updated `peerDependencies`)
6
- - Minor dep updates.
35
+ - ✏ Chore: Support for React 16 (no changes required, updated `peerDependencies`)
36
+ - ✏ Chore: Minor dep updates.
7
37
 
8
38
  ### 1.7.4 (Sep 5, 2017)
9
39
 
10
- - Minor Flow & dependency updates.
40
+ - ✏ Chore: Minor Flow & dependency updates.
11
41
 
12
42
  ### 1.7.3 (Aug 31, 2017)
13
43
 
14
- - Fix React deprecation warnings from `import *`
44
+ - 🐛 Bugfix: React deprecation warnings from `import *`
15
45
  - https://github.com/facebook/react/issues/10583
16
46
 
17
47
  ### 1.7.2 (Aug 21, 2017)
18
48
 
19
- - Pkg: Add `react-draggable@3.0.0` to version range.
49
+ - ✏ Chore: Pkg: Add `react-draggable@3.0.0` to version range.
20
50
  - This package is compatible with both `@2` and `@3` versions.
21
51
 
22
52
  ### 1.7.1 (May 23, 2017)
23
53
 
24
- - Bugfix: Some flow types were improperly specified.
54
+ - 🐛 Bugfix: Some flow types were improperly specified.
25
55
 
26
56
  ### 1.7.0 (May 1, 2017)
27
57
 
28
- - Deprecation: `React.PropTypes` now deprecated in React 15.5, moved to `prop-types` package
29
- - Internal: Update devDeps, upgrade to webpack 2
30
- - Internal: Remove babel `stage-1` and `transform-flow-comments`, bring in only selected plugins, makes for leaner dev/build.
58
+ - Deprecation: `React.PropTypes` now deprecated in React 15.5, moved to `prop-types` package
59
+ - ✏ Chore: Update devDeps, upgrade to webpack 2
60
+ - ✏ Chore: Remove babel `stage-1` and `transform-flow-comments`, bring in only selected plugins, makes for leaner dev/build.
31
61
 
32
62
  ### 1.6.0 (Jan 23, 2017)
33
63
 
34
- - Feature: Allow restricting by axis. (#40, thanks @dnissley-al)
64
+ - Feature: Allow restricting by axis. (#40, thanks @dnissley-al)
35
65
 
36
66
  ### 1.5.0 (Jan 23, 2017)
37
67
 
38
- - Bugfix: Persist SyntheticEvents when needed (#45, #46)
39
- - Feature: Add componentWillReceiveProps to `<ResizableBox>` (#44, thanks @JoaoMosmann)
68
+ - 🐛 Bugfix: Persist SyntheticEvents when needed (#45, #46)
69
+ - Feature: Add componentWillReceiveProps to `<ResizableBox>` (#44, thanks @JoaoMosmann)
40
70
 
41
71
  ### 1.4.6 (Dec 30, 2016)
42
72
 
43
- - Removed unused ref from `<Resizable>`.
44
- - Added development lockfile.
73
+ - ✏ Chore: Removed unused ref from `<Resizable>`.
74
+ - ✏ Chore: Added development lockfile.
45
75
 
46
76
  ### 1.4.5 (Sep 30, 2016)
47
77
 
48
- - Fix bad publish
78
+ - 🐛 Bugfix: Fix bad publish
49
79
 
50
80
  ### 1.4.4 (Sep 30, 2016)
51
81
 
52
- - Bugfix: Minor flow errors
82
+ - 🐛 Bugfix: Minor flow errors
53
83
 
54
84
  ### 1.4.3 (Sep 27, 2016)
55
85
 
56
- - Bugfix: Don't pass `onResize` in `<ResizableBox>`.
57
- - Bugfix: Fix new Flow errors (type parameters no longer optional).
86
+ - 🐛 Bugfix: Don't pass `onResize` in `<ResizableBox>`.
87
+ - 🐛 Bugfix: Fix new Flow errors (type parameters no longer optional).
58
88
 
59
89
  ### 1.4.2 (July 1, 2016)
60
90
 
61
- - Bugfix: Don't pass unknown props to underlying DOM element. Fixes React 15.2.0 warnings.
91
+ - 🐛 Bugfix: Don't pass unknown props to underlying DOM element. Fixes React 15.2.0 warnings.
62
92
 
63
93
  ### 1.4.1 (May 23, 2016)
64
94
 
65
- - Bugfix: Resizable handle should have a `key` when injected. Fixes React warnings on custom components.
95
+ - 🐛 Bugfix: Resizable handle should have a `key` when injected. Fixes React warnings on custom components.
66
96
 
67
97
  ### 1.4.0 (May 20, 2016)
68
98
 
69
- - Update to React-Draggable v2, which changed callback data structure.
99
+ - ✏ Chore: Update to React-Draggable v2, which changed callback data structure.
70
100
 
71
101
  ### 1.3.4 (May 17, 2016)
72
102
 
73
- - Bugfix: Slack was not being reset on resizeStop. Fixes #34, #36.
74
- - Added `flow-bin` to devDeps.
103
+ - 🐛 Bugfix: Slack was not being reset on resizeStop. Fixes #34, #36.
104
+ - ✏ Chore: Added `flow-bin` to devDeps.
75
105
 
76
106
  ### 1.3.3 (Apr 19, 2016)
77
107
 
78
- - Enhancement: Add Flow comments.
108
+ - ➕ Feature: Add Flow comments.
79
109
 
80
110
  ### 1.3.2 (Apr 8, 2016)
81
111
 
82
- - Bugfix: Prevent `width` and `height` from leaking to the underlying DOM element and being written.
112
+ - 🐛 Bugfix: Prevent `width` and `height` from leaking to the underlying DOM element and being written.
83
113
 
84
114
  ### 1.3.1 (Apr 8, 2016)
85
115
 
86
- - Allow React v15 in peerdeps.
116
+ - ✏ Chore: Allow React v15 in peerdeps.
87
117
 
88
118
  ### 1.3.0 (Mar 11, 2016)
89
119
 
90
- - Switch to ES2015 Loose Mode to fix IE9/10 issues.
91
- - Flow typing fixes.
92
- - Styling fixes to the demo page.
120
+ - 🐛 Bugfix: Switch to ES2015 Loose Mode to fix IE9/10 issues.
121
+ - 🐛 Bugfix: Flow typing fixes.
122
+ - 🐛 Bugfix: Styling fixes to the demo page.
93
123
 
94
124
  > Changes before 1.3.0 were not logged. Please see the git commit history.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016-2018 Samuel Reed
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -2,18 +2,18 @@
2
2
 
3
3
  [View the Demo](https://strml.github.io/react-resizable/examples/1.html)
4
4
 
5
- A simple widget that can be resized via a handle.
5
+ A simple widget that can be resized via one or more handles.
6
6
 
7
7
  You can either use the `<Resizable>` element directly, or use the much simpler `<ResizableBox>` element.
8
8
 
9
- See the example and associated code in [TestLayout](/test/TestLayout.js) and
9
+ See the example and associated code in [TestLayout](/examples/TestLayout.js) and
10
10
  [ResizableBox](/lib/ResizableBox.js) for more details.
11
11
 
12
12
  Make sure you use the associated styles in [/css/styles.css](/css/styles.css), as without them, you will have
13
13
  problems with handle placement and visibility.
14
14
 
15
15
  You can pass options directly to the underlying `DraggableCore` instance by using the prop `draggableOpts`.
16
- See the [demo](/test/TestLayout.js) for more on this.
16
+ See the [demo](/examples/TestLayout.js) for more on this.
17
17
 
18
18
  ### Installation
19
19
 
@@ -43,13 +43,15 @@ render() {
43
43
 
44
44
  ### Props
45
45
 
46
- These props apply to both `<Resizable>` and `<ResizableBox>`.
46
+ These props apply to both `<Resizable>` and `<ResizableBox>`. Unknown props that are not in the list below will be passed to the child component.
47
47
 
48
48
  ```js
49
49
  {
50
50
  children: React.Element<any>,
51
51
  width: number,
52
52
  height: number,
53
+ // Either a ReactElement to be used as handle, or a function returning an element that is fed the handle's location as its first argument.
54
+ handle: ReactElement<any> | (resizeHandle: 's' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne') => ReactElement<any>,
53
55
  // If you change this, be sure to update your css
54
56
  handleSize: [number, number] = [10, 10],
55
57
  lockAspectRatio: boolean = false,
@@ -59,6 +61,17 @@ These props apply to both `<Resizable>` and `<ResizableBox>`.
59
61
  onResizeStop?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
60
62
  onResizeStart?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
61
63
  onResize?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
62
- draggableOpts?: ?Object
64
+ draggableOpts?: ?Object,
65
+ resizeHandles?: ?Array<'s' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne'> = ['se']
63
66
  };
64
67
  ```
68
+
69
+ The following props can also be used on `<ResizableBox>`:
70
+
71
+ ```js
72
+ {
73
+ style?: Object
74
+ }
75
+ ```
76
+
77
+ If a `width` or `height` is passed to `<ResizableBox>`'s `style` prop, it will be ignored as it is required for internal function.
@@ -0,0 +1,245 @@
1
+ // @flow
2
+ import React from 'react';
3
+ import renderer from 'react-test-renderer';
4
+ import {shallow} from 'enzyme';
5
+ import {DraggableCore} from "react-draggable";
6
+
7
+ import Resizable from '../lib/Resizable';
8
+
9
+ describe('render Resizable', () => {
10
+ const props = {
11
+ axis: 'both',
12
+ className: 'test-classname',
13
+ draggableOpts: {},
14
+ handleSize: [20, 20],
15
+ height: 50,
16
+ lockAspectRatio: false,
17
+ maxConstraints: [Infinity, Infinity],
18
+ minConstraints: [20, 20],
19
+ onResize: jest.fn(),
20
+ onResizeStart: jest.fn(),
21
+ onResizeStop: jest.fn(),
22
+ resizeHandles: ['se', 'e'],
23
+ transformScale: 1,
24
+ width: 50,
25
+ };
26
+ const userChildren = <span className={'children'} />;
27
+ const resizableBoxChildren = <div style={{width: '50px', height: '50px'}}>{userChildren}</div>;
28
+
29
+ beforeEach(() => {
30
+ jest.clearAllMocks();
31
+ });
32
+
33
+ test('snapshot default props', () => {
34
+ const tree = renderer.create(<Resizable {...props}>{resizableBoxChildren}</Resizable>).toJSON();
35
+ expect(tree).toMatchSnapshot();
36
+ });
37
+
38
+ test('with correct props', () => {
39
+ const element = shallow(<Resizable {...props}>{resizableBoxChildren}</Resizable>);
40
+ expect(element.find('.test-classname').find('.children'));
41
+ expect(element.find(DraggableCore)).toHaveLength(2);
42
+ const cursorSe = element.find('.react-resizable-handle-se');
43
+ const cursorE = element.find('.react-resizable-handle-e');
44
+ expect(cursorSe).toHaveLength(1);
45
+ expect(cursorE).toHaveLength(1);
46
+ });
47
+
48
+ describe('and pass handle props', () => {
49
+ test('as component', () => {
50
+ const customProps = {
51
+ ...props,
52
+ resizeHandles: ['se'],
53
+ handle: <span className={'custom-component'} />
54
+ };
55
+ const element = shallow(<Resizable {...customProps}>{resizableBoxChildren}</Resizable>);
56
+ expect(element.find('.react-resizable-handle-se')).toHaveLength(0);
57
+ expect(element.find('.custom-component')).toHaveLength(1);
58
+ });
59
+ test('as function', () => {
60
+ const customProps = {
61
+ ...props,
62
+ resizeHandles: ['se'],
63
+ handle: (h) => <span className={`custom-component-${h}`} />
64
+ };
65
+ const element = shallow(<Resizable {...customProps}>{resizableBoxChildren}</Resizable>);
66
+ expect(element.find('.custom-component-se')).toHaveLength(1);
67
+ });
68
+ });
69
+
70
+ describe('<Resizable> props filtering', () => {
71
+ const allProps = {
72
+ ...props,
73
+ draggableOpts: {},
74
+ handle: <div />,
75
+ };
76
+
77
+ // Ensure everything in propTypes is represented here. Otherwise the next two tests are not valid
78
+ test('all intended props are in our allProps object', () => {
79
+ expect(['children', ...Object.keys(allProps)].sort()).toEqual(Object.keys(Resizable.propTypes).sort());
80
+ });
81
+
82
+ test('none of these props leak down to the child', () => {
83
+ const element = shallow(<Resizable {...allProps}><div className="foo" /></Resizable>);
84
+ expect(Object.keys(element.find('.foo').props())).toEqual(['className', 'children']);
85
+ });
86
+
87
+ test('className is constructed properly', () => {
88
+ const element = shallow(<Resizable {...allProps}><div className="foo" /></Resizable>);
89
+ expect(element.find('.foo').props().className).toEqual(`foo ${allProps.className} react-resizable`);
90
+ });
91
+ });
92
+
93
+ describe('onResize callback with modified position', () => {
94
+ const customProps = {
95
+ ...props,
96
+ resizeHandles: ['nw', 'sw' ,'ne', 'se', 'n', 's', 'w', 'e'],
97
+ };
98
+ const mockClientRect = {
99
+ left: 0,
100
+ top: 0,
101
+ };
102
+ const node = document.createElement('div');
103
+ // $FlowIgnore need to override to have control over dummy dom element
104
+ node.getBoundingClientRect = () => ({ ...mockClientRect });
105
+ const mockEvent = { };
106
+ const element = shallow(<Resizable {...customProps}>{resizableBoxChildren}</Resizable>);
107
+ const nwHandle = element.find('DraggableCore').first();
108
+
109
+ test('Gradual resizing without movement between does not modify callback', () => {
110
+ expect(props.onResize).not.toHaveBeenCalled();
111
+ nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
112
+ expect(props.onResize).lastCalledWith(
113
+ mockEvent,
114
+ expect.objectContaining({
115
+ size: {
116
+ height: 40,
117
+ width: 45,
118
+ },
119
+ })
120
+ );
121
+ });
122
+
123
+ test('Movement between callbacks modifies response values', () => {
124
+ expect(props.onResize).not.toHaveBeenCalled();
125
+
126
+ mockClientRect.top = -10; // Object moves between callbacks
127
+ nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
128
+ expect(props.onResize).lastCalledWith(
129
+ mockEvent,
130
+ expect.objectContaining({
131
+ size: {
132
+ height: 50, // No height change since deltaY is caused by clientRect moving vertically
133
+ width: 45,
134
+ },
135
+ })
136
+ );
137
+
138
+ mockClientRect.left = 20; // Object moves between callbacks
139
+ nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
140
+ expect(props.onResize).lastCalledWith(
141
+ mockEvent,
142
+ expect.objectContaining({
143
+ size: {
144
+ height: 40, // Height decreased as deltaY increases - no further top position change since last
145
+ width: 25, // Width decreased 25 - 5 from deltaX and 20 from changing position
146
+ },
147
+ })
148
+ );
149
+
150
+ props.onResize.mockClear();
151
+ mockClientRect.left -= 10; // Object moves between callbacks
152
+ mockClientRect.top -= 10; // Object moves between callbacks
153
+ nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 10, deltaY: 10 });
154
+ expect(props.onResize).not.toHaveBeenCalled();
155
+
156
+ mockClientRect.left -= 10; // Object moves between callbacks
157
+ mockClientRect.top -= 10; // Object moves between callbacks
158
+ const swHandle = element.find('DraggableCore').at(1);
159
+ swHandle.prop('onDrag')(mockEvent, { node, deltaX: 10, deltaY: 10 });
160
+ expect(props.onResize).lastCalledWith(
161
+ mockEvent,
162
+ expect.objectContaining({
163
+ size: {
164
+ height: 60, // Changed since resizing from bottom doesn't cause position change
165
+ width: 50, // No change - movement has caused entire delta
166
+ },
167
+ })
168
+ );
169
+
170
+ mockClientRect.left -= 10; // Object moves between callbacks
171
+ mockClientRect.top -= 10; // Object moves between callbacks
172
+ const neHandle = element.find('DraggableCore').at(2);
173
+ neHandle.prop('onDrag')(mockEvent, { node, deltaX: 10, deltaY: 10 });
174
+ expect(props.onResize).lastCalledWith(
175
+ mockEvent,
176
+ expect.objectContaining({
177
+ size: {
178
+ height: 50, // No change - movement has caused entire delta
179
+ width: 60, // Changed since resizing from right doesn't cause position change
180
+ },
181
+ })
182
+ );
183
+
184
+ mockClientRect.left -= 10; // Object moves between callbacks
185
+ mockClientRect.top -= 10; // Object moves between callbacks
186
+ const seHandle = element.find('DraggableCore').at(3);
187
+ seHandle.prop('onDrag')(mockEvent, { node, deltaX: 10, deltaY: 10 });
188
+ expect(props.onResize).lastCalledWith(
189
+ mockEvent,
190
+ expect.objectContaining({
191
+ size: {
192
+ height: 60, // Changed since resizing from right doesn't cause position change
193
+ width: 60, // Changed since resizing from right doesn't cause position change
194
+ },
195
+ })
196
+ );
197
+ });
198
+
199
+ test('use of < 1 transformScale', () => {
200
+ const element = shallow(<Resizable {...customProps} transformScale={0.5}>{resizableBoxChildren}</Resizable>);
201
+ const nwHandle = element.find('DraggableCore').first();
202
+ expect(props.onResize).not.toHaveBeenCalled();
203
+ nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
204
+ expect(props.onResize).lastCalledWith(
205
+ mockEvent,
206
+ expect.objectContaining({
207
+ size: {
208
+ // Should be doubled
209
+ height: 30,
210
+ width: 40,
211
+ },
212
+ })
213
+ );
214
+
215
+ mockClientRect.left = 20; // Object moves between callbacks
216
+ nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
217
+ expect(props.onResize).lastCalledWith(
218
+ mockEvent,
219
+ expect.objectContaining({
220
+ size: {
221
+ height: 30, // Height decreased as deltaY increases - no further top position change since last
222
+ width: 20, // Width decreased 10 from deltaX and 20 from changing position
223
+ },
224
+ })
225
+ );
226
+ });
227
+
228
+ test('use of > 1 transformScale', () => {
229
+ const element = shallow(<Resizable {...customProps} transformScale={2}>{resizableBoxChildren}</Resizable>);
230
+ const nwHandle = element.find('DraggableCore').first();
231
+ expect(props.onResize).not.toHaveBeenCalled();
232
+ nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
233
+ expect(props.onResize).lastCalledWith(
234
+ mockEvent,
235
+ expect.objectContaining({
236
+ size: {
237
+ // Should be halved
238
+ height: 45,
239
+ width: 47.5,
240
+ },
241
+ })
242
+ );
243
+ });
244
+ });
245
+ });
@@ -0,0 +1,99 @@
1
+ // @flow
2
+ import React from 'react';
3
+ import renderer from 'react-test-renderer';
4
+ import {shallow} from 'enzyme';
5
+
6
+ import ResizableBox from '../lib/ResizableBox';
7
+ import Resizable from "../lib/Resizable";
8
+
9
+ describe('render ResizableBox', () => {
10
+ const props = {
11
+ axis: 'x',
12
+ draggableOpts: {},
13
+ handle: jest.fn(resizeHandle => <span className={`test-class-${resizeHandle}`} />),
14
+ handleSize: [20, 20],
15
+ height: 50,
16
+ lockAspectRatio: false,
17
+ maxConstraints: [30, 30],
18
+ minConstraints: [10, 10],
19
+ onResize: jest.fn(),
20
+ onResizeStart: jest.fn(),
21
+ onResizeStop: jest.fn(),
22
+ resizeHandles: ['w'],
23
+ transformScale: 1,
24
+ width: 50,
25
+ };
26
+ const children = <span className="children" />;
27
+
28
+ beforeEach(() => {
29
+ jest.clearAllMocks();
30
+ });
31
+
32
+ test('snapshot default props', () => {
33
+ const tree = renderer.create(<ResizableBox {...props}>{children}</ResizableBox>).toJSON();
34
+ expect(tree).toMatchSnapshot();
35
+ });
36
+
37
+ test('with correct props', () => {
38
+ const element = shallow(<ResizableBox {...props}>{children}</ResizableBox>);
39
+ expect(element.state()).toEqual({
40
+ height: 50,
41
+ propsHeight: 50,
42
+ propsWidth: 50,
43
+ width: 50,
44
+ });
45
+ const resizable = element.find(Resizable);
46
+ const fakeEvent = {persist: jest.fn()};
47
+ const data = {node: children, size: {width: 30, height: 30}, handle: 'w'};
48
+ resizable.simulate('resize', fakeEvent, data);
49
+ expect(element.state()).toEqual({
50
+ height: 30,
51
+ propsHeight: 50,
52
+ propsWidth: 50,
53
+ width: 30,
54
+ });
55
+ expect(element.find('.children')).toHaveLength(1);
56
+ expect(fakeEvent.persist).toHaveBeenCalledTimes(1);
57
+ expect(props.onResize).toHaveBeenCalledWith(fakeEvent, data);
58
+
59
+ resizable.simulate('resizeStart', fakeEvent, data);
60
+ expect(props.onResizeStart).toHaveBeenCalledWith(fakeEvent, data);
61
+
62
+ resizable.simulate('resizeStop', fakeEvent, data);
63
+ expect(props.onResizeStop).toHaveBeenCalledWith(fakeEvent, data);
64
+ });
65
+
66
+ describe('<Resizable> props filtering', () => {
67
+ // Ensure everything in propTypes is represented here. Otherwise the next two tests are not valid
68
+ test('all intended props are in our props object', () => {
69
+ expect(['children', 'className', ...Object.keys(props)].sort()).toEqual(Object.keys(Resizable.propTypes).sort());
70
+ });
71
+
72
+ test('none of these props leak down to the child', () => {
73
+ const element = shallow(<ResizableBox {...props} />);
74
+ expect(Object.keys(element.find('div').props())).toEqual(['style']);
75
+ });
76
+
77
+ test('className is constructed properly', () => {
78
+ const element = shallow(<ResizableBox {...props} className='foo' />);
79
+ expect(element.find('div').props().className).toEqual(`foo`);
80
+ });
81
+ });
82
+
83
+ test('style prop', () => {
84
+ const element = shallow(<ResizableBox {...props} style={{backgroundColor: 'red'}}>{children}</ResizableBox>);
85
+ expect(element.find('div').at(0).prop('style')).toEqual({
86
+ width: '50px',
87
+ height: '50px',
88
+ backgroundColor: 'red'
89
+ });
90
+ });
91
+
92
+ test('style prop width and height ignored', () => {
93
+ const element = shallow(<ResizableBox {...props} style={{width: 10, height: 10}}>{children}</ResizableBox>);
94
+ expect(element.find('div').at(0).prop('style')).toEqual({
95
+ width: '50px',
96
+ height: '50px',
97
+ });
98
+ });
99
+ });