Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtassone committed Nov 11, 2020
1 parent 1015d78 commit e257dda
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
53 changes: 26 additions & 27 deletions packages/grid/data-grid/src/DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,32 @@ describe('<DataGrid />', () => {
);
});
});
describe('state', () => {
it('should allow to control the state using useState', async () => {
function GridStateTest({ direction, sortedRows }) {
const [gridState, setGridState] = React.useState<Partial<GridState>>({
sorting: { sortModel: [{ field: 'brand', sort: direction }], sortedRows },
});

React.useEffect(() => {
setGridState({
sorting: { sortModel: [{ field: 'brand', sort: direction }], sortedRows },
});
}, [direction, sortedRows]);

return (
<div style={{ width: 300, height: 500 }}>
<DataGrid {...defaultProps} state={gridState} />
</div>
);
}

const { setProps } = render(<GridStateTest direction={'desc'} sortedRows={[2, 0, 1]} />);
expect(getColumnValues()).to.deep.equal(['Puma', 'Nike', 'Adidas']);
setProps({ direction: 'asc', sortedRows: [1, 0, 2] });
expect(getColumnValues()).to.deep.equal(['Puma', 'Nike', 'Adidas'].reverse());
});
});
});
describe('warnings', () => {
before(() => {
Expand Down Expand Up @@ -191,31 +217,4 @@ describe('<DataGrid />', () => {
);
});
});

describe('state', () => {
it('should allow to control the state using useState', async () => {
function GridStateTest({ direction, sortedRows }) {
const [gridState, setGridState] = React.useState<Partial<GridState>>({
sorting: { sortModel: [{ field: 'brand', sort: direction }], sortedRows },
});

React.useEffect(() => {
setGridState({
sorting: { sortModel: [{ field: 'brand', sort: direction }], sortedRows },
});
}, [direction, sortedRows]);

return (
<div style={{ width: 300, height: 500 }}>
<DataGrid {...defaultProps} state={gridState} />
</div>
);
}

const { setProps } = render(<GridStateTest direction={'desc'} sortedRows={[2, 0, 1]} />);
expect(getColumnValues()).to.deep.equal(['Puma', 'Nike', 'Adidas']);
setProps({ direction: 'asc', sortedRows: [1, 0, 2] });
expect(getColumnValues()).to.deep.equal(['Puma', 'Nike', 'Adidas'].reverse());
});
});
});
8 changes: 4 additions & 4 deletions packages/grid/x-grid/src/XGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ describe('<XGrid />', () => {
expect(getColumnValues()).to.deep.equal(['Puma', 'Nike', 'Adidas']);
});
});

describe('state', () => {
it('should trigger on state change and pass the correct params', () => {
let onStateParams;
Expand All @@ -239,10 +240,9 @@ describe('<XGrid />', () => {
render(<Test />);
const header = screen.getByRole('columnheader', { name: 'brand' });
fireEvent.click(header);
expect(onStateParams.api).to.eq(apiRef.current);
expect(onStateParams.state).to.eq(apiRef.current.state);
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
expect(onStateParams.state).to.not.be.empty;
expect(onStateParams.api).to.equal(apiRef.current);
expect(onStateParams.state).to.equal(apiRef.current.state);
expect(onStateParams.state).to.not.equal(undefined);
});
it('should allow to control the state using apiRef', () => {
function GridStateTest() {
Expand Down

0 comments on commit e257dda

Please sign in to comment.