Guest User

Untitled

a guest
May 24th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. /* eslint-disable react/prop-types */
  2. import React from 'react';
  3. import ReactDOM from 'react-dom';
  4. import ReactTestUtils from 'react-dom/test-utils';
  5. import FastScrollComponent from './FastScrollComponent';
  6.  
  7. describe('<FastScrollComponent>', function() {
  8. const setupComponent = ({ cacheWhenNotVisible = false, height = 100 }) => {
  9. // eslint-disable-line react/prop-types
  10. const rowHeight = 25;
  11. const rowWidth = 1000;
  12.  
  13. const rowCount = 100;
  14. this.layers = new Array(rowCount);
  15.  
  16. return (
  17. <div
  18. style={{
  19. width: rowWidth,
  20. height: height,
  21. position: 'relative',
  22. }}
  23. >
  24. <FastScrollComponent.Container
  25. width={rowWidth}
  26. height={rowCount * rowHeight}
  27. overscanPx={0}
  28. snapPx={1}
  29. ref={(el) => {
  30. this.container = el;
  31. }}
  32. >
  33. {new Array(rowCount).fill(0).map((_, x) => (
  34. <FastScrollComponent.Layer
  35. key={x}
  36. left={0}
  37. top={rowHeight * x}
  38. width={rowWidth}
  39. height={rowHeight}
  40. ref={(el) => {
  41. this.layers[x] = el;
  42. }}
  43. cacheWhenNotVisible={cacheWhenNotVisible}
  44. >
  45. {() => <div style={{ height: rowHeight }}>{x}</div>}
  46. </FastScrollComponent.Layer>
  47. ))}
  48. </FastScrollComponent.Container>
  49. </div>
  50. );
  51. };
  52.  
  53. describe('cacheWhenNotVisible is false', () => {
  54. beforeEach(() => {
  55. this.component = window.renderComponent(setupComponent({}));
  56. });
  57.  
  58. it('displays the top four elements and hides the rest', () => {
  59. expect(this.layers[3].state.rendered).toBeTruthy();
  60. expect(this.layers[4].state.rendered).toBeFalsy();
  61. });
  62.  
  63. it('un-renders the top elements when scrolled', () => {
  64. this.container._root.scrollTop = 100; // scroll 4th element out
  65. ReactTestUtils.Simulate.scroll(this.container._root);
  66. jasmine.clock().tick(100);
  67.  
  68. expect(this.layers[3].state.rendered).toBeFalsy();
  69. expect(this.layers[4].state.rendered).toBeTruthy();
  70. });
  71.  
  72. it('renders additional elements when height is changed', () => {
  73. window.renderComponentAgain(this.component, setupComponent({ height: 200 }));
  74. jasmine.clock().tick(100); // trigger throttled update
  75.  
  76. expect(this.layers[0].state.rendered).toBeTruthy();
  77. expect(this.layers[7].state.rendered).toBeTruthy();
  78. expect(this.layers[8].state.rendered).toBeFalsy();
  79. });
  80. });
  81.  
  82. describe('cacheWhenNotVisible is true', () => {
  83. beforeEach(() => {
  84. this.component = window.renderComponent(
  85. setupComponent({
  86. cacheWhenNotVisible: true,
  87. }),
  88. );
  89. });
  90.  
  91. it('displays the top four elements and hides the rest', () => {
  92. expect(this.layers[3].state.rendered).toBeTruthy();
  93. expect(ReactDOM.findDOMNode(this.layers[3]).style.display).toEqual('');
  94. expect(this.layers[4].state.rendered).toBeFalsy();
  95. });
  96.  
  97. it('hides the top elements when scrolled', () => {
  98. this.container._root.scrollTop = 100; // scroll 4th element out
  99. ReactTestUtils.Simulate.scroll(this.container._root);
  100. jasmine.clock().tick(100);
  101.  
  102. expect(this.layers[3].state.rendered).toBeTruthy();
  103. expect(ReactDOM.findDOMNode(this.layers[3]).style.display).toEqual('none');
  104. expect(this.layers[4].state.rendered).toBeTruthy();
  105. expect(ReactDOM.findDOMNode(this.layers[4]).style.display).toEqual('');
  106. });
  107. });
  108. });
Add Comment
Please, Sign In to add comment