Skip to content

Commit

Permalink
fix: MessageRow click won't trigger rowClick event
Browse files Browse the repository at this point in the history
  • Loading branch information
aronmi committed Dec 10, 2020
1 parent 8058c3b commit 5e13468
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/gridjs/src/view/table/messageRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export interface MessageRowProps extends BaseProps {
export class MessageRow extends BaseComponent<MessageRowProps, {}> {
render() {
return (
<TR>
<TR stylingRow={true}>
<TD
role="alert"
colSpan={this.props.colSpan}
stylingCell={true}
cell={new Cell(this.props.message)}
className={classJoin(
className('message'),
Expand Down
5 changes: 4 additions & 1 deletion packages/gridjs/src/view/table/td.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { PluginRenderer } from '../../plugin';

export interface TDProps
extends BaseProps,
JSX.HTMLAttributes<HTMLTableCellElement> {
JSX.HTMLAttributes<HTMLTableCellElement> {
cell: Cell;
row?: Row;
column?: TColumn;
style?: CSSDeclaration;
stylingCell?: boolean;
}

export class TD extends BaseComponent<TDProps> {
Expand Down Expand Up @@ -47,6 +48,8 @@ export class TD extends BaseComponent<TDProps> {
}

private handleClick(e: JSX.TargetedMouseEvent<HTMLTableCellElement>): void {
if (this.props.stylingCell)
return;
this.config.eventEmitter.emit(
'cellClick',
e,
Expand Down
3 changes: 3 additions & 0 deletions packages/gridjs/src/view/table/tr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Header from '../../header';
export interface TRProps extends BaseProps {
row?: Row;
header?: Header;
stylingRow?: boolean;
}

export class TR extends BaseComponent<TRProps, {}> {
Expand All @@ -27,6 +28,8 @@ export class TR extends BaseComponent<TRProps, {}> {
}

private handleClick(e: JSX.TargetedMouseEvent<HTMLTableRowElement>): void {
if (this.props.stylingRow)
return;
this.config.eventEmitter.emit('rowClick', e, this.props.row);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`MessageRow component should match the snapshot 1`] = `"<tr class=\\"gridjs-tr\\"><td role=\\"alert\\" class=\\"gridjs-td gridjs-message\\">boo</td></tr>"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TR component should match the snapshot 1`] = `"<tr class=\\"gridjs-tr\\"><td class=\\"gridjs-td\\">boo</td></tr>"`;
42 changes: 42 additions & 0 deletions packages/gridjs/tests/view/table/message-row.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { mount } from 'enzyme';
import { createContext, h } from 'preact';
import { Config } from '../../../src/config';
import { EventEmitter } from '../../../src/util/eventEmitter';
import { TableEvents } from '../../../src/view/table/events';
import { MessageRow } from '../../../src/view/table/messageRow';

describe('MessageRow component', () => {
let config: Config;
const configContext = createContext(null);

beforeEach(() => {
config = new Config();
});

it('should match the snapshot', () => {
const td = mount(
<configContext.Provider value={config}>
<MessageRow message="boo" />
</configContext.Provider>,
);
expect(td.html()).toMatchSnapshot();
});

it('should prevent emit rowClick', async () => {
config.eventEmitter = new EventEmitter<TableEvents>();
const onClick = jest.fn();

const rows = mount(
<configContext.Provider value={config}>
<MessageRow message="boo" />
</configContext.Provider>,
).find('tr');

config.eventEmitter.on('rowClick', onClick)
rows.map(tr => tr.simulate('click'));

expect(rows.length).toEqual(1);
expect(onClick).toHaveBeenCalledTimes(0);
});
});

30 changes: 28 additions & 2 deletions packages/gridjs/tests/view/table/table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { Translator } from '../../../src/i18n/language';
import Tabular from '../../../src/tabular';
import { html } from '../../../src/util/html';
import Row from '../../../src/row';
import { EventEmitter } from '../../../src/util/eventEmitter';
import { TableEvents } from '../../../src/view/table/events';

describe('Table component', () => {
let config: Config;
Expand Down Expand Up @@ -504,8 +506,8 @@ describe('Table component', () => {
attributes: (cell: TCell) =>
cell === 'b'
? {
'data-row-c': true,
}
'data-row-c': true,
}
: {},
},
{
Expand Down Expand Up @@ -575,4 +577,28 @@ describe('Table component', () => {
expect(table.find('th').text()).toBe('e');
expect(table.find('th').length).toBe(1);
});

it('should emit rowClick', async () => {
const header = Header.fromUserConfig({
columns: ['h1', 'h2', 'h3'],
});
config.eventEmitter = new EventEmitter<TableEvents>();
const onClick = jest.fn();

const table = mount(
<configContext.Provider value={config}>
<Table
data={await config.pipeline.process()}
header={header}
status={Status.Rendered}
width={config.width}
height={config.height}
/>
</configContext.Provider>,
);
config.eventEmitter.on('rowClick', onClick)
table.find('tbody tr').map(tr => tr.simulate('click'));

expect(onClick).toHaveBeenCalledTimes(config.data.length);
});
});
19 changes: 19 additions & 0 deletions packages/gridjs/tests/view/table/td.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { createContext, h } from 'preact';
import { TD } from '../../../src/view/table/td';
import Cell from '../../../src/cell';
import { Config } from '../../../src/config';
import { EventEmitter } from '../../../src/util/eventEmitter';
import { TableEvents } from '../../../src/view/table/events';

describe('TD component', () => {
let config: Config;
Expand All @@ -20,4 +22,21 @@ describe('TD component', () => {
);
expect(td.html()).toMatchSnapshot();
});

it('should emit cellClick', async () => {
config.eventEmitter = new EventEmitter<TableEvents>();
const onClick = jest.fn();

const cells = mount(
<configContext.Provider value={config}>
<TD cell={new Cell('boo')} />
</configContext.Provider>,
).find('td');

config.eventEmitter.on('cellClick', onClick)
cells.map(td => td.simulate('click'));

expect(cells.length).toEqual(1);
expect(onClick).toHaveBeenCalledTimes(1);
});
});
48 changes: 48 additions & 0 deletions packages/gridjs/tests/view/table/tr.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import { mount } from 'enzyme';
import { createContext, h } from 'preact';
import { Cell } from '../../..';
import { Config } from '../../../src/config';
import { EventEmitter } from '../../../src/util/eventEmitter';
import { TableEvents } from '../../../src/view/table/events';
import { TD } from '../../../src/view/table/td';
import { TR } from '../../../src/view/table/tr';

describe('TR component', () => {
let config: Config;
const configContext = createContext(null);

beforeEach(() => {
config = new Config();
});

it('should match the snapshot', () => {
const tr = mount(
<configContext.Provider value={config}>
<TR >
<TD cell={new Cell('boo')} />
</TR>
</configContext.Provider>,
);
expect(tr.html()).toMatchSnapshot();
});

it('should emit rowClick', async () => {
config.eventEmitter = new EventEmitter<TableEvents>();
const onClick = jest.fn();

const rows = mount(
<configContext.Provider value={config}>
<TR >
<TD cell={new Cell('boo')} />
</TR>
</configContext.Provider>,
).find('tr');

config.eventEmitter.on('rowClick', onClick)
rows.map(tr => tr.simulate('click'));

expect(rows.length).toEqual(1);
expect(onClick).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 5e13468

Please sign in to comment.