Skip to content

Commit 94842cc

Browse files
committed
Test reset scroll position after children update in CodePreTag
1 parent 4e6431e commit 94842cc

File tree

3 files changed

+83
-12
lines changed

3 files changed

+83
-12
lines changed
Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,97 @@
11
import React from 'react';
2+
import TestUtils from 'react-dom/test-utils';
23
import renderer from 'react-test-renderer';
4+
import { mount } from 'enzyme';
35
import 'jest-styled-components';
46

57
import CodePreTag from '../../src/components/CodePreTag';
68

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+
763
describe('<CodePreTag /> component', () => {
864
it('renders children properly', () => {
9-
const codeToRender = `
10-
function foo() {
11-
return 'bar';
12-
}
13-
`;
1465
const tree = renderer
1566
.create(
1667
<CodePreTag>
17-
<code>{codeToRender}</code>
68+
<code>{shortCodeToRender}</code>
1869
</CodePreTag>
1970
)
2071
.toJSON();
2172

2273
expect(tree).toMatchSnapshot();
2374
});
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+
});
2497
});

__tests__/components/ProgressBar.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ describe('<ProgressBar /> component', () => {
5353

5454
it('has styled-component rendered classes', () => {
5555
const tree = renderer.create(result).toJSON();
56-
// console.log(tree);
57-
5856
expect(tree.children[0].props.className).toBeDefined();
5957
});
6058

__tests__/components/__snapshots__/CodePreTag.test.jsx.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ exports[`<CodePreTag /> component renders children properly 1`] = `
44
<pre>
55
<code>
66
7-
function foo() {
8-
return 'bar';
9-
}
10-
7+
function foo() {
8+
return 'bar';
9+
}
10+
1111
</code>
1212
</pre>
1313
`;

0 commit comments

Comments
 (0)