autosizer.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import * as React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import AutoSizer from '../autoSizer';
  4. function render(markup) {
  5. if (!render._mountNode) {
  6. render._mountNode = document.createElement('div');
  7. // Unless we attach the mount-node to body, getBoundingClientRect() won't work
  8. document.body.appendChild(render._mountNode);
  9. afterEach(render.unmount);
  10. // window.ResizeObserver = ResizeObserver;
  11. }
  12. return ReactDOM.render(markup, render._mountNode);
  13. }
  14. render.unmount = function () {
  15. if (render._mountNode) {
  16. ReactDOM.unmountComponentAtNode(render._mountNode);
  17. document.body.removeChild(render._mountNode);
  18. render._mountNode = null;
  19. }
  20. };
  21. describe('AutoSizer', () => {
  22. function getAutoSizer({
  23. defaultHeight = undefined,
  24. defaultWidth = undefined,
  25. height = 100,
  26. width = 200,
  27. } = {}) {
  28. const wrapperStyle = {
  29. boxSizing: 'border-box',
  30. height,
  31. width,
  32. };
  33. mockOffsetSize(width, height);
  34. return (
  35. <div style={wrapperStyle}>
  36. <AutoSizer
  37. defaultHeight={defaultHeight}
  38. defaultWidth={defaultWidth}
  39. >
  40. {({ height, width }) => (
  41. <div style={{
  42. width,
  43. height,
  44. }}>
  45. {`width:${width}, height:${height}`}
  46. </div>
  47. )}
  48. </AutoSizer>
  49. </div>
  50. );
  51. }
  52. // AutoSizer uses offsetWidth and offsetHeight.
  53. // Jest runs in JSDom which doesn't support measurements APIs.
  54. // refer to https://github.com/bvaughn/react-virtualized-auto-sizer/blob/master/src/__tests__/AutoSizer.js
  55. function mockOffsetSize(width, height) {
  56. Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
  57. configurable: true,
  58. value: height,
  59. });
  60. }
  61. it('should set the correct initial width and height of ChildComponent or React child', () => {
  62. // TODO
  63. // expect(rendered.textContent).toContain('height:100');
  64. // expect(rendered.textContent).toContain('width:100%');
  65. });
  66. async function simulateResize({ element, height, width }) {
  67. mockOffsetSize(width, height);
  68. window.dispatchEvent(new Event('resize'));
  69. // Allow requestAnimationFrame to be invoked before continuing
  70. await new Promise(resolve => setTimeout(resolve, 100));
  71. }
  72. it('should update :height after a resize event', async done => {
  73. render(
  74. getAutoSizer({
  75. height: 100,
  76. }),
  77. );
  78. // 直接使用 mountNode 替代
  79. const rendered = render._mountNode;
  80. // expect(rendered.textContent).toContain('height:100');
  81. await simulateResize({ element: rendered, height: 400, });
  82. // TODO
  83. // expect(rendered.textContent).toContain('height:400');
  84. done();
  85. });
  86. });