Skip to content

Commit 4e1383c

Browse files
committed
Added some edge-case tests to Grid. Batched up some scrollLeft/scrollTop state-updates.
1 parent 001142e commit 4e1383c

2 files changed

Lines changed: 43 additions & 17 deletions

File tree

source/Grid/Grid.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,24 +271,22 @@ export default class Grid extends Component {
271271
componentWillUpdate (nextProps, nextState) {
272272
if (
273273
nextProps.columnsCount === 0 &&
274-
nextState.scrollLeft !== 0
275-
) {
276-
this._setScrollPosition({ scrollLeft: 0 })
277-
}
278-
279-
if (
274+
nextState.scrollLeft !== 0 ||
280275
nextProps.rowsCount === 0 &&
281276
nextState.scrollTop !== 0
282277
) {
283-
this._setScrollPosition({ scrollTop: 0 })
284-
}
285-
286-
if (nextProps.scrollLeft !== this.props.scrollLeft) {
287-
this._setScrollPosition({ scrollLeft: nextProps.scrollLeft })
288-
}
289-
290-
if (nextProps.scrollTop !== this.props.scrollTop) {
291-
this._setScrollPosition({ scrollTop: nextProps.scrollTop })
278+
this._setScrollPosition({
279+
scrollLeft: 0,
280+
scrollTop: 0
281+
})
282+
} else if (
283+
nextProps.scrollLeft !== this.props.scrollLeft ||
284+
nextProps.scrollTop !== this.props.scrollTop
285+
) {
286+
this._setScrollPosition({
287+
scrollLeft: nextProps.scrollLeft,
288+
scrollTop: nextProps.scrollTop
289+
})
292290
}
293291

294292
computeCellMetadataAndUpdateScrollOffsetHelper({

source/Grid/Grid.test.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,36 @@ describe('Grid', () => {
138138
expect(grid.state.scrollTop).toEqual(1900)
139139
})
140140

141-
// @TODO Test updating with new row + scrollTop, removing row, scrolling, then re-adding row (with same scrollTop)
142-
// @TODO Test updating with new row + scrollToIndex, removing row, scrolling, then re-adding row (with same scrollToIndex)
141+
it('should scroll to a row and column just added', () => {
142+
let grid = render(getMarkup())
143+
expect(grid.state.scrollLeft).toEqual(0)
144+
expect(grid.state.scrollTop).toEqual(0)
145+
grid = render(getMarkup({
146+
columnsCount: NUM_COLUMNS + 1,
147+
rowsCount: NUM_ROWS + 1,
148+
scrollToColumn: NUM_COLUMNS,
149+
scrollToRow: NUM_ROWS
150+
}))
151+
expect(grid.state.scrollLeft).toEqual(2350)
152+
expect(grid.state.scrollTop).toEqual(1920)
153+
})
154+
155+
it('should scroll back to a newly-added cell without a change in prop', () => {
156+
let grid = render(getMarkup({
157+
columnsCount: NUM_COLUMNS,
158+
rowsCount: NUM_ROWS,
159+
scrollToColumn: NUM_COLUMNS,
160+
scrollToRow: NUM_ROWS
161+
}))
162+
grid = render(getMarkup({
163+
columnsCount: NUM_COLUMNS + 1,
164+
rowsCount: NUM_ROWS + 1,
165+
scrollToColumn: NUM_COLUMNS,
166+
scrollToRow: NUM_ROWS
167+
}))
168+
expect(grid.state.scrollLeft).toEqual(2350)
169+
expect(grid.state.scrollTop).toEqual(1920)
170+
})
143171
})
144172

145173
describe('property updates', () => {

0 commit comments

Comments
 (0)