Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react-grid): throw warning if getRowId returns undefined #1679

Merged
merged 10 commits into from
Dec 26, 2018
13 changes: 12 additions & 1 deletion packages/dx-grid-core/src/plugins/grid-core/computeds.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { GRID_GROUP_CHECK } from '../integrated-grouping/constants';

const warnIfRowIdUndefined = getRowId => (row) => {
const result = getRowId(row);
if (!row[GRID_GROUP_CHECK] && result === undefined) {
// eslint-disable-next-line no-console
console.warn('The row id is undefined. Check the getRowId function. The row is', row);
}
return result;
};

export const rowIdGetter = (getRowId, rows) => {
if (!getRowId) {
const map = new Map(rows.map((row, rowIndex) => [row, rowIndex]));
return row => map.get(row);
}
return getRowId;
return warnIfRowIdUndefined(getRowId);
};

const defaultGetCellValue = (row, columnName) => row[columnName];
Expand Down
61 changes: 53 additions & 8 deletions packages/dx-grid-core/src/plugins/grid-core/computeds.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { format } from 'util';
import { GRID_GROUP_CHECK } from '../integrated-grouping/constants';
import {
rowIdGetter,
cellValueGetter,
} from './computeds';

describe('GridCore Plugin computeds', () => {
describe('#rowIdGetter', () => {
const data = [
{ a: 1 },
{ a: 2 },
];

it('should work with default ids', () => {
const data = [
{ a: 1 },
{ a: 2 },
];
const getRowId = null;

expect(rowIdGetter(getRowId, data)(data[0]))
Expand All @@ -19,17 +22,59 @@ describe('GridCore Plugin computeds', () => {
});

it('should work with custom func', () => {
const data = [
{ a: 1 },
{ a: 2 },
];
const getRowId = row => row.a;

expect(rowIdGetter(getRowId, data)(data[0]))
.toEqual(1);
expect(rowIdGetter(getRowId, data)(data[1]))
.toEqual(2);
});

describe('warning if custom getter returns undefined', () => {
/* eslint-disable no-console */
let warnLog;
let savedWarn;
beforeEach(() => {
warnLog = [];
savedWarn = console.warn;
console.warn = (...args) => {
warnLog.push(format(...args));
};
});
afterEach(() => {
console.warn = savedWarn;
});
/* eslint-enable no-console */

it('should warn if row is regular', () => {
const getRowId = () => undefined;

rowIdGetter(getRowId, data)(data[0]);

expect(warnLog[0])
.toEqual('The row id is undefined. Check the getRowId function. The row is { a: 1 }');
});

it('should not warn if row is of a group type', () => {
const getRowId = row => row.a;
const rows = [
{ [GRID_GROUP_CHECK]: true },
{ a: 1 },
];

rowIdGetter(getRowId, rows)(rows[0]);

expect(warnLog.length).toBe(0);
});

it('should not warn if func returns valid id', () => {
const getRowId = row => row.a;

rowIdGetter(getRowId, data)(data[0]);

expect(warnLog.length).toBe(0);
});
});
});

describe('#cellValueGetter', () => {
Expand Down