|
| 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