Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.

Commit a207b0d

Browse files
태재영Matt Goo
authored andcommitted
fix(top-app-bar): change scrollTop to offsetTop in getViewportScrollY (#771)
1 parent ffd7776 commit a207b0d

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

packages/top-app-bar/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class TopAppBar<T extends HTMLElement = HTMLHeadingElement> extends React.Compon
208208
},
209209
getViewportScrollY: () => {
210210
return (this.state.scrollTarget && this.state.scrollTarget.current)
211-
? this.state.scrollTarget.current.offsetTop
211+
? this.state.scrollTarget.current.scrollTop
212212
: window.pageYOffset;
213213
},
214214
getTotalActionItems: () => {

test/unit/chips/ChipSet.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ test('#adapter.setSelected adds selectedChipId to state', () => {
8585
wrapper.instance().adapter.setSelected();
8686
assert.isTrue(wrapper.state().selectedChipIds.indexOf('1') > -1);
8787
td.verify(handleSelect(['1']), {times: 1});
88-
td.verify(getSelectedChipIds(), {times: 1});
88+
assert.equal(1, td.explain(getSelectedChipIds).callCount);
8989
});
9090

9191
test('#chip.props.setSelected calls both #chip.props.handleSelect and #chipSet.props.handleSelect', () => {
@@ -407,7 +407,7 @@ test('chip is rendered computeBoundingRect method prop if is filter variant', ()
407407
width: 1,
408408
});
409409
chip.props.computeBoundingRect(chipElement);
410-
td.verify(chipElement.getBoundingClientRect(), {times: 1});
410+
assert.equal(1, td.explain(chipElement.getBoundingClientRect).callCount);
411411
});
412412

413413
test('chip is rendered with computeBoundingRect method prop if is not filter variant', () => {

test/unit/text-field/index.test.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,18 +314,20 @@ test('#adapter.label.hasLabel returns true if label exists', () => {
314314
assert.isTrue(wrapper.state().foundation.adapter_.hasLabel());
315315
});
316316

317-
test('#adapter.label.hasLabel returns true if label exists', () => {
317+
test('#adapter.label.getLabelWidth returns offsetWidth of labelElement', () => {
318318
const div = document.createElement('div');
319-
document.body.append(div);
320-
const options = {attachTo: div};
319+
document.body.appendChild(div);
320+
321321
const wrapper = mount<TextField<HTMLInputElement>>(
322322
<TextField label='my label'>
323323
<Input />
324324
</TextField>,
325-
options
325+
{attachTo: div}
326326
);
327-
assert.equal(wrapper.state().foundation.adapter_.getLabelWidth(), 56);
328-
div.remove();
327+
const labelElement = wrapper.instance().floatingLabelElement.current!.labelElement_;
328+
assert.equal(wrapper.state().foundation.adapter_.getLabelWidth(), labelElement.current!.offsetWidth);
329+
330+
document.body.removeChild(div);
329331
});
330332

331333
test('#adapter.label.getLabelWidth returns state.initialLabelWidth', () => {

test/unit/top-app-bar/index.test.tsx

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ class TopAppBarWithScroll extends React.Component<ScrollProps, ScrollState> {
4343
<TopAppBarSection><TopAppBarTitle>Scroll</TopAppBarTitle></TopAppBarSection>
4444
</TopAppBarRow>
4545
</TopAppBar>
46-
<div ref={this.state.scrollRef}>Scroll Target</div>
46+
<div ref={this.state.scrollRef} style={{height: '1000px', overflow: 'auto'}}>
47+
<div style={{height: '3000px'}}>Scroll Target</div>
48+
</div>
4749
</div>
4850
);
4951
}
@@ -245,6 +247,37 @@ test(
245247
}
246248
);
247249

250+
test('#adapter.getViewportScrollY returns same value with scrollTarget.scrollTop', () => {
251+
const div = document.createElement('div');
252+
document.body.appendChild(div);
253+
254+
const wrapper = mount<TopAppBarWithScroll>(<TopAppBarWithScroll withRef={true} />, {attachTo: div});
255+
const topAppBar: TopAppBar = coerceForTesting<TopAppBar>(wrapper.find('TopAppBar').instance());
256+
const scrollTarget = topAppBar.state.scrollTarget!.current!;
257+
assert.equal(scrollTarget.scrollTop, topAppBar.adapter.getViewportScrollY());
258+
259+
// https://github.com/material-components/material-components-web-react/pull/771
260+
// We commented out the code below because linux puppeteer version doesn't scroll.
261+
// const scrollY = 150;
262+
// scrollTarget.scrollTo(0, scrollY);
263+
// assert.equal(scrollY, topAppBar.adapter.getViewportScrollY());
264+
265+
document.body.removeChild(div);
266+
});
267+
268+
test('#adapter.getViewportScrollY test for changing scrollTarget', () => {
269+
const div = document.createElement('div');
270+
document.body.appendChild(div);
271+
272+
const wrapper = mount<TopAppBarWithScroll>(<TopAppBarWithScroll withRef={false} />, {attachTo: div});
273+
const topAppBar: TopAppBar = coerceForTesting<TopAppBar>(wrapper.find('TopAppBar').instance());
274+
assert.equal(window.pageYOffset, topAppBar.adapter.getViewportScrollY());
275+
wrapper.setState({withRef: true});
276+
277+
const scrollTarget = topAppBar.state.scrollTarget!.current!;
278+
assert.equal(scrollTarget.scrollTop, topAppBar.adapter.getViewportScrollY());
279+
document.body.removeChild(div);
280+
});
248281

249282
test('#adapter.getTotalActionItems returns one with one actionItem passed', () => {
250283
const wrapper = mount<TopAppBar>(
@@ -289,7 +322,7 @@ test('#adapter.getTopAppBarHeight should return clientHeight', () => {
289322
const div = document.createElement('div');
290323
// needs to be attached to real DOM to get width
291324
// https://github.com/airbnb/enzyme/issues/1525
292-
document.body.append(div);
325+
document.body.appendChild(div);
293326
const options = {attachTo: div};
294327
const wrapper = mount<TopAppBar>(
295328
<TopAppBar>
@@ -300,7 +333,7 @@ test('#adapter.getTopAppBarHeight should return clientHeight', () => {
300333
const topAppBarHeight = wrapper.instance().adapter.getTopAppBarHeight();
301334
const realDOMHeight = wrapper.getDOMNode().clientHeight;
302335
assert.equal(topAppBarHeight, realDOMHeight);
303-
div.remove();
336+
document.body.removeChild(div);
304337
});
305338

306339
test('when changes from short to fixed the foundation changes', () => {

0 commit comments

Comments
 (0)