Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 0f6aa70

Browse files
committed
Added tests before the v2 refactors
1 parent aea81bc commit 0f6aa70

10 files changed

+170
-15
lines changed

.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": [["@babel/env"]]
2+
"presets": [["@babel/preset-env"]]
33
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
yarn.lock
44
yarn-error.log
55
dist
6+
.cache

.npmignore

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ rollup.config.js
66
yarn.lock
77
yarn-error.log
88
browserslist
9+
karma.conf.js
10+
.cache
11+
.travis.yml
12+
tests

.travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- 10
4+
addons:
5+
chrome: stable
6+
script:
7+
- yarn build
8+
- yarn test:build
9+
- yarn test

CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# CHANGELOG
22

3+
## 2.0.0
4+
5+
- [BREAKING] ResizeObserver now always expects a function as a child, to which
6+
it passes in a reference as an argument that can be added to the element to be
7+
observed.
8+
- [BREAKING] Returning width and height as the second and third parameters
9+
instead of as part of an object
10+
- [BREAKING] Now reports 1x1 size on before the observer is triggered instead of
11+
0x0
12+
- Added tests using Karma
13+
14+
## 1.0.1
15+
16+
- Relaxed the prop-types dep
17+
318
## 1.0.0
419

520
- Initial release

README.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# @zeecoder/react-resize-observer
22

3-
A React component to use ResizeObserver instances.
3+
A React component that allows you to use a ResizeObserver to measure an element's size.
4+
5+
[![npm version](https://badge.fury.io/js/%40zeecoder%2Freact-resize-observer.svg)](https://npmjs.com/package/@zeecoder/react-resize-observer)
6+
[![build](https://travis-ci.org/ZeeCoder/react-resize-observer.svg?branch=master)](https://travis-ci.org/ZeeCoder/react-resize-observer)
47

58
## Install
69

@@ -15,9 +18,9 @@ npm install --save @zeecoder/react-resize-observer
1518
```js
1619
const App = () => (
1720
<ResizeObserver>
18-
{size => (
19-
<div>
20-
My size is {size.width}x{size.height}
21+
{(ref, width, height) => (
22+
<div ref={ref}>
23+
My size is {width}x{height}
2124
</div>
2225
)}
2326
</ResizeObserver>
@@ -26,17 +29,13 @@ const App = () => (
2629

2730
## Notes
2831

29-
- The children prop must be a function, through which the size changes are
30-
communicated
31-
- If the optional `element` prop is given, the the component will observe the
32-
size changes of that given element, instead of looking for the root DOM node
33-
inside the component. (This avoids calling `ReactDOM.findDOMNode(this)`.)
3432
- Uses [resize-observer-polyfill](https://github.com/que-etc/resize-observer-polyfill)
35-
internally, which falls back to the native ResizeObserver, if available
33+
internally, which falls back to the native ResizeObserver, if available.
3634

3735
## Related
3836

3937
- [use-resize-observer](https://github.com/ZeeCoder/use-resize-observer)
38+
- [@zeecoder/container-query](https://github.com/ZeeCoder/container-query)
4039

4140
## License
4241

karma.conf.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = function(config) {
2+
const singleRun = process.env.KARMA_SINGLE_RUN !== "false";
3+
const browsers = (process.env.KARMA_BROWSERS || "Chrome").split(",");
4+
5+
config.set({
6+
basePath: ".",
7+
frameworks: ["jasmine"],
8+
files: ["tests/dist/index.js"],
9+
autoWatch: true,
10+
11+
browsers,
12+
reporters: ["spec"],
13+
14+
singleRun,
15+
16+
// Max concurrency for SauceLabs OS plan
17+
concurrency: 5,
18+
19+
client: {
20+
jasmine: {
21+
// Order of the tests matter, so don't randomise it
22+
random: false
23+
}
24+
}
25+
});
26+
};

package.json

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "@zeecoder/react-resize-observer",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"main": "dist/bundle.cjs.js",
55
"module": "dist/bundle.esm.js",
66
"repository": "[email protected]:ZeeCoder/react-resize-observer.git",
7-
"description": "A React component to use ResizeObserver.",
7+
"description": "A React component that allows you to use a ResizeObserver to measure an element's size.",
88
"author": "Viktor Hubert <[email protected]>",
99
"license": "MIT",
1010
"scripts": {
1111
"build": "rollup -c",
12+
"test": "karma start",
13+
"test:build": "parcel build tests/index.js --out-dir tests/dist",
1214
"prepublish": "yarn build"
1315
},
1416
"husky": {
@@ -33,10 +35,22 @@
3335
"devDependencies": {
3436
"@babel/core": "^7.1.2",
3537
"@babel/preset-env": "^7.1.0",
38+
"babel-regenerator-runtime": "^6.5.0",
39+
"delay": "^4.1.0",
3640
"husky": "^1.1.2",
41+
"karma": "^3.1.4",
42+
"karma-chrome-launcher": "^2.2.0",
43+
"karma-jasmine": "^2.0.1",
44+
"karma-spec-reporter": "^0.0.32",
3745
"lint-staged": "^7.3.0",
46+
"parcel-bundler": "^1.11.0",
3847
"prettier": "^1.14.3",
48+
"react": "^16.7.0",
49+
"react-dom": "^16.7.0",
3950
"rollup": "^0.66.6",
4051
"rollup-plugin-babel": "^4.0.3"
52+
},
53+
"publishConfig": {
54+
"access": "public"
4155
}
4256
}

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class ResizeObserverComponent extends Component {
88
super(props);
99

1010
this.state = {
11-
width: 0,
12-
height: 0
11+
width: 1,
12+
height: 1
1313
};
1414
}
1515

tests/index.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
import ResizeObserver from "../dist/bundle.esm";
4+
import delay from "delay";
5+
// Using the following to support async/await in tests.
6+
// I'm intentionally not using babel/polyfill, as that would introduce polyfills
7+
// the actual lib might not have, giving the false impression that something
8+
// works while it might actually not, if you use the lib without babel-polyfill.
9+
import "babel-regenerator-runtime";
10+
11+
// const Observed = () => (
12+
// <ResizeObserver>
13+
// {(ref, width, height) => (
14+
// <div
15+
// ref={ref}
16+
// id="observed"
17+
// style={{
18+
// position: "absolute",
19+
// left: 0,
20+
// top: 0,
21+
// width: "100%",
22+
// height: "100%",
23+
// background: "grey",
24+
// color: "white",
25+
// fontWeight: "bold"
26+
// }}
27+
// >
28+
// {width}x{height}
29+
// </div>
30+
// )}
31+
// </ResizeObserver>
32+
// );
33+
34+
const Observed = () => (
35+
<ResizeObserver>
36+
{({ width, height }) => (
37+
<div
38+
id="observed"
39+
style={{
40+
position: "absolute",
41+
left: 0,
42+
top: 0,
43+
width: "100%",
44+
height: "100%",
45+
background: "grey",
46+
color: "white",
47+
fontWeight: "bold"
48+
}}
49+
>
50+
{width}x{height}
51+
</div>
52+
)}
53+
</ResizeObserver>
54+
);
55+
56+
beforeAll(() => {
57+
const app = document.createElement("div");
58+
app.style.position = "relative";
59+
app.style.width = "200px";
60+
app.style.height = "300px";
61+
document.body.appendChild(app);
62+
63+
ReactDOM.render(<Observed />, app);
64+
65+
global.app = app;
66+
global.observed = document.querySelector("#observed");
67+
});
68+
69+
// todo make sure parcel transpiles down to IE10 (example: async and "Set" doesn't work properly)
70+
// todo run in sauce labs with multiple browsers
71+
it("should render with 1x1 initially, before the ResizeObserver is triggered", async () => {
72+
expect(observed.textContent).toBe("1x1");
73+
});
74+
75+
it("should report the correct size after the size is reported by the ResizeObserver", async () => {
76+
await delay(100);
77+
78+
expect(observed.textContent).toBe("200x300");
79+
});
80+
81+
it("should report following size changes", async () => {
82+
app.style.width = "100px";
83+
app.style.height = "100px";
84+
85+
await delay(100);
86+
expect(observed.textContent).toBe("100x100");
87+
});

0 commit comments

Comments
 (0)