Skip to content

Commit

Permalink
Add unit test to IrisGridCopyHander
Browse files Browse the repository at this point in the history
  • Loading branch information
georgecwan committed Dec 22, 2023
1 parent 4678f65 commit 1eb45ec
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
14 changes: 12 additions & 2 deletions packages/grid/src/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ class Grid extends PureComponent<GridProps, GridState> {
this.handleEditCellCommit = this.handleEditCellCommit.bind(this);
this.handleDoubleClick = this.handleDoubleClick.bind(this);
this.notifyKeyboardHandlers = this.notifyKeyboardHandlers.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleKeyUp = this.handleKeyUp.bind(this);
this.handleMouseDown = this.handleMouseDown.bind(this);
this.handleMouseDrag = this.handleMouseDrag.bind(this);
this.handleMouseMove = this.handleMouseMove.bind(this);
Expand Down Expand Up @@ -1741,6 +1743,14 @@ class Grid extends PureComponent<GridProps, GridState> {
}
}

handleKeyDown(event: GridKeyboardEvent): void {
this.notifyKeyboardHandlers('onDown', event);
}

handleKeyUp(event: GridKeyboardEvent): void {
this.notifyKeyboardHandlers('onUp', event);
}

/**
* Notify all of the mouse handlers for this grid of a mouse event.
* @param functionName The name of the function in the mouse handler to call
Expand Down Expand Up @@ -2237,8 +2247,8 @@ class Grid extends PureComponent<GridProps, GridState> {
onClick={this.handleClick}
onContextMenu={this.handleContextMenu}
onDoubleClick={this.handleDoubleClick}
onKeyDown={e => this.notifyKeyboardHandlers('onDown', e)}
onKeyUp={e => this.notifyKeyboardHandlers('onUp', e)}
onKeyDown={this.handleKeyDown}
onKeyUp={this.handleKeyUp}
onMouseDown={this.handleMouseDown}
onMouseMove={this.handleMouseMove}
onMouseLeave={this.handleMouseLeave}
Expand Down
49 changes: 39 additions & 10 deletions packages/iris-grid/src/IrisGridCopyHandler.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { GridTestUtils } from '@deephaven/grid';
import { copyToClipboard } from '@deephaven/utils';
import dh from '@deephaven/jsapi-shim';
import IrisGridTestUtils from './IrisGridTestUtils';
import IrisGridCopyHandler, { CopyOperation } from './IrisGridCopyHandler';
import IrisGridCopyHandler, {
CopyOperation,
CopyHeaderOperation,
CopyRangesOperation,
} from './IrisGridCopyHandler';
import IrisGridProxyModel from './IrisGridProxyModel';

jest.mock('@deephaven/utils', () => ({
...jest.requireActual('@deephaven/utils'),
Expand All @@ -29,12 +34,12 @@ function makeSnapshotFn() {
return jest.fn(() => Promise.resolve(DEFAULT_EXPECTED_TEXT));
}

function makeCopyOperation(
function makeCopyRangesOperation(
ranges = GridTestUtils.makeRanges(),
includeHeaders = false,
movedColumns = [],
userColumnWidths = IrisGridTestUtils.makeUserColumnWidths()
): CopyOperation {
): CopyRangesOperation {
return {
ranges,
includeHeaders,
Expand All @@ -43,16 +48,29 @@ function makeCopyOperation(
};
}

function makeCopyHeaderOperation(
columnIndex = 0,
columnDepth = 0,
movedColumns = []
): CopyHeaderOperation {
return {
columnIndex,
columnDepth,
movedColumns,
};
}

function makeModel() {
const model = irisGridTestUtils.makeModel();
model.textSnapshot = makeSnapshotFn();
model.textForColumnHeader = jest.fn((c: number) => c.toString());
return model;
}

function mountCopySelection({
model = makeModel(),
copyOperation = makeCopyOperation(),
} = {}) {
copyOperation = makeCopyRangesOperation(),
}: { model?: IrisGridProxyModel; copyOperation?: CopyOperation } = {}) {
return render(
<IrisGridCopyHandler model={model} copyOperation={copyOperation} />
);
Expand All @@ -66,9 +84,20 @@ it('renders without crashing', () => {
mountCopySelection();
});

it('copies column header', async () => {
const copyOperation = makeCopyHeaderOperation();
const model = makeModel();
mountCopySelection({ copyOperation, model });
screen.getByRole('progressbar', { hidden: true });
screen.getByText('Fetching header for clipboard...');
expect(model.textForColumnHeader).toHaveBeenCalled();

await waitFor(() => expect(copyToClipboard).toHaveBeenCalledWith('0'));
});

it('copies immediately if less than 10,000 rows of data', async () => {
const ranges = GridTestUtils.makeRanges(1, 10000);
const copyOperation = makeCopyOperation(ranges);
const copyOperation = makeCopyRangesOperation(ranges);
const model = makeModel();
mountCopySelection({ copyOperation, model });
screen.getByRole('progressbar', { hidden: true });
Expand All @@ -84,7 +113,7 @@ it('prompts to copy if more than 10,000 rows of data', async () => {
const user = userEvent.setup({ delay: null });
const model = makeModel();
const ranges = GridTestUtils.makeRanges(1, 10001);
const copyOperation = makeCopyOperation(ranges);
const copyOperation = makeCopyRangesOperation(ranges);
mountCopySelection({ copyOperation, model });
const copyBtn = screen.getByText('Copy');
expect(copyBtn).toBeTruthy();
Expand Down Expand Up @@ -112,7 +141,7 @@ it('shows click to copy if async copy fails', async () => {
mockedCopyToClipboard.mockReturnValueOnce(Promise.reject(error));

const ranges = GridTestUtils.makeRanges();
const copyOperation = makeCopyOperation(ranges);
const copyOperation = makeCopyRangesOperation(ranges);
mountCopySelection({ copyOperation });

await waitFor(() =>
Expand All @@ -138,7 +167,7 @@ it('shows click to copy if async copy fails', async () => {
it('retry option available if fetching fails', async () => {
const user = userEvent.setup({ delay: null });
const ranges = GridTestUtils.makeRanges();
const copyOperation = makeCopyOperation(ranges);
const copyOperation = makeCopyRangesOperation(ranges);
const model = makeModel();
model.textSnapshot = jest.fn(() => Promise.reject());

Expand Down Expand Up @@ -166,7 +195,7 @@ it('shows an error if the copy fails permissions', async () => {
mockedCopyToClipboard.mockReturnValueOnce(Promise.reject(error));

const ranges = GridTestUtils.makeRanges();
const copyOperation = makeCopyOperation(ranges);
const copyOperation = makeCopyRangesOperation(ranges);
mountCopySelection({ copyOperation });

await waitFor(() =>
Expand Down
15 changes: 10 additions & 5 deletions packages/iris-grid/src/IrisGridCopyHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,17 @@ class IrisGridCopyHandler extends Component<
async startFetch(): Promise<void> {
this.stopFetch();

this.setState({
buttonState: IrisGridCopyHandler.BUTTON_STATES.FETCH_IN_PROGRESS,
copyState: IrisGridCopyHandler.COPY_STATES.FETCH_RANGES_IN_PROGRESS,
});

const { model, copyOperation } = this.props;

if (isCopyHeaderOperation(copyOperation)) {
const { columnIndex, columnDepth, movedColumns } = copyOperation;
log.debug('startFetch copyHeader', columnIndex, columnDepth);

this.setState({
buttonState: IrisGridCopyHandler.BUTTON_STATES.FETCH_IN_PROGRESS,
copyState: IrisGridCopyHandler.COPY_STATES.FETCH_HEADER_IN_PROGRESS,
});

const modelIndex = GridUtils.getModelIndex(columnIndex, movedColumns);
const copyText = model.textForColumnHeader(modelIndex, columnDepth);
if (copyText === undefined) {
Expand All @@ -340,6 +340,11 @@ class IrisGridCopyHandler extends Component<
} = copyOperation;
log.debug('startFetch copyRanges', ranges);

this.setState({
buttonState: IrisGridCopyHandler.BUTTON_STATES.FETCH_IN_PROGRESS,
copyState: IrisGridCopyHandler.COPY_STATES.FETCH_RANGES_IN_PROGRESS,
});

const hiddenColumns = IrisGridUtils.getHiddenColumns(userColumnWidths);
let modelRanges = GridUtils.getModelRanges(ranges, movedColumns);
if (hiddenColumns.length > 0) {
Expand Down

0 comments on commit 1eb45ec

Please sign in to comment.