Skip to content

Commit 33a8db7

Browse files
authored
Merge branch 'canary' into ts42
2 parents 37644e6 + 249b522 commit 33a8db7

File tree

8 files changed

+148
-2
lines changed

8 files changed

+148
-2
lines changed

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export default {
1010
'text'
1111
],
1212
restoreMocks: true,
13+
setupFiles: [
14+
'./test/setup.ts'
15+
],
1316
setupFilesAfterEnv: [
1417
'@testing-library/jest-dom'
1518
],

test/column/cellClass.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ const rows: readonly Row[] = [{ id: 0 }, { id: 1 }];
1010
test('cellClass is undefined', () => {
1111
const columns: readonly Column<Row>[] = [{
1212
key: 'id',
13-
name: 'ID',
14-
cellClass: undefined
13+
name: 'ID'
1514
}];
1615
setup({ columns, rows });
1716
const [cell1, cell2] = getCells();

test/column/frozen.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { Column } from '../../src';
2+
import { setup, getHeaderCells } from '../utils';
3+
4+
interface Row {
5+
col1: number;
6+
col2: string;
7+
col3: string;
8+
col4: string;
9+
}
10+
11+
test('frozen column have a specific class, and are stable-sorted before non-frozen columns', () => {
12+
const columns: readonly Column<Row>[] = [{
13+
key: 'col1',
14+
name: 'col1',
15+
frozen: true
16+
}, {
17+
key: 'col2',
18+
name: 'col2'
19+
}, {
20+
key: 'col3',
21+
name: 'col3',
22+
frozen: true
23+
}, {
24+
key: 'col4',
25+
name: 'col4',
26+
frozen: false
27+
}];
28+
29+
setup({ columns, rows: [] });
30+
const [cell1, cell2, cell3, cell4] = getHeaderCells();
31+
32+
expect(cell1).toHaveClass('rdg-cell rdg-cell-frozen', { exact: true });
33+
expect(cell2).toHaveClass('rdg-cell rdg-cell-frozen rdg-cell-frozen', { exact: true });
34+
expect(cell3).toHaveClass('rdg-cell', { exact: true });
35+
expect(cell4).toHaveClass('rdg-cell', { exact: true });
36+
37+
expect(cell1).toHaveTextContent('col1');
38+
expect(cell2).toHaveTextContent('col3');
39+
expect(cell3).toHaveTextContent('col2');
40+
expect(cell4).toHaveTextContent('col4');
41+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Column } from '../../src';
2+
import { setup, getHeaderCells } from '../utils';
3+
4+
interface Row {
5+
id: number;
6+
name: string;
7+
}
8+
9+
test('headerCellClass is either undefined or a string', () => {
10+
const columns: readonly Column<Row>[] = [{
11+
key: 'id',
12+
name: 'ID'
13+
}, {
14+
key: 'name',
15+
name: 'Name',
16+
headerCellClass: 'my-header'
17+
}];
18+
19+
setup({ columns, rows: [] });
20+
const [cell1, cell2] = getHeaderCells();
21+
expect(cell1).toHaveClass('rdg-cell', { exact: true });
22+
expect(cell2).toHaveClass('rdg-cell my-header', { exact: true });
23+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Column, HeaderRendererProps } from '../../src';
2+
import { setup, getHeaderCells } from '../utils';
3+
4+
interface Row {
5+
id: number;
6+
name: string;
7+
}
8+
9+
test('headerRenderer is either undefined or a component', () => {
10+
function Header({ column }: HeaderRendererProps<Row>) {
11+
return <>Fancy! {column.name}</>;
12+
}
13+
14+
const columns: readonly Column<Row>[] = [{
15+
key: 'id',
16+
name: 'ID'
17+
}, {
18+
key: 'name',
19+
name: 'Name',
20+
headerRenderer: Header
21+
}];
22+
23+
setup({ columns, rows: [] });
24+
const [cell1, cell2] = getHeaderCells();
25+
expect(cell1).toHaveTextContent('ID');
26+
expect(cell2).toHaveTextContent('Fancy! Name');
27+
});

test/column/name.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Column } from '../../src';
2+
import { setup, getHeaderCells } from '../utils';
3+
4+
interface Row {
5+
id: number;
6+
name: string;
7+
}
8+
9+
test('name is either a string or an element', () => {
10+
function Header() {
11+
return <>Fancy</>;
12+
}
13+
14+
const columns: readonly Column<Row>[] = [{
15+
key: 'id',
16+
name: 'ID'
17+
}, {
18+
key: 'name',
19+
name: <Header />
20+
}];
21+
22+
setup({ columns, rows: [] });
23+
const [cell1, cell2] = getHeaderCells();
24+
expect(cell1).toHaveTextContent('ID');
25+
expect(cell2).toHaveTextContent('Fancy');
26+
});

test/setup.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @ts-expect-error
2+
window.ResizeObserver ??= function ResizeObserver(callback: () => void) {
3+
callback();
4+
5+
return {
6+
observe() {},
7+
disconnect() {}
8+
};
9+
};
10+
11+
// patch clientWidth/clientHeight to pretend we're rendering DataGrid at 1080p
12+
Object.defineProperties(HTMLDivElement.prototype, {
13+
clientWidth: {
14+
get(this: HTMLDivElement) {
15+
return this.classList.contains('rdg') ? 1920 : 0;
16+
}
17+
},
18+
clientHeight: {
19+
get(this: HTMLDivElement) {
20+
return this.classList.contains('rdg') ? 1080 : 0;
21+
}
22+
}
23+
});

test/utils.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ export function getRows() {
1818
export function getCells() {
1919
return screen.getAllByRole('gridcell');
2020
}
21+
22+
export function getHeaderCells() {
23+
return screen.getAllByRole('columnheader');
24+
}

0 commit comments

Comments
 (0)