I'm testing a GridTiles component which will render a Grid component with a certain number of columns.
grid-tiles.tsx
:
const GridTiles: FC<GridTilesProps> = ({ columns, tiles, ...props }) => {
return (
<Grid templateColumns={`repeat(${columns ?? 1}, 1fr)`} {...props}>
{tiles.map((tile) => (
<GridItem>{tile}</GridItem>
))}
</Grid>
);
};
grid-tiles.test.tsx
:
const mockedGrid = Grid as jest.MockedFunction<typeof Grid>;
describe('Grid Tiles', () => {
afterEach(() => {
jest.clearAllMocks();
});
describe('When passing props with columns', () => {
it('should render component with correct template columns', () => {
render(<GridTiles tiles={[]} columns={2} />);
expect(mockedGrid).toHaveBeenCalledTimes(1);
expect(mockedGrid).toHaveBeenCalledWith(
expect.objectContaining({
templateColumns: 'repeat(2, 1fr)',
}),
);
});
});
});
Apparently, the Grid is being called only once, but when doing toHaveBeenCalledWith
, it shows 2 calls, one with the correct parameters, and the other with a plain {}
, which is the one failing.
I don't know why it shows two parameters, when it is being only called once (as I tested on the previous expect.toHaveBeenCalledTimes(1)
.
The test fails with this message:
Does anyone know why this happens, or how can I solve this?