|
1 | 1 | import React from 'react';
|
| 2 | +import TestUtils from 'react-dom/test-utils'; |
2 | 3 | import renderer from 'react-test-renderer';
|
| 4 | +import { mount } from 'enzyme'; |
3 | 5 | import 'jest-styled-components';
|
4 | 6 |
|
5 | 7 | import CodePreTag from '../../src/components/CodePreTag';
|
6 | 8 |
|
| 9 | +const shortCodeToRender = ` |
| 10 | + function foo() { |
| 11 | + return 'bar'; |
| 12 | + } |
| 13 | +`; |
| 14 | + |
| 15 | +const longCodeToRender = ` |
| 16 | + function EquipmentPattern(name) { |
| 17 | + this.equipments = []; |
| 18 | + this.name = name; |
| 19 | + } |
| 20 | +
|
| 21 | + EquipmentPattern.prototype.add = function(equipment) { |
| 22 | + this.equipments.push(equipment); |
| 23 | + }; |
| 24 | +
|
| 25 | + EquipmentPattern.prototype.getPrice = function() { |
| 26 | + return this.equipments |
| 27 | + .map(function(equipment) { |
| 28 | + return equipment.getPrice(); |
| 29 | + }) |
| 30 | + .reduce(function(a, b) { |
| 31 | + return a + b; |
| 32 | + }); |
| 33 | + }; |
| 34 | +
|
| 35 | + function Equipment() {} |
| 36 | +
|
| 37 | + Equipment.prototype.getPrice = function() { |
| 38 | + return this.price; |
| 39 | + }; |
| 40 | +
|
| 41 | + // -- leafs |
| 42 | + function FloppyDisk() { |
| 43 | + this.name = 'Floppy Disk'; |
| 44 | + this.price = 70; |
| 45 | + } |
| 46 | + FloppyDisk.prototype = Object.create(Equipment.prototype); |
| 47 | +
|
| 48 | + function HardDrive() { |
| 49 | + this.name = 'Hard Drive'; |
| 50 | + this.price = 250; |
| 51 | + } |
| 52 | + HardDrive.prototype = Object.create(Equipment.prototype); |
| 53 | +
|
| 54 | + function Memory() { |
| 55 | + this.name = '8gb memomry'; |
| 56 | + this.price = 280; |
| 57 | + } |
| 58 | + Memory.prototype = Object.create(Equipment.prototype); |
| 59 | +
|
| 60 | + module.exports = [EquipmentPattern, FloppyDisk, HardDrive, Memory]; |
| 61 | +`; |
| 62 | + |
7 | 63 | describe('<CodePreTag /> component', () => {
|
8 | 64 | it('renders children properly', () => {
|
9 |
| - const codeToRender = ` |
10 |
| - function foo() { |
11 |
| - return 'bar'; |
12 |
| - } |
13 |
| - `; |
14 | 65 | const tree = renderer
|
15 | 66 | .create(
|
16 | 67 | <CodePreTag>
|
17 |
| - <code>{codeToRender}</code> |
| 68 | + <code>{shortCodeToRender}</code> |
18 | 69 | </CodePreTag>
|
19 | 70 | )
|
20 | 71 | .toJSON();
|
21 | 72 |
|
22 | 73 | expect(tree).toMatchSnapshot();
|
23 | 74 | });
|
| 75 | + |
| 76 | + it('scrolls to position 0,0 on children change', () => { |
| 77 | + const container = mount( |
| 78 | + <CodePreTag style={{ height: 200 }}> |
| 79 | + <code>{longCodeToRender}</code> |
| 80 | + </CodePreTag> |
| 81 | + ); |
| 82 | + const $container = container.getDOMNode(); |
| 83 | + |
| 84 | + // Initially the scroll position must be at the top |
| 85 | + expect($container.scrollTop).toBe(0); |
| 86 | + |
| 87 | + $container.scrollTop = 100; |
| 88 | + |
| 89 | + // Test that the scroll position is updated correctly |
| 90 | + expect($container.scrollTop).toBe(100); |
| 91 | + |
| 92 | + container.setProps({ children: shortCodeToRender }); |
| 93 | + |
| 94 | + // Test that the scroll position is reset after changing its children |
| 95 | + expect($container.scrollTop).toBe(0); |
| 96 | + }); |
24 | 97 | });
|
0 commit comments