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