|  | 
|  | 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 | +beforeAll(() => { | 
|  | 35 | +  const app = document.createElement("div"); | 
|  | 36 | +  app.style.position = "relative"; | 
|  | 37 | +  app.style.width = "200px"; | 
|  | 38 | +  app.style.height = "300px"; | 
|  | 39 | +  document.body.appendChild(app); | 
|  | 40 | + | 
|  | 41 | +  ReactDOM.render(<Observed />, app); | 
|  | 42 | + | 
|  | 43 | +  global.app = app; | 
|  | 44 | +  global.observed = document.querySelector("#observed"); | 
|  | 45 | +}); | 
|  | 46 | + | 
|  | 47 | +it("should render with 1x1 initially, before the ResizeObserver is triggered", async () => { | 
|  | 48 | +  expect(observed.textContent).toBe("1x1"); | 
|  | 49 | +}); | 
|  | 50 | + | 
|  | 51 | +it("should report the correct size after the size is reported by the ResizeObserver", async () => { | 
|  | 52 | +  await delay(100); | 
|  | 53 | + | 
|  | 54 | +  expect(observed.textContent).toBe("200x300"); | 
|  | 55 | +}); | 
|  | 56 | + | 
|  | 57 | +it("should report following size changes", async () => { | 
|  | 58 | +  app.style.width = "100px"; | 
|  | 59 | +  app.style.height = "100px"; | 
|  | 60 | + | 
|  | 61 | +  await delay(100); | 
|  | 62 | +  expect(observed.textContent).toBe("100x100"); | 
|  | 63 | +}); | 
0 commit comments