|
| 1 | +import React from 'react' |
| 2 | +import { findDOMNode } from 'react-dom' |
| 3 | +import { render } from '../TestUtils' |
| 4 | +import ArrowKeyStepper from './ArrowKeyStepper' |
| 5 | +import { Simulate } from 'react-addons-test-utils' |
| 6 | + |
| 7 | +function renderTextContent (scrollToColumn, scrollToRow) { |
| 8 | + return `scrollToColumn:${scrollToColumn}, scrollToRow:${scrollToRow}` |
| 9 | +} |
| 10 | + |
| 11 | +function ChildComponent ({ scrollToColumn, scrollToRow }) { |
| 12 | + return ( |
| 13 | + <div>{renderTextContent(scrollToColumn, scrollToRow)}</div> |
| 14 | + ) |
| 15 | +} |
| 16 | + |
| 17 | +describe('ArrowKeyStepper', () => { |
| 18 | + function renderHelper ({ |
| 19 | + columnsCount = 10, |
| 20 | + rowsCount = 10 |
| 21 | + } = {}) { |
| 22 | + let onSectionRenderedCallback |
| 23 | + |
| 24 | + const node = findDOMNode(render( |
| 25 | + <ArrowKeyStepper |
| 26 | + columnsCount={columnsCount} |
| 27 | + rowsCount={rowsCount} |
| 28 | + > |
| 29 | + {({ onSectionRendered, scrollToColumn, scrollToRow }) => { |
| 30 | + onSectionRenderedCallback = onSectionRendered |
| 31 | + |
| 32 | + return ( |
| 33 | + <ChildComponent |
| 34 | + scrollToColumn={scrollToColumn} |
| 35 | + scrollToRow={scrollToRow} |
| 36 | + /> |
| 37 | + ) |
| 38 | + }} |
| 39 | + </ArrowKeyStepper> |
| 40 | + )) |
| 41 | + |
| 42 | + return { |
| 43 | + node, |
| 44 | + onSectionRendered: onSectionRenderedCallback |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + function assertCurrentScrollTo (node, scrollToColumn, scrollToRow) { |
| 49 | + expect(node.textContent).toEqual(renderTextContent(scrollToColumn, scrollToRow)) |
| 50 | + } |
| 51 | + |
| 52 | + it('should update :scrollToColumn and :scrollToRow in response to arrow keys', () => { |
| 53 | + const { node } = renderHelper() |
| 54 | + assertCurrentScrollTo(node, 0, 0) |
| 55 | + Simulate.keyDown(node, {key: 'ArrowDown'}) |
| 56 | + assertCurrentScrollTo(node, 0, 1) |
| 57 | + Simulate.keyDown(node, {key: 'ArrowRight'}) |
| 58 | + assertCurrentScrollTo(node, 1, 1) |
| 59 | + Simulate.keyDown(node, {key: 'ArrowUp'}) |
| 60 | + assertCurrentScrollTo(node, 1, 0) |
| 61 | + Simulate.keyDown(node, {key: 'ArrowLeft'}) |
| 62 | + assertCurrentScrollTo(node, 0, 0) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should not scroll past the row and column boundaries provided', () => { |
| 66 | + const { node } = renderHelper({ |
| 67 | + columnsCount: 2, |
| 68 | + rowsCount: 2 |
| 69 | + }) |
| 70 | + Simulate.keyDown(node, {key: 'ArrowDown'}) |
| 71 | + Simulate.keyDown(node, {key: 'ArrowDown'}) |
| 72 | + Simulate.keyDown(node, {key: 'ArrowDown'}) |
| 73 | + assertCurrentScrollTo(node, 0, 1) |
| 74 | + Simulate.keyDown(node, {key: 'ArrowUp'}) |
| 75 | + Simulate.keyDown(node, {key: 'ArrowUp'}) |
| 76 | + Simulate.keyDown(node, {key: 'ArrowUp'}) |
| 77 | + assertCurrentScrollTo(node, 0, 0) |
| 78 | + Simulate.keyDown(node, {key: 'ArrowRight'}) |
| 79 | + Simulate.keyDown(node, {key: 'ArrowRight'}) |
| 80 | + Simulate.keyDown(node, {key: 'ArrowRight'}) |
| 81 | + assertCurrentScrollTo(node, 1, 0) |
| 82 | + Simulate.keyDown(node, {key: 'ArrowLeft'}) |
| 83 | + Simulate.keyDown(node, {key: 'ArrowLeft'}) |
| 84 | + Simulate.keyDown(node, {key: 'ArrowLeft'}) |
| 85 | + assertCurrentScrollTo(node, 0, 0) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should update :scrollToColumn and :scrollToRow relative to the most recent :onSectionRendered event', () => { |
| 89 | + const { node, onSectionRendered } = renderHelper() |
| 90 | + onSectionRendered({ // Simulate a scroll |
| 91 | + columnStartIndex: 0, |
| 92 | + columnStopIndex: 4, |
| 93 | + rowStartIndex: 4, |
| 94 | + rowStopIndex: 6 |
| 95 | + }) |
| 96 | + Simulate.keyDown(node, {key: 'ArrowDown'}) |
| 97 | + assertCurrentScrollTo(node, 0, 7) |
| 98 | + |
| 99 | + onSectionRendered({ // Simulate a scroll |
| 100 | + columnStartIndex: 5, |
| 101 | + columnStopIndex: 10, |
| 102 | + rowStartIndex: 2, |
| 103 | + rowStopIndex: 4 |
| 104 | + }) |
| 105 | + Simulate.keyDown(node, {key: 'ArrowUp'}) |
| 106 | + assertCurrentScrollTo(node, 0, 1) |
| 107 | + |
| 108 | + onSectionRendered({ // Simulate a scroll |
| 109 | + columnStartIndex: 4, |
| 110 | + columnStopIndex: 8, |
| 111 | + rowStartIndex: 5, |
| 112 | + rowStopIndex: 10 |
| 113 | + }) |
| 114 | + Simulate.keyDown(node, {key: 'ArrowRight'}) |
| 115 | + assertCurrentScrollTo(node, 9, 1) |
| 116 | + |
| 117 | + onSectionRendered({ // Simulate a scroll |
| 118 | + columnStartIndex: 2, |
| 119 | + columnStopIndex: 4, |
| 120 | + rowStartIndex: 2, |
| 121 | + rowStopIndex: 4 |
| 122 | + }) |
| 123 | + Simulate.keyDown(node, {key: 'ArrowLeft'}) |
| 124 | + assertCurrentScrollTo(node, 1, 1) |
| 125 | + }) |
| 126 | +}) |
0 commit comments