diff --git a/package.json b/package.json index bb1681b987e52..6fa35053365b5 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,6 @@ "@types/react": "catalog:", "@types/react-dom": "catalog:", "@types/requestidlecallback": "^0.3.7", - "@types/sinon": "^17.0.4", "@types/yargs": "^17.0.34", "@typescript-eslint/parser": "catalog:", "@vitejs/plugin-react": "catalog:", @@ -135,7 +134,6 @@ "remark": "^15.0.1", "rimraf": "catalog:", "serve": "catalog:", - "sinon": "^21.0.0", "stream-browserify": "^3.0.0", "string-replace-loader": "^3.2.0", "stylelint": "^16.25.0", diff --git a/packages/x-charts-premium/src/ChartsRenderer/ChartsRenderer.test.tsx b/packages/x-charts-premium/src/ChartsRenderer/ChartsRenderer.test.tsx index cc2d41834637a..55e2721721fbe 100644 --- a/packages/x-charts-premium/src/ChartsRenderer/ChartsRenderer.test.tsx +++ b/packages/x-charts-premium/src/ChartsRenderer/ChartsRenderer.test.tsx @@ -1,4 +1,4 @@ -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { createRenderer } from '@mui/internal-test-utils/createRenderer'; import { ChartsRenderer } from '@mui/x-charts-premium/ChartsRenderer'; import { screen } from '@mui/internal-test-utils'; @@ -28,7 +28,7 @@ describe('', () => { }); it('should pass the rendering to the onRender callback', () => { - const onRenderSpy = spy(); + const onRenderSpy = vi.fn(); render(
', () => {
, ); - expect(onRenderSpy.lastCall.firstArg).to.equal('line'); + expect(onRenderSpy.mock.calls[onRenderSpy.mock.calls.length - 1][0]).to.equal('line'); }); it('should compute props for the chart', () => { - const onRenderSpy = spy(); + const onRenderSpy = vi.fn(); render(
', () => {
, ); - const props = onRenderSpy.lastCall.args[1]; + const props = onRenderSpy.mock.calls[onRenderSpy.mock.calls.length - 1][1]; expect(props.colors).to.equal(colorPaletteLookup.get('rainbowSurgePalette')); }); it('should override the props if the configuration has an updated value', () => { - const onRenderSpy = spy(); + const onRenderSpy = vi.fn(); render(
', () => {
, ); - const props = onRenderSpy.lastCall.args[1]; + const props = onRenderSpy.mock.calls[onRenderSpy.mock.calls.length - 1][1]; expect(props.colors).to.equal(colorPaletteLookup.get('mangoFusionPalette')); }); it('should place dimensions and values to the correct place in the props', () => { - const onRenderSpy = spy(); + const onRenderSpy = vi.fn(); render(
', () => {
, ); - const props = onRenderSpy.lastCall.args[1]; + const props = onRenderSpy.mock.calls[onRenderSpy.mock.calls.length - 1][1]; expect(props.series[0].data).to.deep.equal([1, 2, 3]); }); }); diff --git a/packages/x-charts-pro/src/ChartZoomSlider/ChartZoomSlider.test.tsx b/packages/x-charts-pro/src/ChartZoomSlider/ChartZoomSlider.test.tsx index f1f50d2e582a1..e265004345801 100644 --- a/packages/x-charts-pro/src/ChartZoomSlider/ChartZoomSlider.test.tsx +++ b/packages/x-charts-pro/src/ChartZoomSlider/ChartZoomSlider.test.tsx @@ -129,7 +129,7 @@ describe.skipIf(isJSDOM)('', () => { expect(getAxisTickValues('x')).to.not.include('A'); // Reset zoom change spy - onZoomChange.resetHistory(); + onZoomChange.mockClear(); // Move the end thumb to zoom in from the right await user.pointer([ diff --git a/packages/x-charts-pro/src/FunnelChart/checkClickEvent.test.tsx b/packages/x-charts-pro/src/FunnelChart/checkClickEvent.test.tsx index 61c1a4eb56741..6da9bb0e16e11 100644 --- a/packages/x-charts-pro/src/FunnelChart/checkClickEvent.test.tsx +++ b/packages/x-charts-pro/src/FunnelChart/checkClickEvent.test.tsx @@ -71,7 +71,7 @@ describe('FunnelChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 0, axisValue: 0, seriesValues: { big: 200, small: 100 }, @@ -85,7 +85,7 @@ describe('FunnelChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 1, axisValue: 1, seriesValues: { big: 100, small: 50 }, @@ -121,7 +121,7 @@ describe('FunnelChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 0, axisValue: 0, seriesValues: { big: 200, small: 100 }, @@ -135,7 +135,7 @@ describe('FunnelChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 1, axisValue: 1, seriesValues: { big: 100, small: 50 }, @@ -172,7 +172,7 @@ describe('FunnelChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 0, axisValue: 'First', seriesValues: { big: 200, small: 100 }, @@ -186,7 +186,7 @@ describe('FunnelChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 1, axisValue: 'Second', seriesValues: { big: 100, small: 50 }, @@ -228,27 +228,27 @@ describe('FunnelChart - click event', () => { ); await user.click(pathsBig[0]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'funnel', seriesId: 'big', dataIndex: 0, }); await user.click(pathsBig[1]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'funnel', seriesId: 'big', dataIndex: 1, }); await user.click(pathsSmall[0]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'funnel', seriesId: 'small', dataIndex: 0, }); await user.click(pathsSmall[1]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'funnel', seriesId: 'small', dataIndex: 1, diff --git a/packages/x-charts/src/BarChart/checkClickEvent.test.tsx b/packages/x-charts/src/BarChart/checkClickEvent.test.tsx index fdb0ee2f6dee1..593f4e88871e8 100644 --- a/packages/x-charts/src/BarChart/checkClickEvent.test.tsx +++ b/packages/x-charts/src/BarChart/checkClickEvent.test.tsx @@ -1,5 +1,5 @@ import { createRenderer } from '@mui/internal-test-utils'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { BarChart } from '@mui/x-charts/BarChart'; import { isJSDOM } from 'test/utils/skipIf'; import { CHART_SELECTOR } from '../tests/constants'; @@ -30,7 +30,7 @@ describe('BarChart - click event', () => { describe('onAxisClick', () => { // can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 it.skipIf(isJSDOM)('should provide the right context as second argument', async () => { - const onAxisClick = spy(); + const onAxisClick = vi.fn(); const { user } = render(
{ }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 0, axisValue: 'A', seriesValues: { s1: 4, s2: 2 }, @@ -73,7 +73,7 @@ describe('BarChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 1, axisValue: 'B', seriesValues: { s1: 1, s2: 1 }, @@ -84,7 +84,7 @@ describe('BarChart - click event', () => { it.skipIf(isJSDOM)( 'should provide the right context as second argument with layout="horizontal"', async () => { - const onAxisClick = spy(); + const onAxisClick = vi.fn(); const { user } = render(
{ }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 0, axisValue: 'A', seriesValues: { s1: 4, s2: 2 }, @@ -128,7 +128,7 @@ describe('BarChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 1, axisValue: 'B', seriesValues: { s1: 1, s2: 1 }, @@ -159,7 +159,7 @@ describe('BarChart - click event', () => { // can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 it.skipIf(isJSDOM)('should provide the right context as second argument', async () => { - const onItemClick = spy(); + const onItemClick = vi.fn(); const { user } = render(
{ const rectangles = document.querySelectorAll('rect.MuiBarElement-root'); await user.click(rectangles[0]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'bar', seriesId: 's1', dataIndex: 0, }); await user.click(rectangles[1]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'bar', seriesId: 's1', dataIndex: 1, }); await user.click(rectangles[2]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'bar', seriesId: 's2', dataIndex: 0, diff --git a/packages/x-charts/src/ChartsLocalizationProvider/ChartsLocalizationProvider.test.tsx b/packages/x-charts/src/ChartsLocalizationProvider/ChartsLocalizationProvider.test.tsx index 3bb6dfaf6cbc0..e7fef8f4f25d8 100644 --- a/packages/x-charts/src/ChartsLocalizationProvider/ChartsLocalizationProvider.test.tsx +++ b/packages/x-charts/src/ChartsLocalizationProvider/ChartsLocalizationProvider.test.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { createRenderer } from '@mui/internal-test-utils'; import { createTheme, ThemeProvider } from '@mui/material/styles'; import { useChartsLocalization } from '@mui/x-charts/hooks'; @@ -24,7 +24,7 @@ describe('', () => { const { render } = createRenderer(); it('should respect localeText from the theme', () => { - const handleContextChange = spy(); + const handleContextChange = vi.fn(); const theme = createTheme({ components: { @@ -44,13 +44,13 @@ describe('', () => { , ); - const localeText: ChartsLocaleText = handleContextChange.lastCall.args[0].localeText; + const localeText: ChartsLocaleText = handleContextChange.mock.calls[handleContextChange.mock.calls.length - 1][0].localeText; expect(localeText.noData).to.equal('Pas de data'); expect(localeText.loading).to.equal('Loading data…'); }); it('should prioritize localeText key passed on LocalizationProvider compared to key passed from the theme', () => { - const handleContextChange = spy(); + const handleContextChange = vi.fn(); const theme = createTheme({ components: { @@ -70,12 +70,12 @@ describe('', () => { , ); - const localeText: ChartsLocaleText = handleContextChange.lastCall.args[0].localeText; + const localeText: ChartsLocaleText = handleContextChange.mock.calls[handleContextChange.mock.calls.length - 1][0].localeText; expect(localeText.noData).to.equal('Prioritized'); }); it('should prioritize deepest LocalizationProvider when using nested ones', () => { - const handleContextChange = spy(); + const handleContextChange = vi.fn(); render( @@ -85,12 +85,12 @@ describe('', () => { , ); - const localeText: ChartsLocaleText = handleContextChange.lastCall.args[0].localeText; + const localeText: ChartsLocaleText = handleContextChange.mock.calls[handleContextChange.mock.calls.length - 1][0].localeText; expect(localeText.noData).to.equal('Prioritized'); }); it("should not lose locales from higher LocalizationProvider when deepest one don't have the translation key", () => { - const handleContextChange = spy(); + const handleContextChange = vi.fn(); render( @@ -100,7 +100,7 @@ describe('', () => { , ); - const localeText: ChartsLocaleText = handleContextChange.lastCall.args[0].localeText; + const localeText: ChartsLocaleText = handleContextChange.mock.calls[handleContextChange.mock.calls.length - 1][0].localeText; expect(localeText.noData).to.equal('Prioritized'); expect(localeText.loading).to.equal('Other Locale'); }); diff --git a/packages/x-charts/src/LineChart/checkClickEvent.test.tsx b/packages/x-charts/src/LineChart/checkClickEvent.test.tsx index 21cd432c5ab99..1ea1dc6da0548 100644 --- a/packages/x-charts/src/LineChart/checkClickEvent.test.tsx +++ b/packages/x-charts/src/LineChart/checkClickEvent.test.tsx @@ -1,5 +1,5 @@ import { createRenderer } from '@mui/internal-test-utils'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { LineChart } from '@mui/x-charts/LineChart'; import { isJSDOM } from 'test/utils/skipIf'; import { CHART_SELECTOR } from '../tests/constants'; @@ -24,7 +24,7 @@ describe('LineChart - click event', () => { describe('onAxisClick', () => { // can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 it.skipIf(isJSDOM)('should provide the right context as second argument', async () => { - const onAxisClick = spy(); + const onAxisClick = vi.fn(); const { user } = render(
{ }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 1, axisValue: 20, seriesValues: { s1: 5, s2: 8 }, @@ -67,7 +67,7 @@ describe('LineChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 2, axisValue: 30, seriesValues: { s1: 8, s2: 5 }, @@ -104,7 +104,7 @@ describe('LineChart - click event', () => { // can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 it.skipIf(isJSDOM)('should provide the right context as second argument', async () => { - const onMarkClick = spy(); + const onMarkClick = vi.fn(); const { user } = render(
{ const marks = document.querySelectorAll('.MuiMarkElement-root'); await user.click(marks[0]); - expect(onMarkClick.lastCall.args[1]).to.deep.equal({ + expect(onMarkClick.mock.calls[onMarkClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'line', seriesId: 's1', dataIndex: 0, }); await user.click(marks[1]); - expect(onMarkClick.lastCall.args[1]).to.deep.equal({ + expect(onMarkClick.mock.calls[onMarkClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'line', seriesId: 's1', dataIndex: 1, }); await user.click(marks[4]); - expect(onMarkClick.lastCall.args[1]).to.deep.equal({ + expect(onMarkClick.mock.calls[onMarkClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'line', seriesId: 's2', dataIndex: 0, @@ -172,7 +172,7 @@ describe('LineChart - click event', () => { // can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 it.skipIf(isJSDOM)('should provide the right context as second argument', async () => { - const onAreaClick = spy(); + const onAreaClick = vi.fn(); const { user } = render(
{ const areas = document.querySelectorAll('path.MuiAreaElement-root'); await user.click(areas[0]); - expect(onAreaClick.lastCall.args[1]).to.deep.equal({ + expect(onAreaClick.mock.calls[onAreaClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'line', seriesId: 's1', }); await user.click(areas[1]); - expect(onAreaClick.lastCall.args[1]).to.deep.equal({ + expect(onAreaClick.mock.calls[onAreaClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'line', seriesId: 's2', }); @@ -231,7 +231,7 @@ describe('LineChart - click event', () => { // can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 it.skipIf(isJSDOM)('should provide the right context as second argument', async () => { - const onLineClick = spy(); + const onLineClick = vi.fn(); const { user } = render(
{ const lines = document.querySelectorAll('path.MuiLineElement-root'); await user.click(lines[0]); - expect(onLineClick.lastCall.args[1]).to.deep.equal({ + expect(onLineClick.mock.calls[onLineClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'line', seriesId: 's1', }); await user.click(lines[1]); - expect(onLineClick.lastCall.args[1]).to.deep.equal({ + expect(onLineClick.mock.calls[onLineClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'line', seriesId: 's2', }); diff --git a/packages/x-charts/src/PieChart/checkClickEvent.test.tsx b/packages/x-charts/src/PieChart/checkClickEvent.test.tsx index 4f6b9a9c8bd37..dddecf810e4ee 100644 --- a/packages/x-charts/src/PieChart/checkClickEvent.test.tsx +++ b/packages/x-charts/src/PieChart/checkClickEvent.test.tsx @@ -1,5 +1,5 @@ import { createRenderer } from '@mui/internal-test-utils'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { PieChart } from '@mui/x-charts/PieChart'; const config = { @@ -39,7 +39,7 @@ describe('PieChart - click event', () => { }); it('should provide the right context as second argument', async () => { - const onItemClick = spy(); + const onItemClick = vi.fn(); const { user } = render( { const slices = document.querySelectorAll('path.MuiPieArc-root'); await user.click(slices[0]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'pie', seriesId: 's1', dataIndex: 0, }); await user.click(slices[1]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'pie', seriesId: 's1', dataIndex: 1, diff --git a/packages/x-charts/src/RadarChart/RadarChart.test.tsx b/packages/x-charts/src/RadarChart/RadarChart.test.tsx index 2b87ffc170b20..800394877a8b1 100644 --- a/packages/x-charts/src/RadarChart/RadarChart.test.tsx +++ b/packages/x-charts/src/RadarChart/RadarChart.test.tsx @@ -1,7 +1,7 @@ import { createRenderer, screen } from '@mui/internal-test-utils/createRenderer'; import { describeConformance } from 'test/utils/describeConformance'; import { Unstable_RadarChart as RadarChart, RadarChartProps } from '@mui/x-charts/RadarChart'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { isJSDOM } from 'test/utils/skipIf'; import { CHART_SELECTOR } from '../tests/constants'; @@ -44,13 +44,13 @@ describe('', () => { // svg.createSVGPoint not supported by JSDom https://github.com/jsdom/jsdom/issues/300 it.skipIf(isJSDOM)('should call onHighlightChange', async () => { - const onHighlightChange = spy(); + const onHighlightChange = vi.fn(); const { user } = render(); const path = document.querySelector('svg .MuiRadarSeriesPlot-area')!; await user.pointer({ target: path }); - expect(onHighlightChange.callCount).to.equal(1); + expect(onHighlightChange).toHaveBeenCalledTimes(1); }); it.skipIf(isJSDOM)('should highlight axis on hover', async () => { diff --git a/packages/x-charts/src/RadarChart/checkClickEvent.test.tsx b/packages/x-charts/src/RadarChart/checkClickEvent.test.tsx index 6bd83a3c062f2..06505aea840f1 100644 --- a/packages/x-charts/src/RadarChart/checkClickEvent.test.tsx +++ b/packages/x-charts/src/RadarChart/checkClickEvent.test.tsx @@ -1,5 +1,5 @@ import { createRenderer } from '@mui/internal-test-utils'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { RadarChart, RadarChartProps } from '@mui/x-charts/RadarChart'; import { isJSDOM } from 'test/utils/skipIf'; import { CHART_SELECTOR } from '../tests/constants'; @@ -23,7 +23,7 @@ describe('RadarChart - click event', () => { describe('onAxisClick', () => { // can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 it.skipIf(isJSDOM)('should provide the right context as second argument', async () => { - const onAxisClick = spy(); + const onAxisClick = vi.fn(); const { user } = render(
{ }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 0, axisValue: 'A', seriesValues: { s1: 2, s2: 6 }, @@ -58,7 +58,7 @@ describe('RadarChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 1, axisValue: 'B', seriesValues: { s1: 3, s2: 5 }, @@ -69,7 +69,7 @@ describe('RadarChart - click event', () => { it.skipIf(isJSDOM)( 'should provide the right context as second argument with startAngle=90', async () => { - const onAxisClick = spy(); + const onAxisClick = vi.fn(); const { user } = render(
{ }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 3, axisValue: 'D', seriesValues: { s1: 5, s2: 1 }, @@ -108,7 +108,7 @@ describe('RadarChart - click event', () => { }, ]); - expect(onAxisClick.lastCall.args[1]).to.deep.equal({ + expect(onAxisClick.mock.calls[onAxisClick.mock.calls.length - 1][1]).to.deep.equal({ dataIndex: 0, axisValue: 'A', seriesValues: { s1: 2, s2: 6 }, @@ -136,7 +136,7 @@ describe('RadarChart - click event', () => { // can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 it.skipIf(isJSDOM)('should provide the right context as second argument', async () => { - const onItemClick = spy(); + const onItemClick = vi.fn(); const { user } = render(
{ const marks = document.querySelectorAll('circle.MuiRadarSeriesPlot-mark'); await user.click(marks[0]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'radar', seriesId: 's1', dataIndex: 0, }); await user.click(marks[1]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'radar', seriesId: 's1', dataIndex: 1, }); await user.click(marks[4]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'radar', seriesId: 's2', dataIndex: 0, @@ -186,7 +186,7 @@ describe('RadarChart - click event', () => { // can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 it.skipIf(isJSDOM)('should provide the right context as second argument', async () => { - const onItemClick = spy(); + const onItemClick = vi.fn(); const { user } = render(
{ coords: { clientX: 50, clientY: 45 }, }, ]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'radar', seriesId: 's1', dataIndex: 0, @@ -220,7 +220,7 @@ describe('RadarChart - click event', () => { coords: { clientX: 50, clientY: 55 }, }, ]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'radar', seriesId: 's2', dataIndex: 2, diff --git a/packages/x-charts/src/ScatterChart/checkClickEvent.test.tsx b/packages/x-charts/src/ScatterChart/checkClickEvent.test.tsx index d89d908477343..d732c8c178b98 100644 --- a/packages/x-charts/src/ScatterChart/checkClickEvent.test.tsx +++ b/packages/x-charts/src/ScatterChart/checkClickEvent.test.tsx @@ -1,5 +1,5 @@ import { createRenderer } from '@mui/internal-test-utils'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { ScatterChart } from '@mui/x-charts/ScatterChart'; import { isJSDOM } from 'test/utils/skipIf'; import { CHART_SELECTOR } from '../tests/constants'; @@ -33,7 +33,7 @@ describe('ScatterChart - click event', () => { // svg.createSVGPoint not supported by JSDom https://github.com/jsdom/jsdom/issues/300 describe.skipIf(isJSDOM)('onItemClick - using voronoi', () => { it('should provide the right context as second argument when clicking svg', async () => { - const onItemClick = spy(); + const onItemClick = vi.fn(); const { user } = render(
{ coords: { clientX: 10, clientY: 10 }, }, ]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'scatter', dataIndex: 0, seriesId: 's1', @@ -70,17 +70,17 @@ describe('ScatterChart - click event', () => { coords: { clientX: 30, clientY: 30 }, }, ]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'scatter', dataIndex: 4, seriesId: 's1', }); - expect(onItemClick.callCount).to.equal(2); + expect(onItemClick).toHaveBeenCalledTimes(2); }); it('should provide the right context as second argument when clicking mark', async () => { - const onItemClick = spy(); + const onItemClick = vi.fn(); const { user } = render(
{ }, }, ]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'scatter', dataIndex: 1, seriesId: 's1', }); - expect(onItemClick.callCount).to.equal(1); // Make sure voronoi + item click does not duplicate event triggering + expect(onItemClick).toHaveBeenCalledTimes(1); // Make sure voronoi + item click does not duplicate event triggering }); }); describe('onItemClick - disabling voronoi', () => { it('should not call onItemClick when clicking the SVG', async () => { - const onItemClick = spy(); + const onItemClick = vi.fn(); const { user } = render(
{ coords: { clientX: 10, clientY: 10 }, }, ]); - expect(onItemClick.callCount).to.equal(0); + expect(onItemClick).toHaveBeenCalledTimes(0); }); it('should provide the right context as second argument when clicking mark', async () => { - const onItemClick = spy(); + const onItemClick = vi.fn(); const { user } = render(
{ }, ]); - expect(onItemClick.lastCall.args[1]).to.deep.equal({ + expect(onItemClick.mock.calls[onItemClick.mock.calls.length - 1][1]).to.deep.equal({ type: 'scatter', dataIndex: 1, seriesId: 's1', }); - expect(onItemClick.callCount).to.equal(1); // Make sure voronoi + item click does not duplicate event triggering + expect(onItemClick).toHaveBeenCalledTimes(1); // Make sure voronoi + item click does not duplicate event triggering }); }); }); diff --git a/packages/x-charts/src/internals/animation/useAnimate.test.tsx b/packages/x-charts/src/internals/animation/useAnimate.test.tsx index 3bf4b4ec6e551..5d456144a6f78 100644 --- a/packages/x-charts/src/internals/animation/useAnimate.test.tsx +++ b/packages/x-charts/src/internals/animation/useAnimate.test.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { createRenderer, reactMajor, screen, waitFor } from '@mui/internal-test-utils'; import { interpolateNumber } from '@mui/x-charts-vendor/d3-interpolate'; // It's not publicly exported, so, using a relative import @@ -25,10 +25,10 @@ describe('useAnimate', () => { const lastCallWidth = () => applyProps.lastCall?.args[1].width; const firstCallWidth = () => applyProps.firstCall?.args[1].width; - const callCount = () => applyProps.callCount; + const callCount = () => applyProps.mock.calls.length; afterEach(() => { - applyProps.resetHistory(); + applyProps.mockClear(); }); it('starts animating from initial props', async () => { diff --git a/packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/useChartCartesianAxis.axisHighlight.test.tsx b/packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/useChartCartesianAxis.axisHighlight.test.tsx index 8e4d62c7823aa..e384d38386c7f 100644 --- a/packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/useChartCartesianAxis.axisHighlight.test.tsx +++ b/packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/useChartCartesianAxis.axisHighlight.test.tsx @@ -1,4 +1,4 @@ -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { createRenderer, waitFor } from '@mui/internal-test-utils'; import { isJSDOM } from 'test/utils/skipIf'; import { ChartDataProvider } from '@mui/x-charts/ChartDataProvider'; @@ -13,7 +13,7 @@ describe.skipIf(isJSDOM)('useChartCartesianAxis - axis highlight', () => { const { render } = createRenderer(); it('should call onHighlightedAxisChange when crossing any value', async () => { - const onHighlightedAxisChange = spy(); + const onHighlightedAxisChange = vi.fn(); const { user } = render( plugins={[useChartInteraction, useChartCartesianAxis]} @@ -32,7 +32,7 @@ describe.skipIf(isJSDOM)('useChartCartesianAxis - axis highlight', () => { await user.pointer([{ keys: '[TouchA>]', target: svg, coords: { clientX: 75, clientY: 60 } }]); - await waitFor(() => expect(onHighlightedAxisChange.callCount).to.equal(1)); + await waitFor(() => expect(onHighlightedAxisChange).toHaveBeenCalledTimes(1)); await user.pointer([ { @@ -50,7 +50,7 @@ describe.skipIf(isJSDOM)('useChartCartesianAxis - axis highlight', () => { }, ]); - expect(onHighlightedAxisChange.callCount).to.equal(4); + expect(onHighlightedAxisChange).toHaveBeenCalledTimes(4); expect(onHighlightedAxisChange.getCall(0).firstArg).to.deep.equal([ { axisId: 'x-axis', dataIndex: 1 }, @@ -71,7 +71,7 @@ describe.skipIf(isJSDOM)('useChartCartesianAxis - axis highlight', () => { }); it('should call onHighlightedAxisChange when axis got modified', async () => { - const onHighlightedAxisChange = spy(); + const onHighlightedAxisChange = vi.fn(); const { user, setProps } = render( plugins={[useChartInteraction, useChartCartesianAxis]} @@ -90,8 +90,8 @@ describe.skipIf(isJSDOM)('useChartCartesianAxis - axis highlight', () => { await user.pointer([{ keys: '[TouchA>]', target: svg, coords: { clientX: 45, clientY: 60 } }]); - await waitFor(() => expect(onHighlightedAxisChange.callCount).to.equal(1)); - expect(onHighlightedAxisChange.lastCall.firstArg).to.deep.equal([ + await waitFor(() => expect(onHighlightedAxisChange).toHaveBeenCalledTimes(1)); + expect(onHighlightedAxisChange.mock.calls[onHighlightedAxisChange.mock.calls.length - 1][0]).to.deep.equal([ { axisId: 'x-axis', dataIndex: 0 }, ]); @@ -99,14 +99,14 @@ describe.skipIf(isJSDOM)('useChartCartesianAxis - axis highlight', () => { xAxis: [{ id: 'x-axis', scaleType: 'band', data: ['A', 'B', 'C'], position: 'none' }], }); - expect(onHighlightedAxisChange.callCount).to.equal(2); - expect(onHighlightedAxisChange.lastCall.firstArg).to.deep.equal([ + expect(onHighlightedAxisChange).toHaveBeenCalledTimes(2); + expect(onHighlightedAxisChange.mock.calls[onHighlightedAxisChange.mock.calls.length - 1][0]).to.deep.equal([ { axisId: 'x-axis', dataIndex: 1 }, ]); }); it('should not call onHighlightedAxisChange when axis got modified but highlighted item stay the same', async () => { - const onHighlightedAxisChange = spy(); + const onHighlightedAxisChange = vi.fn(); const { user, setProps } = render( plugins={[useChartInteraction, useChartCartesianAxis]} @@ -125,8 +125,8 @@ describe.skipIf(isJSDOM)('useChartCartesianAxis - axis highlight', () => { await user.pointer([{ keys: '[TouchA>]', target: svg, coords: { clientX: 10, clientY: 60 } }]); - await waitFor(() => expect(onHighlightedAxisChange.callCount).to.equal(1)); - expect(onHighlightedAxisChange.lastCall.firstArg).to.deep.equal([ + await waitFor(() => expect(onHighlightedAxisChange).toHaveBeenCalledTimes(1)); + expect(onHighlightedAxisChange.mock.calls[onHighlightedAxisChange.mock.calls.length - 1][0]).to.deep.equal([ { axisId: 'x-axis', dataIndex: 0 }, ]); @@ -134,11 +134,11 @@ describe.skipIf(isJSDOM)('useChartCartesianAxis - axis highlight', () => { xAxis: [{ id: 'x-axis', scaleType: 'band', data: ['A', 'B', 'C'], position: 'none' }], }); - expect(onHighlightedAxisChange.callCount).to.equal(1); + expect(onHighlightedAxisChange).toHaveBeenCalledTimes(1); }); it('should call onHighlightedAxisChange when highlighted axis got removed', async () => { - const onHighlightedAxisChange = spy(); + const onHighlightedAxisChange = vi.fn(); const { user, setProps } = render( plugins={[useChartInteraction, useChartCartesianAxis]} @@ -157,8 +157,8 @@ describe.skipIf(isJSDOM)('useChartCartesianAxis - axis highlight', () => { await user.pointer([{ keys: '[TouchA>]', target: svg, coords: { clientX: 10, clientY: 60 } }]); - await waitFor(() => expect(onHighlightedAxisChange.callCount).to.equal(1)); - expect(onHighlightedAxisChange.lastCall.firstArg).to.deep.equal([ + await waitFor(() => expect(onHighlightedAxisChange).toHaveBeenCalledTimes(1)); + expect(onHighlightedAxisChange.mock.calls[onHighlightedAxisChange.mock.calls.length - 1][0]).to.deep.equal([ { axisId: 'x-axis', dataIndex: 0 }, ]); @@ -166,8 +166,8 @@ describe.skipIf(isJSDOM)('useChartCartesianAxis - axis highlight', () => { xAxis: [{ id: 'new-axis', scaleType: 'band', data: ['A', 'B'], position: 'none' }], }); - expect(onHighlightedAxisChange.callCount).to.equal(2); - expect(onHighlightedAxisChange.lastCall.firstArg).to.deep.equal([ + expect(onHighlightedAxisChange).toHaveBeenCalledTimes(2); + expect(onHighlightedAxisChange.mock.calls[onHighlightedAxisChange.mock.calls.length - 1][0]).to.deep.equal([ { axisId: 'new-axis', dataIndex: 0 }, ]); }); diff --git a/packages/x-data-grid-premium/src/tests/aggregation.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/aggregation.DataGridPremium.test.tsx index ff39ca701c38b..680c10cad8e46 100644 --- a/packages/x-data-grid-premium/src/tests/aggregation.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/aggregation.DataGridPremium.test.tsx @@ -2,7 +2,7 @@ import { RefObject } from '@mui/x-internals/types'; import { createRenderer, screen, within, act, fireEvent, waitFor } from '@mui/internal-test-utils'; import { getCell, getColumnHeaderCell, getColumnValues, microtasks } from 'test/utils/helperFn'; import { fireUserEvent } from 'test/utils/fireUserEvent'; -import { SinonSpy, spy } from 'sinon'; +import { vi, MockInstance } from 'vitest'; import { DataGridPremium, DataGridPremiumProps, @@ -82,7 +82,7 @@ describe(' - Aggregation', () => { describe('prop: aggregationModel', () => { it('should not call onAggregationModelChange on initialisation or on aggregationModel prop change', async () => { - const onAggregationModelChange = spy(); + const onAggregationModelChange = vi.fn(); const { setProps } = await render( - Aggregation', () => { />, ); - expect(onAggregationModelChange.callCount).to.equal(0); + expect(onAggregationModelChange).toHaveBeenCalledTimes(0); setProps({ id: 'min' }); - expect(onAggregationModelChange.callCount).to.equal(0); + expect(onAggregationModelChange).toHaveBeenCalledTimes(0); }); it('should allow to update the aggregation model from the outside', async () => { @@ -883,7 +883,7 @@ describe(' - Aggregation', () => { }); it('should pass aggregation meta with `hasCellUnit: true` if the aggregation function have no hasCellUnit property ', async () => { - const renderCell: SinonSpy<[GridRenderCellParams]> = spy((params) => `- ${params.value}`); + const renderCell: MockInstance<[GridRenderCellParams]> = spy((params) => `- ${params.value}`); const customAggregationFunction: GridAggregationFunction = { apply: () => 'Agg value', @@ -910,7 +910,7 @@ describe(' - Aggregation', () => { }); it('should pass aggregation meta with `hasCellUnit: false` if the aggregation function have `hasCellUnit: false` ', async () => { - const renderCell: SinonSpy<[GridRenderCellParams]> = spy((params) => `- ${params.value}`); + const renderCell: MockInstance<[GridRenderCellParams]> = spy((params) => `- ${params.value}`); const customAggregationFunction: GridAggregationFunction = { apply: () => 'Agg value', diff --git a/packages/x-data-grid-premium/src/tests/aiAssistant.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/aiAssistant.DataGridPremium.test.tsx index 7dd335354629b..ed0a8630e098b 100644 --- a/packages/x-data-grid-premium/src/tests/aiAssistant.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/aiAssistant.DataGridPremium.test.tsx @@ -89,8 +89,8 @@ describe(' - AI Assistant', () => { await user.keyboard('{Enter}'); expect(promptSpy.callCount).to.equal(1); - expect(promptSpy.firstCall.args[1]).contains('Example1'); - expect(promptSpy.firstCall.args[1]).not.contains('CatA'); + expect(promptSpy.firstCall.mock.calls[1]).contains('Example1'); + expect(promptSpy.firstCall.mock.calls[1]).not.contains('CatA'); }); it('should sample rows to generate the prompt context', async () => { @@ -104,8 +104,8 @@ describe(' - AI Assistant', () => { await user.keyboard('{Enter}'); expect(promptSpy.callCount).to.equal(1); - expect(promptSpy.firstCall.args[1]).not.contains('Example1'); - expect(promptSpy.firstCall.args[1]).contains('CatA'); + expect(promptSpy.firstCall.mock.calls[1]).not.contains('Example1'); + expect(promptSpy.firstCall.mock.calls[1]).contains('CatA'); }); }); diff --git a/packages/x-data-grid-premium/src/tests/cellSelection.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/cellSelection.DataGridPremium.test.tsx index 7b1714f7204e6..4cdea2400bdf0 100644 --- a/packages/x-data-grid-premium/src/tests/cellSelection.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/cellSelection.DataGridPremium.test.tsx @@ -139,8 +139,8 @@ describe(' - Cell selection', () => { await user.keyboard('{Shift>}'); await user.click(getCell(2, 1)); await user.keyboard('{/Shift}'); - expect(spiedSelectCellsBetweenRange.lastCall.args[0]).to.deep.equal({ id: 0, field: 'id' }); - expect(spiedSelectCellsBetweenRange.lastCall.args[1]).to.deep.equal({ + expect(spiedSelectCellsBetweenRange.mock.calls[spiedSelectCellsBetweenRange.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'id' }); + expect(spiedSelectCellsBetweenRange.mock.calls[spiedSelectCellsBetweenRange.mock.calls.length - 1][1]).to.deep.equal({ id: 2, field: 'currencyPair', }); @@ -192,8 +192,8 @@ describe(' - Cell selection', () => { cell.focus(); await user.click(cell); await user.keyboard('{Shift>}{ArrowDown}{/Shift}'); - expect(spiedSelectCellsBetweenRange.lastCall.args[0]).to.deep.equal({ id: 0, field: 'id' }); - expect(spiedSelectCellsBetweenRange.lastCall.args[1]).to.deep.equal({ id: 1, field: 'id' }); + expect(spiedSelectCellsBetweenRange.mock.calls[spiedSelectCellsBetweenRange.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'id' }); + expect(spiedSelectCellsBetweenRange.mock.calls[spiedSelectCellsBetweenRange.mock.calls.length - 1][1]).to.deep.equal({ id: 1, field: 'id' }); }); it('should call selectCellRange when ArrowUp is pressed', async () => { @@ -205,8 +205,8 @@ describe(' - Cell selection', () => { }); await user.click(cell); await user.keyboard('{Shift>}{ArrowUp}{/Shift}'); - expect(spiedSelectCellsBetweenRange.lastCall.args[0]).to.deep.equal({ id: 1, field: 'id' }); - expect(spiedSelectCellsBetweenRange.lastCall.args[1]).to.deep.equal({ id: 0, field: 'id' }); + expect(spiedSelectCellsBetweenRange.mock.calls[spiedSelectCellsBetweenRange.mock.calls.length - 1][0]).to.deep.equal({ id: 1, field: 'id' }); + expect(spiedSelectCellsBetweenRange.mock.calls[spiedSelectCellsBetweenRange.mock.calls.length - 1][1]).to.deep.equal({ id: 0, field: 'id' }); }); it('should call selectCellRange when ArrowLeft is pressed', async () => { @@ -216,11 +216,11 @@ describe(' - Cell selection', () => { cell.focus(); await user.click(cell); await user.keyboard('{Shift>}{ArrowLeft}{/Shift}'); - expect(spiedSelectCellsBetweenRange.lastCall.args[0]).to.deep.equal({ + expect(spiedSelectCellsBetweenRange.mock.calls[spiedSelectCellsBetweenRange.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'currencyPair', }); - expect(spiedSelectCellsBetweenRange.lastCall.args[1]).to.deep.equal({ id: 0, field: 'id' }); + expect(spiedSelectCellsBetweenRange.mock.calls[spiedSelectCellsBetweenRange.mock.calls.length - 1][1]).to.deep.equal({ id: 0, field: 'id' }); }); it('should call selectCellRange when ArrowRight is pressed', async () => { @@ -230,8 +230,8 @@ describe(' - Cell selection', () => { cell.focus(); await user.click(cell); await user.keyboard('{Shift>}{ArrowRight}{/Shift}'); - expect(spiedSelectCellsBetweenRange.lastCall.args[0]).to.deep.equal({ id: 0, field: 'id' }); - expect(spiedSelectCellsBetweenRange.lastCall.args[1]).to.deep.equal({ + expect(spiedSelectCellsBetweenRange.mock.calls[spiedSelectCellsBetweenRange.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'id' }); + expect(spiedSelectCellsBetweenRange.mock.calls[spiedSelectCellsBetweenRange.mock.calls.length - 1][1]).to.deep.equal({ id: 0, field: 'currencyPair', }); @@ -259,7 +259,7 @@ describe(' - Cell selection', () => { await user.click(getCell(0, 0)); expect(onCellSelectionModelChange.callCount).to.equal(1); - expect(onCellSelectionModelChange.lastCall.args[0]).to.deep.equal({ '0': { id: true } }); + expect(onCellSelectionModelChange.mock.calls[onCellSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal({ '0': { id: true } }); }); // Context: https://github.com/mui/mui-x/issues/14184 @@ -286,7 +286,7 @@ describe(' - Cell selection', () => { ]); await user.keyboard(isMac ? '{/Meta}' : '{/Control}'); - expect(onCellSelectionModelChange.lastCall.args[0]).to.deep.equal({ + expect(onCellSelectionModelChange.mock.calls[onCellSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal({ '0': { id: true }, '2': { id: true }, '3': { id: true }, diff --git a/packages/x-data-grid-premium/src/tests/chartsIntegration.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/chartsIntegration.DataGridPremium.test.tsx index 2f4a0eb4f77a9..688fee4ba422f 100644 --- a/packages/x-data-grid-premium/src/tests/chartsIntegration.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/chartsIntegration.DataGridPremium.test.tsx @@ -1,4 +1,4 @@ -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { RefObject, GridChartsConfigurationOptions } from '@mui/x-internals/types'; import { act, createRenderer, screen, waitFor } from '@mui/internal-test-utils'; import { @@ -115,7 +115,7 @@ const configurationOptions: GridChartsConfigurationOptions = { describe(' - Charts Integration', () => { const { render } = createRenderer(); - const renderSpy = spy(); + const renderSpy = vi.fn(); let apiRef: RefObject; let integrationContext: GridChartsIntegrationContextValue | null = null; @@ -157,7 +157,7 @@ describe(' - Charts Integration', () => { } beforeEach(() => { - renderSpy.resetHistory(); + renderSpy.mockClear(); }); describe('GridChartsIntegrationContextProvider', () => { @@ -700,9 +700,9 @@ describe(' - Charts Integration', () => { it('should intercept rendering with custom renderer', async () => { render(); - expect(renderSpy.called).to.equal(true); + expect(renderSpy).toHaveBeenCalled(); await waitFor(() => { - expect(renderSpy.lastCall.firstArg.chartStateLookup.test.dimensions[0].id).to.equal( + expect(renderSpy.mock.calls[renderSpy.mock.calls.length - 1][0].chartStateLookup.test.dimensions[0].id).to.equal( 'category1', ); }); @@ -711,8 +711,8 @@ describe(' - Charts Integration', () => { it('should trigger another render when the context is updated', async () => { render(); - renderSpy.resetHistory(); - expect(renderSpy.callCount).to.equal(0); + renderSpy.mockClear(); + expect(renderSpy).toHaveBeenCalledTimes(0); act(() => { apiRef!.current?.sortColumn('amount', 'desc'); diff --git a/packages/x-data-grid-premium/src/tests/clipboard.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/clipboard.DataGridPremium.test.tsx index 388cb9f15c578..ee490c7a1f98b 100644 --- a/packages/x-data-grid-premium/src/tests/clipboard.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/clipboard.DataGridPremium.test.tsx @@ -8,7 +8,7 @@ import { GridColDef, } from '@mui/x-data-grid-premium'; import { act, createRenderer, fireEvent, waitFor } from '@mui/internal-test-utils'; -import { SinonSpy, spy, stub, SinonStub } from 'sinon'; +import { vi, MockInstance } from 'vitest'; import { getCell, getColumnValues, includeRowSelection, sleep } from 'test/utils/helperFn'; import { getBasicGridData } from '@mui/x-data-grid-generator'; import { isJSDOM } from 'test/utils/skipIf'; @@ -59,7 +59,7 @@ describe(' - Clipboard', () => { } describe('copy', () => { - let writeText: SinonSpy | undefined; + let writeText: MockInstance | undefined; afterEach(function afterEachHook() { writeText?.restore(); @@ -69,7 +69,7 @@ describe(' - Clipboard', () => { it(`should copy the selected cells to the clipboard when ${key} + C is pressed`, async () => { const { user } = render(); - writeText = spy(navigator.clipboard, 'writeText'); + writeText = vi.spyOn(navigator.clipboard, 'writeText'); const cell = getCell(0, 0); await act(() => cell.focus()); @@ -79,7 +79,7 @@ describe(' - Clipboard', () => { fireEvent.click(getCell(2, 2), { shiftKey: true }); fireEvent.keyDown(cell, { key: 'c', keyCode: 67, [key]: true }); - expect(writeText.firstCall.args[0]).to.equal( + expect(writeText.firstCall.mock.calls[0]).to.equal( [ ['0', 'USDGBP', '1'].join('\t'), ['1', 'USDEUR', '11'].join('\t'), @@ -92,7 +92,7 @@ describe(' - Clipboard', () => { it(`should copy cells range selected in one row`, async () => { const { user } = render(); - writeText = spy(navigator.clipboard, 'writeText'); + writeText = vi.spyOn(navigator.clipboard, 'writeText'); const cell = getCell(0, 0); await act(() => cell.focus()); @@ -102,7 +102,7 @@ describe(' - Clipboard', () => { fireEvent.click(getCell(0, 2), { shiftKey: true }); fireEvent.keyDown(cell, { key: 'c', keyCode: 67, ctrlKey: true }); - expect(writeText.firstCall.args[0]).to.equal([['0', 'USDGBP', '1'].join('\t')].join('\r\n')); + expect(writeText.firstCall.mock.calls[0]).to.equal([['0', 'USDGBP', '1'].join('\t')].join('\r\n')); }); it(`should copy cells range selected based on their sorted order`, async () => { @@ -123,7 +123,7 @@ describe(' - Clipboard', () => {
, ); - writeText = spy(navigator.clipboard, 'writeText'); + writeText = vi.spyOn(navigator.clipboard, 'writeText'); const cell = getCell(0, 0); await act(() => cell.focus()); @@ -136,7 +136,7 @@ describe(' - Clipboard', () => { fireEvent.click(getCell(2, 0), { ctrlKey: true }); fireEvent.keyDown(cell, { key: 'c', keyCode: 67, ctrlKey: true }); - expect(writeText.lastCall.firstArg).to.equal(['Adidas', 'Nike', 'Puma'].join('\r\n')); + expect(writeText.mock.calls[writeText.mock.calls.length - 1][0]).to.equal(['Adidas', 'Nike', 'Puma'].join('\r\n')); }); it('should not escape double quotes when copying multiple cells to clipboard', async () => { @@ -154,7 +154,7 @@ describe(' - Clipboard', () => {
, ); - writeText = spy(navigator.clipboard, 'writeText'); + writeText = vi.spyOn(navigator.clipboard, 'writeText'); const cell = getCell(0, 0); await act(() => cell.focus()); @@ -164,7 +164,7 @@ describe(' - Clipboard', () => { fireEvent.click(getCell(1, 0), { ctrlKey: true }); fireEvent.keyDown(cell, { key: 'c', keyCode: 67, ctrlKey: true }); - expect(writeText.lastCall.firstArg).to.equal(['1 " 1', '2'].join('\r\n')); + expect(writeText.mock.calls[writeText.mock.calls.length - 1][0]).to.equal(['1 " 1', '2'].join('\r\n')); }); it('should copy aggregation cell value to clipboard', async () => { @@ -179,7 +179,7 @@ describe(' - Clipboard', () => { // set aggregation model through API to avoid act error await act(async () => apiRef.current?.setAggregationModel({ value: 'sum' })); - writeText = spy(navigator.clipboard, 'writeText'); + writeText = vi.spyOn(navigator.clipboard, 'writeText'); // Because of the row grouping, the value column only displays the aggregation cells initially const aggregationCell = getCell(0, 2); @@ -189,7 +189,7 @@ describe(' - Clipboard', () => { await user.click(aggregationCell); fireEvent.keyDown(aggregationCell, { key: 'c', keyCode: 67, ctrlKey: true }); - expect(writeText.firstCall.args[0]).to.equal('30'); + expect(writeText.firstCall.mock.calls[0]).to.equal('30'); }); }); @@ -211,12 +211,12 @@ describe(' - Clipboard', () => { it(`should not enter cell edit mode when ${key} + V is pressed`, async () => { const { user } = render(); - const listener = spy(); + const listener = vi.fn(); apiRef.current?.subscribeEvent('cellEditStart', listener); const cell = getCell(0, 1); await user.click(cell); fireEvent.keyDown(cell, { key: 'v', keyCode: 86, [key]: true }); // Ctrl+V - expect(listener.callCount).to.equal(0); + expect(listener).toHaveBeenCalledTimes(0); }); }); @@ -224,12 +224,12 @@ describe(' - Clipboard', () => { it(`should not enter row edit mode when ${key} + V is pressed`, async () => { const { user } = render(); - const listener = spy(); + const listener = vi.fn(); apiRef.current?.subscribeEvent('rowEditStart', listener); const cell = getCell(0, 1); await user.click(cell); fireEvent.keyDown(cell, { key: 'v', keyCode: 86, [key]: true }); // Ctrl+V - expect(listener.callCount).to.equal(0); + expect(listener).toHaveBeenCalledTimes(0); }); }); @@ -587,7 +587,7 @@ describe(' - Clipboard', () => { }); it('should use valueSetter if the column has one', async () => { - const processRowUpdateSpy = spy((newRow) => newRow); + const processRowUpdateSpy = vi.fn((newRow) => newRow); const columns: GridColDef<(typeof rows)[number]>[] = [ { field: 'firstName' }, @@ -630,8 +630,8 @@ describe(' - Clipboard', () => { paste(cell, 'John Doe'); await waitFor(() => expect(getColumnValues(0)).to.deep.equal(['Jon', 'John'])); - expect(processRowUpdateSpy.callCount).to.equal(1); - expect(processRowUpdateSpy.args[0]).to.deep.equal([ + expect(processRowUpdateSpy).toHaveBeenCalledTimes(1); + expect(processRowUpdateSpy.mock.calls[0]).to.deep.equal([ { id: 1, firstName: 'John', lastName: 'Doe' }, { id: 1, firstName: 'Cersei', lastName: 'Lannister' }, { rowId: '1' }, @@ -722,7 +722,7 @@ describe(' - Clipboard', () => { }); it('should call `processRowUpdate` with each row impacted by the paste', async () => { - const processRowUpdateSpy = spy((newRow) => { + const processRowUpdateSpy = vi.fn((newRow) => { return newRow; }); const { user } = render(); @@ -740,7 +740,7 @@ describe(' - Clipboard', () => { expect(getCell(2, 2).textContent).to.equal('12'); }); - expect(processRowUpdateSpy.callCount).to.equal(3); + expect(processRowUpdateSpy).toHaveBeenCalledTimes(3); expect(processRowUpdateSpy.args).to.deep.equal([ [ { id: 0, currencyPair: '12', price1M: '12' }, // new row @@ -839,7 +839,7 @@ describe(' - Clipboard', () => { }); it('should call `onProcessRowUpdateError()` if `processRowUpdate()` fails', async () => { - const onProcessRowUpdateError = spy(); + const onProcessRowUpdateError = vi.fn(); const error = new Error('Something went wrong'); const { user } = render( - Clipboard', () => { paste(cell, '12'); await waitFor(() => { - expect(onProcessRowUpdateError.callCount).to.equal(1); + expect(onProcessRowUpdateError).toHaveBeenCalledTimes(1); }); - expect(onProcessRowUpdateError.args[0][0]).to.equal(error); + expect(onProcessRowUpdateError.mock.calls[0][0]).to.equal(error); }); it('should emit clipboard paste events', async () => { const calls: string[] = []; - const onClipboardPasteStartSpy = spy(() => { + const onClipboardPasteStartSpy = vi.fn(() => { calls.push('onClipboardPasteStart'); }); - const onClipboardPasteEndSpy = spy(() => { + const onClipboardPasteEndSpy = vi.fn(() => { calls.push('onClipboardPasteEnd'); }); - const processRowUpdateSpy = spy((newRow) => { + const processRowUpdateSpy = vi.fn((newRow) => { calls.push('processRowUpdate'); return newRow; }); @@ -912,10 +912,10 @@ describe(' - Clipboard', () => { clipboardData = data; return Promise.resolve(); }; - let writeTextStub: SinonStub; + let writeTextStub: MockInstance; const stubClipboard = () => { - writeTextStub = stub(navigator.clipboard, 'writeText').callsFake(writeText); + writeTextStub = vi.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeText); }; afterEach(function afterEachHook() { diff --git a/packages/x-data-grid-premium/src/tests/dataSourceAggregation.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/dataSourceAggregation.DataGridPremium.test.tsx index cacbfddcf3e3e..90e3d25bee250 100644 --- a/packages/x-data-grid-premium/src/tests/dataSourceAggregation.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/dataSourceAggregation.DataGridPremium.test.tsx @@ -12,7 +12,7 @@ import { GRID_AGGREGATION_ROOT_FOOTER_ROW_ID, GRID_ROOT_GROUP_ID, } from '@mui/x-data-grid-premium'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { getColumnHeaderCell, getCell } from 'test/utils/helperFn'; import { isJSDOM } from 'test/utils/skipIf'; @@ -20,13 +20,13 @@ describe.skipIf(isJSDOM)(' - Data source aggregation', () => const { render } = createRenderer(); let apiRef: RefObject; - const fetchRowsSpy = spy(); - const editRowSpy = spy(); + const fetchRowsSpy = vi.fn(); + const editRowSpy = vi.fn(); // TODO: Resets strictmode calls, need to find a better fix for this, maybe an AbortController? function Reset() { React.useLayoutEffect(() => { - fetchRowsSpy.resetHistory(); + fetchRowsSpy.mockClear(); }, []); return null; } @@ -162,7 +162,7 @@ describe.skipIf(isJSDOM)(' - Data source aggregation', () => expect(fetchRowsSpy.callCount).to.be.greaterThan(0); }); - expect(fetchRowsSpy.lastCall.args[0].aggregationModel).to.deep.equal({ id: 'size' }); + expect(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0].aggregationModel).to.deep.equal({ id: 'size' }); }); it('should show the aggregation footer row when aggregation is enabled', async () => { @@ -218,7 +218,7 @@ describe.skipIf(isJSDOM)(' - Data source aggregation', () => />, ); - expect(fetchRowsSpy.callCount).to.equal(1); + expect(fetchRowsSpy).toHaveBeenCalledTimes(1); await waitFor(() => { expect(Object.keys(apiRef.current!.state.rows.tree).length).to.be.greaterThan(1); }); @@ -227,7 +227,7 @@ describe.skipIf(isJSDOM)(' - Data source aggregation', () => await user.click(within(cell11).getByRole('button')); await waitFor(() => { - expect(fetchRowsSpy.callCount).to.equal(2); + expect(fetchRowsSpy).toHaveBeenCalledTimes(2); }); const cell = getCell(1, apiRef.current!.state.columns.orderedFields.indexOf('gross')); @@ -236,10 +236,10 @@ describe.skipIf(isJSDOM)(' - Data source aggregation', () => await user.keyboard('{Enter}{Delete}1{Enter}'); - expect(editRowSpy.callCount).to.equal(1); + expect(editRowSpy).toHaveBeenCalledTimes(1); // Two additional calls should be made await waitFor(() => { - expect(fetchRowsSpy.callCount).to.equal(4); + expect(fetchRowsSpy).toHaveBeenCalledTimes(4); }); }); }); diff --git a/packages/x-data-grid-premium/src/tests/dataSourcePivoting.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/dataSourcePivoting.DataGridPremium.test.tsx index 496a3053c56b1..df8124dd6c706 100644 --- a/packages/x-data-grid-premium/src/tests/dataSourcePivoting.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/dataSourcePivoting.DataGridPremium.test.tsx @@ -34,7 +34,7 @@ describe(' - Data source pivoting', () => { // TODO: Resets strictmode calls, need to find a better fix for this, maybe an AbortController? function Reset() { React.useLayoutEffect(() => { - fetchRowsSpy.resetHistory(); + fetchRowsSpy.mockClear(); }, []); return null; } @@ -135,7 +135,7 @@ describe(' - Data source pivoting', () => { expect(fetchRowsSpy.callCount).to.be.greaterThan(0); }); - expect(fetchRowsSpy.lastCall.args[0].pivotModel).to.deep.equal(pivotModel); + expect(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0].pivotModel).to.deep.equal(pivotModel); }); it('should not pass hidden rows, columns and values in the `pivotModel` in the `getRows` params', async () => { @@ -159,7 +159,7 @@ describe(' - Data source pivoting', () => { expect(fetchRowsSpy.callCount).to.be.greaterThan(0); }); - expect(fetchRowsSpy.lastCall.args[0].pivotModel).to.deep.equal({ + expect(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0].pivotModel).to.deep.equal({ rows: [], columns: [], values: [], @@ -230,7 +230,7 @@ describe(' - Data source pivoting', () => { expect(fetchRowsSpy.callCount).to.be.greaterThan(0); }); - expect(fetchRowsSpy.lastCall.args[0].pivotModel).to.deep.equal(pivotModel); + expect(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0].pivotModel).to.deep.equal(pivotModel); await waitFor(() => { expect(getColumnHeadersTextContent()).to.deep.equal([ '', @@ -284,7 +284,7 @@ describe(' - Data source pivoting', () => { }); // Verify that pivot model is passed to getRows - expect(fetchRowsSpy.lastCall.args[0].pivotModel).to.deep.equal(pivotModel); + expect(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0].pivotModel).to.deep.equal(pivotModel); // Verify displayed data await waitFor(() => { @@ -339,7 +339,7 @@ describe(' - Data source pivoting', () => { }); // Verify that pivot model is passed to getRows - expect(fetchRowsSpy.lastCall.args[0].pivotModel).to.deep.equal(pivotModel); + expect(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0].pivotModel).to.deep.equal(pivotModel); // Verify displayed data await waitFor(() => { diff --git a/packages/x-data-grid-premium/src/tests/exportExcel.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/exportExcel.DataGridPremium.test.tsx index 63c33f90e23fb..ee820e600f4eb 100644 --- a/packages/x-data-grid-premium/src/tests/exportExcel.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/exportExcel.DataGridPremium.test.tsx @@ -408,13 +408,13 @@ describe(' - Export Excel', () => { render(); const getDataAsExcelSpy = spyApi(apiRef.current!, 'getDataAsExcel'); await act(() => apiRef.current?.exportDataAsExcel({ worker: () => workerMock as any })); - expect(getDataAsExcelSpy.calledOnce).to.equal(false); + expect(getDataAsExcelSpy).not.toHaveBeenCalled(); }); it('should post a message to the web worker with the serialized columns', async () => { render(); await act(() => apiRef.current?.exportDataAsExcel({ worker: () => workerMock as any })); - expect(workerMock.postMessage.lastCall.args[0].serializedColumns).to.deep.equal([ + expect(workerMock.postMessage.mock.calls[postMessage.mock.calls.length - 1][0].serializedColumns).to.deep.equal([ { key: 'id', headerText: 'id', style: {}, width: 100 / 7.5 }, { key: 'brand', headerText: 'Brand', style: {}, width: 100 / 7.5 }, ]); @@ -423,7 +423,7 @@ describe(' - Export Excel', () => { it('should post a message to the web worker with the serialized rows', async () => { render(); await act(() => apiRef.current?.exportDataAsExcel({ worker: () => workerMock as any })); - expect(workerMock.postMessage.lastCall.args[0].serializedRows).to.deep.equal([ + expect(workerMock.postMessage.mock.calls[postMessage.mock.calls.length - 1][0].serializedRows).to.deep.equal([ { dataValidation: {}, mergedCells: [], diff --git a/packages/x-data-grid-premium/src/tests/pivoting.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/pivoting.DataGridPremium.test.tsx index 27e4e0babf2a5..cd8c651f94686 100644 --- a/packages/x-data-grid-premium/src/tests/pivoting.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/pivoting.DataGridPremium.test.tsx @@ -17,7 +17,7 @@ import { getRowValues, sleep, } from 'test/utils/helperFn'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { isJSDOM } from 'test/utils/skipIf'; const ROWS = [ @@ -897,7 +897,7 @@ describe(' - Pivoting', () => { it.skipIf(isJSDOM)( 'should not hide the pivot column on double click on the column separator', async () => { - const onColumnWidthChange = spy(); + const onColumnWidthChange = vi.fn(); const { user } = render( - Pivoting', () => { const separators = $$(`.${gridClasses['columnSeparator--resizable']}`); - expect(onColumnWidthChange.callCount).to.equal(0); + expect(onColumnWidthChange).toHaveBeenCalledTimes(0); await user.dblClick(separators[1]); - expect(onColumnWidthChange.callCount).to.equal(1); - expect(onColumnWidthChange.args[0][0].colDef.field).to.equal('2024>->volume'); - expect(onColumnWidthChange.args[0][0].width).to.equal(68); + expect(onColumnWidthChange).toHaveBeenCalledTimes(1); + expect(onColumnWidthChange.mock.calls[0][0].colDef.field).to.equal('2024>->volume'); + expect(onColumnWidthChange.mock.calls[0][0].width).to.equal(68); expect(getColumnHeaderCell(1).offsetWidth).to.equal(68); await waitFor(() => { diff --git a/packages/x-data-grid-premium/src/tests/rowGrouping.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/rowGrouping.DataGridPremium.test.tsx index 22a62ad3c63e6..0eaf364efd4fd 100644 --- a/packages/x-data-grid-premium/src/tests/rowGrouping.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/rowGrouping.DataGridPremium.test.tsx @@ -20,7 +20,7 @@ import { useGridApiRef, GridLogicOperator, } from '@mui/x-data-grid-premium'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { isJSDOM } from 'test/utils/skipIf'; interface BaselineProps extends DataGridPremiumProps { @@ -105,7 +105,7 @@ describe(' - Row grouping', () => { describe('prop: rowGroupingModel', () => { it('should not call onRowGroupingModelChange on initialisation or on rowGroupingModel prop change', () => { - const onRowGroupingModelChange = spy(); + const onRowGroupingModelChange = vi.fn(); const { setProps } = render( - Row grouping', () => { />, ); - expect(onRowGroupingModelChange.callCount).to.equal(0); + expect(onRowGroupingModelChange).toHaveBeenCalledTimes(0); setProps({ rowGroupingModel: ['category2'] }); - expect(onRowGroupingModelChange.callCount).to.equal(0); + expect(onRowGroupingModelChange).toHaveBeenCalledTimes(0); }); it('should allow to update the row grouping model from the outside', () => { @@ -1348,11 +1348,11 @@ describe(' - Row grouping', () => { />, ); - const onFilteredRowsSet = spy(); + const onFilteredRowsSet = vi.fn(); apiRef.current?.subscribeEvent('filteredRowsSet', onFilteredRowsSet); fireEvent.click(getCell(0, 0).querySelector('button')!); - expect(onFilteredRowsSet.callCount).to.equal(0); + expect(onFilteredRowsSet).toHaveBeenCalledTimes(0); }); it('should not apply filters when the row is collapsed', () => { @@ -1365,11 +1365,11 @@ describe(' - Row grouping', () => { />, ); - const onFilteredRowsSet = spy(); + const onFilteredRowsSet = vi.fn(); apiRef.current?.subscribeEvent('filteredRowsSet', onFilteredRowsSet); fireEvent.click(getCell(0, 0).querySelector('button')!); - expect(onFilteredRowsSet.callCount).to.equal(0); + expect(onFilteredRowsSet).toHaveBeenCalledTimes(0); }); }); diff --git a/packages/x-data-grid-premium/src/tests/rowGroupingProp.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/rowGroupingProp.DataGridPremium.test.tsx index 799fcd6b6b74d..f6e64bead4acc 100644 --- a/packages/x-data-grid-premium/src/tests/rowGroupingProp.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/rowGroupingProp.DataGridPremium.test.tsx @@ -736,7 +736,7 @@ describe(' - Row grouping', () => { ); fireUserEvent.mousePress(getCell(1, 0)); - expect(renderIdCell.lastCall.firstArg.field).to.equal('id'); + expect(renderIdCell.mock.calls[renderIdCell.mock.calls.length - 1][0].field).to.equal('id'); expect(getCell(1, 0)).to.have.text('Focused: true'); }); }); diff --git a/packages/x-data-grid-premium/src/tests/rowReorder.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/rowReorder.DataGridPremium.test.tsx index 5c4082e297e90..aa0807c9a8d78 100644 --- a/packages/x-data-grid-premium/src/tests/rowReorder.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/rowReorder.DataGridPremium.test.tsx @@ -643,10 +643,10 @@ describe.skipIf(isJSDOM)(' - Row reorder with row grouping', // Verify groupingValueSetter was called expect(groupingValueSetter.callCount).to.equal(1); - expect(groupingValueSetter.firstCall.args[0]).to.equal('Clothing'); // groupingValue should be 'Clothing' + expect(groupingValueSetter.firstCall.mock.calls[0]).to.equal('Clothing'); // groupingValue should be 'Clothing' // Verify the row passed to the setter matches iPhone data - const passedRow = groupingValueSetter.firstCall.args[1]; + const passedRow = groupingValueSetter.firstCall.mock.calls[1]; expect(passedRow.name).to.equal('iPhone'); expect(passedRow.price).to.equal(999); @@ -1023,14 +1023,14 @@ describe.skipIf(isJSDOM)(' - Row reorder with row grouping', expect(deptValueSetter.callCount).to.equal(1); // Verify company setter was called with correct parameters - expect(companyValueSetter.firstCall.args[0]).to.equal('Google'); // target company - const companySetterRow = companyValueSetter.firstCall.args[1]; + expect(companyValueSetter.firstCall.mock.calls[0]).to.equal('Google'); // target company + const companySetterRow = companyValueSetter.firstCall.mock.calls[1]; expect(companySetterRow.company).to.equal('Microsoft'); // Original company expect(companySetterRow.dept).to.equal('Engineering'); // Original dept // Verify dept setter was called with correct parameters - expect(deptValueSetter.firstCall.args[0]).to.equal('Engineering'); // target dept (Google has Engineering) - const deptSetterRow = deptValueSetter.firstCall.args[1]; + expect(deptValueSetter.firstCall.mock.calls[0]).to.equal('Engineering'); // target dept (Google has Engineering) + const deptSetterRow = deptValueSetter.firstCall.mock.calls[1]; expect(deptSetterRow.company).to.equal('Google'); // Already updated by company setter // Verify the final row data was updated correctly @@ -1128,7 +1128,7 @@ describe.skipIf(isJSDOM)(' - Row reorder with row grouping', // Verify callback was called expect(onRowOrderChange.callCount).to.equal(1); - const params = onRowOrderChange.firstCall.args[0]; + const params = onRowOrderChange.firstCall.mock.calls[0]; expect(params.row.id).to.equal(1); // Item A1 expect(params.oldIndex).to.be.a('number'); expect(params.targetIndex).to.be.a('number'); @@ -1389,7 +1389,7 @@ describe.skipIf(isJSDOM)(' - Row reorder with row grouping', }); // Verify error was passed to handler - const error = onProcessRowUpdateError.firstCall.args[0]; + const error = onProcessRowUpdateError.firstCall.mock.calls[0]; expect(error.message).to.equal('Validation failed'); // Verify row order was not changed due to error @@ -1430,7 +1430,7 @@ describe.skipIf(isJSDOM)(' - Row reorder with row grouping', }); // Verify error was passed to handler - const error = onProcessRowUpdateError.firstCall.args[0]; + const error = onProcessRowUpdateError.firstCall.mock.calls[0]; expect(error.message).to.equal('Async validation failed'); }); @@ -1542,7 +1542,7 @@ describe.skipIf(isJSDOM)(' - Row reorder with row grouping', }); // Verify correct parameters were passed - const params = editRowSpy.firstCall.args[0]; + const params = editRowSpy.firstCall.mock.calls[0]; expect(params.rowId).to.equal(1); expect(params.previousRow.id).to.equal(1); expect(params.previousRow.category).to.equal('A'); @@ -1713,20 +1713,20 @@ describe.skipIf(isJSDOM)(' - Row reorder with row grouping', }); // Verify `setGroupKey()` was called with correct parameters - expect(setGroupKeySpy.firstCall.args[0]).to.deep.include({ + expect(setGroupKeySpy.firstCall.mock.calls[0]).to.deep.include({ id: 1, category: 'A', name: 'Item A1', value: 10, }); - expect(setGroupKeySpy.firstCall.args[1]).to.equal('autogenerated-parent-category-B'); // Group key of the target row's parent group + expect(setGroupKeySpy.firstCall.mock.calls[1]).to.equal('autogenerated-parent-category-B'); // Group key of the target row's parent group await waitFor(() => { expect(editRowSpy.callCount).to.equal(1); }); // Verify updateRow was called with the result from `setGroupKey()` - const updateRowParams = editRowSpy.firstCall.args[0]; + const updateRowParams = editRowSpy.firstCall.mock.calls[0]; expect(updateRowParams.updatedRow).to.deep.include({ id: 1, category: 'B', @@ -3144,7 +3144,7 @@ describe.skipIf(isJSDOM)(' - Row reorder with row grouping', await waitFor(() => { // Verify the setter was called with correct parameters - expect(regionGroupingValueSetter.firstCall.args[0]).to.equal('EU'); // target region + expect(regionGroupingValueSetter.firstCall.mock.calls[0]).to.equal('EU'); // target region }); await waitFor(() => { @@ -3370,7 +3370,7 @@ describe.skipIf(isJSDOM)(' - Row reorder with row grouping', expect(dataCorpGroup).to.equal(undefined, 'DataCorp group should be removed'); // Verify David was moved successfully - const updatedRows = processRowUpdate.getCalls().map((call) => call.args[0]); + const updatedRows = processRowUpdate.getCalls().map((call) => call.mock.calls[0]); const davidRow = updatedRows.find((row: any) => row.name === 'David'); expect(davidRow?.company).to.equal('TechCorp', 'David should be moved to TechCorp'); }); diff --git a/packages/x-data-grid-premium/src/tests/rowSelection.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/rowSelection.DataGridPremium.test.tsx index 6b9dfc4662f31..b66d4d585bd3e 100644 --- a/packages/x-data-grid-premium/src/tests/rowSelection.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/rowSelection.DataGridPremium.test.tsx @@ -77,7 +77,7 @@ describe(' - Row selection', () => { />, ); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([3, 4, 'auto-generated-row-category1/Cat B']), ); }); @@ -96,7 +96,7 @@ describe(' - Row selection', () => { setProps({ rowSelectionModel: includeRowSelection([3, 4]) }); }); expect(onRowSelectionModelChange.callCount).to.equal(1); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([3, 4, 'auto-generated-row-category1/Cat B']), ); }); @@ -117,7 +117,7 @@ describe(' - Row selection', () => { }); }); expect(onRowSelectionModelChange.callCount).to.equal(1); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([3, 4, 'auto-generated-row-category1/Cat B']), ); }); @@ -229,7 +229,7 @@ describe(' - Row selection', () => { setProps({ rows }); }); expect(onRowSelectionModelChange.callCount).to.equal(1); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([3, 4, 'auto-generated-row-category1/Cat B']), ); }); @@ -251,7 +251,7 @@ describe(' - Row selection', () => { setProps({ rows }); }); expect(onRowSelectionModelChange.callCount).to.equal(1); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection(['auto-generated-row-category1/Cat B', 3, 4]), ); }); diff --git a/packages/x-data-grid-pro/src/tests/cellEditing.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/cellEditing.DataGridPro.test.tsx index 136f242013413..c8f899c6d497b 100644 --- a/packages/x-data-grid-pro/src/tests/cellEditing.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/cellEditing.DataGridPro.test.tsx @@ -85,9 +85,9 @@ describe(' - Cell editing', () => { render(); act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair' })); - expect(renderEditCell.lastCall.args[0].value).to.equal('USDGBP'); - expect(renderEditCell.lastCall.args[0].error).to.equal(false); - expect(renderEditCell.lastCall.args[0].isProcessingProps).to.equal(false); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USDGBP'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].error).to.equal(false); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].isProcessingProps).to.equal(false); }); it('should empty the value if deleteValue is true', () => { @@ -98,9 +98,9 @@ describe(' - Cell editing', () => { act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair', deleteValue: true }), ); - expect(renderEditCell.lastCall.args[0].value).to.equal(''); - expect(renderEditCell.lastCall.args[0].error).to.equal(false); - expect(renderEditCell.lastCall.args[0].isProcessingProps).to.equal(false); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal(''); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].error).to.equal(false); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].isProcessingProps).to.equal(false); }); }); @@ -111,11 +111,11 @@ describe(' - Cell editing', () => { render(); act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair' })); - expect(renderEditCell.lastCall.args[0].value).to.equal('USDGBP'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USDGBP'); await act(() => apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'usdgbp' }), ); - expect(renderEditCell.lastCall.args[0].value).to.equal('usdgbp'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('usdgbp'); }); it('should pass to renderEditCell the row with the value updated', async () => { @@ -128,11 +128,11 @@ describe(' - Cell editing', () => { render(); act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair' })); - expect(renderEditCell.lastCall.args[0].row).to.deep.equal(defaultData.rows[0]); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].row).to.deep.equal(defaultData.rows[0]); await act(() => apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: ' usdgbp ' }), ); - expect(renderEditCell.lastCall.args[0].row).to.deep.equal({ + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].row).to.deep.equal({ ...defaultData.rows[0], currencyPair: 'usdgbp', }); @@ -150,7 +150,7 @@ describe(' - Cell editing', () => { apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); expect(valueParser.callCount).to.equal(1); - expect(renderEditCell.lastCall.args[0].value).to.equal('usd gbp'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('usd gbp'); }); it('should return true if no preProcessEditCellProps is defined', async () => { @@ -178,7 +178,7 @@ describe(' - Cell editing', () => { value: 'USD GBP', }) as Promise; }); - expect(renderEditCell.lastCall.args[0].isProcessingProps).to.equal(true); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].isProcessingProps).to.equal(true); return act(() => promise); }); @@ -193,7 +193,7 @@ describe(' - Cell editing', () => { value: 'USD GBP', }), ); - const args = preProcessEditCellProps.lastCall.args[0]; + const args = preProcessEditCellProps.mock.calls[preProcessEditCellProps.mock.calls.length - 1][0]; expect(args.id).to.equal(0); expect(args.row).to.deep.equal(defaultData.rows[0]); expect(args.hasChanged).to.equal(true); @@ -244,11 +244,11 @@ describe(' - Cell editing', () => { render(); act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair' })); - expect(renderEditCell.lastCall.args[0].foo).to.equal(undefined); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].foo).to.equal(undefined); await act(() => apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); - expect(renderEditCell.lastCall.args[0].foo).to.equal('bar'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].foo).to.equal('bar'); }); it('should not pass to renderEditCell the value returned by preProcessEditCellProps', async () => { @@ -260,11 +260,11 @@ describe(' - Cell editing', () => { render(); act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair' })); - expect(renderEditCell.lastCall.args[0].value).to.equal('USDGBP'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USDGBP'); await act(() => apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); - expect(renderEditCell.lastCall.args[0].value).to.equal('USD GBP'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USD GBP'); }); it('should set isProcessingProps to false after calling preProcessEditCellProps', async () => { @@ -282,9 +282,9 @@ describe(' - Cell editing', () => { value: 'USD GBP', }) as Promise; }); - expect(renderEditCell.lastCall.args[0].isProcessingProps).to.equal(true); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].isProcessingProps).to.equal(true); await act(() => promise); - expect(renderEditCell.lastCall.args[0].isProcessingProps).to.equal(false); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].isProcessingProps).to.equal(false); }); it('should return false if preProcessEditCellProps sets an error', async () => { @@ -344,8 +344,8 @@ describe(' - Cell editing', () => { render(); act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair' })); - expect(renderEditCell.lastCall.args[0].value).to.equal('USDGBP'); - renderEditCell.resetHistory(); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USDGBP'); + renderEditCell.mockClear(); apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', @@ -365,7 +365,7 @@ describe(' - Cell editing', () => { expect(renderEditCell.callCount).not.to.equal(0); }); - expect(renderEditCell.lastCall.args[0].value).to.equal('USD GBP'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USD GBP'); }); }); }); @@ -472,7 +472,7 @@ describe(' - Cell editing', () => { apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); act(() => apiRef.current?.stopCellEditMode({ id: 0, field: 'currencyPair' })); - expect(onCellModesModelChange.lastCall.args[0]).to.deep.equal({ + expect(onCellModesModelChange.mock.calls[onCellModesModelChange.mock.calls.length - 1][0]).to.deep.equal({ 0: { currencyPair: { mode: 'edit' } }, }); }); @@ -531,11 +531,11 @@ describe(' - Cell editing', () => { ); act(() => apiRef.current?.stopCellEditMode({ id: 0, field: 'currencyPair' })); await act(() => Promise.resolve()); - expect(processRowUpdate.lastCall.args[0]).to.deep.equal({ + expect(processRowUpdate.mock.calls[processRowUpdate.mock.calls.length - 1][0]).to.deep.equal({ ...defaultData.rows[0], currencyPair: 'USD GBP', }); - expect(processRowUpdate.lastCall.args[1]).to.deep.equal(defaultData.rows[0]); + expect(processRowUpdate.mock.calls[processRowUpdate.mock.calls.length - 1][1]).to.deep.equal(defaultData.rows[0]); }); it('should stay in edit mode if processRowUpdate throws an error', async () => { @@ -571,7 +571,7 @@ describe(' - Cell editing', () => { ); act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair' })); act(() => apiRef.current?.stopCellEditMode({ id: 0, field: 'currencyPair' })); - expect(onProcessRowUpdateError.lastCall.args[0]).to.equal(error); + expect(onProcessRowUpdateError.mock.calls[onProcessRowUpdateError.mock.calls.length - 1][0]).to.equal(error); }); it('should call onProcessRowUpdateError if processRowUpdate rejects', async () => { @@ -589,7 +589,7 @@ describe(' - Cell editing', () => { act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair' })); act(() => apiRef.current?.stopCellEditMode({ id: 0, field: 'currencyPair' })); await act(() => Promise.resolve()); - expect(onProcessRowUpdateError.lastCall.args[0]).to.equal(error); + expect(onProcessRowUpdateError.mock.calls[onProcessRowUpdateError.mock.calls.length - 1][0]).to.equal(error); }); it('should keep mode=edit if processRowUpdate rejects', async () => { @@ -611,7 +611,7 @@ describe(' - Cell editing', () => { apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); act(() => apiRef.current?.stopCellEditMode({ id: 0, field: 'currencyPair' })); - expect(onCellModesModelChange.lastCall.args[0]).to.deep.equal({ + expect(onCellModesModelChange.mock.calls[onCellModesModelChange.mock.calls.length - 1][0]).to.deep.equal({ 0: { currencyPair: { mode: 'edit' } }, }); }); @@ -633,8 +633,8 @@ describe(' - Cell editing', () => { currencyPair: 'USDGBP', _currencyPair: 'USD GBP', }); - expect(valueSetter.lastCall.args[0]).to.equal('USD GBP'); - expect(valueSetter.lastCall.args[1]).to.deep.equal(defaultData.rows[0]); + expect(valueSetter.mock.calls[valueSetter.mock.calls.length - 1][0]).to.equal('USD GBP'); + expect(valueSetter.mock.calls[valueSetter.mock.calls.length - 1][1]).to.deep.equal(defaultData.rows[0]); }); it('should move focus to the cell below when cellToFocusAfter=below', async () => { @@ -730,7 +730,7 @@ describe(' - Cell editing', () => { }), ); act(() => apiRef.current?.stopCellEditMode({ id: 0, field: 'currencyPair' })); - expect(renderEditCell.lastCall.args[0].value).to.equal('USD GBP'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USD GBP'); expect((processRowUpdate.lastCall as any).args[0].currencyPair).to.equal('USD GBP'); }); @@ -753,19 +753,19 @@ describe(' - Cell editing', () => { apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); act(() => apiRef.current?.stopCellEditMode({ id: 0, field: 'currencyPair' })); - expect(onCellModesModelChange.lastCall.args[0]).to.deep.equal({ + expect(onCellModesModelChange.mock.calls[onCellModesModelChange.mock.calls.length - 1][0]).to.deep.equal({ 0: { currencyPair: { mode: 'view' } }, }); act(() => apiRef.current?.startCellEditMode({ id: 1, field: 'currencyPair' })); - expect(onCellModesModelChange.lastCall.args[0]).to.have.keys('0', '1'); - expect(onCellModesModelChange.lastCall.args[0][1]).to.deep.equal({ + expect(onCellModesModelChange.mock.calls[onCellModesModelChange.mock.calls.length - 1][0]).to.have.keys('0', '1'); + expect(onCellModesModelChange.mock.calls[onCellModesModelChange.mock.calls.length - 1][0][1]).to.deep.equal({ currencyPair: { mode: 'edit' }, }); resolveCallback!(); await act(() => Promise.resolve()); - expect(onCellModesModelChange.lastCall.args[0]).to.deep.equal({ + expect(onCellModesModelChange.mock.calls[onCellModesModelChange.mock.calls.length - 1][0]).to.deep.equal({ 1: { currencyPair: { mode: 'edit' } }, }); }); @@ -780,7 +780,7 @@ describe(' - Cell editing', () => { apiRef.current?.subscribeEvent('cellEditStart', listener); const cell = getCell(0, 1); fireEvent.doubleClick(cell); - expect(listener.lastCall.args[0].reason).to.equal('cellDoubleClick'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('cellDoubleClick'); }); it(`should not publish 'cellEditStart' if the cell is not editable`, () => { @@ -809,7 +809,7 @@ describe(' - Cell editing', () => { const cell = getCell(0, 1); fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: '$' }); - expect(listener.lastCall.args[0].reason).to.equal('printableKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('printableKeyDown'); }); it(`should not publish 'cellEditStart' if space is pressed`, async () => { @@ -831,7 +831,7 @@ describe(' - Cell editing', () => { const cell = getCell(0, 1); fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: '1' }); - expect(listener.lastCall.args[0].reason).to.equal('printableKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('printableKeyDown'); }); }); @@ -843,7 +843,7 @@ describe(' - Cell editing', () => { const cell = getCell(0, 1); fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'Enter' }); - expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('enterKeyDown'); }); it(`should not publish 'cellEditStart' if the cell is not editable`, () => { @@ -874,7 +874,7 @@ describe(' - Cell editing', () => { const cell = getCell(0, 1); fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'Delete' }); - expect(listener.lastCall.args[0].reason).to.equal('deleteKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('deleteKeyDown'); }); it(`should not publish 'cellEditStart' if the cell is not editable`, () => { @@ -903,7 +903,7 @@ describe(' - Cell editing', () => { fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'Delete' }); expect(spiedStartCellEditMode.callCount).to.equal(1); - expect(spiedStartCellEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStartCellEditMode.mock.calls[spiedStartCellEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'currencyPair', deleteValue: true, @@ -920,7 +920,7 @@ describe(' - Cell editing', () => { expect(preProcessEditCellProps.callCount).to.equal(1); - expect(preProcessEditCellProps.lastCall.args[0].props).to.deep.equal({ + expect(preProcessEditCellProps.mock.calls[preProcessEditCellProps.mock.calls.length - 1][0].props).to.deep.equal({ value: '', error: false, isProcessingProps: true, @@ -945,7 +945,7 @@ describe(' - Cell editing', () => { const cell = getCell(0, 1); fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'a' }); // A - expect(listener.lastCall.args[0].reason).to.equal('printableKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('printableKeyDown'); }); it(`should not publish 'cellEditStart' if the cell is not editable`, () => { @@ -1007,7 +1007,7 @@ describe(' - Cell editing', () => { fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'a' }); expect(spiedStartCellEditMode.callCount).to.equal(1); - expect(spiedStartCellEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStartCellEditMode.mock.calls[spiedStartCellEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'currencyPair', deleteValue: true, @@ -1027,7 +1027,7 @@ describe(' - Cell editing', () => { fireEvent.keyDown(cell, { key: 'Enter', keyCode: 13 }); expect(listener.callCount).to.equal(1); expect(input.value).to.equal('あ'); - expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('enterKeyDown'); }); it(`should ignore keydown event until the IME is confirmed with multiple letters`, () => { @@ -1043,7 +1043,7 @@ describe(' - Cell editing', () => { fireEvent.keyDown(cell, { key: 'Enter', keyCode: 13 }); expect(listener.callCount).to.equal(1); expect(input.value).to.equal('ありがとう'); - expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('enterKeyDown'); }); }); }); @@ -1057,7 +1057,7 @@ describe(' - Cell editing', () => { fireEvent.doubleClick(getCell(0, 1)); expect(listener.callCount).to.equal(0); fireUserEvent.mousePress(getCell(1, 1)); - expect(listener.lastCall.args[0].reason).to.equal('cellFocusOut'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('cellFocusOut'); }); it('should call stopCellEditMode with ignoreModifications=false and cellToFocusAfter=undefined', () => { @@ -1066,7 +1066,7 @@ describe(' - Cell editing', () => { fireEvent.doubleClick(getCell(0, 1)); fireUserEvent.mousePress(getCell(1, 1)); expect(spiedStopCellEditMode.callCount).to.equal(1); - expect(spiedStopCellEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStopCellEditMode.mock.calls[spiedStopCellEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'currencyPair', cellToFocusAfter: undefined, @@ -1088,7 +1088,7 @@ describe(' - Cell editing', () => { ); fireUserEvent.mousePress(getCell(1, 1)); expect(spiedStopCellEditMode.callCount).to.equal(1); - expect(spiedStopCellEditMode.lastCall.args[0].ignoreModifications).to.equal(false); + expect(spiedStopCellEditMode.mock.calls[spiedStopCellEditMode.mock.calls.length - 1][0].ignoreModifications).to.equal(false); }); }); @@ -1102,7 +1102,7 @@ describe(' - Cell editing', () => { fireEvent.doubleClick(cell); expect(listener.callCount).to.equal(0); fireEvent.keyDown(cell, { key: 'Escape' }); - expect(listener.lastCall.args[0].reason).to.equal('escapeKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('escapeKeyDown'); }); it('should call stopCellEditMode with ignoreModifications=true and cellToFocusAfter=undefined', () => { @@ -1113,7 +1113,7 @@ describe(' - Cell editing', () => { fireEvent.doubleClick(cell); fireEvent.keyDown(cell, { key: 'Escape' }); expect(spiedStopCellEditMode.callCount).to.equal(1); - expect(spiedStopCellEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStopCellEditMode.mock.calls[spiedStopCellEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'currencyPair', cellToFocusAfter: undefined, @@ -1132,7 +1132,7 @@ describe(' - Cell editing', () => { fireEvent.doubleClick(cell); expect(listener.callCount).to.equal(0); fireEvent.keyDown(cell, { key: 'Enter' }); - expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('enterKeyDown'); }); it('should call stopCellEditMode with ignoreModifications=false and cellToFocusAfter=below', () => { @@ -1143,7 +1143,7 @@ describe(' - Cell editing', () => { fireEvent.doubleClick(cell); fireEvent.keyDown(cell, { key: 'Enter' }); expect(spiedStopCellEditMode.callCount).to.equal(1); - expect(spiedStopCellEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStopCellEditMode.mock.calls[spiedStopCellEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'currencyPair', cellToFocusAfter: 'below', @@ -1167,7 +1167,7 @@ describe(' - Cell editing', () => { ); fireEvent.keyDown(cell, { key: 'Enter' }); expect(spiedStopCellEditMode.callCount).to.equal(1); - expect(spiedStopCellEditMode.lastCall.args[0].ignoreModifications).to.equal(false); + expect(spiedStopCellEditMode.mock.calls[spiedStopCellEditMode.mock.calls.length - 1][0].ignoreModifications).to.equal(false); }); }); @@ -1181,7 +1181,7 @@ describe(' - Cell editing', () => { await user.dblClick(cell); expect(listener.callCount).to.equal(0); await user.keyboard('{Tab}'); - expect(listener.lastCall.args[0].reason).to.equal('tabKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('tabKeyDown'); }); it('should call stopCellEditMode with ignoreModifications=false and cellToFocusAfter=right', () => { @@ -1192,7 +1192,7 @@ describe(' - Cell editing', () => { fireEvent.doubleClick(cell); fireEvent.keyDown(cell, { key: 'Tab' }); expect(spiedStopCellEditMode.callCount).to.equal(1); - expect(spiedStopCellEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStopCellEditMode.mock.calls[spiedStopCellEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'currencyPair', cellToFocusAfter: 'right', @@ -1216,7 +1216,7 @@ describe(' - Cell editing', () => { ); fireEvent.keyDown(cell, { key: 'Tab' }); expect(spiedStopCellEditMode.callCount).to.equal(1); - expect(spiedStopCellEditMode.lastCall.args[0].ignoreModifications).to.equal(false); + expect(spiedStopCellEditMode.mock.calls[spiedStopCellEditMode.mock.calls.length - 1][0].ignoreModifications).to.equal(false); }); }); }); @@ -1278,7 +1278,7 @@ describe(' - Cell editing', () => { apiRef.current?.subscribeEvent('cellModesModelChange', listener); const cell = getCell(0, 1); fireEvent.doubleClick(cell); - expect(listener.lastCall.args[0]).to.deep.equal({ + expect(listener.mock.calls[listener.mock.calls.length - 1][0]).to.deep.equal({ 0: { currencyPair: { mode: 'edit' } }, }); }); @@ -1289,7 +1289,7 @@ describe(' - Cell editing', () => { expect(listener.callCount).to.equal(0); apiRef.current?.subscribeEvent('cellModesModelChange', listener); setProps({ cellModesModel: { 0: { currencyPair: { mode: 'edit' } } } }); - expect(listener.lastCall.args[0]).to.deep.equal({ + expect(listener.mock.calls[listener.mock.calls.length - 1][0]).to.deep.equal({ 0: { currencyPair: { mode: 'edit' } }, }); }); @@ -1331,7 +1331,7 @@ describe(' - Cell editing', () => { expect(onCellModesModelChange.callCount).to.equal(0); act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair' })); expect(onCellModesModelChange.callCount).to.equal(1); - expect(onCellModesModelChange.lastCall.args[0]).to.deep.equal({ + expect(onCellModesModelChange.mock.calls[onCellModesModelChange.mock.calls.length - 1][0]).to.deep.equal({ 0: { currencyPair: { mode: 'edit' } }, }); }); @@ -1340,12 +1340,12 @@ describe(' - Cell editing', () => { const onCellModesModelChange = spy(); render(); act(() => apiRef.current?.startCellEditMode({ id: 0, field: 'currencyPair' })); - onCellModesModelChange.resetHistory(); + onCellModesModelChange.mockClear(); act(() => apiRef.current?.stopCellEditMode({ id: 0, field: 'currencyPair' })); - expect(onCellModesModelChange.args[0][0]).to.deep.equal({ + expect(onCellModesModelChange.mock.calls[0][0]).to.deep.equal({ 0: { currencyPair: { mode: 'view' } }, }); - expect(onCellModesModelChange.args[1][0]).to.deep.equal({}); + expect(onCellModesModelChange.mock.calls[1][0]).to.deep.equal({}); }); it(`should not be called when changing the cellModesModel prop`, () => { diff --git a/packages/x-data-grid-pro/src/tests/clipboard.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/clipboard.DataGridPro.test.tsx index 7846f6f7effc8..94e03cc4d03b6 100644 --- a/packages/x-data-grid-pro/src/tests/clipboard.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/clipboard.DataGridPro.test.tsx @@ -55,7 +55,7 @@ describe(' - Clipboard', () => { const cell = getCell(0, 0); fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'c', keyCode: 67, [key]: true }); - expect(writeText.firstCall.args[0]).to.equal(['0\tNike', '1\tAdidas'].join('\r\n')); + expect(writeText.firstCall.mock.calls[0]).to.equal(['0\tNike', '1\tAdidas'].join('\r\n')); }); }); @@ -75,7 +75,7 @@ describe(' - Clipboard', () => { fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'c', keyCode: 67, ctrlKey: true }); - expect(writeText.lastCall.firstArg).to.equal('1 " 1'); + expect(writeText.mock.calls[writeText.mock.calls.length - 1][0]).to.equal('1 " 1'); }); it('should not escape double quotes when copying rows to clipboard', () => { @@ -96,7 +96,7 @@ describe(' - Clipboard', () => { const cell = getCell(0, 0); fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'c', keyCode: 67, ctrlKey: true }); - expect(writeText.firstCall.args[0]).to.equal(['1 " 1', '2'].join('\r\n')); + expect(writeText.firstCall.mock.calls[0]).to.equal(['1 " 1', '2'].join('\r\n')); }); }); }); diff --git a/packages/x-data-grid-pro/src/tests/columnPinning.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/columnPinning.DataGridPro.test.tsx index ac2688bb6966f..c569c1b3d23eb 100644 --- a/packages/x-data-grid-pro/src/tests/columnPinning.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/columnPinning.DataGridPro.test.tsx @@ -264,12 +264,12 @@ describe(' - Column pinning', () => { const handlePinnedColumnsChange = spy(); render(); await act(() => apiRef.current?.pinColumn('currencyPair', GridPinnedColumnPosition.LEFT)); - expect(handlePinnedColumnsChange.lastCall.args[0]).to.deep.equal({ + expect(handlePinnedColumnsChange.mock.calls[handlePinnedColumnsChange.mock.calls.length - 1][0]).to.deep.equal({ left: ['currencyPair'], right: [], }); await act(() => apiRef.current?.pinColumn('price17M', GridPinnedColumnPosition.RIGHT)); - expect(handlePinnedColumnsChange.lastCall.args[0]).to.deep.equal({ + expect(handlePinnedColumnsChange.mock.calls[handlePinnedColumnsChange.mock.calls.length - 1][0]).to.deep.equal({ left: ['currencyPair'], right: ['price17M'], }); @@ -287,7 +287,7 @@ describe(' - Column pinning', () => { await act(() => apiRef.current?.pinColumn('price17M', GridPinnedColumnPosition.LEFT)); await microtasks(); expect($$(`[role="gridcell"].${gridClasses['cell--pinnedLeft']}`)).to.have.length(1); - expect(handlePinnedColumnsChange.lastCall.args[0]).to.deep.equal({ + expect(handlePinnedColumnsChange.mock.calls[handlePinnedColumnsChange.mock.calls.length - 1][0]).to.deep.equal({ left: ['currencyPair', 'price17M'], right: [], }); diff --git a/packages/x-data-grid-pro/src/tests/columnReorder.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/columnReorder.DataGridPro.test.tsx index 33b56345372ad..a58926caccbee 100644 --- a/packages/x-data-grid-pro/src/tests/columnReorder.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/columnReorder.DataGridPro.test.tsx @@ -218,7 +218,7 @@ describe(' - Columns reorder', () => { fireEvent(dragCol, dragEndEvent); expect(onColumnOrderChange.callCount).to.equal(1); - expect(onColumnOrderChange.lastCall.args[2].api.state.columns.orderedFields).to.deep.equal([ + expect(onColumnOrderChange.mock.calls[onColumnOrderChange.mock.calls.length - 1][2].api.state.columns.orderedFields).to.deep.equal([ 'currencyPair', 'price1M', 'id', diff --git a/packages/x-data-grid-pro/src/tests/columns.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/columns.DataGridPro.test.tsx index 092518f88f674..eb326e7221c61 100644 --- a/packages/x-data-grid-pro/src/tests/columns.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/columns.DataGridPro.test.tsx @@ -120,8 +120,8 @@ describe(' - Columns', () => { ]); expect(onColumnResize.callCount).to.equal(2); - expect(onColumnResize.args[0][0].width).to.equal(110); - expect(onColumnResize.args[1][0].width).to.equal(120); + expect(onColumnResize.mock.calls[0][0].width).to.equal(110); + expect(onColumnResize.mock.calls[1][0].width).to.equal(120); }); it('should call onColumnWidthChange after resizing', async () => { @@ -138,7 +138,7 @@ describe(' - Columns', () => { ]); expect(onColumnWidthChange.callCount).to.equal(1); - expect(onColumnWidthChange.args[0][0].width).to.equal(120); + expect(onColumnWidthChange.mock.calls[0][0].width).to.equal(120); }); it('should call onColumnWidthChange with correct width after resizing and then clicking the separator', async () => { @@ -155,7 +155,7 @@ describe(' - Columns', () => { ]); expect(onColumnWidthChange.callCount).to.equal(1); - expect(onColumnWidthChange.args[0][0].width).to.equal(120); + expect(onColumnWidthChange.mock.calls[0][0].width).to.equal(120); await user.dblClick(separator); expect(onColumnWidthChange.callCount).to.be.at.least(2); diff --git a/packages/x-data-grid-pro/src/tests/columnsVisibility.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/columnsVisibility.DataGridPro.test.tsx index 8bac210329e83..b9b2fc266babd 100644 --- a/packages/x-data-grid-pro/src/tests/columnsVisibility.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/columnsVisibility.DataGridPro.test.tsx @@ -89,14 +89,14 @@ describe(' - Columns visibility', () => { act(() => apiRef.current?.setColumnVisibility('id', false)); expect(onColumnVisibilityModelChange.callCount).to.equal(1); - expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({ + expect(onColumnVisibilityModelChange.mock.calls[onColumnVisibilityModelChange.mock.calls.length - 1][0]).to.deep.equal({ id: false, idBis: false, }); act(() => apiRef.current?.setColumnVisibility('id', true)); expect(onColumnVisibilityModelChange.callCount).to.equal(2); - expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({ + expect(onColumnVisibilityModelChange.mock.calls[onColumnVisibilityModelChange.mock.calls.length - 1][0]).to.deep.equal({ idBis: false, id: true, }); @@ -115,7 +115,7 @@ describe(' - Columns visibility', () => { ); act(() => apiRef.current?.setColumnVisibilityModel({})); expect(onColumnVisibilityModelChange.callCount).to.equal(1); - expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({}); + expect(onColumnVisibilityModelChange.mock.calls[onColumnVisibilityModelChange.mock.calls.length - 1][0]).to.deep.equal({}); }); }); diff --git a/packages/x-data-grid-pro/src/tests/components.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/components.DataGridPro.test.tsx index 7d4119facf2e3..23ddcf7dd76e0 100644 --- a/packages/x-data-grid-pro/src/tests/components.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/components.DataGridPro.test.tsx @@ -73,7 +73,7 @@ describe(' - Components', () => { await waitFor(() => { expect(propHandler.callCount).to.equal(1); }); - expect(propHandler.lastCall.args[0]).not.to.equal(undefined); + expect(propHandler.mock.calls[propHandler.mock.calls.length - 1][0]).not.to.equal(undefined); expect(eventHandler.callCount).to.equal(1); }); }); @@ -91,7 +91,7 @@ describe(' - Components', () => { await user.keyboard('a'); expect(propHandler.callCount).to.equal(1); - expect(propHandler.lastCall.args[0]).not.to.equal(undefined); + expect(propHandler.mock.calls[propHandler.mock.calls.length - 1][0]).not.to.equal(undefined); expect(eventHandler.callCount).to.equal(1); }); @@ -116,7 +116,7 @@ describe(' - Components', () => { fireEvent[eventToFire](getRow(0)); expect(propHandler.callCount).to.equal(1); - expect(propHandler.lastCall.args[0]).not.to.equal(undefined); + expect(propHandler.mock.calls[propHandler.mock.calls.length - 1][0]).not.to.equal(undefined); expect(eventHandler.callCount).to.equal(1); }); }); diff --git a/packages/x-data-grid-pro/src/tests/dataSource.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/dataSource.DataGridPro.test.tsx index e854919a77337..643bd35c1d59f 100644 --- a/packages/x-data-grid-pro/src/tests/dataSource.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/dataSource.DataGridPro.test.tsx @@ -24,7 +24,7 @@ describe.skipIf(isJSDOM)(' - Data source', () => { // TODO: Resets strictmode calls, need to find a better fix for this, maybe an AbortController? function Reset() { React.useLayoutEffect(() => { - fetchRowsSpy.resetHistory(); + fetchRowsSpy.mockClear(); }, []); return null; } diff --git a/packages/x-data-grid-pro/src/tests/dataSourceLazyLoader.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/dataSourceLazyLoader.DataGridPro.test.tsx index 62fc83089d40c..02173327a2626 100644 --- a/packages/x-data-grid-pro/src/tests/dataSourceLazyLoader.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/dataSourceLazyLoader.DataGridPro.test.tsx @@ -40,7 +40,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { // TODO: Resets strictmode calls, need to find a better fix for this, maybe an AbortController? function Reset() { React.useLayoutEffect(() => { - fetchRowsSpy.resetHistory(); + fetchRowsSpy.mockClear(); }, []); return null; } @@ -183,7 +183,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { await waitFor(() => expect(getRow(0)).not.to.be.undefined); // reset the spy call count - fetchRowsSpy.resetHistory(); + fetchRowsSpy.mockClear(); await act(async () => apiRef.current?.scrollToIndexes({ rowIndex: 10 })); @@ -197,7 +197,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { // wait until the rows are rendered await waitFor(() => expect(getRow(0)).not.to.be.undefined); - const initialSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const initialSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; expect(initialSearchParams.get('end')).to.equal('9'); await act(async () => apiRef.current?.scrollToIndexes({ rowIndex: 10 })); @@ -206,7 +206,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { expect(fetchRowsSpy.callCount).to.equal(2); }); - const beforeSortSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const beforeSortSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; expect(beforeSortSearchParams.get('end')).not.to.equal('9'); await act(async () => apiRef.current?.sortColumn(mockServer.columns[0].field, 'asc')); @@ -215,7 +215,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { expect(fetchRowsSpy.callCount).to.equal(3); }); - const afterSortSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const afterSortSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; expect(afterSortSearchParams.get('end')).to.equal('9'); }); @@ -229,7 +229,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { // wait until the rows are rendered await waitFor(() => expect(fetchRowsSpy.callCount).to.equal(2)); - const beforeFilteringSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const beforeFilteringSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; // first row is not the first page anymore expect(beforeFilteringSearchParams.get('start')).to.equal('10'); @@ -249,7 +249,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { expect(fetchRowsSpy.callCount).to.equal(3); }); - const afterFilteringSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const afterFilteringSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; // first row is the start of the first page expect(afterFilteringSearchParams.get('start')).to.equal('0'); }); @@ -276,7 +276,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { await waitFor(() => expect(getRow(0)).not.to.be.undefined); // reset the spy call count - fetchRowsSpy.resetHistory(); + fetchRowsSpy.mockClear(); // make one small and one big scroll that makes sure that the bottom of the grid window is reached await act(async () => { @@ -304,7 +304,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { await waitFor(() => { expect(fetchRowsSpy.callCount).to.equal(3); // grid is 4 rows high and the threshold is 60px, so 3 pages are loaded }); - const lastSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const lastSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; expect(lastSearchParams.get('end')).to.equal('5'); // 6th row }); @@ -320,7 +320,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { await waitFor(() => { expect(fetchRowsSpy.callCount).to.equal(2); }); - const lastSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const lastSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; // 3rd and 4th row were requested but not added expect(lastSearchParams.get('start')).to.equal('2'); expect(lastSearchParams.get('end')).to.equal('3'); @@ -336,13 +336,13 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { // wait until the rows are rendered await waitFor(() => expect(getRow(10)).not.to.be.undefined); - const beforeSortingSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const beforeSortingSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; // last row is not the first page anymore expect(beforeSortingSearchParams.get('end')).not.to.equal('9'); await act(async () => apiRef.current?.sortColumn(mockServer.columns[0].field, 'asc')); - const afterSortingSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const afterSortingSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; // last row is the end of the first page expect(afterSortingSearchParams.get('end')).to.equal('9'); }); @@ -357,7 +357,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { // wait until the rows are rendered await waitFor(() => expect(getRow(10)).not.to.be.undefined); - const beforeFilteringSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const beforeFilteringSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; // last row is not the first page anymore expect(beforeFilteringSearchParams.get('end')).not.to.equal('9'); @@ -373,7 +373,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { }); }); - const afterFilteringSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams; + const afterFilteringSearchParams = new URL(fetchRowsSpy.mock.calls[fetchRowsSpy.mock.calls.length - 1][0]).searchParams; // last row is the end of the first page expect(afterFilteringSearchParams.get('end')).to.equal('9'); }); @@ -426,7 +426,7 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { await waitFor(() => expect(getRow(0)).not.to.be.undefined); // reset the spy call count - fetchRowsSpy.resetHistory(); + fetchRowsSpy.mockClear(); // reduce the rowCount to be more than the number of rows await act(async () => { @@ -468,11 +468,11 @@ describe.skipIf(isJSDOM)(' - Data source lazy loader', () => { render(); await waitFor(() => { - expect(cacheGetSpy.called).to.equal(true); + expect(cacheGetSpy).toHaveBeenCalled(); }); - cacheGetSpy.resetHistory(); - fetchRowsSpy.resetHistory(); + cacheGetSpy.mockClear(); + fetchRowsSpy.mockClear(); act(() => { apiRef.current?.dataSource.fetchRows(GRID_ROOT_GROUP_ID, { diff --git a/packages/x-data-grid-pro/src/tests/dataSourceTreeData.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/dataSourceTreeData.DataGridPro.test.tsx index bfaa2f03c0f19..ffdac3c419d3c 100644 --- a/packages/x-data-grid-pro/src/tests/dataSourceTreeData.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/dataSourceTreeData.DataGridPro.test.tsx @@ -38,7 +38,7 @@ describe.skipIf(isJSDOM)(' - Data source tree data', () => { // TODO: Resets strictmode calls, need to find a better fix for this, maybe an AbortController? function Reset() { React.useLayoutEffect(() => { - fetchRowsSpy.resetHistory(); + fetchRowsSpy.mockClear(); }, []); return null; } diff --git a/packages/x-data-grid-pro/src/tests/detailPanel.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/detailPanel.DataGridPro.test.tsx index a093718f20249..2ed2a5eefbc7a 100644 --- a/packages/x-data-grid-pro/src/tests/detailPanel.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/detailPanel.DataGridPro.test.tsx @@ -444,7 +444,7 @@ describe(' - Detail panel', () => { const expectedCallCount = reactMajor >= 19 ? 3 : 5; expect(getDetailPanelHeight.callCount).to.equal(expectedCallCount); - expect(getDetailPanelHeight.lastCall.args[0].id).to.equal(0); + expect(getDetailPanelHeight.mock.calls[getDetailPanelHeight.mock.calls.length - 1][0].id).to.equal(0); }); it('should not select the row when opening the detail panel', async () => { @@ -556,15 +556,15 @@ describe(' - Detail panel', () => { />, ); await user.click(screen.getAllByRole('button', { name: 'Expand' })[0]); // Expand the 1st row - expect(handleDetailPanelsExpandedRowIdsChange.lastCall.args[0]).to.deep.equal(new Set([0])); + expect(handleDetailPanelsExpandedRowIdsChange.mock.calls[handleDetailPanelsExpandedRowIdsChange.mock.calls.length - 1][0]).to.deep.equal(new Set([0])); await user.click(screen.getAllByRole('button', { name: 'Expand' })[0]); // Expand the 2nd row - expect(handleDetailPanelsExpandedRowIdsChange.lastCall.args[0]).to.deep.equal( + expect(handleDetailPanelsExpandedRowIdsChange.mock.calls[handleDetailPanelsExpandedRowIdsChange.mock.calls.length - 1][0]).to.deep.equal( new Set([0, 1]), ); await user.click(screen.getAllByRole('button', { name: 'Collapse' })[0]); // Close the 1st row - expect(handleDetailPanelsExpandedRowIdsChange.lastCall.args[0]).to.deep.equal(new Set([1])); + expect(handleDetailPanelsExpandedRowIdsChange.mock.calls[handleDetailPanelsExpandedRowIdsChange.mock.calls.length - 1][0]).to.deep.equal(new Set([1])); await user.click(screen.getAllByRole('button', { name: 'Collapse' })[0]); // Close the 2nd row - expect(handleDetailPanelsExpandedRowIdsChange.lastCall.args[0]).to.deep.equal(new Set([])); + expect(handleDetailPanelsExpandedRowIdsChange.mock.calls[handleDetailPanelsExpandedRowIdsChange.mock.calls.length - 1][0]).to.deep.equal(new Set([])); }); it('should not change the open detail panels when called while detailPanelsExpandedRowIds is the same', async () => { @@ -578,7 +578,7 @@ describe(' - Detail panel', () => { ); expect(screen.getByText('Row 0')).not.to.equal(null); await user.click(screen.getByRole('button', { name: 'Collapse' })); - expect(handleDetailPanelsExpandedRowIdsChange.lastCall.args[0]).to.deep.equal(new Set([])); + expect(handleDetailPanelsExpandedRowIdsChange.mock.calls[handleDetailPanelsExpandedRowIdsChange.mock.calls.length - 1][0]).to.deep.equal(new Set([])); expect(screen.getByText('Row 0')).not.to.equal(null); }); }); diff --git a/packages/x-data-grid-pro/src/tests/editComponents.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/editComponents.DataGridPro.test.tsx index 1229948069696..32a1eb434128e 100644 --- a/packages/x-data-grid-pro/src/tests/editComponents.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/editComponents.DataGridPro.test.tsx @@ -70,7 +70,7 @@ describe(' - Edit components', () => { await user.type(input, '[Backspace>4]Puma'); - expect(spiedSetEditCellValue.lastCall.args[0]).to.deep.equal({ + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'brand', value: 'Puma', @@ -138,7 +138,7 @@ describe(' - Edit components', () => { await user.type(input, '[Backspace>4]Puma'); expect(onValueChange.callCount).to.equal(8); - expect(onValueChange.lastCall.args[1]).to.equal('Puma'); + expect(onValueChange.mock.calls[onValueChange.mock.calls.length - 1][1]).to.equal('Puma'); }); }); @@ -159,7 +159,7 @@ describe(' - Edit components', () => { expect(input.value).to.equal('100'); await user.type(input, '[Backspace>2]10'); - expect(spiedSetEditCellValue.lastCall.args[0]).to.deep.equal({ + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'quantity', value: 110, @@ -194,7 +194,7 @@ describe(' - Edit components', () => { await user.type(input, '[Backspace>2]10'); await waitFor(() => - expect(preProcessEditCellPropsSpy.lastCall.args[0].props.value).to.equal(110), + expect(preProcessEditCellPropsSpy.mock.calls[preProcessEditCellPropsSpy.mock.calls.length - 1][0].props.value).to.equal(110), ); }); @@ -249,10 +249,10 @@ describe(' - Edit components', () => { initialSelectionEnd: Infinity, }); - expect(spiedSetEditCellValue.lastCall.args[0].id).to.equal(0); - expect(spiedSetEditCellValue.lastCall.args[0].field).to.equal('createdAt'); - expect(spiedSetEditCellValue.lastCall.args[0].debounceMs).to.equal(undefined); - expect(spiedSetEditCellValue.lastCall.args[0].value?.toISOString()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].id).to.equal(0); + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].field).to.equal('createdAt'); + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].debounceMs).to.equal(undefined); + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value?.toISOString()).to.equal( new Date(2022, 1, 10).toISOString(), ); }); @@ -269,7 +269,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: Infinity, }); - expect(spiedSetEditCellValue.lastCall.args[0].value).to.equal(null); + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value).to.equal(null); }); it('should pass the value prop to the input', async () => { @@ -307,7 +307,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: 10, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(2021, 0, 5), ); @@ -316,7 +316,7 @@ describe(' - Edit components', () => { initialSelectionStart: 8, initialSelectionEnd: 10, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(2021, 0, 1), ); @@ -325,7 +325,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: 4, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(1, 0, 1), ); @@ -334,7 +334,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: 4, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(19, 0, 1), ); @@ -343,7 +343,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: 4, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(199, 0, 1), ); @@ -352,7 +352,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: 4, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(1999, 0, 1), ); }); @@ -375,7 +375,7 @@ describe(' - Edit components', () => { }); expect(onValueChange.callCount).to.equal(1); - expect((onValueChange.lastCall.args[1]! as Date).toISOString()).to.equal( + expect((onValueChange.mock.calls[onValueChange.mock.calls.length - 1][1]! as Date).toISOString()).to.equal( new Date(2022, 1, 10).toISOString(), ); }); @@ -402,10 +402,10 @@ describe(' - Edit components', () => { initialSelectionEnd: 16, }); - expect(spiedSetEditCellValue.lastCall.args[0].id).to.equal(0); - expect(spiedSetEditCellValue.lastCall.args[0].field).to.equal('createdAt'); - expect(spiedSetEditCellValue.lastCall.args[0].debounceMs).to.equal(undefined); - expect((spiedSetEditCellValue.lastCall.args[0].value! as Date).toISOString()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].id).to.equal(0); + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].field).to.equal('createdAt'); + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].debounceMs).to.equal(undefined); + expect((spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value! as Date).toISOString()).to.equal( new Date(2022, 1, 10, 15, 30, 0).toISOString(), ); }); @@ -419,7 +419,7 @@ describe(' - Edit components', () => { const input = cell.querySelector('input')!; await user.type(input, '[Backspace]'); - expect(spiedSetEditCellValue.lastCall.args[0].value).to.equal(null); + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value).to.equal(null); }); it('should pass the value prop to the input', async () => { @@ -457,7 +457,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: 10, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(2021, 0, 5, 14, 30), ); @@ -466,7 +466,7 @@ describe(' - Edit components', () => { initialSelectionStart: 8, initialSelectionEnd: 10, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(2021, 0, 1, 14, 30), ); @@ -475,7 +475,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: 4, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(1, 0, 1, 14, 30), ); @@ -484,7 +484,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: 4, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(19, 0, 1, 14, 30), ); @@ -493,7 +493,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: 4, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(199, 0, 1, 14, 30), ); @@ -502,7 +502,7 @@ describe(' - Edit components', () => { initialSelectionStart: 0, initialSelectionEnd: 4, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(1999, 0, 1, 14, 30), ); @@ -511,7 +511,7 @@ describe(' - Edit components', () => { initialSelectionStart: 11, initialSelectionEnd: 16, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(1999, 0, 1, 20, 30), ); @@ -520,7 +520,7 @@ describe(' - Edit components', () => { initialSelectionStart: 14, initialSelectionEnd: 16, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(1999, 0, 1, 20, 2), ); @@ -529,7 +529,7 @@ describe(' - Edit components', () => { initialSelectionStart: 14, initialSelectionEnd: 16, }); - expect(spiedSetEditCellValue.lastCall.args[0].value.getTime()).to.equal( + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0].value.getTime()).to.equal( generateDate(1999, 0, 1, 20, 25), ); }); @@ -551,7 +551,7 @@ describe(' - Edit components', () => { await user.dblClick(cell); await user.click(screen.queryAllByRole('option')[1]); - expect(spiedSetEditCellValue.lastCall.args[0]).to.deep.equal({ + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'brand', value: 'Adidas', @@ -578,7 +578,7 @@ describe(' - Edit components', () => { await user.dblClick(cell); await user.click(screen.queryAllByRole('option')[1]); - expect(spiedSetEditCellValue.lastCall.args[0]).to.deep.equal({ + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'brand', value: 1, @@ -601,7 +601,7 @@ describe(' - Edit components', () => { await user.dblClick(cell); await user.click(screen.queryAllByRole('option')[1]); - expect(spiedSetEditCellValue.lastCall.args[0]).to.deep.equal({ + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'brand', value: 'Adidas', @@ -634,7 +634,7 @@ describe(' - Edit components', () => { await user.click(screen.queryAllByRole('option')[1]); expect(onValueChange.callCount).to.equal(1); - expect(onValueChange.lastCall.args[1]).to.equal('Adidas'); + expect(onValueChange.mock.calls[onValueChange.mock.calls.length - 1][1]).to.equal('Adidas'); }); it('should call onCellEditStop', async () => { @@ -688,7 +688,7 @@ describe(' - Edit components', () => { expect(input.checked).to.equal(false); await user.click(input); - expect(spiedSetEditCellValue.lastCall.args[0]).to.deep.equal({ + expect(spiedSetEditCellValue.mock.calls[spiedSetEditCellValue.mock.calls.length - 1][0]).to.deep.equal({ id: 0, field: 'isAdmin', value: true, @@ -710,7 +710,7 @@ describe(' - Edit components', () => { await user.click(input); expect(onValueChange.callCount).to.equal(1); - expect(onValueChange.lastCall.args[1]).to.equal(true); + expect(onValueChange.mock.calls[onValueChange.mock.calls.length - 1][1]).to.equal(true); }); }); }); diff --git a/packages/x-data-grid-pro/src/tests/events.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/events.DataGridPro.test.tsx index b6e7b19a50618..d541922413042 100644 --- a/packages/x-data-grid-pro/src/tests/events.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/events.DataGridPro.test.tsx @@ -237,7 +237,7 @@ describe(' - Events params', () => { const cell11 = getCell(1, 1); fireEvent.click(cell11); expect(handleRowSelectionModelChange.callCount).to.equal(1); - expect(handleRowSelectionModelChange.lastCall.firstArg).to.deep.equal( + expect(handleRowSelectionModelChange.mock.calls[handleRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([2]), ); }); @@ -345,7 +345,7 @@ describe(' - Events params', () => { />, ); expect(handleFetchRows.callCount).to.equal(1); - expect(handleFetchRows.lastCall.firstArg).to.contain({ + expect(handleFetchRows.mock.calls[handleFetchRows.mock.calls.length - 1][0]).to.contain({ firstRowToRender: 3, lastRowToRender: 50, }); @@ -375,7 +375,7 @@ describe(' - Events params', () => { }); }); expect(handleFetchRows.callCount).to.equal(2); - expect(handleFetchRows.lastCall.firstArg).to.contain({ + expect(handleFetchRows.mock.calls[handleFetchRows.mock.calls.length - 1][0]).to.contain({ firstRowToRender: 3, lastRowToRender: 10, }); @@ -388,6 +388,6 @@ describe(' - Events params', () => { act(() => apiRef.current?.subscribeEvent('unmount', onUnmount)); unmount(); - expect(onUnmount.calledOnce).to.equal(true); + expect(onUnmount).toHaveBeenCalledOnce(); }); }); diff --git a/packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx index a09424dbd96f4..780286f9c547c 100644 --- a/packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx @@ -504,7 +504,7 @@ describe(' - Filter', () => { ); fireEvent.change(input!, { target: { value: 'or' } }); expect(onFilterModelChange.callCount).to.equal(1); - expect(onFilterModelChange.lastCall.args[1].reason).to.equal('changeLogicOperator'); + expect(onFilterModelChange.mock.calls[onFilterModelChange.mock.calls.length - 1][1].reason).to.equal('changeLogicOperator'); expect(getColumnValues(0)).to.deep.equal([]); }); @@ -535,7 +535,7 @@ describe(' - Filter', () => { expect(onFilterModelChange.callCount).to.equal(1); }); - expect(onFilterModelChange.lastCall.args[1].reason).to.equal('upsertFilterItem'); + expect(onFilterModelChange.mock.calls[onFilterModelChange.mock.calls.length - 1][1].reason).to.equal('upsertFilterItem'); }); it('should call onFilterModelChange with reason=deleteFilterItem when a filter is removed', () => { @@ -567,7 +567,7 @@ describe(' - Filter', () => { expect(onFilterModelChange.callCount).to.equal(0); fireEvent.click(screen.queryAllByRole('button', { name: 'Delete' })[0]); expect(onFilterModelChange.callCount).to.equal(1); - expect(onFilterModelChange.lastCall.args[1].reason).to.equal('deleteFilterItem'); + expect(onFilterModelChange.mock.calls[onFilterModelChange.mock.calls.length - 1][1].reason).to.equal('deleteFilterItem'); }); it('should call onFilterModelChange with reason=upsertFilterItems when a filter is added', () => { @@ -586,7 +586,7 @@ describe(' - Filter', () => { expect(onFilterModelChange.callCount).to.equal(0); fireEvent.click(screen.getByRole('button', { name: 'Add filter' })); expect(onFilterModelChange.callCount).to.equal(1); - expect(onFilterModelChange.lastCall.args[1].reason).to.equal('upsertFilterItems'); + expect(onFilterModelChange.mock.calls[onFilterModelChange.mock.calls.length - 1][1].reason).to.equal('upsertFilterItems'); }); it('should publish filterModelChange with the reason whenever the model changes', () => { @@ -602,7 +602,7 @@ describe(' - Filter', () => { expect(listener.callCount).to.equal(0); fireEvent.click(screen.getByRole('button', { name: 'Add filter' })); expect(listener.callCount).to.equal(1); - expect(listener.lastCall.args[1].reason).to.equal('upsertFilterItems'); + expect(listener.mock.calls[listener.mock.calls.length - 1][1].reason).to.equal('upsertFilterItems'); }); it('should only select visible rows', () => { @@ -873,8 +873,8 @@ describe(' - Filter', () => { const filterForms = document.querySelectorAll(`.MuiDataGrid-filterForm`); expect(filterForms).to.have.length(2); expect(onModelChange.callCount).to.equal(1); - expect(onModelChange.lastCall.firstArg.items.length).to.deep.equal(2); - expect(onModelChange.lastCall.firstArg.logicOperator).to.deep.equal(GridLogicOperator.And); + expect(onModelChange.mock.calls[onModelChange.mock.calls.length - 1][0].items.length).to.deep.equal(2); + expect(onModelChange.mock.calls[onModelChange.mock.calls.length - 1][0].logicOperator).to.deep.equal(GridLogicOperator.And); }); it('should control filter state when the model and the onChange are set', () => { @@ -998,7 +998,7 @@ describe(' - Filter', () => { fireEvent.click(screen.getByRole('menuitem', { name: 'Equals' })); expect(onFilterModelChange.callCount).to.equal(1); - expect(onFilterModelChange.lastCall.firstArg.items[0].operator).to.equal('equals'); + expect(onFilterModelChange.mock.calls[onFilterModelChange.mock.calls.length - 1][0].items[0].operator).to.equal('equals'); expect(getColumnValues(0)).to.deep.equal([]); }); @@ -1251,23 +1251,23 @@ describe(' - Filter', () => { await user.keyboard('0'); await waitFor(() => expect(getColumnValues(0)).to.deep.equal(['10', '100', '1,000'])); - expect(changeSpy.lastCall.args[0].items[0].value).to.equal(0); + expect(changeSpy.mock.calls[changeSpy.mock.calls.length - 1][0].items[0].value).to.equal(0); await user.keyboard('.'); await waitFor(() => expect(getColumnValues(0)).to.deep.equal(['10', '100', '1,000'])); - expect(changeSpy.lastCall.args[0].items[0].value).to.equal(0); // 0. + expect(changeSpy.mock.calls[changeSpy.mock.calls.length - 1][0].items[0].value).to.equal(0); // 0. await user.keyboard('1'); await waitFor(() => expect(getColumnValues(0)).to.deep.equal(['10', '100', '1,000'])); - await waitFor(() => expect(changeSpy.lastCall.args[0].items[0].value).to.equal(0.1)); // 0.1 + await waitFor(() => expect(changeSpy.mock.calls[changeSpy.mock.calls.length - 1][0].items[0].value).to.equal(0.1)); // 0.1 await user.keyboard('e'); await waitFor(() => expect(getColumnValues(0)).to.deep.equal(['-10', '10', '100', '1,000'])); - expect(changeSpy.lastCall.args[0].items[0].value).to.equal(undefined); // 0.1e + expect(changeSpy.mock.calls[changeSpy.mock.calls.length - 1][0].items[0].value).to.equal(undefined); // 0.1e await user.keyboard('2'); await waitFor(() => expect(getColumnValues(0)).to.deep.equal(['100', '1,000'])); - expect(changeSpy.lastCall.args[0].items[0].value).to.equal(10); // 0.1e2 + expect(changeSpy.mock.calls[changeSpy.mock.calls.length - 1][0].items[0].value).to.equal(10); // 0.1e2 }); it('should allow to navigate to the header filter cell when there are no rows', async () => { diff --git a/packages/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx index 983eee28b14a5..777c613a27c38 100644 --- a/packages/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx @@ -128,12 +128,12 @@ describe(' - Row editing', () => { />, ); act(() => apiRef.current?.startRowEditMode({ id: 0 })); - expect(renderEditCell1.lastCall.args[0].value).to.equal('USDGBP'); - expect(renderEditCell1.lastCall.args[0].error).to.equal(false); - expect(renderEditCell1.lastCall.args[0].isProcessingProps).to.equal(false); - expect(renderEditCell2.lastCall.args[0].value).to.equal(1); - expect(renderEditCell2.lastCall.args[0].error).to.equal(false); - expect(renderEditCell2.lastCall.args[0].isProcessingProps).to.equal(false); + expect(renderEditCell1.mock.calls[renderEditCell1.mock.calls.length - 1][0].value).to.equal('USDGBP'); + expect(renderEditCell1.mock.calls[renderEditCell1.mock.calls.length - 1][0].error).to.equal(false); + expect(renderEditCell1.mock.calls[renderEditCell1.mock.calls.length - 1][0].isProcessingProps).to.equal(false); + expect(renderEditCell2.mock.calls[renderEditCell2.mock.calls.length - 1][0].value).to.equal(1); + expect(renderEditCell2.mock.calls[renderEditCell2.mock.calls.length - 1][0].error).to.equal(false); + expect(renderEditCell2.mock.calls[renderEditCell2.mock.calls.length - 1][0].isProcessingProps).to.equal(false); }); it('should empty the value if deleteValue is true', () => { @@ -154,8 +154,8 @@ describe(' - Row editing', () => { deleteValue: true, }), ); - expect(renderEditCell1.lastCall.args[0].value).to.equal(''); - expect(renderEditCell2.lastCall.args[0].value).to.equal(1); + expect(renderEditCell1.mock.calls[renderEditCell1.mock.calls.length - 1][0].value).to.equal(''); + expect(renderEditCell2.mock.calls[renderEditCell2.mock.calls.length - 1][0].value).to.equal(1); }); }); @@ -172,11 +172,11 @@ describe(' - Row editing', () => { ); act(() => apiRef.current?.startRowEditMode({ id: 0 })); - expect(renderEditCell1.lastCall.args[0].value).to.equal('USDGBP'); + expect(renderEditCell1.mock.calls[renderEditCell1.mock.calls.length - 1][0].value).to.equal('USDGBP'); await act(() => apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'usdgbp' }), ); - expect(renderEditCell1.lastCall.args[0].value).to.equal('usdgbp'); + expect(renderEditCell1.mock.calls[renderEditCell1.mock.calls.length - 1][0].value).to.equal('usdgbp'); }); it('should pass to renderEditCell the row with the values updated', async () => { @@ -194,12 +194,12 @@ describe(' - Row editing', () => { />, ); act(() => apiRef.current?.startRowEditMode({ id: 0 })); - expect(renderEditCell1.lastCall.args[0].row).to.deep.equal(defaultData.rows[0]); + expect(renderEditCell1.mock.calls[renderEditCell1.mock.calls.length - 1][0].row).to.deep.equal(defaultData.rows[0]); await act(() => apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: ' usdgbp ' }), ); await act(() => apiRef.current?.setEditCellValue({ id: 0, field: 'price1M', value: 100 })); - expect(renderEditCell2.lastCall.args[0].row).to.deep.equal({ + expect(renderEditCell2.mock.calls[renderEditCell2.mock.calls.length - 1][0].row).to.deep.equal({ ...defaultData.rows[0], currencyPair: 'usdgbp', price1M: 100, @@ -217,7 +217,7 @@ describe(' - Row editing', () => { apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); expect(valueParser.callCount).to.equal(1); - expect(renderEditCell.lastCall.args[0].value).to.equal('usd gbp'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('usd gbp'); }); it('should return true if no preProcessEditCellProps is defined', async () => { @@ -242,7 +242,7 @@ describe(' - Row editing', () => { resolve(); }), ); - expect(renderEditCell.lastCall.args[0].isProcessingProps).to.equal(true); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].isProcessingProps).to.equal(true); }); it('should call all preProcessEditCellProps with the correct params', async () => { @@ -259,7 +259,7 @@ describe(' - Row editing', () => { apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); - const args1 = preProcessEditCellProps1.lastCall.args[0]; + const args1 = preProcessEditCellProps1.mock.calls[preProcessEditCellProps1.mock.calls.length - 1][0]; expect(args1.id).to.equal(0); expect(args1.row).to.deep.equal(defaultData.rows[0]); expect(args1.hasChanged).to.equal(true); @@ -270,7 +270,7 @@ describe(' - Row editing', () => { changeReason: 'setEditCellValue', }); - const args2 = preProcessEditCellProps2.lastCall.args[0]; + const args2 = preProcessEditCellProps2.mock.calls[preProcessEditCellProps2.mock.calls.length - 1][0]; expect(args2.id).to.equal(0); expect(args2.row).to.deep.equal(defaultData.rows[0]); expect(args2.hasChanged).to.equal(false); @@ -289,11 +289,11 @@ describe(' - Row editing', () => { }); render(); act(() => apiRef.current?.startRowEditMode({ id: 0 })); - expect(renderEditCell.lastCall.args[0].foo).to.equal(undefined); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].foo).to.equal(undefined); await act(() => apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); - expect(renderEditCell.lastCall.args[0].foo).to.equal('bar'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].foo).to.equal('bar'); }); it('should not pass to renderEditCell the value returned by preProcessEditCellProps', async () => { @@ -304,11 +304,11 @@ describe(' - Row editing', () => { }); render(); act(() => apiRef.current?.startRowEditMode({ id: 0 })); - expect(renderEditCell.lastCall.args[0].value).to.equal('USDGBP'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USDGBP'); await act(() => apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); - expect(renderEditCell.lastCall.args[0].value).to.equal('USD GBP'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USD GBP'); }); it('should set isProcessingProps to false after calling preProcessEditCellProps', async () => { @@ -351,13 +351,13 @@ describe(' - Row editing', () => { resolve(); }), ); - expect(renderEditCell1.lastCall.args[0].isProcessingProps).to.equal(true); - expect(renderEditCell2.lastCall.args[0].isProcessingProps).to.equal(true); + expect(renderEditCell1.mock.calls[renderEditCell1.mock.calls.length - 1][0].isProcessingProps).to.equal(true); + expect(renderEditCell2.mock.calls[renderEditCell2.mock.calls.length - 1][0].isProcessingProps).to.equal(true); resolve1!(); resolve2!(); await act(() => promise); - expect(renderEditCell1.lastCall.args[0].isProcessingProps).to.equal(false); - expect(renderEditCell2.lastCall.args[0].isProcessingProps).to.equal(false); + expect(renderEditCell1.mock.calls[renderEditCell1.mock.calls.length - 1][0].isProcessingProps).to.equal(false); + expect(renderEditCell2.mock.calls[renderEditCell2.mock.calls.length - 1][0].isProcessingProps).to.equal(false); }); it('should return false if preProcessEditCellProps sets an error', async () => { @@ -422,8 +422,8 @@ describe(' - Row editing', () => { render(); await act(async () => apiRef.current?.startRowEditMode({ id: 0 })); - expect(renderEditCell.lastCall.args[0].value).to.equal('USDGBP'); - renderEditCell.resetHistory(); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USDGBP'); + renderEditCell.mockClear(); await act(async () => { apiRef.current?.setEditCellValue({ id: 0, @@ -447,7 +447,7 @@ describe(' - Row editing', () => { await vi.advanceTimersByTimeAsync(100); }); expect(renderEditCell.callCount).not.to.equal(0); - expect(renderEditCell.lastCall.args[0].value).to.equal('USD GBP'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USD GBP'); }); }); }); @@ -540,7 +540,7 @@ describe(' - Row editing', () => { apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); act(() => apiRef.current?.stopRowEditMode({ id: 0 })); - expect(onRowModesModelChange.lastCall.args[0]).to.deep.equal({ 0: { mode: 'edit' } }); + expect(onRowModesModelChange.mock.calls[onRowModesModelChange.mock.calls.length - 1][0]).to.deep.equal({ 0: { mode: 'edit' } }); }); it('should allow a 2nd call if the first call was when error=true', async () => { @@ -597,11 +597,11 @@ describe(' - Row editing', () => { ); act(() => apiRef.current?.stopRowEditMode({ id: 0 })); await act(() => Promise.resolve()); - expect(processRowUpdate.lastCall.args[0]).to.deep.equal({ + expect(processRowUpdate.mock.calls[processRowUpdate.mock.calls.length - 1][0]).to.deep.equal({ ...defaultData.rows[0], currencyPair: 'USD GBP', }); - expect(processRowUpdate.lastCall.args[1]).to.deep.equal(defaultData.rows[0]); + expect(processRowUpdate.mock.calls[processRowUpdate.mock.calls.length - 1][1]).to.deep.equal(defaultData.rows[0]); }); it('should call processRowUpdate with the old row even if the row is not there anymore', async () => { @@ -625,11 +625,11 @@ describe(' - Row editing', () => { act(() => apiRef.current?.stopRowEditMode({ id: testRow.id })); await act(() => Promise.resolve()); // deleted row data is still passed to `processRowUpdate` as `oldRow` parameter - expect(processRowUpdate.lastCall.args[0]).to.deep.equal({ + expect(processRowUpdate.mock.calls[processRowUpdate.mock.calls.length - 1][0]).to.deep.equal({ ...defaultData.rows[0], currencyPair: testValue, }); - expect(processRowUpdate.lastCall.args[1]).to.deep.equal(testRow); + expect(processRowUpdate.mock.calls[processRowUpdate.mock.calls.length - 1][1]).to.deep.equal(testRow); // all rows are there after `processRowUpdate` returns deleted row data expect(apiRef.current?.getRowsCount()).to.equal(allRows.length); }); @@ -667,7 +667,7 @@ describe(' - Row editing', () => { ); act(() => apiRef.current?.startRowEditMode({ id: 0 })); act(() => apiRef.current?.stopRowEditMode({ id: 0 })); - expect(onProcessRowUpdateError.lastCall.args[0]).to.equal(error); + expect(onProcessRowUpdateError.mock.calls[onProcessRowUpdateError.mock.calls.length - 1][0]).to.equal(error); }); it('should call onProcessRowUpdateError if processRowUpdate rejects', async () => { @@ -685,7 +685,7 @@ describe(' - Row editing', () => { act(() => apiRef.current?.startRowEditMode({ id: 0 })); act(() => apiRef.current?.stopRowEditMode({ id: 0 })); await Promise.resolve(); - expect(onProcessRowUpdateError.lastCall.args[0]).to.equal(error); + expect(onProcessRowUpdateError.mock.calls[onProcessRowUpdateError.mock.calls.length - 1][0]).to.equal(error); }); it('should keep mode=edit if processRowUpdate rejects', async () => { @@ -707,7 +707,7 @@ describe(' - Row editing', () => { apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); act(() => apiRef.current?.stopRowEditMode({ id: 0 })); - expect(onRowModesModelChange.lastCall.args[0]).to.deep.equal({ 0: { mode: 'edit' } }); + expect(onRowModesModelChange.mock.calls[onRowModesModelChange.mock.calls.length - 1][0]).to.deep.equal({ 0: { mode: 'edit' } }); }); it('should pass the new value through all value setters before calling processRowUpdate', async () => { @@ -733,18 +733,18 @@ describe(' - Row editing', () => { ); act(() => apiRef.current?.stopRowEditMode({ id: 0 })); await act(() => Promise.resolve()); - expect(processRowUpdate.lastCall.args[0]).to.deep.equal({ + expect(processRowUpdate.mock.calls[processRowUpdate.mock.calls.length - 1][0]).to.deep.equal({ ...defaultData.rows[0], currencyPair: 'USDGBP', _currencyPair: 'USD GBP', price1M: 1, _price1M: 1, }); - expect(valueSetter1.lastCall.args[0]).to.equal('USD GBP'); - expect(valueSetter1.lastCall.args[1]).to.deep.equal(defaultData.rows[0]); + expect(valueSetter1.mock.calls[valueSetter1.mock.calls.length - 1][0]).to.equal('USD GBP'); + expect(valueSetter1.mock.calls[valueSetter1.mock.calls.length - 1][1]).to.deep.equal(defaultData.rows[0]); - expect(valueSetter2.lastCall.args[0]).to.equal(1); - expect(valueSetter2.lastCall.args[1]).to.deep.equal({ + expect(valueSetter2.mock.calls[valueSetter2.mock.calls.length - 1][0]).to.equal(1); + expect(valueSetter2.mock.calls[valueSetter2.mock.calls.length - 1][1]).to.deep.equal({ // Ensure that the row contains the values from the previous setter); ...defaultData.rows[0], currencyPair: 'USDGBP', @@ -809,20 +809,20 @@ describe(' - Row editing', () => { apiRef.current?.setEditCellValue({ id: 0, field: 'currencyPair', value: 'USD GBP' }), ); act(() => apiRef.current?.stopRowEditMode({ id: 0, field: 'price1M' })); - expect(onRowModesModelChange.lastCall.args[0]).to.deep.equal({ + expect(onRowModesModelChange.mock.calls[onRowModesModelChange.mock.calls.length - 1][0]).to.deep.equal({ 0: { mode: 'view', field: 'price1M' }, }); act(() => apiRef.current?.startRowEditMode({ id: 1, fieldToFocus: 'price1M' })); - expect(onRowModesModelChange.lastCall.args[0]).to.have.keys('0', '1'); - expect(onRowModesModelChange.lastCall.args[0][1]).to.deep.equal({ + expect(onRowModesModelChange.mock.calls[onRowModesModelChange.mock.calls.length - 1][0]).to.have.keys('0', '1'); + expect(onRowModesModelChange.mock.calls[onRowModesModelChange.mock.calls.length - 1][0][1]).to.deep.equal({ mode: 'edit', fieldToFocus: 'price1M', }); resolveCallback!(); await act(() => Promise.resolve()); - expect(onRowModesModelChange.lastCall.args[0]).to.deep.equal({ + expect(onRowModesModelChange.mock.calls[onRowModesModelChange.mock.calls.length - 1][0]).to.deep.equal({ 1: { mode: 'edit', fieldToFocus: 'price1M' }, }); }); @@ -846,8 +846,8 @@ describe(' - Row editing', () => { }); act(() => apiRef.current?.stopRowEditMode({ id: 0 })); await act(() => Promise.resolve()); - expect(renderEditCell.lastCall.args[0].value).to.equal('USD GBP'); - expect(processRowUpdate.lastCall.args[0].currencyPair).to.equal('USD GBP'); + expect(renderEditCell.mock.calls[renderEditCell.mock.calls.length - 1][0].value).to.equal('USD GBP'); + expect(processRowUpdate.mock.calls[processRowUpdate.mock.calls.length - 1][0].currencyPair).to.equal('USD GBP'); }); }); }); @@ -861,7 +861,7 @@ describe(' - Row editing', () => { apiRef.current?.subscribeEvent('rowEditStart', listener); const cell = getCell(0, 1); fireEvent.doubleClick(cell); - expect(listener.lastCall.args[0].reason).to.equal('cellDoubleClick'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('cellDoubleClick'); }); it(`should not publish 'rowEditStart' if the cell is not editable`, () => { @@ -890,7 +890,7 @@ describe(' - Row editing', () => { const cell = getCell(0, 1); fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'Enter' }); - expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('enterKeyDown'); }); it(`should not publish 'rowEditStart' if the cell is not editable`, () => { @@ -910,7 +910,7 @@ describe(' - Row editing', () => { fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'Enter' }); expect(spiedStartRowEditMode.callCount).to.equal(1); - expect(spiedStartRowEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStartRowEditMode.mock.calls[spiedStartRowEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, fieldToFocus: 'currencyPair', }); @@ -925,7 +925,7 @@ describe(' - Row editing', () => { const cell = getCell(0, 1); fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'Delete' }); - expect(listener.lastCall.args[0].reason).to.equal('deleteKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('deleteKeyDown'); }); it(`should not publish 'rowEditStart' if the cell is not editable`, () => { @@ -945,7 +945,7 @@ describe(' - Row editing', () => { fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'Delete' }); expect(spiedStartRowEditMode.callCount).to.equal(1); - expect(spiedStartRowEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStartRowEditMode.mock.calls[spiedStartRowEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, fieldToFocus: 'currencyPair', deleteValue: true, @@ -962,7 +962,7 @@ describe(' - Row editing', () => { expect(preProcessEditCellProps.callCount).to.equal(1); - expect(preProcessEditCellProps.lastCall.args[0].props).to.deep.equal({ + expect(preProcessEditCellProps.mock.calls[preProcessEditCellProps.mock.calls.length - 1][0].props).to.deep.equal({ value: '', error: false, isProcessingProps: true, @@ -978,7 +978,7 @@ describe(' - Row editing', () => { const cell = getCell(0, 1); fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'a' }); - expect(listener.lastCall.args[0].reason).to.equal('printableKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('printableKeyDown'); }); it(`should not publish 'rowEditStart' if the cell is not editable`, () => { @@ -1058,7 +1058,7 @@ describe(' - Row editing', () => { fireUserEvent.mousePress(cell); fireEvent.keyDown(cell, { key: 'a' }); expect(spiedStartRowEditMode.callCount).to.equal(1); - expect(spiedStartRowEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStartRowEditMode.mock.calls[spiedStartRowEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, fieldToFocus: 'currencyPair', deleteValue: true, @@ -1078,7 +1078,7 @@ describe(' - Row editing', () => { fireEvent.keyDown(input, { key: 'Enter', keyCode: 13 }); expect(listener.callCount).to.equal(1); expect(input.value).to.equal('あ'); - expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('enterKeyDown'); }); it(`should ignore keydown event until the IME is confirmed with multiple letters`, () => { @@ -1094,7 +1094,7 @@ describe(' - Row editing', () => { fireEvent.keyDown(input, { key: 'Enter', keyCode: 13 }); expect(listener.callCount).to.equal(1); expect(input.value).to.equal('ありがとう'); - expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('enterKeyDown'); }); }); }); @@ -1110,7 +1110,7 @@ describe(' - Row editing', () => { fireUserEvent.mousePress(getCell(1, 1)); await waitFor(() => { - expect(listener.lastCall.args[0].reason).to.equal('rowFocusOut'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('rowFocusOut'); }); }); @@ -1140,7 +1140,7 @@ describe(' - Row editing', () => { await user.dblClick(getCell(0, 1)); await user.click(getCell(1, 1)); expect(spiedStopRowEditMode.callCount).to.equal(1); - expect(spiedStopRowEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStopRowEditMode.mock.calls[spiedStopRowEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, ignoreModifications: false, field: 'currencyPair', @@ -1162,7 +1162,7 @@ describe(' - Row editing', () => { expect(spiedStopRowEditMode.callCount).to.equal(1); }); - expect(spiedStopRowEditMode.lastCall.args[0].ignoreModifications).to.equal(false); + expect(spiedStopRowEditMode.mock.calls[spiedStopRowEditMode.mock.calls.length - 1][0].ignoreModifications).to.equal(false); }); }); @@ -1176,7 +1176,7 @@ describe(' - Row editing', () => { fireEvent.doubleClick(cell); expect(listener.callCount).to.equal(0); fireEvent.keyDown(cell.querySelector('input')!, { key: 'Escape' }); - expect(listener.lastCall.args[0].reason).to.equal('escapeKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('escapeKeyDown'); }); it(`should publish 'rowEditStop' even if field has error`, async () => { @@ -1195,7 +1195,7 @@ describe(' - Row editing', () => { expect(listener.callCount).to.equal(0); fireEvent.keyDown(cell.querySelector('input')!, { key: 'Escape' }); - expect(listener.lastCall.args[0].reason).to.equal('escapeKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('escapeKeyDown'); }); it('should call stopRowEditMode with ignoreModifications=true', () => { @@ -1206,7 +1206,7 @@ describe(' - Row editing', () => { fireEvent.doubleClick(cell); fireEvent.keyDown(cell.querySelector('input')!, { key: 'Escape' }); expect(spiedStopRowEditMode.callCount).to.equal(1); - expect(spiedStopRowEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStopRowEditMode.mock.calls[spiedStopRowEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, ignoreModifications: true, field: 'currencyPair', @@ -1225,7 +1225,7 @@ describe(' - Row editing', () => { fireEvent.doubleClick(cell); expect(listener.callCount).to.equal(0); fireEvent.keyDown(cell.querySelector('input')!, { key: 'Enter' }); - expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('enterKeyDown'); }); it(`should not publish 'rowEditStop' if field has error`, async () => { @@ -1255,7 +1255,7 @@ describe(' - Row editing', () => { fireEvent.doubleClick(cell); fireEvent.keyDown(cell.querySelector('input')!, { key: 'Enter' }); expect(spiedStopRowEditMode.callCount).to.equal(1); - expect(spiedStopRowEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStopRowEditMode.mock.calls[spiedStopRowEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, ignoreModifications: false, field: 'currencyPair', @@ -1275,7 +1275,7 @@ describe(' - Row editing', () => { }); fireEvent.keyDown(cell.querySelector('input')!, { key: 'Enter' }); expect(spiedStopRowEditMode.callCount).to.equal(1); - expect(spiedStopRowEditMode.lastCall.args[0].ignoreModifications).to.equal(false); + expect(spiedStopRowEditMode.mock.calls[spiedStopRowEditMode.mock.calls.length - 1][0].ignoreModifications).to.equal(false); }); }); @@ -1289,7 +1289,7 @@ describe(' - Row editing', () => { fireEvent.doubleClick(cell); expect(listener.callCount).to.equal(0); fireEvent.keyDown(cell.querySelector('input')!, { key: 'Tab' }); - expect(listener.lastCall.args[0].reason).to.equal('tabKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('tabKeyDown'); }); it(`should publish 'rowEditStop' with reason=shiftTabKeyDown if on the first column and Shift is pressed`, () => { @@ -1301,7 +1301,7 @@ describe(' - Row editing', () => { fireEvent.doubleClick(cell); expect(listener.callCount).to.equal(0); fireEvent.keyDown(cell.querySelector('input')!, { key: 'Tab', shiftKey: true }); - expect(listener.lastCall.args[0].reason).to.equal('shiftTabKeyDown'); + expect(listener.mock.calls[listener.mock.calls.length - 1][0].reason).to.equal('shiftTabKeyDown'); }); it('should call stopRowEditMode with ignoreModifications=false and cellToFocusAfter=right', async () => { @@ -1312,7 +1312,7 @@ describe(' - Row editing', () => { await user.dblClick(cell); await user.keyboard('{Tab}'); expect(spiedStopRowEditMode.callCount).to.equal(1); - expect(spiedStopRowEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStopRowEditMode.mock.calls[spiedStopRowEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, ignoreModifications: false, field: 'price1M', @@ -1328,7 +1328,7 @@ describe(' - Row editing', () => { fireEvent.doubleClick(cell); fireEvent.keyDown(cell.querySelector('input')!, { key: 'Tab', shiftKey: true }); expect(spiedStopRowEditMode.callCount).to.equal(1); - expect(spiedStopRowEditMode.lastCall.args[0]).to.deep.equal({ + expect(spiedStopRowEditMode.mock.calls[spiedStopRowEditMode.mock.calls.length - 1][0]).to.deep.equal({ id: 0, ignoreModifications: false, field: 'currencyPair', @@ -1348,7 +1348,7 @@ describe(' - Row editing', () => { }); fireEvent.keyDown(cell.querySelector('input')!, { key: 'Tab' }); expect(spiedStopRowEditMode.callCount).to.equal(1); - expect(spiedStopRowEditMode.lastCall.args[0].ignoreModifications).to.equal(false); + expect(spiedStopRowEditMode.mock.calls[spiedStopRowEditMode.mock.calls.length - 1][0].ignoreModifications).to.equal(false); }); it('should keep focus on the first column when editing the first column of the first row of the 2nd page', () => { @@ -1447,7 +1447,7 @@ describe(' - Row editing', () => { act(() => apiRef.current?.subscribeEvent('rowModesModelChange', listener)); const cell = getCell(0, 1); fireEvent.doubleClick(cell); - expect(listener.lastCall.args[0]).to.deep.equal({ + expect(listener.mock.calls[listener.mock.calls.length - 1][0]).to.deep.equal({ 0: { mode: 'edit', fieldToFocus: 'currencyPair' }, }); }); @@ -1458,7 +1458,7 @@ describe(' - Row editing', () => { expect(listener.callCount).to.equal(0); act(() => apiRef.current?.subscribeEvent('rowModesModelChange', listener)); setProps({ rowModesModel: { 0: { currencyPair: { mode: 'edit' } } } }); - expect(listener.lastCall.args[0]).to.deep.equal({ + expect(listener.mock.calls[listener.mock.calls.length - 1][0]).to.deep.equal({ 0: { currencyPair: { mode: 'edit' } }, }); }); @@ -1500,7 +1500,7 @@ describe(' - Row editing', () => { expect(onRowModesModelChange.callCount).to.equal(0); act(() => apiRef.current?.startRowEditMode({ id: 0, fieldToFocus: 'currencyPair' })); expect(onRowModesModelChange.callCount).to.equal(1); - expect(onRowModesModelChange.lastCall.args[0]).to.deep.equal({ + expect(onRowModesModelChange.mock.calls[onRowModesModelChange.mock.calls.length - 1][0]).to.deep.equal({ 0: { mode: 'edit', fieldToFocus: 'currencyPair' }, }); }); @@ -1509,12 +1509,12 @@ describe(' - Row editing', () => { const onRowModesModelChange = spy(); render(); act(() => apiRef.current?.startRowEditMode({ id: 0, fieldToFocus: 'currencyPair' })); - onRowModesModelChange.resetHistory(); + onRowModesModelChange.mockClear(); act(() => apiRef.current?.stopRowEditMode({ id: 0 })); - expect(onRowModesModelChange.args[0][0]).to.deep.equal({ + expect(onRowModesModelChange.mock.calls[0][0]).to.deep.equal({ 0: { mode: 'view' }, }); - expect(onRowModesModelChange.args[1][0]).to.deep.equal({}); + expect(onRowModesModelChange.mock.calls[1][0]).to.deep.equal({}); }); it(`should not be called when changing the rowModesModel prop`, () => { diff --git a/packages/x-data-grid-pro/src/tests/rowPinning.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/rowPinning.DataGridPro.test.tsx index a31b076303562..cc3f156b112db 100644 --- a/packages/x-data-grid-pro/src/tests/rowPinning.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/rowPinning.DataGridPro.test.tsx @@ -760,7 +760,7 @@ describe(' - Row pinning', () => { expect(cell.textContent).to.equal('Marcus'); expect(processRowUpdate.callCount).to.equal(1); - expect(processRowUpdate.lastCall.args[0]).to.deep.equal({ id: 3, name: 'Marcus' }); + expect(processRowUpdate.mock.calls[processRowUpdate.mock.calls.length - 1][0]).to.deep.equal({ id: 3, name: 'Marcus' }); }); // flaky in JSDOM @@ -798,7 +798,7 @@ describe(' - Row pinning', () => { expect(cell.textContent).to.equal('Marcus'); expect(processRowUpdate.callCount).to.equal(1); - expect(processRowUpdate.lastCall.args[0]).to.deep.equal({ id: 3, name: 'Marcus' }); + expect(processRowUpdate.mock.calls[processRowUpdate.mock.calls.length - 1][0]).to.deep.equal({ id: 3, name: 'Marcus' }); }); it('should support `updateRows`', async () => { diff --git a/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx index c9d1972e7f296..66894f072da92 100644 --- a/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx @@ -767,7 +767,7 @@ describe(' - Row selection', () => { setProps({ rowSelectionModel: includeRowSelection([1]) }); }); expect(onRowSelectionModelChange.callCount).to.equal(1); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([1, 2, 3, 4, 5, 6, 7]), ); }); @@ -818,7 +818,7 @@ describe(' - Row selection', () => { ); expect(onRowSelectionModelChange.callCount).to.equal(3); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([2, 3, 4, 5, 6, 7, 1]), ); }); @@ -888,7 +888,7 @@ describe(' - Row selection', () => { setProps({ rowSelectionModel: includeRowSelection([2, 3, 4, 5, 6, 7]) }); }); expect(onRowSelectionModelChange.callCount).to.equal(1); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([2, 3, 4, 5, 6, 7, 1]), ); }); @@ -1005,7 +1005,7 @@ describe(' - Row selection', () => { setProps({ rowSelectionModel: includeRowSelection([2, 3, 4, 5, 6, 7]) }); }); expect(onRowSelectionModelChange.callCount).to.equal(1); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([2, 3, 4, 5, 6, 7, 1]), ); }); @@ -1024,7 +1024,7 @@ describe(' - Row selection', () => { setProps({ rowSelectionModel: includeRowSelection([1]) }); }); expect(onRowSelectionModelChange.callCount).to.equal(1); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([1, 2, 3, 4, 5, 6, 7]), ); }); @@ -1123,21 +1123,21 @@ describe(' - Row selection', () => { const handleRowSelectionModelChange = spy(); render(); await act(async () => apiRef.current?.selectRow(1)); - expect(handleRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(handleRowSelectionModelChange.mock.calls[handleRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([1]), ); // Reset old selection await act(async () => apiRef.current?.selectRow(2, true, true)); - expect(handleRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(handleRowSelectionModelChange.mock.calls[handleRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([2]), ); // Keep old selection await act(async () => apiRef.current?.selectRow(3)); - expect(handleRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(handleRowSelectionModelChange.mock.calls[handleRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([2, 3]), ); await act(async () => apiRef.current?.selectRow(3, false)); - expect(handleRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(handleRowSelectionModelChange.mock.calls[handleRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([2]), ); }); @@ -1168,23 +1168,23 @@ describe(' - Row selection', () => { ); await act(async () => apiRef.current?.selectRows([1, 2])); - expect(handleRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(handleRowSelectionModelChange.mock.calls[handleRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([1, 2]), ); await act(async () => apiRef.current?.selectRows([3])); - expect(handleRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(handleRowSelectionModelChange.mock.calls[handleRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([1, 2, 3]), ); await act(async () => apiRef.current?.selectRows([1, 2], false)); - expect(handleRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(handleRowSelectionModelChange.mock.calls[handleRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([3]), ); // Deselect others await act(async () => apiRef.current?.selectRows([4, 5], true, true)); - expect(handleRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(handleRowSelectionModelChange.mock.calls[handleRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([4, 5]), ); }); @@ -1198,7 +1198,7 @@ describe(' - Row selection', () => { />, ); await act(async () => apiRef.current?.selectRows([0, 1, 2])); - expect(handleRowSelectionModelChange.lastCall.args[0]).to.deep.equal( + expect(handleRowSelectionModelChange.mock.calls[handleRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal( includeRowSelection([1, 2]), ); }); @@ -1374,7 +1374,7 @@ describe(' - Row selection', () => { ); const selectAllCheckbox = screen.getByRole('checkbox', { name: 'Select all rows' }); await user.click(selectAllCheckbox); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal({ + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal({ type: 'exclude', ids: new Set(), }); diff --git a/packages/x-data-grid-pro/src/tests/rows.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/rows.DataGridPro.test.tsx index 3cf9db802ae4f..c4ac70053858a 100644 --- a/packages/x-data-grid-pro/src/tests/rows.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/rows.DataGridPro.test.tsx @@ -878,8 +878,8 @@ describe(' - Rows', () => { expect(handleCellFocusOut.callCount).to.equal(0); await user.click(document.body); expect(handleCellFocusOut.callCount).to.equal(1); - expect(handleCellFocusOut.args[0][0].id).to.equal(baselineProps.rows[1].id); - expect(handleCellFocusOut.args[0][0].field).to.equal(baselineProps.columns[0].field); + expect(handleCellFocusOut.mock.calls[0][0].id).to.equal(baselineProps.rows[1].id); + expect(handleCellFocusOut.mock.calls[0][0].field).to.equal(baselineProps.columns[0].field); }); it('should not crash when the row is removed during the click', async () => { diff --git a/packages/x-data-grid-pro/src/tests/sorting.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/sorting.DataGridPro.test.tsx index 5ebeb6375616b..45ff925ae5fb5 100644 --- a/packages/x-data-grid-pro/src/tests/sorting.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/sorting.DataGridPro.test.tsx @@ -267,7 +267,7 @@ describe(' - Sorting', () => { expect(onModelChange.callCount).to.equal(0); fireEvent.click(getColumnHeaderCell(0)); expect(onModelChange.callCount).to.equal(1); - expect(onModelChange.lastCall.firstArg).to.deep.equal([{ field: 'brand', sort: 'asc' }]); + expect(onModelChange.mock.calls[onModelChange.mock.calls.length - 1][0]).to.deep.equal([{ field: 'brand', sort: 'asc' }]); }); it('should control sort state when the model and the onChange are set', () => { diff --git a/packages/x-data-grid/src/tests/cells.DataGrid.test.tsx b/packages/x-data-grid/src/tests/cells.DataGrid.test.tsx index 7b5a8a9d32667..d667f85a474f0 100644 --- a/packages/x-data-grid/src/tests/cells.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/cells.DataGrid.test.tsx @@ -151,10 +151,10 @@ describe(' - Cells', () => {
, ); expect(getCell(0, 0)).to.have.text('Yes'); - // expect(valueFormatter.lastCall.args[0]).to.have.keys('id', 'field', 'value', 'api'); - expect(valueFormatter.lastCall.args[0]).to.equal(true); - expect(valueFormatter.lastCall.args[1]).to.deep.equal({ id: 0, isActive: true }); - expect(valueFormatter.lastCall.args[2].field).to.equal('isActive'); + // expect(valueFormatter.mock.calls[valueFormatter.mock.calls.length - 1][0]).to.have.keys('id', 'field', 'value', 'api'); + expect(valueFormatter.mock.calls[valueFormatter.mock.calls.length - 1][0]).to.equal(true); + expect(valueFormatter.mock.calls[valueFormatter.mock.calls.length - 1][1]).to.deep.equal({ id: 0, isActive: true }); + expect(valueFormatter.mock.calls[valueFormatter.mock.calls.length - 1][2].field).to.equal('isActive'); }); it('should throw when focusing cell without updating the state', async () => { diff --git a/packages/x-data-grid/src/tests/columnsVisibility.DataGrid.test.tsx b/packages/x-data-grid/src/tests/columnsVisibility.DataGrid.test.tsx index cc3aaa0fb1b2f..f15de73a0df7d 100644 --- a/packages/x-data-grid/src/tests/columnsVisibility.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/columnsVisibility.DataGrid.test.tsx @@ -68,7 +68,7 @@ describe(' - Columns visibility', () => { await user.click(screen.getByRole('checkbox', { name: 'id' })); expect(getColumnHeadersTextContent()).to.deep.equal(['idBis']); expect(onColumnVisibilityModelChange.callCount).to.equal(1); - expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({ + expect(onColumnVisibilityModelChange.mock.calls[onColumnVisibilityModelChange.mock.calls.length - 1][0]).to.deep.equal({ id: false, }); }); @@ -88,7 +88,7 @@ describe(' - Columns visibility', () => { fireEvent.click(screen.getByRole('checkbox', { name: 'id' })); expect(getColumnHeadersTextContent()).to.deep.equal(['id']); expect(onColumnVisibilityModelChange.callCount).to.equal(1); - expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({ + expect(onColumnVisibilityModelChange.mock.calls[onColumnVisibilityModelChange.mock.calls.length - 1][0]).to.deep.equal({ id: false, idBis: false, }); @@ -119,12 +119,12 @@ describe(' - Columns visibility', () => { // Hide all await user.click(showHideAllCheckbox); expect(onColumnVisibilityModelChange.callCount).to.equal(1); - expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({}); + expect(onColumnVisibilityModelChange.mock.calls[onColumnVisibilityModelChange.mock.calls.length - 1][0]).to.deep.equal({}); // Show all await user.click(showHideAllCheckbox); expect(onColumnVisibilityModelChange.callCount).to.equal(2); - expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({ + expect(onColumnVisibilityModelChange.mock.calls[onColumnVisibilityModelChange.mock.calls.length - 1][0]).to.deep.equal({ id: false, idBis: false, }); diff --git a/packages/x-data-grid/src/tests/dataSource.DataGrid.test.tsx b/packages/x-data-grid/src/tests/dataSource.DataGrid.test.tsx index ca0e6ace047b4..899def81cab9c 100644 --- a/packages/x-data-grid/src/tests/dataSource.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/dataSource.DataGrid.test.tsx @@ -32,7 +32,7 @@ describe.skipIf(isJSDOM)(' - Data source', () => { // TODO: Resets strictmode calls, need to find a better fix for this, maybe an AbortController? function Reset() { React.useLayoutEffect(() => { - fetchRowsSpy.resetHistory(); + fetchRowsSpy.mockClear(); }, []); return null; } @@ -336,7 +336,7 @@ describe.skipIf(isJSDOM)(' - Data source', () => {
, ); await waitFor(() => { - expect(getRows.called).to.equal(true); + expect(getRows).toHaveBeenCalled(); }); unmount(); reject(); @@ -376,7 +376,7 @@ describe.skipIf(isJSDOM)(' - Data source', () => { await user.click(cell); expect(cell).toHaveFocus(); - clearSpy.resetHistory(); + clearSpy.mockClear(); expect(cache.size).to.equal(1); @@ -384,7 +384,7 @@ describe.skipIf(isJSDOM)(' - Data source', () => { await user.keyboard('{Enter} updated{Enter}'); expect(editRowSpy.callCount).to.equal(1); - expect(editRowSpy.lastCall.args[0].updatedRow.commodity).to.contain('updated'); + expect(editRowSpy.mock.calls[editRowSpy.mock.calls.length - 1][0].updatedRow.commodity).to.contain('updated'); await waitFor(() => { expect(clearSpy.callCount).to.equal(1); @@ -425,13 +425,13 @@ describe.skipIf(isJSDOM)(' - Data source', () => { await user.click(cell); expect(cell).toHaveFocus(); - editRowSpy.resetHistory(); + editRowSpy.mockClear(); // edit the cell await user.keyboard('{Enter}{Enter}'); expect(editRowSpy.callCount).to.equal(1); - expect(editRowSpy.lastCall.args[0].updatedRow.commodity).to.contain('-edited'); + expect(editRowSpy.mock.calls[editRowSpy.mock.calls.length - 1][0].updatedRow.commodity).to.contain('-edited'); }); }); }); diff --git a/packages/x-data-grid/src/tests/density.DataGrid.test.tsx b/packages/x-data-grid/src/tests/density.DataGrid.test.tsx index 58ce1ca0b7cd7..0ccb06546a6ab 100644 --- a/packages/x-data-grid/src/tests/density.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/density.DataGrid.test.tsx @@ -125,7 +125,7 @@ describe.skipIf(isJSDOM)(' - Density', () => { fireEvent.click(screen.getByText('Density')); fireEvent.click(screen.getByText('Comfortable')); expect(onDensityChange.callCount).to.equal(1); - expect(onDensityChange.firstCall.args[0]).to.equal('comfortable'); + expect(onDensityChange.firstCall.mock.calls[0]).to.equal('comfortable'); }); }); diff --git a/packages/x-data-grid/src/tests/export.DataGrid.test.tsx b/packages/x-data-grid/src/tests/export.DataGrid.test.tsx index d05be5820295f..623cff7a6e77f 100644 --- a/packages/x-data-grid/src/tests/export.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/export.DataGrid.test.tsx @@ -35,7 +35,7 @@ describe.skipIf(isJSDOM)(' - Export', () => { expect(screen.queryByRole('menu')).not.to.equal(null); fireEvent.click(screen.getByRole('menuitem', { name: 'Download as CSV' })); expect(spyCreateObjectURL.callCount).to.equal(1); - const csv = await spyCreateObjectURL.lastCall.firstArg.text(); + const csv = await spyCreateObjectURL.mock.calls[spyCreateObjectURL.mock.calls.length - 1][0].text(); expect(csv).to.equal(['id,Currency Pair', '0,USDGBP', '1,USDEUR', '2,GBPEUR'].join('\r\n')); }); @@ -45,7 +45,7 @@ describe.skipIf(isJSDOM)(' - Export', () => { expect(screen.queryByRole('menu')).not.to.equal(null); fireEvent.click(screen.getByRole('menuitem', { name: 'Download as CSV' })); expect(spyCreateObjectURL.callCount).to.equal(1); - const csv = await spyCreateObjectURL.lastCall.firstArg.text(); + const csv = await spyCreateObjectURL.mock.calls[spyCreateObjectURL.mock.calls.length - 1][0].text(); expect(csv).to.equal(['id;Currency Pair', '0;USDGBP', '1;USDEUR', '2;GBPEUR'].join('\r\n')); }); @@ -86,7 +86,7 @@ describe.skipIf(isJSDOM)(' - Export', () => { expect(screen.queryByRole('menu')).not.to.equal(null); fireEvent.click(screen.getByRole('menuitem', { name: 'Download as CSV' })); expect(spyCreateObjectURL.callCount).to.equal(1); - const csv = await spyCreateObjectURL.lastCall.firstArg.text(); + const csv = await spyCreateObjectURL.mock.calls[spyCreateObjectURL.mock.calls.length - 1][0].text(); expect(csv).to.equal( [ @@ -123,7 +123,7 @@ describe.skipIf(isJSDOM)(' - Export', () => { expect(screen.queryByRole('menu')).not.to.equal(null); fireEvent.click(screen.getByRole('menuitem', { name: 'Download as CSV' })); expect(spyCreateObjectURL.callCount).to.equal(1); - const csv = await spyCreateObjectURL.lastCall.firstArg.text(); + const csv = await spyCreateObjectURL.mock.calls[spyCreateObjectURL.mock.calls.length - 1][0].text(); expect(csv).to.equal(['name', 'Name', '', '', '1234'].join('\r\n')); }); @@ -137,7 +137,7 @@ describe.skipIf(isJSDOM)(' - Export', () => { expect(screen.queryByRole('menu')).not.to.equal(null); fireEvent.click(screen.getByRole('menuitem', { name: 'Download as CSV' })); expect(spyCreateObjectURL.callCount).to.equal(1); - const csv = await spyCreateObjectURL.lastCall.firstArg.text(); + const csv = await spyCreateObjectURL.mock.calls[spyCreateObjectURL.mock.calls.length - 1][0].text(); expect(csv).to.equal(['id,Currency Pair', '0,USDGBP', '1,USDEUR', '2,GBPEUR'].join('\r\n')); }); @@ -153,7 +153,7 @@ describe.skipIf(isJSDOM)(' - Export', () => { expect(screen.queryByRole('menu')).not.to.equal(null); fireEvent.click(screen.getByRole('menuitem', { name: 'Download as CSV' })); expect(spyCreateObjectURL.callCount).to.equal(1); - const csv = await spyCreateObjectURL.lastCall.firstArg.text(); + const csv = await spyCreateObjectURL.mock.calls[spyCreateObjectURL.mock.calls.length - 1][0].text(); expect(csv).to.equal(['id;Currency Pair', '0;USDGBP', '1;USDEUR', '2;GBPEUR'].join('\r\n')); }); diff --git a/packages/x-data-grid/src/tests/filterPanel.DataGrid.test.tsx b/packages/x-data-grid/src/tests/filterPanel.DataGrid.test.tsx index 882add36522dc..5388f0d1f0fca 100644 --- a/packages/x-data-grid/src/tests/filterPanel.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/filterPanel.DataGrid.test.tsx @@ -251,7 +251,7 @@ describe(' - Filter panel', () => { setOperatorValue('isEmpty'); expect(onFilterModelChange.callCount).to.equal(1); - expect(onFilterModelChange.lastCall.args[0].items[0].value).to.equal(undefined); + expect(onFilterModelChange.mock.calls[onFilterModelChange.mock.calls.length - 1][0].items[0].value).to.equal(undefined); expect(getSelectByName('Operator').value).to.equal('isEmpty'); }); diff --git a/packages/x-data-grid/src/tests/filtering.DataGrid.test.tsx b/packages/x-data-grid/src/tests/filtering.DataGrid.test.tsx index 8d46ef1a703c0..16413b70e93c2 100644 --- a/packages/x-data-grid/src/tests/filtering.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/filtering.DataGrid.test.tsx @@ -1591,7 +1591,7 @@ describe(' - Filter', () => { setProps({ columns: [{ field: 'id' }] }); expect(getColumnValues(0)).to.deep.equal(['0', '1', '2']); expect(onFilterModelChange.callCount).to.equal(2); - expect(onFilterModelChange.lastCall.firstArg).to.deep.equal({ items: [] }); + expect(onFilterModelChange.mock.calls[onFilterModelChange.mock.calls.length - 1][0]).to.deep.equal({ items: [] }); }); // See https://github.com/mui/mui-x/issues/9204 diff --git a/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx b/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx index 6c10326b1f898..dbb8afe9d6561 100644 --- a/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx @@ -1,5 +1,5 @@ import { createRenderer, fireEvent, screen, act } from '@mui/internal-test-utils'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { getActiveCell, getActiveColumnHeader, @@ -634,7 +634,7 @@ describe(' - Keyboard', () => { }); it('should call preventDefault when using keyboard navigation', async () => { - const handleKeyDown = spy((event) => event.defaultPrevented); + const handleKeyDown = vi.fn((event) => event.defaultPrevented); const columns = [{ field: 'id' }, { field: 'name' }]; const rows = [{ id: 1, name: 'John' }]; @@ -646,7 +646,7 @@ describe(' - Keyboard', () => { ); await user.click(getCell(0, 0)); await user.keyboard('{ArrowRight}'); - expect(handleKeyDown.returnValues).to.deep.equal([true]); + expect(handleKeyDown.mock.results.map((r) => r.value)).to.deep.equal([true]); }); it('should sort column when pressing enter and column header is selected', async () => { @@ -697,7 +697,7 @@ describe(' - Keyboard', () => { }); it('should not rerender when pressing a key inside an already focused cell', () => { - const renderCell = spy(() => ); + const renderCell = vi.fn(() => ); const columns = [{ field: 'name', renderCell }]; const rows = [{ id: 1, name: 'John' }]; render( @@ -705,13 +705,13 @@ describe(' - Keyboard', () => {
, ); - expect(renderCell.callCount).to.equal(2); + expect(renderCell).toHaveBeenCalledTimes(2); const input = screen.getByTestId('custom-input'); input.focus(); fireEvent.keyDown(input, { key: 'a' }); - expect(renderCell.callCount).to.equal(4); + expect(renderCell).toHaveBeenCalledTimes(4); fireEvent.keyDown(input, { key: 'b' }); - expect(renderCell.callCount).to.equal(4); + expect(renderCell).toHaveBeenCalledTimes(4); }); it('should not scroll horizontally when cell is wider than viewport', async () => { @@ -865,7 +865,7 @@ describe(' - Keyboard', () => { columns: GridColDef[], editMode: DataGridProps['editMode'], ) { - const valueSetterMock = spy>( + const valueSetterMock = vi.fn>( (value, row, column) => { return { ...row, @@ -906,7 +906,7 @@ describe(' - Keyboard', () => { return { cell: cell.textContent, - deletedValue: valueSetterMock.lastCall.args[0], + deletedValue: valueSetterMock.mock.calls[valueSetterMock.mock.calls.length - 1][0], }; } diff --git a/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx b/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx index f521905aeb121..6aebd56350a4b 100644 --- a/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx @@ -92,12 +92,12 @@ describe(' - Pagination', () => { fireEvent.click(screen.getByRole('button', { name: /next page/i })); expect(onPaginationModelChange.callCount).to.equal(1); - expect(onPaginationModelChange.lastCall.args[0].page).to.equal(1); + expect(onPaginationModelChange.mock.calls[onPaginationModelChange.mock.calls.length - 1][0].page).to.equal(1); expect(getColumnValues(0)).to.deep.equal(['1']); fireEvent.click(screen.getByRole('button', { name: /previous page/i })); expect(onPaginationModelChange.callCount).to.equal(2); - expect(onPaginationModelChange.lastCall.args[0].page).to.equal(0); + expect(onPaginationModelChange.mock.calls[onPaginationModelChange.mock.calls.length - 1][0].page).to.equal(0); expect(getColumnValues(0)).to.deep.equal(['0']); }); @@ -124,7 +124,7 @@ describe(' - Pagination', () => { fireEvent.click(screen.queryAllByRole('option')[1]); expect(onPaginationModelChange.callCount).to.equal(1); - expect(onPaginationModelChange.lastCall.args[0].pageSize).to.equal(2); + expect(onPaginationModelChange.mock.calls[onPaginationModelChange.mock.calls.length - 1][0].pageSize).to.equal(2); expect(getColumnValues(0)).to.deep.equal(['0', '1']); }); @@ -152,11 +152,11 @@ describe(' - Pagination', () => { render(); fireEvent.click(screen.getByRole('button', { name: /next page/i })); - expect(onPaginationModelChange.lastCall.args[0]).to.deep.equal({ page: 1, pageSize: 1 }); + expect(onPaginationModelChange.mock.calls[onPaginationModelChange.mock.calls.length - 1][0]).to.deep.equal({ page: 1, pageSize: 1 }); expect(getColumnValues(0)).to.deep.equal(['1']); fireEvent.click(screen.getByRole('button', { name: /previous page/i })); - expect(onPaginationModelChange.lastCall.args[0]).to.deep.equal({ page: 0, pageSize: 1 }); + expect(onPaginationModelChange.mock.calls[onPaginationModelChange.mock.calls.length - 1][0]).to.deep.equal({ page: 0, pageSize: 1 }); expect(getColumnValues(0)).to.deep.equal(['0']); }); @@ -174,9 +174,9 @@ describe(' - Pagination', () => { ); fireEvent.click(screen.getByRole('button', { name: /next page/i })); expect(onPaginationModelChange.callCount).to.equal(1); - expect(onPaginationModelChange.lastCall.args[0]).to.deep.equal({ page: 1, pageSize: 1 }); + expect(onPaginationModelChange.mock.calls[onPaginationModelChange.mock.calls.length - 1][0]).to.deep.equal({ page: 1, pageSize: 1 }); fireEvent.click(screen.getByRole('button', { name: /previous page/i })); - expect(onPaginationModelChange.lastCall.args[0]).to.deep.equal({ page: 0, pageSize: 1 }); + expect(onPaginationModelChange.mock.calls[onPaginationModelChange.mock.calls.length - 1][0]).to.deep.equal({ page: 0, pageSize: 1 }); }); it('should not change the state when clicking on next button and a `paginationModel` prop is provided', () => { @@ -247,7 +247,7 @@ describe(' - Pagination', () => { expect(getColumnValues(0)).to.deep.equal(['0', '1', '2', '3']); expect(onPaginationModelChange.callCount).to.equal(1); - expect(onPaginationModelChange.lastCall.args[0]).to.deep.equal({ page: 0, pageSize: 5 }); + expect(onPaginationModelChange.mock.calls[onPaginationModelChange.mock.calls.length - 1][0]).to.deep.equal({ page: 0, pageSize: 5 }); }); it('should scroll to the top of the page when changing page', () => { @@ -293,7 +293,7 @@ describe(' - Pagination', () => { fireEvent.click(screen.queryAllByRole('option')[1]); expect(onPaginationModelChange.callCount).to.equal(1); - expect(onPaginationModelChange.lastCall.args[0]).to.deep.equal({ pageSize: 2, page: 0 }); + expect(onPaginationModelChange.mock.calls[onPaginationModelChange.mock.calls.length - 1][0]).to.deep.equal({ pageSize: 2, page: 0 }); }); it('should not change the pageSize state when clicking on a page size option when paginationModel prop is provided', () => { @@ -503,7 +503,7 @@ describe(' - Pagination', () => { rows = document.querySelectorAll('.MuiDataGrid-virtualScrollerRenderZone [role="row"]'); expect(rows.length).to.equal(expectedViewportRowsLengthAfter); - expect(onPaginationModelChange.lastCall.args[0].pageSize).to.equal( + expect(onPaginationModelChange.mock.calls[onPaginationModelChange.mock.calls.length - 1][0].pageSize).to.equal( expectedViewportRowsLengthAfter, ); }); diff --git a/packages/x-data-grid/src/tests/quickFiltering.DataGrid.test.tsx b/packages/x-data-grid/src/tests/quickFiltering.DataGrid.test.tsx index b5499cbe38bda..c92776a823f64 100644 --- a/packages/x-data-grid/src/tests/quickFiltering.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/quickFiltering.DataGrid.test.tsx @@ -91,7 +91,7 @@ describe(' - Quick filter', () => { await user.type(screen.getByRole('searchbox'), 'adid, nik'); await waitFor(() => { - expect(onFilterModelChange.lastCall.firstArg).to.deep.equal({ + expect(onFilterModelChange.mock.calls[onFilterModelChange.mock.calls.length - 1][0]).to.deep.equal({ items: [], logicOperator: 'and', quickFilterValues: ['adid', 'nik'], diff --git a/packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx b/packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx index 9ce3ec401c7e0..9cfebb5efcd65 100644 --- a/packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx @@ -283,7 +283,7 @@ describe(' - Row selection', () => { // The callback should be called with exclude type and empty ids expect(onRowSelectionModelChange.callCount).to.equal(1); - const selectionModel = onRowSelectionModelChange.firstCall.args[0]; + const selectionModel = onRowSelectionModelChange.firstCall.mock.calls[0]; expect(selectionModel.type).to.equal('exclude'); expect(selectionModel.ids.size).to.equal(0); @@ -315,14 +315,14 @@ describe(' - Row selection', () => { await user.click(selectAllCheckbox); // Reset the spy to check the next call - onRowSelectionModelChange.resetHistory(); + onRowSelectionModelChange.mockClear(); // Deselect one row await user.click(getCell(1, 0).querySelector('input')!); // Should still be exclude type but with the deselected row ID expect(onRowSelectionModelChange.callCount).to.equal(1); - const selectionModel = onRowSelectionModelChange.firstCall.args[0]; + const selectionModel = onRowSelectionModelChange.firstCall.mock.calls[0]; expect(selectionModel.type).to.equal('exclude'); expect(selectionModel.ids.size).to.equal(1); expect(selectionModel.ids.has(1)).to.equal(true); @@ -836,7 +836,7 @@ describe(' - Row selection', () => { rows: data.rows.slice(0, 1), }); - expect(onRowSelectionModelChangeSpy.called).to.equal(true); + expect(onRowSelectionModelChangeSpy).toHaveBeenCalled(); }); it('should retain the outdated selected rows when the rows prop changes when keepNonExistentRowsSelected is true', () => { @@ -896,12 +896,12 @@ describe(' - Row selection', () => { />, ); await user.click(getCell(0, 0).querySelector('input')!); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal(includeRowSelection([0])); + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal(includeRowSelection([0])); await user.click(screen.getByRole('button', { name: /next page/i })); await user.click(getCell(2, 0).querySelector('input')!); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal(includeRowSelection([0, 2])); + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal(includeRowSelection([0, 2])); setProps({ checkboxSelection: false, isRowSelectable: () => false }); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal(includeRowSelection([])); + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal(includeRowSelection([])); }); it('should call onRowSelectionModelChange with the correct reason when clicking on a row', async () => { @@ -914,7 +914,7 @@ describe(' - Row selection', () => { />, ); await user.click(getCell(0, 0).querySelector('input')!); - expect(onRowSelectionModelChange.lastCall.args[1].reason).to.equal('singleRowSelection'); + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][1].reason).to.equal('singleRowSelection'); }); it('should call onRowSelectionModelChange with the correct reason when clicking on the header checkbox', async () => { @@ -927,7 +927,7 @@ describe(' - Row selection', () => { />, ); await user.click(getColumnHeaderCell(0).querySelector('input')!); - expect(onRowSelectionModelChange.lastCall.args[1].reason).to.equal('multipleRowsSelection'); + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][1].reason).to.equal('multipleRowsSelection'); }); it('should call onRowSelectionModelChange with an empty array if there is no selected row in the current page when turning off checkboxSelection', async () => { @@ -942,10 +942,10 @@ describe(' - Row selection', () => { ); await user.click(getCell(0, 0).querySelector('input')!); await user.click(getCell(1, 0).querySelector('input')!); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal(includeRowSelection([0, 1])); + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal(includeRowSelection([0, 1])); await user.click(screen.getByRole('button', { name: /next page/i })); setProps({ checkboxSelection: false }); - expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal(includeRowSelection([])); + expect(onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]).to.deep.equal(includeRowSelection([])); }); it('should deselect the old selected rows when updating rowSelectionModel', () => { @@ -1109,7 +1109,7 @@ describe(' - Row selection', () => { await user.click(selectAllCheckbox); expect(onRowSelectionModelChange.callCount).to.equal(1); - const selectionModel = onRowSelectionModelChange.lastCall.args[0]; + const selectionModel = onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]; // With disableRowSelectionExcludeModel=true, it should use include model with all IDs expect(selectionModel.type).to.equal('include'); expect(selectionModel.ids.size).to.equal(4); // 4 rows in defaultData @@ -1130,7 +1130,7 @@ describe(' - Row selection', () => { await user.click(selectAllCheckbox); expect(onRowSelectionModelChange.callCount).to.equal(1); - const selectionModel = onRowSelectionModelChange.lastCall.args[0]; + const selectionModel = onRowSelectionModelChange.mock.calls[onRowSelectionModelChange.mock.calls.length - 1][0]; // By default (disableRowSelectionExcludeModel=false), it should use exclude model with empty IDs expect(selectionModel.type).to.equal('exclude'); expect(selectionModel.ids.size).to.equal(0); diff --git a/packages/x-data-grid/src/tests/rows.DataGrid.test.tsx b/packages/x-data-grid/src/tests/rows.DataGrid.test.tsx index 82ff3140dfd0f..12fff800f9375 100644 --- a/packages/x-data-grid/src/tests/rows.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/rows.DataGrid.test.tsx @@ -9,7 +9,7 @@ import { waitFor, } from '@mui/internal-test-utils'; import clsx from 'clsx'; -import { spy, stub } from 'sinon'; +import { vi } from 'vitest'; import Portal from '@mui/material/Portal'; import { DataGrid, @@ -98,7 +98,7 @@ describe(' - Rows', () => { }); it('should ignore events coming from a portal in the cell', async () => { - const handleRowClick = spy(); + const handleRowClick = vi.fn(); function InputCell() { return ; } @@ -129,9 +129,9 @@ describe(' - Rows', () => {
, ); await user.click(document.querySelector('input[name="portal-input"]')!); - expect(handleRowClick.callCount).to.equal(0); + expect(handleRowClick).toHaveBeenCalledTimes(0); await user.click(document.querySelector('input[name="input"]')!); - expect(handleRowClick.callCount).to.equal(1); + expect(handleRowClick).toHaveBeenCalledTimes(1); }); // https://github.com/mui/mui-x/issues/8042 @@ -254,10 +254,10 @@ describe(' - Rows', () => { }); it('should call getActions with the row params', () => { - const getActions = stub().returns([]); + const getActions = vi.fn().mockReturnValue([]); render(); - expect(getActions.args[0][0].id).to.equal(1); - expect(getActions.args[0][0].row).to.deep.equal({ id: 1 }); + expect(getActions.mock.calls[0][0].id).to.equal(1); + expect(getActions.mock.calls[0][0].row).to.deep.equal({ id: 1 }); }); it('should always show the actions not marked as showInMenu', () => { @@ -923,7 +923,7 @@ describe(' - Rows', () => { } it('should be called with the correct params', async () => { - const getRowSpacing = stub().returns({}); + const getRowSpacing = vi.fn().mockReturnValue({}); const { user } = render( - Rows', () => { pageSizeOptions={[2]} />, ); - expect(getRowSpacing.args[0][0]).to.deep.equal({ + expect(getRowSpacing.mock.calls[0][0]).to.deep.equal({ isFirstVisible: true, isLastVisible: false, indexRelativeToCurrentPage: 0, id: 0, model: rows[0], }); - expect(getRowSpacing.args[1][0]).to.deep.equal({ + expect(getRowSpacing.mock.calls[1][0]).to.deep.equal({ isFirstVisible: false, isLastVisible: true, indexRelativeToCurrentPage: 1, @@ -946,17 +946,17 @@ describe(' - Rows', () => { model: rows[1], }); - getRowSpacing.resetHistory(); + getRowSpacing.mockClear(); await user.click(screen.getByRole('button', { name: /next page/i })); - expect(getRowSpacing.args[0][0]).to.deep.equal({ + expect(getRowSpacing.mock.calls[0][0]).to.deep.equal({ isFirstVisible: true, isLastVisible: false, indexRelativeToCurrentPage: 0, id: 2, model: rows[2], }); - expect(getRowSpacing.args[1][0]).to.deep.equal({ + expect(getRowSpacing.mock.calls[1][0]).to.deep.equal({ isFirstVisible: false, isLastVisible: true, indexRelativeToCurrentPage: 1, diff --git a/packages/x-data-grid/src/tests/slots.DataGrid.test.tsx b/packages/x-data-grid/src/tests/slots.DataGrid.test.tsx index db31f78312fb0..8b50f7d23c2f9 100644 --- a/packages/x-data-grid/src/tests/slots.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/slots.DataGrid.test.tsx @@ -115,8 +115,8 @@ describe(' - Slots', () => { expect(onClick.callCount).to.equal(0); const button = screen.getByLabelText('Show filters'); await user.click(button); - expect(onClick.lastCall.args[0]).to.have.property('field', 'brand'); - expect(onClick.lastCall.args[1]).to.have.property('target', button); + expect(onClick.mock.calls[onClick.mock.calls.length - 1][0]).to.have.property('field', 'brand'); + expect(onClick.mock.calls[onClick.mock.calls.length - 1][1]).to.have.property('target', button); }); }); diff --git a/packages/x-data-grid/src/tests/sorting.DataGrid.test.tsx b/packages/x-data-grid/src/tests/sorting.DataGrid.test.tsx index f5d6490926fb7..82dd64503e6d3 100644 --- a/packages/x-data-grid/src/tests/sorting.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/sorting.DataGrid.test.tsx @@ -179,7 +179,7 @@ describe(' - Sorting', () => { fireEvent.click(header); expect(getColumnValues(0)).to.deep.equal(['0', '5', '10']); expect(onSortModelChange.callCount).to.equal(1); - expect(onSortModelChange.lastCall.firstArg).to.deep.equal([{ field: 'id', sort: 'asc' }]); + expect(onSortModelChange.mock.calls[onSortModelChange.mock.calls.length - 1][0]).to.deep.equal([{ field: 'id', sort: 'asc' }]); // Clear the sort using `apiRef` act(() => apiRef.current?.sortColumn('id', null)); @@ -187,7 +187,7 @@ describe(' - Sorting', () => { expect(onSortModelChange.callCount).to.equal(2); // Confirm that the sort item is cleared and not passed to `onSortModelChange` - expect(onSortModelChange.lastCall.firstArg).to.deep.equal([]); + expect(onSortModelChange.mock.calls[onSortModelChange.mock.calls.length - 1][0]).to.deep.equal([]); }); it('should always set correct `aria-sort` attribute', () => { @@ -714,7 +714,7 @@ describe(' - Sorting', () => { setProps({ columns: [{ field: 'id' }] }); expect(getColumnValues(0)).to.deep.equal(['0', '1', '2']); expect(onSortModelChange.callCount).to.equal(2); - expect(onSortModelChange.lastCall.firstArg).to.deep.equal([]); + expect(onSortModelChange.mock.calls[onSortModelChange.mock.calls.length - 1][0]).to.deep.equal([]); }); // See https://github.com/mui/mui-x/issues/9204 diff --git a/packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.test.tsx b/packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.test.tsx index 0c3988dbdc3eb..eabd5cb967a2d 100644 --- a/packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.test.tsx +++ b/packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.test.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { screen, fireEvent, @@ -46,7 +46,7 @@ describe('', () => { describe('Selection', () => { it('should select the range from the next month', async () => { - const onChange = spy(); + const onChange = vi.fn(); const { user } = render( ', () => { await user.click(getPickerDay('19', 'March 2019')); - expect(onChange.callCount).to.equal(2); + expect(onChange).toHaveBeenCalledTimes(2); const rangeOn1stCall = onChange.firstCall.firstArg; expect(rangeOn1stCall[0]).to.toEqualDateTime(new Date(2019, 0, 1)); expect(rangeOn1stCall[1]).to.equal(null); - const rangeOn2ndCall = onChange.lastCall.firstArg; + const rangeOn2ndCall = onChange.mock.calls[onChange.mock.calls.length - 1][0]; expect(rangeOn2ndCall[0]).to.toEqualDateTime(new Date(2019, 0, 1)); expect(rangeOn2ndCall[1]).to.toEqualDateTime(new Date(2019, 2, 19)); }); it('should continue start selection if selected "end" date is before start', () => { - const onChange = spy(); + const onChange = vi.fn(); render( , @@ -93,8 +93,8 @@ describe('', () => { fireEvent.click(getPickerDay('30', 'January 2019')); - expect(onChange.callCount).to.equal(3); - const range = onChange.lastCall.firstArg; + expect(onChange).toHaveBeenCalledTimes(3); + const range = onChange.mock.calls[onChange.mock.calls.length - 1][0]; expect(range[0]).to.toEqualDateTime(new Date(2019, 0, 19)); expect(range[1]).to.toEqualDateTime(new Date(2019, 0, 30)); }); @@ -164,7 +164,7 @@ describe('', () => { }); it('should not emit "onChange" when dragging is ended where it was started', () => { - const onChange = spy(); + const onChange = vi.fn(); render( ', () => { const startDay = screen.getByRole('gridcell', { name: '31', selected: true }); const dragToDay = screen.getByRole('gridcell', { name: '30' }); - expect(onChange.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); executeDateDrag(startDay, dragToDay, startDay); - expect(onChange.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); }); it.skipIf(!document.elementFromPoint)( 'should not emit "onChange" when touch dragging is ended where it was started', () => { - const onChange = spy(); + const onChange = vi.fn(); render( ', () => { ); const startDay = screen.getByRole('gridcell', { name: '1', selected: true }); - expect(onChange.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); executeDateTouchDrag( startDay, @@ -202,12 +202,12 @@ describe('', () => { rangeCalendarDayTouches['2018-01-01'], ); - expect(onChange.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); }, ); it('should emit "onChange" when dragging end date', () => { - const onChange = spy(); + const onChange = vi.fn(); const initialValue: [any, any] = [ adapterToUse.date('2018-01-10'), adapterToUse.date('2018-01-31'), @@ -221,9 +221,9 @@ describe('', () => { screen.getByRole('gridcell', { name: '29' }), ); - expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(initialValue[0]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 29)); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(initialValue[0]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 29)); expect(document.activeElement).toHaveAccessibleName('29'); // test range expansion @@ -232,9 +232,9 @@ describe('', () => { screen.getByRole('gridcell', { name: '30' }), ); - expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(initialValue[0]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 30)); + expect(onChange).toHaveBeenCalledTimes(2); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(initialValue[0]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 30)); expect(document.activeElement).toHaveAccessibleName('30'); // test range flip @@ -243,16 +243,16 @@ describe('', () => { getPickerDay('2'), ); - expect(onChange.callCount).to.equal(3); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 2)); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(initialValue[0]); + expect(onChange).toHaveBeenCalledTimes(3); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 2)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(initialValue[0]); expect(document.activeElement).toHaveAccessibleName('2'); }); it.skipIf(!document.elementFromPoint)( 'should emit "onChange" when touch dragging end date', () => { - const onChange = spy(); + const onChange = vi.fn(); const initialValue: [any, any] = [ adapterToUse.date('2018-01-02'), adapterToUse.date('2018-01-11'), @@ -266,9 +266,9 @@ describe('', () => { rangeCalendarDayTouches['2018-01-10'], ); - expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(initialValue[0]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 10)); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(initialValue[0]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 10)); // test range expansion executeDateTouchDrag( @@ -277,9 +277,9 @@ describe('', () => { rangeCalendarDayTouches['2018-01-11'], ); - expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(initialValue[0]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(initialValue[1]); + expect(onChange).toHaveBeenCalledTimes(2); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(initialValue[0]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(initialValue[1]); // test range flip executeDateTouchDrag( @@ -288,14 +288,14 @@ describe('', () => { rangeCalendarDayTouches['2018-01-01'], ); - expect(onChange.callCount).to.equal(3); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 1)); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(initialValue[0]); + expect(onChange).toHaveBeenCalledTimes(3); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 1)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(initialValue[0]); }, ); it('should emit "onChange" when dragging start date', () => { - const onChange = spy(); + const onChange = vi.fn(); const initialValue: [any, any] = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-20'), @@ -305,32 +305,32 @@ describe('', () => { // test range reduction executeDateDrag(getPickerDay('1'), getPickerDay('2'), getPickerDay('3')); - expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(initialValue[1]); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(initialValue[1]); expect(document.activeElement).toHaveAccessibleName('3'); // test range expansion executeDateDrag(getPickerDay('3'), getPickerDay('1')); - expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(initialValue[0]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(initialValue[1]); + expect(onChange).toHaveBeenCalledTimes(2); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(initialValue[0]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(initialValue[1]); expect(document.activeElement).toHaveAccessibleName('1'); // test range flip executeDateDrag(getPickerDay('1'), getPickerDay('22')); - expect(onChange.callCount).to.equal(3); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(initialValue[1]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 22)); + expect(onChange).toHaveBeenCalledTimes(3); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(initialValue[1]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 22)); expect(document.activeElement).toHaveAccessibleName('22'); }); it.skipIf(!document.elementFromPoint)( 'should emit "onChange" when touch dragging start date', () => { - const onChange = spy(); + const onChange = vi.fn(); const initialValue: [any, any] = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-10'), @@ -344,9 +344,9 @@ describe('', () => { rangeCalendarDayTouches['2018-01-02'], ); - expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 2)); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(initialValue[1]); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 2)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(initialValue[1]); // test range expansion executeDateTouchDrag( @@ -355,9 +355,9 @@ describe('', () => { rangeCalendarDayTouches['2018-01-01'], ); - expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(initialValue[0]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(initialValue[1]); + expect(onChange).toHaveBeenCalledTimes(2); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(initialValue[0]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(initialValue[1]); // test range flip executeDateTouchDrag( @@ -366,9 +366,9 @@ describe('', () => { rangeCalendarDayTouches['2018-01-11'], ); - expect(onChange.callCount).to.equal(3); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(initialValue[1]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 11)); + expect(onChange).toHaveBeenCalledTimes(3); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(initialValue[1]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 11)); }, ); @@ -517,7 +517,7 @@ describe('', () => { ['readOnly', 'disabled'].forEach((prop) => { it(`prop: ${prop}="true" should not allow date editing`, () => { - const handleChange = spy(); + const handleChange = vi.fn(); render( ', () => { ); } fireEvent.click(getPickerDay('2')); - expect(handleChange.callCount).to.equal(0); + expect(handleChange).toHaveBeenCalledTimes(0); }); }); @@ -560,9 +560,9 @@ describe('', () => { />, ); - const renderCountBeforeChange = RenderCount.callCount; + const renderCountBeforeChange = RenderCount.mock.calls.length; fireEvent.click(getPickerDay('2')); - expect(RenderCount.callCount - renderCountBeforeChange).to.equal(2); // 2 render * 1 day + expect(RenderCount.mock.calls.length - renderCountBeforeChange).to.equal(2); // 2 render * 1 day }); it('should only render the day inside range when selecting the end day', () => { @@ -579,9 +579,9 @@ describe('', () => { fireEvent.click(getPickerDay('2')); - const renderCountBeforeChange = RenderCount.callCount; + const renderCountBeforeChange = RenderCount.mock.calls.length; fireEvent.click(getPickerDay('4')); - expect(RenderCount.callCount - renderCountBeforeChange).to.equal(6); // 2 render * 3 day + expect(RenderCount.mock.calls.length - renderCountBeforeChange).to.equal(6); // 2 render * 3 day }); }); }); diff --git a/packages/x-date-pickers-pro/src/DateRangePicker/DateRangePicker.test.tsx b/packages/x-date-pickers-pro/src/DateRangePicker/DateRangePicker.test.tsx index be500a020510e..1ab9df31d90b5 100644 --- a/packages/x-date-pickers-pro/src/DateRangePicker/DateRangePicker.test.tsx +++ b/packages/x-date-pickers-pro/src/DateRangePicker/DateRangePicker.test.tsx @@ -1,6 +1,6 @@ import { DateRangePicker, DateRangePickerProps } from '@mui/x-date-pickers-pro/DateRangePicker'; import { screen } from '@mui/internal-test-utils/createRenderer'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { buildFieldInteractions, createPickerRenderer, @@ -71,22 +71,22 @@ describe('', () => { } it('should submit the form when "Enter" is pressed on the input', async () => { - const handleSubmit = spy(); + const handleSubmit = vi.fn(); const { user } = render(); // focus the input await user.keyboard('{Tab}'); await user.keyboard('{Enter}'); - expect(handleSubmit.callCount).to.equal(1); - expect([...handleSubmit.lastCall.args[0]][0]).to.deep.equal([ + expect(handleSubmit).toHaveBeenCalledTimes(1); + expect([...handleSubmit.mock.calls[handleSubmit.mock.calls.length - 1][0]][0]).to.deep.equal([ 'testDate', '04/17/2022 – 04/21/2022', ]); }); it('should not submit the form when "Enter" is pressed on the multi input field', async () => { - const handleSubmit = spy(); + const handleSubmit = vi.fn(); const { user } = render( ', () => { await user.keyboard('{Tab}'); await user.keyboard('{Enter}'); - expect(handleSubmit.callCount).to.equal(0); + expect(handleSubmit).toHaveBeenCalledTimes(0); }); it('should not submit the form when "Enter" is pressed on the input with "defaultMuiPrevented" set to "true"', async () => { - const handleSubmit = spy(); + const handleSubmit = vi.fn(); const { user } = render( ', () => { await user.keyboard('{Tab}'); await user.keyboard('{Enter}'); - expect(handleSubmit.callCount).to.equal(0); + expect(handleSubmit).toHaveBeenCalledTimes(0); }); }); }); diff --git a/packages/x-date-pickers-pro/src/DesktopDateRangePicker/tests/DesktopDateRangePicker.test.tsx b/packages/x-date-pickers-pro/src/DesktopDateRangePicker/tests/DesktopDateRangePicker.test.tsx index 1d9275587cf76..30fa2937f8aa0 100644 --- a/packages/x-date-pickers-pro/src/DesktopDateRangePicker/tests/DesktopDateRangePicker.test.tsx +++ b/packages/x-date-pickers-pro/src/DesktopDateRangePicker/tests/DesktopDateRangePicker.test.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { fireEvent, screen, act, within, waitFor } from '@mui/internal-test-utils'; import { createTheme, ThemeProvider } from '@mui/material/styles'; import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; @@ -134,8 +134,8 @@ describe('', () => { describe('Component slot: Popper', () => { it('should forward onClick and onTouchStart', async () => { - const handleClick = spy(); - const handleTouchStart = spy(); + const handleClick = vi.fn(); + const handleTouchStart = vi.fn(); render( ', () => { fireEvent.click(popper); fireEvent.touchStart(popper); - expect(handleClick.callCount).to.equal(1); - expect(handleTouchStart.callCount).to.equal(1); + expect(handleClick).toHaveBeenCalledTimes(1); + expect(handleTouchStart).toHaveBeenCalledTimes(1); }); }); describe('picker state', () => { it('should open when clicking the start input (multi input field)', async () => { - const onOpen = spy(); + const onOpen = vi.fn(); const { user } = render( , @@ -173,12 +173,12 @@ describe('', () => { fieldType: 'multi-input', }); - expect(onOpen.callCount).to.equal(1); + expect(onOpen).toHaveBeenCalledTimes(1); expect(screen.getByRole('tooltip')).toBeVisible(); }); it('should open when clicking the end input (multi input field)', async () => { - const onOpen = spy(); + const onOpen = vi.fn(); const { user } = render( , @@ -190,13 +190,13 @@ describe('', () => { fieldType: 'multi-input', }); - expect(onOpen.callCount).to.equal(1); + expect(onOpen).toHaveBeenCalledTimes(1); expect(screen.getByRole('tooltip')).toBeVisible(); }); ['Enter', 'Space'].forEach((key) => it(`should open when pressing "${key}" in the start input (multi input field)`, async () => { - const onOpen = spy(); + const onOpen = vi.fn(); const { user } = render( , @@ -206,14 +206,14 @@ describe('', () => { await act(async () => startInput.focus()); await user.keyboard(`[${key}]`); - expect(onOpen.callCount).to.equal(1); + expect(onOpen).toHaveBeenCalledTimes(1); expect(screen.getByRole('tooltip')).toBeVisible(); }), ); ['Enter', 'Space'].forEach((key) => it(`should open when pressing "${key}" in the end input (multi input field)`, async () => { - const onOpen = spy(); + const onOpen = vi.fn(); const { user } = render( , @@ -223,15 +223,15 @@ describe('', () => { await act(async () => endInput.focus()); await user.keyboard(`[${key}]`); - expect(onOpen.callCount).to.equal(1); + expect(onOpen).toHaveBeenCalledTimes(1); expect(screen.getByRole('tooltip')).toBeVisible(); }), ); it('should call onChange with updated start date then call onChange with updated end date, onClose and onAccept with update date range when opening from start input', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -253,32 +253,32 @@ describe('', () => { initialFocus: 'start', fieldType: 'multi-input', }); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); // Change the start date await user.click(getPickerDay('3')); - expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(defaultValue[1]); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(defaultValue[1]); // Change the end date await user.click(getPickerDay('5')); - expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 5)); - - expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onAccept.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 5)); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(2); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 5)); + + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 5)); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onChange with updated end date, onClose and onAccept with update date range when opening from end input (multi input field)', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -300,24 +300,24 @@ describe('', () => { initialFocus: 'end', fieldType: 'multi-input', }); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); // Change the end date fireEvent.click(getPickerDay('3')); - expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(defaultValue[0]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0][0]).toEqualDateTime(defaultValue[0]); - expect(onAccept.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(defaultValue[0]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][0]).toEqualDateTime(defaultValue[0]); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should not call onClose and onAccept when selecting the end date if props.closeOnSelect = false (multi input field)', async () => { - const onAccept = spy(); - const onClose = spy(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -342,14 +342,14 @@ describe('', () => { // Change the end date await user.click(getPickerDay('3')); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); }); it('should call onClose and onAccept with the live value when pressing Escape', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -375,17 +375,17 @@ describe('', () => { // Dismiss the picker await user.keyboard('[Escape]'); - expect(onChange.callCount).to.equal(1); // Start date change - expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onAccept.lastCall.args[0][1]).toEqualDateTime(defaultValue[1]); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); // Start date change + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][1]).toEqualDateTime(defaultValue[1]); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onClose when clicking outside of the picker without prior change (multi input field)', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { user } = render(
@@ -410,15 +410,15 @@ describe('', () => { await user.pointer({ keys: '[MouseLeft>]', target: input }); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onClose and onAccept with the live value when clicking outside of the picker (multi input field)', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -452,17 +452,17 @@ describe('', () => { await user.click(input); // Start date change - expect(onChange.callCount).to.equal(1); - expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onAccept.lastCall.args[0][1]).toEqualDateTime(defaultValue[1]); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][1]).toEqualDateTime(defaultValue[1]); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should not call onClose or onAccept when clicking outside of the picker if not opened (multi input field)', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { user } = render( ', () => { // Dismiss the picker await user.click(document.body); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); }); // test:unit does not call `blur` when focusing another element. it.skipIf(isJSDOM)( 'should call onClose when blur the current field without prior change (multi input field)', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { user } = render( @@ -511,18 +511,18 @@ describe('', () => { await act(async () => document.querySelector('#test')!.focus()); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); await waitFor(() => { - expect(onClose.callCount).to.equal(1); + expect(onClose).toHaveBeenCalledTimes(1); }); }, ); it('should call onClose and onAccept when blur the current field (multi input field)', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -551,22 +551,22 @@ describe('', () => { // Change the start date (already tested) await user.click(getPickerDay('3')); - expect(onAccept.callCount).to.equal(0); + expect(onAccept).toHaveBeenCalledTimes(0); await user.click(document.querySelector('#test')!); // Start date change - expect(onChange.callCount).to.equal(1); - expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onAccept.lastCall.args[0][1]).toEqualDateTime(defaultValue[1]); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][1]).toEqualDateTime(defaultValue[1]); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onClose, onChange with empty value and onAccept with empty value when pressing the "Clear" button', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -590,17 +590,17 @@ describe('', () => { // Clear the date await user.click(screen.getByText(/clear/i)); - expect(onChange.callCount).to.equal(1); // Start date change - expect(onChange.lastCall.args[0]).to.deep.equal([null, null]); - expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0]).to.deep.equal([null, null]); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); // Start date change + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).to.deep.equal([null, null]); + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0]).to.deep.equal([null, null]); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should not call onChange or onAccept when pressing "Clear" button with an already null value', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { user } = render( ', () => { // Clear the date await user.click(screen.getByText(/clear/i)); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(1); }); // TODO: Write test // it('should call onClose and onAccept with the live value when clicking outside of the picker', () => { // }) it('should not close picker when switching focus from start to end input (multi input field)', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { user } = render( ', () => { initialFocus: 'end', fieldType: 'multi-input', }); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); }); it('should not close picker when switching focus from end to start input (multi input field)', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { user } = render( ', () => { initialFocus: 'start', fieldType: 'multi-input', }); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); }); it('should work with separate start and end "reference" dates', async () => { diff --git a/packages/x-date-pickers-pro/src/MobileDateRangePicker/tests/MobileDateRangePicker.test.tsx b/packages/x-date-pickers-pro/src/MobileDateRangePicker/tests/MobileDateRangePicker.test.tsx index c812b48f39922..d34f79456cd93 100644 --- a/packages/x-date-pickers-pro/src/MobileDateRangePicker/tests/MobileDateRangePicker.test.tsx +++ b/packages/x-date-pickers-pro/src/MobileDateRangePicker/tests/MobileDateRangePicker.test.tsx @@ -1,4 +1,4 @@ -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { screen, fireEvent } from '@mui/internal-test-utils'; import { MobileDateRangePicker } from '@mui/x-date-pickers-pro/MobileDateRangePicker'; import { @@ -40,7 +40,7 @@ describe('', () => { describe('picker state', () => { it('should open when focusing the start input (multi input field)', async () => { - const onOpen = spy(); + const onOpen = vi.fn(); const { user } = render( , @@ -52,12 +52,12 @@ describe('', () => { fieldType: 'multi-input', }); - expect(onOpen.callCount).to.equal(1); + expect(onOpen).toHaveBeenCalledTimes(1); expect(screen.queryByRole('dialog')).toBeVisible(); }); it('should open when focusing the end input (multi input field)', async () => { - const onOpen = spy(); + const onOpen = vi.fn(); const { user } = render( , @@ -69,14 +69,14 @@ describe('', () => { fieldType: 'multi-input', }); - expect(onOpen.callCount).to.equal(1); + expect(onOpen).toHaveBeenCalledTimes(1); expect(screen.queryByRole('dialog')).toBeVisible(); }); it('should call onChange with updated start date then call onChange with updated end date when opening from start input', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -97,30 +97,30 @@ describe('', () => { initialFocus: 'start', fieldType: 'single-input', }); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); // Change the start date fireEvent.click(screen.getByRole('gridcell', { name: '3' })); - expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(defaultValue[1]); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(defaultValue[1]); // Change the end date fireEvent.click(screen.getByRole('gridcell', { name: '5' })); - expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 5)); + expect(onChange).toHaveBeenCalledTimes(2); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 5)); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); }); it('should call onChange with updated end date when opening from end input (multi input field)', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -142,22 +142,22 @@ describe('', () => { initialFocus: 'end', fieldType: 'multi-input', }); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); // Change the end date fireEvent.click(screen.getByRole('gridcell', { name: '3' })); - expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0][0]).toEqualDateTime(defaultValue[0]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(defaultValue[0]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); }); it('should call onClose and onAccept when selecting the end date if props.closeOnSelect = true (multi input field)', () => { - const onAccept = spy(); - const onClose = spy(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -178,16 +178,16 @@ describe('', () => { // Change the end date fireEvent.click(screen.getByRole('gridcell', { name: '3' })); - expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0][0]).toEqualDateTime(defaultValue[0]); - expect(onAccept.lastCall.args[0][1]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onClose.callCount).to.equal(1); + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][0]).toEqualDateTime(defaultValue[0]); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onClose and onChange with the initial value when clicking "Cancel" button', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -210,17 +210,17 @@ describe('', () => { // Cancel the modifications fireEvent.click(screen.getByText(/cancel/i)); - expect(onChange.callCount).to.equal(2); // Start date change + reset - expect(onChange.lastCall.args[0][0]).toEqualDateTime(defaultValue[0]); - expect(onChange.lastCall.args[0][1]).toEqualDateTime(defaultValue[1]); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(2); // Start date change + reset + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][0]).toEqualDateTime(defaultValue[0]); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][1]).toEqualDateTime(defaultValue[1]); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onClose and onAccept with the live value and onAccept with the live value when clicking the "OK"', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -242,17 +242,17 @@ describe('', () => { // Accept the modifications fireEvent.click(screen.getByText(/ok/i)); - expect(onChange.callCount).to.equal(1); // Start date change - expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0][0]).toEqualDateTime(new Date(2018, 0, 3)); - expect(onAccept.lastCall.args[0][1]).toEqualDateTime(defaultValue[1]); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); // Start date change + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2018, 0, 3)); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0][1]).toEqualDateTime(defaultValue[1]); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onClose, onChange with empty value and onAccept with empty value when pressing the "Clear" button', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const defaultValue: DateRange = [ adapterToUse.date('2018-01-01'), adapterToUse.date('2018-01-06'), @@ -272,17 +272,17 @@ describe('', () => { // Clear the date fireEvent.click(screen.getByText(/clear/i)); - expect(onChange.callCount).to.equal(1); // Start date change - expect(onChange.lastCall.args[0]).to.deep.equal([null, null]); - expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0]).to.deep.equal([null, null]); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); // Start date change + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).to.deep.equal([null, null]); + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0]).to.deep.equal([null, null]); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should not call onChange or onAccept when pressing "Clear" button with an already null value', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( ', () => { // Clear the date fireEvent.click(screen.getByText(/clear/i)); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should correctly set focused styles when input is focused', () => { diff --git a/packages/x-date-pickers-pro/src/SingleInputDateRangeField/tests/editing.SingleInputDateRangeField.test.tsx b/packages/x-date-pickers-pro/src/SingleInputDateRangeField/tests/editing.SingleInputDateRangeField.test.tsx index d4e54f49401f6..1d72d17cb97e5 100644 --- a/packages/x-date-pickers-pro/src/SingleInputDateRangeField/tests/editing.SingleInputDateRangeField.test.tsx +++ b/packages/x-date-pickers-pro/src/SingleInputDateRangeField/tests/editing.SingleInputDateRangeField.test.tsx @@ -1,4 +1,4 @@ -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { SingleInputDateRangeField } from '@mui/x-date-pickers-pro/SingleInputDateRangeField'; import { fireEvent, waitFor } from '@mui/internal-test-utils'; import { @@ -200,9 +200,9 @@ describe(' - Editing', () => { view.pressKey(2, 'ArrowUp'); expectFieldValueV7(view.getSectionsContainer(), '06/04/2022 – 06/05/2022'); - expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg[0]).toEqualDateTime(new Date(2023, 5, 4)); - expect(onChangeV7.lastCall.firstArg[1]).toEqualDateTime(new Date(2022, 5, 5)); + expect(onChangeV7).toHaveBeenCalledTimes(1); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2023, 5, 4)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2022, 5, 5)); view.unmount(); @@ -220,9 +220,9 @@ describe(' - Editing', () => { fireUserEvent.keyPress(input, { key: 'ArrowUp' }); expectFieldValueV6(getTextbox(), '06/04/2022 – 06/05/2022'); - expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg[0]).toEqualDateTime(new Date(2023, 5, 4)); - expect(onChangeV6.lastCall.firstArg[1]).toEqualDateTime(new Date(2022, 5, 5)); + expect(onChangeV6).toHaveBeenCalledTimes(1); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2023, 5, 4)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2022, 5, 5)); }); it('should call the onChange callback when the value is updated and should change the displayed value if the value is not controlled', async () => { @@ -239,9 +239,9 @@ describe(' - Editing', () => { view.pressKey(2, 'ArrowUp'); expectFieldValueV7(view.getSectionsContainer(), '06/04/2023 – 06/05/2022'); - expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg[0]).toEqualDateTime(new Date(2023, 5, 4)); - expect(onChangeV7.lastCall.firstArg[1]).toEqualDateTime(new Date(2022, 5, 5)); + expect(onChangeV7).toHaveBeenCalledTimes(1); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2023, 5, 4)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2022, 5, 5)); view.unmount(); @@ -259,9 +259,9 @@ describe(' - Editing', () => { fireUserEvent.keyPress(input, { key: 'ArrowUp' }); expectFieldValueV6(getTextbox(), '06/04/2023 – 06/05/2022'); - expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg[0]).toEqualDateTime(new Date(2023, 5, 4)); - expect(onChangeV6.lastCall.firstArg[1]).toEqualDateTime(new Date(2022, 5, 5)); + expect(onChangeV6).toHaveBeenCalledTimes(1); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2023, 5, 4)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][1]).toEqualDateTime(new Date(2022, 5, 5)); }); it('should not call the onChange callback before filling the last section of the active date when starting from a null value', async () => { @@ -277,14 +277,14 @@ describe(' - Editing', () => { await view.selectSectionAsync('day'); view.pressKey(0, '4'); - expect(onChangeV7.callCount).to.equal(0); + expect(onChangeV7).toHaveBeenCalledTimes(0); expectFieldValueV7(view.getSectionsContainer(), '04 MMMM – DD MMMM'); view.pressKey(1, 'S'); // // We reset the value displayed because the `onChange` callback did not update the controlled value. - expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg[0]).toEqualDateTime(new Date(2022, 8, 4)); - expect(onChangeV7.lastCall.firstArg[1]).to.equal(null); + expect(onChangeV7).toHaveBeenCalledTimes(1); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2022, 8, 4)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][1]).to.equal(null); await waitFor(() => { expectFieldValueV7(view.getSectionsContainer(), 'DD MMMM – DD MMMM'); }); @@ -305,13 +305,13 @@ describe(' - Editing', () => { const input = getTextbox(); fireEvent.change(input, { target: { value: '4 MMMM – DD MMMM' } }); // Press 4 - expect(onChangeV6.callCount).to.equal(0); + expect(onChangeV6).toHaveBeenCalledTimes(0); expectFieldValueV6(input, '04 MMMM – DD MMMM'); fireEvent.change(input, { target: { value: '04 S – DD MMMM' } }); // Press S - expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg[0]).toEqualDateTime(new Date(2022, 8, 4)); - expect(onChangeV6.lastCall.firstArg[1]).to.equal(null); + expect(onChangeV6).toHaveBeenCalledTimes(1); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][0]).toEqualDateTime(new Date(2022, 8, 4)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][1]).to.equal(null); // // We reset the value displayed because the `onChange` callback did not update the controlled value. await waitFor(() => { expectFieldValueV6(input, 'DD MMMM – DD MMMM'); @@ -427,7 +427,7 @@ describe(' - Editing', () => { }); fireEvent.keyDown(view.getSectionsContainer(), { key: 'Delete' }); - expect(onChangeV7.callCount).to.equal(0); + expect(onChangeV7).toHaveBeenCalledTimes(0); view.unmount(); @@ -447,7 +447,7 @@ describe(' - Editing', () => { fireEvent.keyDown(input, { key: 'a', keyCode: 65, ctrlKey: true }); fireEvent.keyDown(input, { key: 'Delete' }); - expect(onChangeV6.callCount).to.equal(0); + expect(onChangeV6).toHaveBeenCalledTimes(0); }); it('should call `onChange` when clearing the first section of each date', async () => { @@ -464,32 +464,32 @@ describe(' - Editing', () => { // Start date fireEvent.keyDown(view.getActiveSection(0), { key: 'Delete' }); - expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg[0]).to.equal(null); - expect(onChangeV7.lastCall.firstArg[1]).toEqualDateTime(adapter.addYears(adapter.date(), 1)); + expect(onChangeV7).toHaveBeenCalledTimes(1); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][0]).to.equal(null); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][1]).toEqualDateTime(adapter.addYears(adapter.date(), 1)); fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowRight' }); fireEvent.keyDown(view.getActiveSection(1), { key: 'Delete' }); - expect(onChangeV7.callCount).to.equal(1); + expect(onChangeV7).toHaveBeenCalledTimes(1); fireEvent.keyDown(view.getActiveSection(1), { key: 'ArrowRight' }); fireEvent.keyDown(view.getActiveSection(2), { key: 'Delete' }); - expect(onChangeV7.callCount).to.equal(1); + expect(onChangeV7).toHaveBeenCalledTimes(1); // End date fireEvent.keyDown(view.getActiveSection(2), { key: 'ArrowRight' }); fireEvent.keyDown(view.getActiveSection(3), { key: 'Delete' }); - expect(onChangeV7.callCount).to.equal(2); - expect(onChangeV7.lastCall.firstArg[0]).to.equal(null); - expect(onChangeV7.lastCall.firstArg[1]).to.equal(null); + expect(onChangeV7).toHaveBeenCalledTimes(2); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][0]).to.equal(null); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][1]).to.equal(null); fireEvent.keyDown(view.getActiveSection(3), { key: 'ArrowRight' }); fireEvent.keyDown(view.getActiveSection(4), { key: 'Delete' }); - expect(onChangeV7.callCount).to.equal(2); + expect(onChangeV7).toHaveBeenCalledTimes(2); fireEvent.keyDown(view.getActiveSection(4), { key: 'ArrowRight' }); fireEvent.keyDown(view.getActiveSection(5), { key: 'Delete' }); - expect(onChangeV7.callCount).to.equal(2); + expect(onChangeV7).toHaveBeenCalledTimes(2); view.unmount(); @@ -507,32 +507,32 @@ describe(' - Editing', () => { // Start date fireEvent.keyDown(input, { key: 'Delete' }); - expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg[0]).to.equal(null); - expect(onChangeV6.lastCall.firstArg[1]).toEqualDateTime(adapter.addYears(adapter.date(), 1)); + expect(onChangeV6).toHaveBeenCalledTimes(1); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][0]).to.equal(null); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][1]).toEqualDateTime(adapter.addYears(adapter.date(), 1)); fireEvent.keyDown(input, { key: 'ArrowRight' }); fireEvent.keyDown(input, { key: 'Delete' }); - expect(onChangeV6.callCount).to.equal(1); + expect(onChangeV6).toHaveBeenCalledTimes(1); fireEvent.keyDown(input, { key: 'ArrowRight' }); fireEvent.keyDown(input, { key: 'Delete' }); - expect(onChangeV6.callCount).to.equal(1); + expect(onChangeV6).toHaveBeenCalledTimes(1); // End date fireEvent.keyDown(input, { key: 'ArrowRight' }); fireEvent.keyDown(input, { key: 'Delete' }); - expect(onChangeV6.callCount).to.equal(2); - expect(onChangeV6.lastCall.firstArg[0]).to.equal(null); - expect(onChangeV6.lastCall.firstArg[1]).to.equal(null); + expect(onChangeV6).toHaveBeenCalledTimes(2); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][0]).to.equal(null); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][1]).to.equal(null); fireEvent.keyDown(input, { key: 'ArrowRight' }); fireEvent.keyDown(input, { key: 'Delete' }); - expect(onChangeV6.callCount).to.equal(2); + expect(onChangeV6).toHaveBeenCalledTimes(2); fireEvent.keyDown(input, { key: 'ArrowRight' }); fireEvent.keyDown(input, { key: 'Delete' }); - expect(onChangeV6.callCount).to.equal(2); + expect(onChangeV6).toHaveBeenCalledTimes(2); }); it('should not call `onChange` if the section is already empty', async () => { @@ -549,10 +549,10 @@ describe(' - Editing', () => { await view.selectSectionAsync('month'); fireEvent.keyDown(view.getActiveSection(0), { key: 'Delete' }); - expect(onChangeV7.callCount).to.equal(1); + expect(onChangeV7).toHaveBeenCalledTimes(1); fireEvent.keyDown(view.getActiveSection(0), { key: 'Delete' }); - expect(onChangeV7.callCount).to.equal(1); + expect(onChangeV7).toHaveBeenCalledTimes(1); view.unmount(); @@ -570,10 +570,10 @@ describe(' - Editing', () => { await view.selectSectionAsync('month'); fireEvent.keyDown(input, { key: 'Delete' }); - expect(onChangeV6.callCount).to.equal(1); + expect(onChangeV6).toHaveBeenCalledTimes(1); fireEvent.keyDown(input, { key: 'Delete' }); - expect(onChangeV6.callCount).to.equal(1); + expect(onChangeV6).toHaveBeenCalledTimes(1); }); }); @@ -687,7 +687,7 @@ describe(' - Editing', () => { }); view.pressKey(null, ''); - expect(onChangeV7.callCount).to.equal(0); + expect(onChangeV7).toHaveBeenCalledTimes(0); view.unmount(); @@ -707,7 +707,7 @@ describe(' - Editing', () => { fireEvent.keyDown(input, { key: 'a', keyCode: 65, ctrlKey: true }); fireEvent.change(input, { target: { value: 'Delete' } }); - expect(onChangeV6.callCount).to.equal(0); + expect(onChangeV6).toHaveBeenCalledTimes(0); }); it('should call `onChange` when clearing the first section of each date (Backspace)', async () => { @@ -724,34 +724,34 @@ describe(' - Editing', () => { // Start date view.pressKey(0, ''); - expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg[0]).to.equal(null); - expect(onChangeV7.lastCall.firstArg[1]).toEqualDateTime( + expect(onChangeV7).toHaveBeenCalledTimes(1); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][0]).to.equal(null); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][1]).toEqualDateTime( adapter.addYears(adapter.date(), 1), ); fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowRight' }); view.pressKey(1, ''); - expect(onChangeV7.callCount).to.equal(1); + expect(onChangeV7).toHaveBeenCalledTimes(1); fireEvent.keyDown(view.getActiveSection(1), { key: 'ArrowRight' }); view.pressKey(2, ''); - expect(onChangeV7.callCount).to.equal(1); + expect(onChangeV7).toHaveBeenCalledTimes(1); // End date fireEvent.keyDown(view.getActiveSection(2), { key: 'ArrowRight' }); view.pressKey(3, ''); - expect(onChangeV7.callCount).to.equal(2); - expect(onChangeV7.lastCall.firstArg[0]).to.equal(null); - expect(onChangeV7.lastCall.firstArg[1]).to.equal(null); + expect(onChangeV7).toHaveBeenCalledTimes(2); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][0]).to.equal(null); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0][1]).to.equal(null); fireEvent.keyDown(view.getActiveSection(3), { key: 'ArrowRight' }); view.pressKey(4, ''); - expect(onChangeV7.callCount).to.equal(2); + expect(onChangeV7).toHaveBeenCalledTimes(2); fireEvent.keyDown(view.getActiveSection(4), { key: 'ArrowRight' }); view.pressKey(5, ''); - expect(onChangeV7.callCount).to.equal(2); + expect(onChangeV7).toHaveBeenCalledTimes(2); view.unmount(); @@ -769,34 +769,34 @@ describe(' - Editing', () => { // Start date fireEvent.change(input, { target: { value: '/15/2022 – 06/15/2023' } }); - expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg[0]).to.equal(null); - expect(onChangeV6.lastCall.firstArg[1]).toEqualDateTime( + expect(onChangeV6).toHaveBeenCalledTimes(1); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][0]).to.equal(null); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][1]).toEqualDateTime( adapter.addYears(adapter.date(), 1), ); await view.user.keyboard('{ArrowRight}'); fireEvent.change(input, { target: { value: 'MM//2022 – 06/15/2023' } }); - expect(onChangeV6.callCount).to.equal(1); + expect(onChangeV6).toHaveBeenCalledTimes(1); await view.user.keyboard('{ArrowRight}'); fireEvent.change(input, { target: { value: 'MM/DD/ – 06/15/2023' } }); - expect(onChangeV6.callCount).to.equal(1); + expect(onChangeV6).toHaveBeenCalledTimes(1); // End date await view.user.keyboard('{ArrowRight}'); fireEvent.change(input, { target: { value: 'MM/DD/YYYY – /15/2023' } }); - expect(onChangeV6.callCount).to.equal(2); - expect(onChangeV6.lastCall.firstArg[0]).to.equal(null); - expect(onChangeV6.lastCall.firstArg[1]).to.equal(null); + expect(onChangeV6).toHaveBeenCalledTimes(2); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][0]).to.equal(null); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0][1]).to.equal(null); await view.user.keyboard('{ArrowRight}'); fireEvent.change(input, { target: { value: 'MM/DD/YYYY – MM//2023' } }); - expect(onChangeV6.callCount).to.equal(2); + expect(onChangeV6).toHaveBeenCalledTimes(2); await view.user.keyboard('{ArrowRight}'); fireEvent.change(input, { target: { value: 'MM/DD/YYYY – MM/DD/' } }); - expect(onChangeV6.callCount).to.equal(2); + expect(onChangeV6).toHaveBeenCalledTimes(2); }); it('should not call `onChange` if the section is already empty (Backspace)', async () => { @@ -813,10 +813,10 @@ describe(' - Editing', () => { await view.selectSectionAsync('month'); await view.user.keyboard('{Backspace}'); - expect(onChangeV7.callCount).to.equal(1); + expect(onChangeV7).toHaveBeenCalledTimes(1); await view.user.keyboard('{Backspace}'); - expect(onChangeV7.callCount).to.equal(1); + expect(onChangeV7).toHaveBeenCalledTimes(1); view.unmount(); @@ -834,10 +834,10 @@ describe(' - Editing', () => { await view.selectSectionAsync('month'); fireEvent.change(input, { target: { value: ' 2022 – June 2023' } }); - expect(onChangeV6.callCount).to.equal(1); + expect(onChangeV6).toHaveBeenCalledTimes(1); fireEvent.change(input, { target: { value: ' 2022 – June 2023' } }); - expect(onChangeV6.callCount).to.equal(1); + expect(onChangeV6).toHaveBeenCalledTimes(1); }); }, ); diff --git a/packages/x-date-pickers/src/DateCalendar/tests/DateCalendar.test.tsx b/packages/x-date-pickers/src/DateCalendar/tests/DateCalendar.test.tsx index 7f29b2c6d1d2b..9775c72942402 100644 --- a/packages/x-date-pickers/src/DateCalendar/tests/DateCalendar.test.tsx +++ b/packages/x-date-pickers/src/DateCalendar/tests/DateCalendar.test.tsx @@ -158,7 +158,7 @@ describe('', () => { // select the current year with a date in the past to trigger "findClosestEnabledDate" await user.click(screen.getByRole('radio', { name: '2019' })); - expect(onChange.lastCall.firstArg).toEqualDateTime(defaultDate); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(defaultDate); }); }); @@ -205,7 +205,7 @@ describe('', () => { await user.click(screen.getByRole('gridcell', { name: '2' })); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2022, 3, 2, 12, 30)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 3, 2, 12, 30)); }); it('should not use `referenceDate` when a value is defined', async () => { @@ -222,7 +222,7 @@ describe('', () => { await user.click(screen.getByRole('gridcell', { name: '2' })); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 0, 2, 12, 20)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 0, 2, 12, 20)); }); it('should not use `referenceDate` when a defaultValue is defined', async () => { @@ -239,7 +239,7 @@ describe('', () => { await user.click(screen.getByRole('gridcell', { name: '2' })); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 0, 2, 12, 20)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 0, 2, 12, 20)); }); it('should keep the time of the currently provided date', async () => { @@ -255,7 +255,7 @@ describe('', () => { await user.click(screen.getByRole('gridcell', { name: '2' })); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime( + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime( adapterToUse.date('2018-01-02T11:11:11.111'), ); }); @@ -316,7 +316,7 @@ describe('', () => { await user.click(april); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 3, 6)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 3, 6)); }); it('should respect minDate when selecting closest enabled date', async () => { @@ -336,7 +336,7 @@ describe('', () => { await user.click(april); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 3, 7)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 3, 7)); }); it('should respect maxDate when selecting closest enabled date', async () => { @@ -356,7 +356,7 @@ describe('', () => { await user.click(april); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 3, 22)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 3, 22)); }); it('should go to next view without changing the date when no date of the new month is enabled', async () => { @@ -395,7 +395,7 @@ describe('', () => { await user.click(april); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2018, 3, 1, 12, 30)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 3, 1, 12, 30)); }); it('should not use `referenceDate` when a value is defined', async () => { @@ -415,7 +415,7 @@ describe('', () => { await user.click(april); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 3, 1, 12, 20)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 3, 1, 12, 20)); }); it('should not use `referenceDate` when a defaultValue is defined', async () => { @@ -435,7 +435,7 @@ describe('', () => { await user.click(april); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 3, 1, 12, 20)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 3, 1, 12, 20)); }); }); @@ -465,7 +465,7 @@ describe('', () => { await user.click(year2022); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2022, 4, 1)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 4, 1)); }); it('should respect minDate when selecting closest enabled date', async () => { @@ -485,7 +485,7 @@ describe('', () => { await user.click(year2017); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2017, 4, 12)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2017, 4, 12)); }); it('should respect maxDate when selecting closest enabled date', async () => { @@ -505,7 +505,7 @@ describe('', () => { await user.click(year2022); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2022, 2, 31)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 2, 31)); }); it('should go to next view without changing the date when no date of the new year is enabled', async () => { @@ -567,7 +567,7 @@ describe('', () => { await user.click(year2022); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2022, 0, 1, 12, 30)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 0, 1, 12, 30)); }); it('should not use `referenceDate` when a value is defined', async () => { @@ -587,7 +587,7 @@ describe('', () => { await user.click(year2022); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2022, 0, 1, 12, 20)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 0, 1, 12, 20)); }); it('should not use `referenceDate` when a defaultValue is defined', async () => { @@ -607,7 +607,7 @@ describe('', () => { await user.click(year2022); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2022, 0, 1, 12, 20)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 0, 1, 12, 20)); }); }); diff --git a/packages/x-date-pickers/src/DateCalendar/tests/timezone.DateCalendar.test.tsx b/packages/x-date-pickers/src/DateCalendar/tests/timezone.DateCalendar.test.tsx index d6696dd4434b3..3f69d0ddeea0d 100644 --- a/packages/x-date-pickers/src/DateCalendar/tests/timezone.DateCalendar.test.tsx +++ b/packages/x-date-pickers/src/DateCalendar/tests/timezone.DateCalendar.test.tsx @@ -17,7 +17,7 @@ describe(' - Timezone', () => { const expectedDate = adapter.setDate(adapter.date(undefined, 'default'), 25); // Check the `onChange` value (uses default timezone, for example: UTC, see TZ env variable) - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; // On dayjs, we are not able to know if a date is UTC because it's the system timezone or because it was created as UTC. // In a real world scenario, this should probably never occur. @@ -37,7 +37,7 @@ describe(' - Timezone', () => { const expectedDate = adapter.setDate(value, 25); // Check the `onChange` value (uses timezone prop) - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; expect(adapter.getTimezone(actualDate)).to.equal( adapter.lib === 'dayjs' ? 'UTC' : 'system', ); @@ -90,7 +90,7 @@ describe(' - Timezone', () => { ); // Check the `onChange` value (uses timezone prop) - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; expect(adapter.getTimezone(actualDate)).to.equal(timezone); expect(actualDate).toEqualDateTime(expectedDate); }); @@ -105,7 +105,7 @@ describe(' - Timezone', () => { const expectedDate = adapter.setDate(value, 25); // Check the `onChange` value (uses timezone prop) - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; expect(adapter.getTimezone(actualDate)).to.equal(timezone); expect(actualDate).toEqualDateTime(expectedDate); }); diff --git a/packages/x-date-pickers/src/DateField/tests/editing.DateField.test.tsx b/packages/x-date-pickers/src/DateField/tests/editing.DateField.test.tsx index d0c685bc590d7..1790e0c84299f 100644 --- a/packages/x-date-pickers/src/DateField/tests/editing.DateField.test.tsx +++ b/packages/x-date-pickers/src/DateField/tests/editing.DateField.test.tsx @@ -30,7 +30,7 @@ describe(' - Editing', () => { expectFieldValueV7(view.getSectionsContainer(), '06/04/2022'); expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2023, 5, 4)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2023, 5, 4)); view.unmount(); @@ -48,7 +48,7 @@ describe(' - Editing', () => { expectFieldValueV6(getTextbox(), '06/04/2022'); expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2023, 5, 4)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2023, 5, 4)); }); it('should call the onChange callback when the value is updated and should change the displayed value if the value is not controlled', async () => { @@ -66,7 +66,7 @@ describe(' - Editing', () => { expectFieldValueV7(view.getSectionsContainer(), '06/04/2023'); expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2023, 5, 4)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2023, 5, 4)); view.unmount(); @@ -83,7 +83,7 @@ describe(' - Editing', () => { expectFieldValueV6(getTextbox(), '06/04/2023'); expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2023, 5, 4)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2023, 5, 4)); }); it('should not call the onChange callback before filling the last section when starting from a null value', async () => { @@ -105,7 +105,7 @@ describe(' - Editing', () => { view.pressKey(1, 'S'); // // We reset the value displayed because the `onChange` callback did not update the controlled value. expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2022, 8, 4)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 8, 4)); await waitFor(() => { expectFieldValueV7(view.getSectionsContainer(), 'DD MMMM'); }); @@ -130,7 +130,7 @@ describe(' - Editing', () => { fireEvent.change(input, { target: { value: '04 S' } }); // Press S expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2022, 8, 4)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 8, 4)); // // We reset the value displayed because the `onChange` callback did not update the controlled value. await waitFor(() => { expectFieldValueV6(input, 'DD MMMM'); @@ -821,7 +821,7 @@ describe(' - Editing', () => { await view.selectSectionAsync('month'); view.pressKey(0, ''); expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).to.equal(null); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).to.equal(null); await view.selectSectionAsync('year'); view.pressKey(1, ''); @@ -843,7 +843,7 @@ describe(' - Editing', () => { await view.selectSectionAsync('month'); fireEvent.change(input, { target: { value: ' 2022' } }); expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).to.equal(null); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).to.equal(null); await view.user.keyboard('{ArrowRight}'); @@ -949,7 +949,7 @@ describe(' - Editing', () => { await firePasteEventV7(view.getSectionsContainer(), '09/16/2022'); expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2022, 8, 16)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 8, 16)); view.unmount(); @@ -969,7 +969,7 @@ describe(' - Editing', () => { await firePasteEventV6(input, '09/16/2022'); expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2022, 8, 16)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 8, 16)); }); it('should set the date when all sections are selected, the pasted value is valid and no value is provided', async () => { @@ -991,7 +991,7 @@ describe(' - Editing', () => { await firePasteEventV7(view.getSectionsContainer(), '09/16/2022'); expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2022, 8, 16)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 8, 16)); view.unmount(); // Test with non-accessible DOM structure @@ -1009,7 +1009,7 @@ describe(' - Editing', () => { await firePasteEventV6(input, '09/16/2022'); expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2022, 8, 16)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 8, 16)); }); it('should not set the date when all sections are selected and the pasted value is not valid', async () => { @@ -1070,7 +1070,7 @@ describe(' - Editing', () => { await firePasteEventV7(view.getSectionsContainer(), `Escaped 2014`); expect(onChangeV7.callCount).to.equal(1); - expect(adapter.getYear(onChangeV7.lastCall.firstArg)).to.equal(2014); + expect(adapter.getYear(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0])).to.equal(2014); view.unmount(); // Test with non-accessible DOM structure @@ -1089,7 +1089,7 @@ describe(' - Editing', () => { await firePasteEventV6(input, `Escaped 2014`); expect(onChangeV6.callCount).to.equal(1); - expect(adapter.getYear(onChangeV6.lastCall.firstArg)).to.equal(2014); + expect(adapter.getYear(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0])).to.equal(2014); }); it('should not set the date when all sections are selected and props.readOnly = true', async () => { @@ -1188,7 +1188,7 @@ describe(' - Editing', () => { await firePasteEventV7(view.getActiveSection(0), '12'); expectFieldValueV7(view.getSectionsContainer(), '12/13/2018'); expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2018, 11, 13)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 11, 13)); view.unmount(); @@ -1208,7 +1208,7 @@ describe(' - Editing', () => { await firePasteEventV6(input, '12'); expectFieldValueV6(input, '12/13/2018'); expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2018, 11, 13)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 11, 13)); }); it('should not update the section when one section is selected and the pasted value has incorrect type', async () => { @@ -1387,7 +1387,7 @@ describe(' - Editing', () => { }); await view.selectSectionAsync('year'); await view.user.keyboard('{ArrowDown}'); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2009, 3, 3, 3, 3, 3)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2009, 3, 3, 3, 3, 3)); view.unmount(); @@ -1400,7 +1400,7 @@ describe(' - Editing', () => { }); await view.selectSectionAsync('year'); await view.user.keyboard('{ArrowDown}'); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2009, 3, 3, 3, 3, 3)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2009, 3, 3, 3, 3, 3)); }); it('should not loose time information when cleaning the date then filling it again', async () => { @@ -1434,7 +1434,7 @@ describe(' - Editing', () => { await view.user.keyboard('2009'); expectFieldValueV7(view.getSectionsContainer(), '11/25/2009'); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2009, 10, 25, 3, 3, 3)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2009, 10, 25, 3, 3, 3)); view.unmount(); @@ -1467,7 +1467,7 @@ describe(' - Editing', () => { await view.user.keyboard('2009'); expectFieldValueV6(input, '11/25/2009'); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2009, 10, 25, 3, 3, 3)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2009, 10, 25, 3, 3, 3)); }); it('should not loose date information when using the year format and value is provided', async () => { @@ -1484,7 +1484,7 @@ describe(' - Editing', () => { await view.selectSectionAsync('year'); await view.user.keyboard('{ArrowDown}'); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2009, 3, 3, 3, 3, 3)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2009, 3, 3, 3, 3, 3)); view.unmount(); @@ -1501,7 +1501,7 @@ describe(' - Editing', () => { await view.selectSectionAsync('year'); await view.user.keyboard('{ArrowDown}'); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2009, 3, 3, 3, 3, 3)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2009, 3, 3, 3, 3, 3)); }); it('should not loose date information when using the month format and value is provided', async () => { @@ -1517,7 +1517,7 @@ describe(' - Editing', () => { await view.selectSectionAsync('month'); await view.user.keyboard('{ArrowDown}'); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2010, 2, 3, 3, 3, 3)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2010, 2, 3, 3, 3, 3)); view.unmount(); @@ -1533,7 +1533,7 @@ describe(' - Editing', () => { await view.selectSectionAsync('month'); await view.user.keyboard('{ArrowDown}'); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2010, 2, 3, 3, 3, 3)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2010, 2, 3, 3, 3, 3)); }); }, ); @@ -1552,7 +1552,7 @@ describe(' - Editing', () => { fireEvent.change(view.getHiddenInput(), { target: { value: '09/16/2022' } }); expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2022, 8, 16)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 8, 16)); view.unmount(); @@ -1566,7 +1566,7 @@ describe(' - Editing', () => { fireEvent.change(input, { target: { value: '09/16/2022' } }); expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2022, 8, 16)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 8, 16)); }); it('should set the date when the change value is valid and a value is provided', () => { @@ -1582,7 +1582,7 @@ describe(' - Editing', () => { fireEvent.change(view.getHiddenInput(), { target: { value: '09/16/2022' } }); expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2022, 8, 16, 3, 3, 3)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 8, 16, 3, 3, 3)); view.unmount(); @@ -1599,7 +1599,7 @@ describe(' - Editing', () => { fireEvent.change(input, { target: { value: '09/16/2022' } }); expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2022, 8, 16, 3, 3, 3)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2022, 8, 16, 3, 3, 3)); }); }, ); diff --git a/packages/x-date-pickers/src/DateField/tests/editingKeyboard.DateField.test.tsx b/packages/x-date-pickers/src/DateField/tests/editingKeyboard.DateField.test.tsx index d7b566f157a81..ed59e6b0962ff 100644 --- a/packages/x-date-pickers/src/DateField/tests/editingKeyboard.DateField.test.tsx +++ b/packages/x-date-pickers/src/DateField/tests/editingKeyboard.DateField.test.tsx @@ -450,7 +450,7 @@ describe(' - Editing Keyboard', () => { await view.user.keyboard('[Delete]'); expect(onChangeV7.callCount).to.equal(1); - expect(onChangeV7.lastCall.firstArg).to.equal(null); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).to.equal(null); await view.user.keyboard('[ArrowRight][Delete]'); @@ -472,7 +472,7 @@ describe(' - Editing Keyboard', () => { await view.user.keyboard('[Delete]'); expect(onChangeV6.callCount).to.equal(1); - expect(onChangeV6.lastCall.firstArg).to.equal(null); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).to.equal(null); await view.user.keyboard('[ArrowRight][Delete]'); expect(onChangeV6.callCount).to.equal(1); diff --git a/packages/x-date-pickers/src/DatePicker/tests/DatePicker.test.tsx b/packages/x-date-pickers/src/DatePicker/tests/DatePicker.test.tsx index fd41b110fefb2..9e38f284c5946 100644 --- a/packages/x-date-pickers/src/DatePicker/tests/DatePicker.test.tsx +++ b/packages/x-date-pickers/src/DatePicker/tests/DatePicker.test.tsx @@ -1,4 +1,4 @@ -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { DatePicker, DatePickerProps } from '@mui/x-date-pickers/DatePicker'; import { screen } from '@mui/internal-test-utils'; import { createPickerRenderer, stubMatchMedia } from 'test/utils/pickers'; @@ -34,19 +34,20 @@ describe('', () => { } it('should submit the form when "Enter" is pressed on the input', async () => { - const handleSubmit = spy(); + const handleSubmit = vi.fn(); const { user } = render(); // focus the input await user.keyboard('{Tab}'); await user.keyboard('{Enter}'); - expect(handleSubmit.callCount).to.equal(1); - expect([...handleSubmit.lastCall.args[0]][0]).to.deep.equal(['testDate', '04/17/2022']); + expect(handleSubmit).toHaveBeenCalledTimes(1); + const lastCall = handleSubmit.mock.calls[handleSubmit.mock.calls.length - 1]; + expect([...lastCall[0]][0]).to.deep.equal(['testDate', '04/17/2022']); }); it('should not submit the form when "Enter" is pressed on the input with "defaultMuiPrevented" set to "true"', async () => { - const handleSubmit = spy(); + const handleSubmit = vi.fn(); const { user } = render( ', () => { await user.keyboard('{Tab}'); await user.keyboard('{Enter}'); - expect(handleSubmit.callCount).to.equal(0); + expect(handleSubmit).toHaveBeenCalledTimes(0); }); }); }); diff --git a/packages/x-date-pickers/src/DateTimeField/tests/editing.DateTimeField.test.tsx b/packages/x-date-pickers/src/DateTimeField/tests/editing.DateTimeField.test.tsx index 7d627d1d2b9fa..1da346c24c4fd 100644 --- a/packages/x-date-pickers/src/DateTimeField/tests/editing.DateTimeField.test.tsx +++ b/packages/x-date-pickers/src/DateTimeField/tests/editing.DateTimeField.test.tsx @@ -34,7 +34,7 @@ describe(' - Editing', () => { fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowUp' }); // All sections not present should equal the one from the referenceDate, and the month should equal January (because it's an ArrowUp on an empty month). - expect(onChange.lastCall.firstArg).toEqualDateTime(adapterToUse.setMonth(referenceDate, 0)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(adapterToUse.setMonth(referenceDate, 0)); }); it('should not use the referenceDate prop when a value is defined', () => { @@ -54,7 +54,7 @@ describe(' - Editing', () => { fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowUp' }); // Should equal the initial `value` prop with one less month. - expect(onChange.lastCall.firstArg).toEqualDateTime(adapterToUse.setMonth(value, 11)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(adapterToUse.setMonth(value, 11)); }); it('should not use the referenceDate prop when a defaultValue is defined', () => { @@ -74,7 +74,7 @@ describe(' - Editing', () => { fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowUp' }); // Should equal the initial `defaultValue` prop with one less month. - expect(onChange.lastCall.firstArg).toEqualDateTime(adapterToUse.setMonth(defaultValue, 11)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(adapterToUse.setMonth(defaultValue, 11)); }); describe('Reference value based on section granularity', () => { @@ -90,7 +90,7 @@ describe(' - Editing', () => { view.selectSection('month'); fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowUp' }); - expect(onChange.lastCall.firstArg).toEqualDateTime('2012-01-01'); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime('2012-01-01'); }); it('should only keep year and month when granularity = day', () => { @@ -105,7 +105,7 @@ describe(' - Editing', () => { view.selectSection('day'); fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowUp' }); - expect(onChange.lastCall.firstArg).toEqualDateTime('2012-05-01'); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime('2012-05-01'); }); it('should only keep up to the hours when granularity = minutes', () => { @@ -126,7 +126,7 @@ describe(' - Editing', () => { fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowRight' }); fireEvent.keyDown(view.getActiveSection(1), { key: 'ArrowUp' }); - expect(onChange.lastCall.firstArg).toEqualDateTime('2012-05-03T00:00:00.000Z'); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime('2012-05-03T00:00:00.000Z'); }); }); @@ -146,7 +146,7 @@ describe(' - Editing', () => { fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowUp' }); // Respect the granularity and the minDate - expect(onChange.lastCall.firstArg).toEqualDateTime('2030-01-01T00:00'); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime('2030-01-01T00:00'); }); it("should ignore the `minDate` if it's before the current date", () => { @@ -164,7 +164,7 @@ describe(' - Editing', () => { fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowUp' }); // Respect the granularity but not the minDate - expect(onChange.lastCall.firstArg).toEqualDateTime('2012-01-01T00:00'); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime('2012-01-01T00:00'); }); it("should create a reference date just before the `maxDate` if it's before the current date", () => { @@ -182,7 +182,7 @@ describe(' - Editing', () => { fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowUp' }); // Respect the granularity and the minDate - expect(onChange.lastCall.firstArg).toEqualDateTime('2007-01-01T00:00'); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime('2007-01-01T00:00'); }); it("should ignore the `maxDate` if it's after the current date", () => { @@ -200,7 +200,7 @@ describe(' - Editing', () => { fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowUp' }); // Respect the granularity but not the maxDate - expect(onChange.lastCall.firstArg).toEqualDateTime('2012-01-01T00:00'); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime('2012-01-01T00:00'); }); }); }); diff --git a/packages/x-date-pickers/src/DateTimeField/tests/timezone.DateTimeField.test.tsx b/packages/x-date-pickers/src/DateTimeField/tests/timezone.DateTimeField.test.tsx index fe80d612ef3a6..3e9bfafc5fcf3 100644 --- a/packages/x-date-pickers/src/DateTimeField/tests/timezone.DateTimeField.test.tsx +++ b/packages/x-date-pickers/src/DateTimeField/tests/timezone.DateTimeField.test.tsx @@ -55,7 +55,7 @@ describe(' - Timezone', () => { expectFieldValueV7(view.getSectionsContainer(), '12/31/2022 23'); // Check the `onChange` value (uses default timezone, for example: UTC, see TZ env variable) - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; // On dayjs, we are not able to know if a date is UTC because it's the system timezone or because it was created as UTC. // In a real world scenario, this should probably never occur. @@ -81,7 +81,7 @@ describe(' - Timezone', () => { expectFieldValueV7(view.getSectionsContainer(), '12/31/2022 23'); // Check the `onChange` value (uses timezone prop) - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; expect(adapter.getTimezone(actualDate)).to.equal(timezone); expect(actualDate).toEqualDateTime(expectedDate); }); @@ -104,7 +104,7 @@ describe(' - Timezone', () => { // Check the `onChange` value (uses timezone prop) const expectedDate = adapter.addMonths(adapter.date(undefined, timezone), -1); - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; expect(adapter.getTimezone(actualDate)).to.equal(timezone); expect(actualDate).toEqualDateTime(expectedDate); }); diff --git a/packages/x-date-pickers/src/DesktopDatePicker/tests/DesktopDatePicker.test.tsx b/packages/x-date-pickers/src/DesktopDatePicker/tests/DesktopDatePicker.test.tsx index 6bbe95ba3aa3e..6843e5373a7b6 100644 --- a/packages/x-date-pickers/src/DesktopDatePicker/tests/DesktopDatePicker.test.tsx +++ b/packages/x-date-pickers/src/DesktopDatePicker/tests/DesktopDatePicker.test.tsx @@ -52,7 +52,7 @@ describe('', () => { await openPickerAsync(user, { type: 'date' }); expect(handleViewChange.callCount).to.equal(2); - expect(handleViewChange.lastCall.firstArg).to.equal('day'); + expect(handleViewChange.mock.calls[handleViewChange.mock.calls.length - 1][0]).to.equal('day'); }); it('should go to the `openTo` view when re-opening the picker', async () => { @@ -77,7 +77,7 @@ describe('', () => { await openPickerAsync(user, { type: 'date' }); expect(handleViewChange.callCount).to.equal(2); - expect(handleViewChange.lastCall.firstArg).to.equal('month'); + expect(handleViewChange.mock.calls[handleViewChange.mock.calls.length - 1][0]).to.equal('month'); }); it('should go to the relevant `view` when `views` prop changes', async () => { @@ -240,7 +240,7 @@ describe('', () => { // Select year await user.click(screen.getByRole('radio', { name: '2025' })); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0]).toEqualDateTime(new Date(2025, 0, 1)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2025, 0, 1)); expect(onAccept.callCount).to.equal(0); expect(onClose.callCount).to.equal(0); @@ -248,7 +248,7 @@ describe('', () => { await user.click(screen.getByRole('gridcell', { name: '1' })); expect(onChange.callCount).to.equal(1); // Don't call onChange again since the value did not change expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0]).toEqualDateTime(new Date(2025, 0, 1)); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0]).toEqualDateTime(new Date(2025, 0, 1)); expect(onClose.callCount).to.equal(1); }); @@ -285,7 +285,7 @@ describe('', () => { await user.click(screen.getByRole('gridcell', { name: '2' })); expect(onChange.callCount).to.equal(1); expect(onAccept.callCount).to.equal(1); - expect(onAccept.lastCall.args[0]).toEqualDateTime(new Date(2018, 0, 2)); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 0, 2)); expect(onClose.callCount).to.equal(1); await openPickerAsync(user, { type: 'date' }); @@ -293,7 +293,7 @@ describe('', () => { await user.click(screen.getByRole('gridcell', { name: '1' })); expect(onChange.callCount).to.equal(2); expect(onAccept.callCount).to.equal(2); - expect(onAccept.lastCall.args[0]).toEqualDateTime(new Date(2018, 0, 1)); + expect(onAccept.mock.calls[onAccept.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 0, 1)); expect(onClose.callCount).to.equal(2); }); }); diff --git a/packages/x-date-pickers/src/DesktopDateTimePicker/tests/DesktopDateTimePicker.test.tsx b/packages/x-date-pickers/src/DesktopDateTimePicker/tests/DesktopDateTimePicker.test.tsx index e2f26b7cd9cf5..9a0ba8ffdef44 100644 --- a/packages/x-date-pickers/src/DesktopDateTimePicker/tests/DesktopDateTimePicker.test.tsx +++ b/packages/x-date-pickers/src/DesktopDateTimePicker/tests/DesktopDateTimePicker.test.tsx @@ -45,7 +45,7 @@ describe('', () => { // Select year await user.click(screen.getByRole('radio', { name: '2025' })); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0]).toEqualDateTime(new Date(2025, 0, 1, 11, 55)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2025, 0, 1, 11, 55)); expect(onAccept.callCount).to.equal(0); expect(onClose.callCount).to.equal(0); @@ -164,7 +164,7 @@ describe('', () => { await user.click(screen.getByRole('option', { name: '03:00 AM' })); expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.args[0]).toEqualDateTime(new Date(2018, 0, 2, 3, 0, 0)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 0, 2, 3, 0, 0)); expect(onAccept.callCount).to.equal(0); // onAccept false by default // Click on 'accept' action to close the picker @@ -189,7 +189,7 @@ describe('', () => { await user.click(screen.getByRole('option', { name: '03:00 AM' })); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0]).toEqualDateTime(new Date(2018, 0, 1, 3, 0, 0)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 0, 1, 3, 0, 0)); expect(onAccept.callCount).to.equal(0); // onAccept false by default // Click on 'accept' action to close the picker diff --git a/packages/x-date-pickers/src/DesktopTimePicker/tests/DesktopTimePicker.test.tsx b/packages/x-date-pickers/src/DesktopTimePicker/tests/DesktopTimePicker.test.tsx index b6e8e5fed3f96..1128c69ea69ce 100644 --- a/packages/x-date-pickers/src/DesktopTimePicker/tests/DesktopTimePicker.test.tsx +++ b/packages/x-date-pickers/src/DesktopTimePicker/tests/DesktopTimePicker.test.tsx @@ -74,7 +74,7 @@ describe('', () => { await user.click(screen.getByRole('option', { name: '09:00 AM' })); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0]).toEqualDateTime(new Date(2018, 0, 1, 9, 0)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 0, 1, 9, 0)); // closeOnSelect false by default expect(onAccept.callCount).to.equal(0); expect(onClose.callCount).to.equal(0); diff --git a/packages/x-date-pickers/src/DigitalClock/tests/DigitalClock.test.tsx b/packages/x-date-pickers/src/DigitalClock/tests/DigitalClock.test.tsx index c590613f28036..c71482c2c7e6a 100644 --- a/packages/x-date-pickers/src/DigitalClock/tests/DigitalClock.test.tsx +++ b/packages/x-date-pickers/src/DigitalClock/tests/DigitalClock.test.tsx @@ -37,7 +37,7 @@ describe('', () => { adapterToUse.setMinutes(adapterToUse.setHours(adapterToUse.date(), 15), 30), ); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2018, 0, 1, 15, 30)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 0, 1, 15, 30)); }); it('should fallback to making the first entry focusable when `referenceDate` does not map to any option', () => { @@ -68,7 +68,7 @@ describe('', () => { adapterToUse.setMinutes(adapterToUse.setHours(adapterToUse.date(), 15), 30), ); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 0, 1, 15, 30)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 0, 1, 15, 30)); }); it('should not use `referenceDate` when a defaultValue is defined', () => { @@ -87,7 +87,7 @@ describe('', () => { adapterToUse.setMinutes(adapterToUse.setHours(adapterToUse.date(), 15), 30), ); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 0, 1, 15, 30)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 0, 1, 15, 30)); }); }); diff --git a/packages/x-date-pickers/src/DigitalClock/tests/timezone.DigitalClock.test.tsx b/packages/x-date-pickers/src/DigitalClock/tests/timezone.DigitalClock.test.tsx index 3b3e85b12ffa8..298c358e1f584 100644 --- a/packages/x-date-pickers/src/DigitalClock/tests/timezone.DigitalClock.test.tsx +++ b/packages/x-date-pickers/src/DigitalClock/tests/timezone.DigitalClock.test.tsx @@ -25,7 +25,7 @@ describe(' - Timezone', () => { const expectedDate = adapter.setHours(adapter.date(), 8); // Check the `onChange` value (uses default timezone, for example: UTC, see TZ env variable) - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; // On dayjs, we are not able to know if a date is UTC because it's the system timezone or because it was created as UTC. // In a real world scenario, this should probably never occur. @@ -117,7 +117,7 @@ describe(' - Timezone', () => { ); // Check the `onChange` value (uses timezone prop) - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; expect(adapter.getTimezone(actualDate)).to.equal(timezone); expect(actualDate).toEqualDateTime(expectedDate); }); @@ -142,7 +142,7 @@ describe(' - Timezone', () => { fireEvent.click(screen.getByRole('option', { name: '08:30 PM' })); - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; const renderedHourAfter = get24HourFromDigitalClock(); expect(renderedHourAfter).to.equal( diff --git a/packages/x-date-pickers/src/LocalizationProvider/LocalizationProvider.test.tsx b/packages/x-date-pickers/src/LocalizationProvider/LocalizationProvider.test.tsx index 1e1a3b9967147..bad936aaba269 100644 --- a/packages/x-date-pickers/src/LocalizationProvider/LocalizationProvider.test.tsx +++ b/packages/x-date-pickers/src/LocalizationProvider/LocalizationProvider.test.tsx @@ -45,7 +45,7 @@ describe('', () => { , ); - const localeText: PickersLocaleText = handleContextChange.lastCall.args[0].localeText; + const localeText: PickersLocaleText = handleContextChange.mock.calls[handleContextChange.mock.calls.length - 1][0].localeText; expect(localeText.start).to.equal('Debut'); expect(localeText.end).to.equal('End'); }); @@ -71,7 +71,7 @@ describe('', () => { , ); - const localeText: PickersLocaleText = handleContextChange.lastCall.args[0].localeText; + const localeText: PickersLocaleText = handleContextChange.mock.calls[handleContextChange.mock.calls.length - 1][0].localeText; expect(localeText.start).to.equal('Start'); }); @@ -86,7 +86,7 @@ describe('', () => { , ); - const localeText: PickersLocaleText = handleContextChange.lastCall.args[0].localeText; + const localeText: PickersLocaleText = handleContextChange.mock.calls[handleContextChange.mock.calls.length - 1][0].localeText; expect(localeText.start).to.equal('Début'); }); @@ -101,7 +101,7 @@ describe('', () => { , ); - const localeText: PickersLocaleText = handleContextChange.lastCall.args[0].localeText; + const localeText: PickersLocaleText = handleContextChange.mock.calls[handleContextChange.mock.calls.length - 1][0].localeText; expect(localeText.start).to.equal('Empezar'); }); }); diff --git a/packages/x-date-pickers/src/MobileDateTimePicker/tests/MobileDateTimePicker.test.tsx b/packages/x-date-pickers/src/MobileDateTimePicker/tests/MobileDateTimePicker.test.tsx index e758cfec8a081..09784b38865a1 100644 --- a/packages/x-date-pickers/src/MobileDateTimePicker/tests/MobileDateTimePicker.test.tsx +++ b/packages/x-date-pickers/src/MobileDateTimePicker/tests/MobileDateTimePicker.test.tsx @@ -102,23 +102,23 @@ describe('', () => { await user.click(screen.getByText('2010', { selector: 'button' })); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0]).toEqualDateTime(new Date(2010, 0, 1)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2010, 0, 1)); // Change the date await user.click(screen.getByRole('gridcell', { name: '15' })); expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.args[0]).toEqualDateTime(new Date(2010, 0, 15)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2010, 0, 15)); // Change the hours fireEvent.click(screen.getByRole('button', { name: 'Next' })); fireEvent.click(screen.getByRole('option', { name: '11 hours' })); expect(onChange.callCount).to.equal(3); - expect(onChange.lastCall.args[0]).toEqualDateTime(adapterToUse.date('2010-01-15T11:00:00')); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(adapterToUse.date('2010-01-15T11:00:00')); // Change the minutes fireEvent.click(screen.getByRole('option', { name: '55 minutes' })); expect(onChange.callCount).to.equal(4); - expect(onChange.lastCall.args[0]).toEqualDateTime(adapterToUse.date('2010-01-15T11:55:00')); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(adapterToUse.date('2010-01-15T11:55:00')); expect(onAccept.callCount).to.equal(0); expect(onClose.callCount).to.equal(0); }); diff --git a/packages/x-date-pickers/src/MobileTimePicker/tests/MobileTimePicker.test.tsx b/packages/x-date-pickers/src/MobileTimePicker/tests/MobileTimePicker.test.tsx index d291deba3d201..7d3d2588d8fc6 100644 --- a/packages/x-date-pickers/src/MobileTimePicker/tests/MobileTimePicker.test.tsx +++ b/packages/x-date-pickers/src/MobileTimePicker/tests/MobileTimePicker.test.tsx @@ -29,7 +29,7 @@ describe('', () => { fireEvent.click(buttonPM); expect(handleChange.callCount).to.equal(1); - expect(handleChange.firstCall.args[0]).toEqualDateTime(new Date(2019, 0, 1, 16, 20)); + expect(handleChange.firstCall.mock.calls[0]).toEqualDateTime(new Date(2019, 0, 1, 16, 20)); }); it.skipIf(!hasTouchSupport)('should call onChange when selecting each view', async () => { @@ -60,7 +60,7 @@ describe('', () => { }, ]); expect(onChange.callCount).to.equal(1); - expect(onChange.lastCall.args[0]).toEqualDateTime(adapterToUse.date('2018-01-01T11:00:00')); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(adapterToUse.date('2018-01-01T11:00:00')); // Change the minutes const minuteClockEvent = getClockTouchEvent(53, 'minutes'); @@ -73,7 +73,7 @@ describe('', () => { }, ]); expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.args[0]).toEqualDateTime(adapterToUse.date('2018-01-01T11:53:00')); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(adapterToUse.date('2018-01-01T11:53:00')); expect(onAccept.callCount).to.equal(0); expect(onClose.callCount).to.equal(0); }); diff --git a/packages/x-date-pickers/src/MonthCalendar/tests/MonthCalendar.test.tsx b/packages/x-date-pickers/src/MonthCalendar/tests/MonthCalendar.test.tsx index 2975d01749e95..2be08c993938d 100644 --- a/packages/x-date-pickers/src/MonthCalendar/tests/MonthCalendar.test.tsx +++ b/packages/x-date-pickers/src/MonthCalendar/tests/MonthCalendar.test.tsx @@ -22,7 +22,7 @@ describe('', () => { fireEvent.click(targetMonth); expect(onChange.callCount).to.equal(1); - expect(onChange.args[0][0]).toEqualDateTime(new Date(2019, 1, 2)); + expect(onChange.mock.calls[0][0]).toEqualDateTime(new Date(2019, 1, 2)); }); describe('with fake timers', () => { @@ -41,7 +41,7 @@ describe('', () => { fireEvent.click(screen.getByRole('radio', { name: 'February' })); expect(onChange.callCount).to.equal(1); - expect(onChange.args[0][0]).toEqualDateTime(new Date(2019, 1, 1, 0, 0, 0)); + expect(onChange.mock.calls[0][0]).toEqualDateTime(new Date(2019, 1, 1, 0, 0, 0)); }); it('should mark only the month in the current year as `aria-current="date"`', () => { diff --git a/packages/x-date-pickers/src/MultiSectionDigitalClock/tests/MultiSectionDigitalClock.test.tsx b/packages/x-date-pickers/src/MultiSectionDigitalClock/tests/MultiSectionDigitalClock.test.tsx index d5753f431bef2..d0f9a0b1772c9 100644 --- a/packages/x-date-pickers/src/MultiSectionDigitalClock/tests/MultiSectionDigitalClock.test.tsx +++ b/packages/x-date-pickers/src/MultiSectionDigitalClock/tests/MultiSectionDigitalClock.test.tsx @@ -41,7 +41,7 @@ describe('', () => { adapterToUse.setMinutes(adapterToUse.setHours(adapterToUse.date(), 15), 30), ); expect(onChange.callCount).to.equal(3); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2018, 0, 1, 15, 30)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 0, 1, 15, 30)); }); it('should fallback to making the first entry focusable when `referenceDate` does not map to an option', () => { @@ -83,7 +83,7 @@ describe('', () => { adapterToUse.setMinutes(adapterToUse.setHours(adapterToUse.date(), 15), 30), ); expect(onChange.callCount).to.equal(3); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 0, 1, 15, 30)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 0, 1, 15, 30)); }); it('should not use `referenceDate` when a defaultValue is defined', () => { @@ -102,7 +102,7 @@ describe('', () => { adapterToUse.setMinutes(adapterToUse.setHours(adapterToUse.date(), 15), 30), ); expect(onChange.callCount).to.equal(3); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 0, 1, 15, 30)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 0, 1, 15, 30)); }); }); diff --git a/packages/x-date-pickers/src/PickerDay2/PickerDay2.test.tsx b/packages/x-date-pickers/src/PickerDay2/PickerDay2.test.tsx index 651160e95880d..9d27e0f53d2d6 100644 --- a/packages/x-date-pickers/src/PickerDay2/PickerDay2.test.tsx +++ b/packages/x-date-pickers/src/PickerDay2/PickerDay2.test.tsx @@ -52,7 +52,7 @@ describe('', () => { fireEvent.click(targetDay); expect(handleDaySelect.callCount).to.equal(1); - expect(handleDaySelect.args[0][0]).toEqualDateTime(day); + expect(handleDaySelect.mock.calls[0][0]).toEqualDateTime(day); }); it('renders the day of the month by default', () => { diff --git a/packages/x-date-pickers/src/PickersDay/PickersDay.test.tsx b/packages/x-date-pickers/src/PickersDay/PickersDay.test.tsx index 6b6151fc044d1..e8d278eb321cd 100644 --- a/packages/x-date-pickers/src/PickersDay/PickersDay.test.tsx +++ b/packages/x-date-pickers/src/PickersDay/PickersDay.test.tsx @@ -52,7 +52,7 @@ describe('', () => { fireEvent.click(targetDay); expect(handleDaySelect.callCount).to.equal(1); - expect(handleDaySelect.args[0][0]).toEqualDateTime(day); + expect(handleDaySelect.mock.calls[0][0]).toEqualDateTime(day); }); it('renders the day of the month by default', () => { diff --git a/packages/x-date-pickers/src/TimeClock/tests/TimeClock.test.tsx b/packages/x-date-pickers/src/TimeClock/tests/TimeClock.test.tsx index 914b177f6ccc8..9530ebdbb1c35 100644 --- a/packages/x-date-pickers/src/TimeClock/tests/TimeClock.test.tsx +++ b/packages/x-date-pickers/src/TimeClock/tests/TimeClock.test.tsx @@ -602,7 +602,7 @@ describe('', () => { 'hours', ); expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2018, 0, 1, 15, 30)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2018, 0, 1, 15, 30)); }); it('should not use `referenceDate` when a value is defined', () => { @@ -622,7 +622,7 @@ describe('', () => { 'hours', ); expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 0, 1, 15, 20)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 0, 1, 15, 20)); }); it('should not use `referenceDate` when a defaultValue is defined', () => { @@ -642,7 +642,7 @@ describe('', () => { 'hours', ); expect(onChange.callCount).to.equal(2); - expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2019, 0, 1, 15, 20)); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(new Date(2019, 0, 1, 15, 20)); }); }); }); diff --git a/packages/x-date-pickers/src/TimeClock/tests/timezone.TimeClock.test.tsx b/packages/x-date-pickers/src/TimeClock/tests/timezone.TimeClock.test.tsx index 0d6ac1905e697..4776c962c54c7 100644 --- a/packages/x-date-pickers/src/TimeClock/tests/timezone.TimeClock.test.tsx +++ b/packages/x-date-pickers/src/TimeClock/tests/timezone.TimeClock.test.tsx @@ -24,7 +24,7 @@ describe(' - Timezone', () => { const expectedDate = adapter.setHours(adapter.date(), 8); // Check the `onChange` value (uses default timezone, for example: UTC, see TZ env variable) - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; // On dayjs, we are not able to know if a date is UTC because it's the system timezone or because it was created as UTC. // In a real world scenario, this should probably never occur. @@ -50,7 +50,7 @@ describe(' - Timezone', () => { ); // Check the `onChange` value (uses timezone prop) - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; expect(adapter.getTimezone(actualDate)).to.equal(timezone); expect(actualDate).toEqualDateTime(expectedDate); }); @@ -81,7 +81,7 @@ describe(' - Timezone', () => { fireTouchChangedEvent(screen.getByTestId('clock'), 'touchmove', hourClockEvent); fireTouchChangedEvent(screen.getByTestId('clock'), 'touchend', hourClockEvent); - const actualDate = onChange.lastCall.firstArg; + const actualDate = onChange.mock.calls[onChange.mock.calls.length - 1][0]; const renderedHourAfter = getTimeClockValue(); expect(renderedHourAfter).to.equal( diff --git a/packages/x-date-pickers/src/TimeField/tests/editing.TimeField.test.tsx b/packages/x-date-pickers/src/TimeField/tests/editing.TimeField.test.tsx index e3f550075a73d..93b72312452bb 100644 --- a/packages/x-date-pickers/src/TimeField/tests/editing.TimeField.test.tsx +++ b/packages/x-date-pickers/src/TimeField/tests/editing.TimeField.test.tsx @@ -646,7 +646,7 @@ describe(' - Editing', () => { await view.selectSectionAsync('hours'); fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowDown' }); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2010, 3, 3, 2, 3, 3)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2010, 3, 3, 2, 3, 3)); view.unmount(); @@ -663,7 +663,7 @@ describe(' - Editing', () => { await view.selectSectionAsync('hours'); fireEvent.keyDown(input, { key: 'ArrowDown' }); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2010, 3, 3, 2, 3, 3)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2010, 3, 3, 2, 3, 3)); }); it('should not loose date information when cleaning the date then filling it again', async () => { @@ -691,7 +691,7 @@ describe(' - Editing', () => { view.pressKey(1, '4'); expectFieldValueV7(view.getSectionsContainer(), '03:04'); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2010, 3, 3, 3, 4, 3)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2010, 3, 3, 3, 4, 3)); view.unmount(); @@ -716,7 +716,7 @@ describe(' - Editing', () => { fireEvent.change(input, { target: { value: '03:4' } }); // Press "3" expectFieldValueV6(input, '03:04'); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2010, 3, 3, 3, 4, 3)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2010, 3, 3, 3, 4, 3)); }); it('should not loose time information when using the hour format and value is provided', async () => { @@ -733,7 +733,7 @@ describe(' - Editing', () => { await view.selectSectionAsync('hours'); fireEvent.keyDown(view.getActiveSection(0), { key: 'ArrowDown' }); - expect(onChangeV7.lastCall.firstArg).toEqualDateTime(new Date(2010, 3, 3, 2, 3, 3)); + expect(onChangeV7.mock.calls[onChangeV7.mock.calls.length - 1][0]).toEqualDateTime(new Date(2010, 3, 3, 2, 3, 3)); view.unmount(); @@ -751,7 +751,7 @@ describe(' - Editing', () => { await view.selectSectionAsync('hours'); fireEvent.keyDown(input, { key: 'ArrowDown' }); - expect(onChangeV6.lastCall.firstArg).toEqualDateTime(new Date(2010, 3, 3, 2, 3, 3)); + expect(onChangeV6.mock.calls[onChangeV6.mock.calls.length - 1][0]).toEqualDateTime(new Date(2010, 3, 3, 2, 3, 3)); }); }, ); diff --git a/packages/x-date-pickers/src/YearCalendar/tests/YearCalendar.test.tsx b/packages/x-date-pickers/src/YearCalendar/tests/YearCalendar.test.tsx index 16372c2968137..9ef0404d31090 100644 --- a/packages/x-date-pickers/src/YearCalendar/tests/YearCalendar.test.tsx +++ b/packages/x-date-pickers/src/YearCalendar/tests/YearCalendar.test.tsx @@ -22,7 +22,7 @@ describe('', () => { fireEvent.click(targetYear); expect(onChange.callCount).to.equal(1); - expect(onChange.args[0][0]).toEqualDateTime(new Date(2025, 1, 2)); + expect(onChange.mock.calls[0][0]).toEqualDateTime(new Date(2025, 1, 2)); }); it('should select start of year without time when no initial value is present', () => { @@ -32,7 +32,7 @@ describe('', () => { fireEvent.click(screen.getByRole('radio', { name: '2025' })); expect(onChange.callCount).to.equal(1); - expect(onChange.args[0][0]).toEqualDateTime(new Date(2025, 0, 1, 0, 0, 0, 0)); + expect(onChange.mock.calls[0][0]).toEqualDateTime(new Date(2025, 0, 1, 0, 0, 0, 0)); }); it('does not allow to pick year if readOnly prop is passed', () => { diff --git a/packages/x-internals/src/EventManager/EventManager.test.ts b/packages/x-internals/src/EventManager/EventManager.test.ts index a70160653fa8c..7a2aea030cf74 100644 --- a/packages/x-internals/src/EventManager/EventManager.test.ts +++ b/packages/x-internals/src/EventManager/EventManager.test.ts @@ -1,47 +1,53 @@ -import { spy, assert as sinonAssert } from 'sinon'; +import { vi, expect } from 'vitest'; import { EventManager } from './EventManager'; describe('EventManager', () => { it('should run regular-priority event in the registration order', () => { const manager = new EventManager(); - const listener1 = spy(); - const listener2 = spy(); + const listener1 = vi.fn(); + const listener2 = vi.fn(); manager.on('testEvent', listener1); manager.on('testEvent', listener2); manager.emit('testEvent'); - sinonAssert.callOrder(listener1, listener2); + expect(listener1.mock.invocationCallOrder[0]).toBeLessThan( + listener2.mock.invocationCallOrder[0], + ); }); it('should run high-priority event in the registration reversed order', () => { const manager = new EventManager(); - const listener1 = spy(); - const listener2 = spy(); + const listener1 = vi.fn(); + const listener2 = vi.fn(); manager.on('testEvent', listener1, { isFirst: true }); manager.on('testEvent', listener2, { isFirst: true }); manager.emit('testEvent'); - sinonAssert.callOrder(listener2, listener1); + expect(listener2.mock.invocationCallOrder[0]).toBeLessThan( + listener1.mock.invocationCallOrder[0], + ); }); it('should run high-priority event before regular priority event', () => { const manager = new EventManager(); - const listener1 = spy(); - const listener2 = spy(); + const listener1 = vi.fn(); + const listener2 = vi.fn(); manager.on('testEvent', listener1); manager.on('testEvent', listener2, { isFirst: true }); manager.emit('testEvent'); - sinonAssert.callOrder(listener2, listener1); + expect(listener2.mock.invocationCallOrder[0]).toBeLessThan( + listener1.mock.invocationCallOrder[0], + ); }); it('should apply event un-registration even when asked after the emission', () => { const manager = new EventManager(); - const listener2 = spy(); - const listener1 = spy(() => { + const listener2 = vi.fn(); + const listener1 = vi.fn(() => { manager.removeListener('testEvent', listener2); }); manager.on('testEvent', listener1); manager.on('testEvent', listener2); manager.emit('testEvent'); - sinonAssert.callCount(listener1, 1); - sinonAssert.callCount(listener2, 0); + expect(listener1).toHaveBeenCalledTimes(1); + expect(listener2).toHaveBeenCalledTimes(0); }); }); diff --git a/packages/x-scheduler-headless/src/use-event-calendar/tests/date.EventCalendarStore.test.ts b/packages/x-scheduler-headless/src/use-event-calendar/tests/date.EventCalendarStore.test.ts index 562998893271f..7da50039daff5 100644 --- a/packages/x-scheduler-headless/src/use-event-calendar/tests/date.EventCalendarStore.test.ts +++ b/packages/x-scheduler-headless/src/use-event-calendar/tests/date.EventCalendarStore.test.ts @@ -30,10 +30,10 @@ describe('Date - EventCalendarStore', () => { expect(store.state.view).to.equal('day'); expect(store.state.visibleDate).toEqualDateTime(nextDate); - expect(onVisibleDateChange.calledOnce).to.equal(true); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime(nextDate); - expect(onViewChange.calledOnce).to.equal(true); - expect(onViewChange.lastCall.firstArg).to.equal('day'); + expect(onVisibleDateChange).toHaveBeenCalledOnce(); + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime(nextDate); + expect(onViewChange).toHaveBeenCalledOnce(); + expect(onViewChange.mock.calls[onViewChange.mock.calls.length - 1][0]).to.equal('day'); }); it('should NOT mutate store but calls both callbacks when both change when is controlled', () => { @@ -59,10 +59,10 @@ describe('Date - EventCalendarStore', () => { expect(store.state.view).to.equal('week'); expect(store.state.visibleDate).toEqualDateTime(initialDate); - expect(onVisibleDateChange.calledOnce).to.equal(true); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime(nextDate); - expect(onViewChange.calledOnce).to.equal(true); - expect(onViewChange.lastCall.firstArg).to.equal('day'); + expect(onVisibleDateChange).toHaveBeenCalledOnce(); + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime(nextDate); + expect(onViewChange).toHaveBeenCalledOnce(); + expect(onViewChange.mock.calls[onViewChange.mock.calls.length - 1][0]).to.equal('day'); }); it('should update date in store and calls onVisibleDateChange if only date changes when is uncontrolled', () => { @@ -87,8 +87,8 @@ describe('Date - EventCalendarStore', () => { expect(store.state.view).to.equal('day'); expect(store.state.visibleDate).toEqualDateTime(nextDate); - expect(onVisibleDateChange.calledOnce).to.equal(true); - expect(onViewChange.called).to.equal(false); + expect(onVisibleDateChange).toHaveBeenCalledOnce(); + expect(onViewChange).not.toHaveBeenCalled(); }); it('should update date and calls only onVisibleDateChange when is partially controlled (view controlled, visibleDate uncontrolled)', () => { @@ -113,8 +113,8 @@ describe('Date - EventCalendarStore', () => { expect(store.state.view).to.equal('day'); expect(store.state.visibleDate).toEqualDateTime(nextDate); - expect(onVisibleDateChange.calledOnce).to.equal(true); - expect(onViewChange.called).to.equal(false); + expect(onVisibleDateChange).toHaveBeenCalledOnce(); + expect(onViewChange).not.toHaveBeenCalled(); }); it('should update view and calls only onViewChange when is partially controlled (view uncontrolled, visibleDate controlled)', () => { @@ -138,9 +138,9 @@ describe('Date - EventCalendarStore', () => { expect(store.state.view).to.equal('day'); expect(store.state.visibleDate).toEqualDateTime(currentDate); - expect(onVisibleDateChange.calledOnce).to.equal(false); - expect(onViewChange.calledOnce).to.equal(true); - expect(onViewChange.lastCall.firstArg).to.equal('day'); + expect(onVisibleDateChange).not.toHaveBeenCalled(); + expect(onViewChange).toHaveBeenCalledOnce(); + expect(onViewChange.mock.calls[onViewChange.mock.calls.length - 1][0]).to.equal('day'); }); it('should do nothing if nothing changes, does not update store or call callbacks', () => { @@ -163,8 +163,8 @@ describe('Date - EventCalendarStore', () => { expect(store.state.view).to.equal('day'); expect(store.state.visibleDate).toEqualDateTime(sameDate); - expect(onVisibleDateChange.called).to.equal(false); - expect(onViewChange.called).to.equal(false); + expect(onVisibleDateChange).not.toHaveBeenCalled(); + expect(onViewChange).not.toHaveBeenCalled(); }); it('should throw if the view is not an allowed view', () => { @@ -197,7 +197,7 @@ describe('Date - EventCalendarStore', () => { store.setViewConfig({ siblingVisibleDateGetter }); store.goToPreviousVisibleDate({} as any); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime(targetDate); + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime(targetDate); expect(siblingVisibleDateGetter.lastCall.lastArg).toEqual(-1); }); }); @@ -220,7 +220,7 @@ describe('Date - EventCalendarStore', () => { store.setViewConfig({ siblingVisibleDateGetter }); store.goToNextVisibleDate({} as any); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime(targetDate); + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime(targetDate); expect(siblingVisibleDateGetter.lastCall.lastArg).toEqual(1); }); }); diff --git a/packages/x-scheduler-headless/src/use-event-calendar/tests/view.EventCalendarStore.test.ts b/packages/x-scheduler-headless/src/use-event-calendar/tests/view.EventCalendarStore.test.ts index 101c47f8396ba..bffc2ccd1e45e 100644 --- a/packages/x-scheduler-headless/src/use-event-calendar/tests/view.EventCalendarStore.test.ts +++ b/packages/x-scheduler-headless/src/use-event-calendar/tests/view.EventCalendarStore.test.ts @@ -14,8 +14,8 @@ describe('View - EventCalendarStore', () => { store.setView('day', {} as any); expect(store.state.view).to.equal('day'); - expect(onViewChange.calledOnce).to.equal(true); - expect(onViewChange.lastCall.firstArg).to.equal('day'); + expect(onViewChange).toHaveBeenCalledOnce(); + expect(onViewChange.mock.calls[onViewChange.mock.calls.length - 1][0]).to.equal('day'); }); it('should NOT mutate store but calls onViewChange when is controlled', () => { @@ -28,8 +28,8 @@ describe('View - EventCalendarStore', () => { store.setView('day', {} as any); expect(store.state.view).to.equal('week'); - expect(onViewChange.calledOnce).to.equal(true); - expect(onViewChange.lastCall.firstArg).to.equal('day'); + expect(onViewChange).toHaveBeenCalledOnce(); + expect(onViewChange.mock.calls[onViewChange.mock.calls.length - 1][0]).to.equal('day'); }); it('should do nothing if setting the same view: no state change, no callback', () => { @@ -42,7 +42,7 @@ describe('View - EventCalendarStore', () => { store.setView('month', {} as any); expect(store.state.view).to.equal('month'); - expect(onViewChange.called).to.equal(false); + expect(onViewChange).not.toHaveBeenCalled(); }); it('should throw when switching to a view not included in the allowed views', () => { diff --git a/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/date.SchedulerStore.test.ts b/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/date.SchedulerStore.test.ts index 7c26c72650332..ab5422e1c6a13 100644 --- a/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/date.SchedulerStore.test.ts +++ b/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/date.SchedulerStore.test.ts @@ -19,8 +19,8 @@ storeClasses.forEach((storeClass) => { const expected = adapter.startOfDay(adapter.date()); expect(store.state.visibleDate).toEqualDateTime(expected); - expect(onVisibleDateChange.calledOnce).to.equal(true); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime(expected); + expect(onVisibleDateChange).toHaveBeenCalledOnce(); + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime(expected); }); it('should not change the state but calls onVisibleDateChange with today when is controlled', () => { @@ -36,8 +36,8 @@ storeClasses.forEach((storeClass) => { const expected = adapter.startOfDay(adapter.date()); expect(store.state.visibleDate).toEqualDateTime(controlledDate); - expect(onVisibleDateChange.calledOnce).to.equal(true); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime(expected); + expect(onVisibleDateChange).toHaveBeenCalledOnce(); + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime(expected); }); it('should do nothing if already at today (no state change, no callback)', () => { @@ -52,7 +52,7 @@ storeClasses.forEach((storeClass) => { store.goToToday({} as any); expect(store.state.visibleDate).toEqualDateTime(todayStart); - expect(onVisibleDateChange.called).to.equal(false); + expect(onVisibleDateChange).not.toHaveBeenCalled(); }); }); }); diff --git a/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/event.SchedulerStore.test.ts b/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/event.SchedulerStore.test.ts index ec3838c2cafce..662dd89d4c9d8 100644 --- a/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/event.SchedulerStore.test.ts +++ b/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/event.SchedulerStore.test.ts @@ -1,4 +1,4 @@ -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { adapter } from 'test/utils/scheduler'; import { SchedulerEvent, @@ -75,7 +75,7 @@ storeClasses.forEach((storeClass) => { }); it('should use the provided event model structure to write event properties', () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const events: MyEvent[] = [ { @@ -100,8 +100,8 @@ storeClasses.forEach((storeClass) => { }); // Should call onEventsChange with the updated event using the custom model structure - expect(onEventsChange.calledOnce).to.equal(true); - expect(onEventsChange.lastCall.firstArg).to.deep.equal([ + expect(onEventsChange).toHaveBeenCalledOnce(); + expect(onEventsChange.mock.calls[onEventsChange.mock.calls.length - 1][0]).to.deep.equal([ { myId: '1', myTitle: 'Event 1 updated', @@ -113,7 +113,7 @@ storeClasses.forEach((storeClass) => { }); it('should use the provided event model structure to create an event', () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const events: MyEvent[] = []; @@ -130,8 +130,8 @@ storeClasses.forEach((storeClass) => { }); // Should call onEventsChange with the created event using the custom model structure - expect(onEventsChange.calledOnce).to.equal(true); - expect(onEventsChange.lastCall.firstArg).to.deep.equal([ + expect(onEventsChange).toHaveBeenCalledOnce(); + expect(onEventsChange.mock.calls[onEventsChange.mock.calls.length - 1][0]).to.deep.equal([ { myId: '1', myTitle: 'Event 1', @@ -150,7 +150,7 @@ storeClasses.forEach((storeClass) => { end: SchedulerValidDate; } - const idGetter = spy((event: MyEvent2) => event.myId); + const idGetter = vi.fn((event: MyEvent2) => event.myId); const eventModelStructure2: SchedulerEventModelStructure = { id: { @@ -177,7 +177,7 @@ storeClasses.forEach((storeClass) => { ); // Called to convert Event 1 on mount. - expect(idGetter.callCount).to.equal(1); + expect(idGetter).toHaveBeenCalledTimes(1); store.updateStateFromParameters( { @@ -189,7 +189,7 @@ storeClasses.forEach((storeClass) => { ); // Not called again when updating a non-related parameter. - expect(idGetter.callCount).to.equal(1); + expect(idGetter).toHaveBeenCalledTimes(1); const events2: MyEvent2[] = [ { @@ -216,7 +216,7 @@ storeClasses.forEach((storeClass) => { ); // Called again to convert Event 1 and Event 2 because props.events changed. - expect(idGetter.callCount).to.equal(3); + expect(idGetter).toHaveBeenCalledTimes(3); store.updateStateFromParameters( { @@ -228,13 +228,13 @@ storeClasses.forEach((storeClass) => { ); // Called again to convert Event 1 and Event 2 because props.eventModelStructure changed. - expect(idGetter.callCount).to.equal(5); + expect(idGetter).toHaveBeenCalledTimes(5); }); }); describe('Method: updateEvent', () => { it('should replace matching id and emit onEventsChange with the updated events', () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const events = [ buildEvent( '1', @@ -265,8 +265,8 @@ storeClasses.forEach((storeClass) => { store.updateEvent(updatedEvent); - expect(onEventsChange.calledOnce).to.equal(true); - const updatedEvents = onEventsChange.lastCall.firstArg; + expect(onEventsChange).toHaveBeenCalledOnce(); + const updatedEvents = onEventsChange.mock.calls[onEventsChange.mock.calls.length - 1][0]; expect(updatedEvents).to.have.length(2); expect(updatedEvents[0].title).to.equal('Event 1'); @@ -279,7 +279,7 @@ storeClasses.forEach((storeClass) => { describe('Method: deleteEvent', () => { it('should remove by id and call onEventsChange with the updated events', () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const events = [ buildEvent( '1', @@ -304,15 +304,15 @@ storeClasses.forEach((storeClass) => { const store = new storeClass.Value({ events, onEventsChange }, adapter); store.deleteEvent('2'); - expect(onEventsChange.calledOnce).to.equal(true); - const updatedEvents = onEventsChange.lastCall.firstArg; + expect(onEventsChange).toHaveBeenCalledOnce(); + const updatedEvents = onEventsChange.mock.calls[onEventsChange.mock.calls.length - 1][0]; expect(updatedEvents.map((event) => event.id)).to.deep.equal(['1', '3']); }); }); describe('Method: createEvent', () => { it('should append the new event and emit onEventsChange with the updated list', () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const existing = [ buildEvent( '1', @@ -334,13 +334,13 @@ storeClasses.forEach((storeClass) => { store.createEvent(newEvent); - expect(onEventsChange.calledOnce).to.equal(true); - const created = onEventsChange.lastCall.firstArg.find( + expect(onEventsChange).toHaveBeenCalledOnce(); + const created = onEventsChange.mock.calls[onEventsChange.mock.calls.length - 1][0].find( (event: SchedulerEvent) => event.id === '2', ); expect(created.id).to.equal('2'); expect(created.title).to.equal('New Event'); - const updated = onEventsChange.lastCall.firstArg; + const updated = onEventsChange.mock.calls[onEventsChange.mock.calls.length - 1][0]; expect(getIds(updated)).to.deep.equal(['1', '2']); const appended = updated[1]; @@ -352,7 +352,7 @@ storeClasses.forEach((storeClass) => { }); it('should throw when an event with the same id already exists and not call onEventsChange', () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const events = [ buildEvent( 'Event 1', @@ -374,7 +374,7 @@ storeClasses.forEach((storeClass) => { expect(() => store.createEvent(duplicate)).to.throw( `${store.instanceName}: an event with id="Event 1" already exists. Use updateEvent(...) instead.`, ); - expect(onEventsChange.called).to.equal(false); + expect(onEventsChange).not.toHaveBeenCalled(); }); }); }); diff --git a/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/resource.SchedulerStore.test.ts b/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/resource.SchedulerStore.test.ts index 2fdaa24838206..d4d6952492dd2 100644 --- a/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/resource.SchedulerStore.test.ts +++ b/packages/x-scheduler-headless/src/utils/SchedulerStore/tests/resource.SchedulerStore.test.ts @@ -142,7 +142,7 @@ storeClasses.forEach((storeClass) => { const setSpy = spy(store, 'set'); store.setVisibleResources(resourcesMap); - expect(setSpy.called).to.equal(false); + expect(setSpy).not.toHaveBeenCalled(); }); }); }); diff --git a/packages/x-scheduler/src/agenda-view/AgendaView.test.tsx b/packages/x-scheduler/src/agenda-view/AgendaView.test.tsx index b385e5a7f968a..5c2cefeb410a6 100644 --- a/packages/x-scheduler/src/agenda-view/AgendaView.test.tsx +++ b/packages/x-scheduler/src/agenda-view/AgendaView.test.tsx @@ -24,7 +24,7 @@ describe('', () => { ); await user.click(screen.getByRole('button', { name: /previous agenda/i })); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime( + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime( adapter.addDays(DEFAULT_TESTING_VISIBLE_DATE, -12), ); }); @@ -42,7 +42,7 @@ describe('', () => { ); await user.click(screen.getByRole('button', { name: /next agenda/i })); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime( + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime( adapter.addDays(DEFAULT_TESTING_VISIBLE_DATE, 12), ); }); diff --git a/packages/x-scheduler/src/day-view/DayView.test.tsx b/packages/x-scheduler/src/day-view/DayView.test.tsx index 11fe9f5f6d452..5c5120709bbc7 100644 --- a/packages/x-scheduler/src/day-view/DayView.test.tsx +++ b/packages/x-scheduler/src/day-view/DayView.test.tsx @@ -24,7 +24,7 @@ describe('', () => { ); await user.click(screen.getByRole('button', { name: /previous day/i })); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime( + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime( adapter.addDays(DEFAULT_TESTING_VISIBLE_DATE, -1), ); }); @@ -42,7 +42,7 @@ describe('', () => { ); await user.click(screen.getByRole('button', { name: /next day/i })); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime( + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime( adapter.addDays(DEFAULT_TESTING_VISIBLE_DATE, 1), ); }); diff --git a/packages/x-scheduler/src/internals/components/event-popover/EventPopover.test.tsx b/packages/x-scheduler/src/internals/components/event-popover/EventPopover.test.tsx index 34a07fcda25c4..0e6b9555227a8 100644 --- a/packages/x-scheduler/src/internals/components/event-popover/EventPopover.test.tsx +++ b/packages/x-scheduler/src/internals/components/event-popover/EventPopover.test.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { adapter, createOccurrenceFromEvent, @@ -83,7 +83,7 @@ describe('', () => { }); it('should call "onEventsChange" with updated values on submit', async () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const { user } = render( ', () => { await user.click(await screen.findByRole('option', { name: /work/i })); await user.click(screen.getByRole('button', { name: /save changes/i })); - expect(onEventsChange.calledOnce).to.equal(true); + expect(onEventsChange).toHaveBeenCalledOnce(); const updated = onEventsChange.firstCall.firstArg[0]; const expectedUpdatedEvent = { @@ -141,7 +141,7 @@ describe('', () => { }); it('should call "onEventsChange" with the updated values when delete button is clicked', async () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const { user } = render( @@ -150,7 +150,7 @@ describe('', () => { , ); await user.click(screen.getByRole('button', { name: /delete event/i })); - expect(onEventsChange.calledOnce).to.equal(true); + expect(onEventsChange).toHaveBeenCalledOnce(); expect(onEventsChange.firstCall.firstArg).to.deep.equal([]); }); @@ -222,7 +222,7 @@ describe('', () => { }); it('should handle a resource without an eventColor (fallback to default)', async () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const resourcesNoColor: SchedulerResource[] = [ { id: 'r1', title: 'Work', eventColor: 'blue' }, @@ -257,7 +257,7 @@ describe('', () => { }); it('should fallback to "No resource" with default color when the event has no resource', async () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const eventWithoutResource: SchedulerEvent = { ...DEFAULT_EVENT, @@ -289,7 +289,7 @@ describe('', () => { await user.click(screen.getByRole('button', { name: /save changes/i })); - expect(onEventsChange.calledOnce).to.equal(true); + expect(onEventsChange).toHaveBeenCalledOnce(); const updated = onEventsChange.firstCall.firstArg[0]; expect(updated.resource).to.equal(undefined); }); @@ -298,7 +298,7 @@ describe('', () => { it('should change surface of the placeholder to day-grid when all-day is changed to true', async () => { const start = adapter.date('2025-05-26T07:30:00'); const end = adapter.date('2025-05-26T08:30:00'); - const handleSurfaceChange = spy(); + const handleSurfaceChange = vi.fn(); const creationOccurrence = createOccurrenceFromEvent({ id: 'tmp', @@ -345,7 +345,7 @@ describe('', () => { it('should change surface of the placeholder to time-grid when all-day is changed to false', async () => { const start = adapter.date('2025-05-26T07:30:00'); const end = adapter.date('2025-05-26T08:30:00'); - const handleSurfaceChange = spy(); + const handleSurfaceChange = vi.fn(); const creationOccurrence = createOccurrenceFromEvent({ id: 'tmp', @@ -392,7 +392,7 @@ describe('', () => { it('should not change surfaceType when all day changed to true and lockSurfaceType=true', async () => { const start = adapter.date('2025-05-26T07:30:00'); const end = adapter.date('2025-05-26T08:30:00'); - const handleSurfaceChange = spy(); + const handleSurfaceChange = vi.fn(); const creationOccurrence = createOccurrenceFromEvent({ id: 'tmp', @@ -456,7 +456,7 @@ describe('', () => { allDay: false, }); - const onEventsChange = spy(); + const onEventsChange = vi.fn(); let createEventSpy; const { user } = render( @@ -488,8 +488,8 @@ describe('', () => { await user.click(await screen.findByRole('option', { name: /daily/i })); await user.click(screen.getByRole('button', { name: /save changes/i })); - expect(createEventSpy?.calledOnce).to.equal(true); - const payload = createEventSpy.lastCall.firstArg; + expect(createEventSpy?).toHaveBeenCalledOnce(); + const payload = createEventSpy.mock.calls[createEventSpy.mock.calls.length - 1][0]; expect(payload.id).to.be.a('string'); expect(payload.title).to.equal('New title'); @@ -555,8 +555,8 @@ describe('', () => { await user.click(screen.getByLabelText(/All events in the series/i)); await user.click(screen.getByRole('button', { name: /Cancel/i })); - expect(updateRecurringEventSpy?.calledOnce).to.equal(true); - expect(selectRecurringEventUpdateScopeSpy?.called).to.equal(true); + expect(updateRecurringEventSpy?).toHaveBeenCalledOnce(); + expect(selectRecurringEventUpdateScopeSpy?).toHaveBeenCalled(); expect(selectRecurringEventUpdateScopeSpy?.lastCall.firstArg).to.equal(null); expect(updateRecurringEventSpy?.callCount).to.equal(1); }); @@ -604,8 +604,8 @@ describe('', () => { await user.click(screen.getByLabelText(/All events in the series/i)); await user.click(screen.getByRole('button', { name: /Confirm/i })); - expect(updateRecurringEventSpy?.calledOnce).to.equal(true); - const openPayload = updateRecurringEventSpy.lastCall.firstArg; + expect(updateRecurringEventSpy?).toHaveBeenCalledOnce(); + const openPayload = updateRecurringEventSpy.mock.calls[updateRecurringEventSpy.mock.calls.length - 1][0]; expect(openPayload.changes.id).to.equal(originalRecurringEvent.id); expect(openPayload.changes.title).to.equal('Daily standup'); @@ -615,7 +615,7 @@ describe('', () => { expect(openPayload.changes.end).to.toEqualDateTime(adapter.date('2025-06-11T10:35:00')); expect(openPayload.changes).to.not.have.property('rrule'); - expect(selectRecurringEventUpdateScopeSpy?.calledOnce).to.equal(true); + expect(selectRecurringEventUpdateScopeSpy?).toHaveBeenCalledOnce(); expect(selectRecurringEventUpdateScopeSpy?.lastCall.firstArg).to.equal('all'); }); @@ -661,8 +661,8 @@ describe('', () => { await user.click(screen.getByLabelText(/Only this event/i)); await user.click(screen.getByRole('button', { name: /Confirm/i })); - expect(updateRecurringEventSpy?.calledOnce).to.equal(true); - const openPayload = updateRecurringEventSpy.lastCall.firstArg; + expect(updateRecurringEventSpy?).toHaveBeenCalledOnce(); + const openPayload = updateRecurringEventSpy.mock.calls[updateRecurringEventSpy.mock.calls.length - 1][0]; expect(openPayload.changes.id).to.equal(originalRecurringEvent.id); expect(openPayload.changes.title).to.equal(originalRecurringEventOccurrence.title); @@ -675,7 +675,7 @@ describe('', () => { interval: 1, byDay: ['WE'], }); - expect(selectRecurringEventUpdateScopeSpy?.calledOnce).to.equal(true); + expect(selectRecurringEventUpdateScopeSpy?).toHaveBeenCalledOnce(); expect(selectRecurringEventUpdateScopeSpy?.lastCall.firstArg).to.equal('only-this'); }); @@ -721,13 +721,13 @@ describe('', () => { await user.click(screen.getByLabelText(/This and following events/i)); await user.click(screen.getByRole('button', { name: /Confirm/i })); - expect(updateRecurringEventSpy?.calledOnce).to.equal(true); - const openPayload = updateRecurringEventSpy.lastCall.firstArg; + expect(updateRecurringEventSpy?).toHaveBeenCalledOnce(); + const openPayload = updateRecurringEventSpy.mock.calls[updateRecurringEventSpy.mock.calls.length - 1][0]; expect(openPayload.changes.id).to.equal(originalRecurringEvent.id); expect(openPayload.changes.rrule).to.equal(undefined); - expect(selectRecurringEventUpdateScopeSpy?.calledOnce).to.equal(true); + expect(selectRecurringEventUpdateScopeSpy?).toHaveBeenCalledOnce(); expect(selectRecurringEventUpdateScopeSpy?.lastCall.firstArg).to.equal( 'this-and-following', ); @@ -788,7 +788,7 @@ describe('', () => { }); it('should submit custom recurrence with Ends: after', async () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const { user } = render( ', () => { await user.click(screen.getByRole('button', { name: /save changes/i })); - expect(onEventsChange.calledOnce).to.equal(true); + expect(onEventsChange).toHaveBeenCalledOnce(); const updated = onEventsChange.firstCall.firstArg[0]; expect(updated.rrule).to.deep.equal({ @@ -843,7 +843,7 @@ describe('', () => { }); it('should submit custom recurrence with Ends: never', async () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const { user } = render( ', () => { await user.click(screen.getByRole('button', { name: /save changes/i })); - expect(onEventsChange.calledOnce).to.equal(true); + expect(onEventsChange).toHaveBeenCalledOnce(); const updated = onEventsChange.firstCall.firstArg[0]; expect(updated.rrule).to.deep.equal({ @@ -892,7 +892,7 @@ describe('', () => { }); it('should submit custom recurrence with Ends: until and selected date', async () => { - const onEventsChange = spy(); + const onEventsChange = vi.fn(); const { user } = render( ', () => { await user.click(screen.getByRole('button', { name: /save changes/i })); - expect(onEventsChange.calledOnce).to.equal(true); + expect(onEventsChange).toHaveBeenCalledOnce(); const updated = onEventsChange.firstCall.firstArg[0]; expect(updated.rrule).to.deep.include({ freq: 'YEARLY', interval: 3 }); @@ -978,8 +978,8 @@ describe('', () => { await user.click(await screen.findByRole('option', { name: /work/i })); await user.click(screen.getByRole('button', { name: /save changes/i })); - expect(updateEventSpy?.calledOnce).to.equal(true); - const payload = updateEventSpy.lastCall.firstArg; + expect(updateEventSpy?).toHaveBeenCalledOnce(); + const payload = updateEventSpy.mock.calls[updateEventSpy.mock.calls.length - 1][0]; expect(payload.id).to.equal(nonRecurringEvent.id); expect(payload.title).to.equal('Task updated'); @@ -1013,8 +1013,8 @@ describe('', () => { await user.click(await screen.findByRole('option', { name: /repeats daily/i })); await user.click(screen.getByRole('button', { name: /save changes/i })); - expect(updateEventSpy?.calledOnce).to.equal(true); - const payload = updateEventSpy.lastCall.firstArg; + expect(updateEventSpy?).toHaveBeenCalledOnce(); + const payload = updateEventSpy.mock.calls[updateEventSpy.mock.calls.length - 1][0]; expect(payload.id).to.equal(nonRecurringEvent.id); expect(payload.rrule).to.deep.equal({ diff --git a/packages/x-scheduler/src/month-view/MonthView.test.tsx b/packages/x-scheduler/src/month-view/MonthView.test.tsx index 0e2d045e318c0..edc8f7ca43e51 100644 --- a/packages/x-scheduler/src/month-view/MonthView.test.tsx +++ b/packages/x-scheduler/src/month-view/MonthView.test.tsx @@ -67,9 +67,9 @@ describe('', () => { const button = screen.getByRole('button', { name: '15' }); await user.click(button); - expect(handleViewChange.calledOnce).to.equal(true); + expect(handleViewChange).toHaveBeenCalledOnce(); expect(handleViewChange.firstCall.firstArg).to.equal('day'); - expect(handleVisibleDateChange.calledOnce).to.equal(true); + expect(handleVisibleDateChange).toHaveBeenCalledOnce(); expect(handleVisibleDateChange.firstCall.firstArg).toEqualDateTime( adapter.date('2025-05-15T00:00:00'), ); @@ -286,7 +286,7 @@ describe('', () => { ); await user.click(screen.getByRole('button', { name: /previous month/i })); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime( + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime( adapter.addMonths(adapter.startOfMonth(DEFAULT_TESTING_VISIBLE_DATE), -1), ); }); @@ -304,7 +304,7 @@ describe('', () => { ); await user.click(screen.getByRole('button', { name: /next month/i })); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime( + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime( adapter.addMonths(adapter.startOfMonth(DEFAULT_TESTING_VISIBLE_DATE), 1), ); }); diff --git a/packages/x-scheduler/src/week-view/WeekView.test.tsx b/packages/x-scheduler/src/week-view/WeekView.test.tsx index bf19ff1c8158f..af053e059872f 100644 --- a/packages/x-scheduler/src/week-view/WeekView.test.tsx +++ b/packages/x-scheduler/src/week-view/WeekView.test.tsx @@ -174,7 +174,7 @@ describe('', () => { ); await user.click(screen.getByRole('button', { name: /previous week/i })); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime( + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime( adapter.addWeeks(adapter.startOfWeek(DEFAULT_TESTING_VISIBLE_DATE), -1), ); }); @@ -192,7 +192,7 @@ describe('', () => { ); await user.click(screen.getByRole('button', { name: /next week/i })); - expect(onVisibleDateChange.lastCall.firstArg).toEqualDateTime( + expect(onVisibleDateChange.mock.calls[onVisibleDateChange.mock.calls.length - 1][0]).toEqualDateTime( adapter.addWeeks(adapter.startOfWeek(DEFAULT_TESTING_VISIBLE_DATE), 1), ); }); diff --git a/packages/x-tree-view-pro/src/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.test.tsx b/packages/x-tree-view-pro/src/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.test.tsx index b320c6fd8cf80..3f451881c9168 100644 --- a/packages/x-tree-view-pro/src/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.test.tsx +++ b/packages/x-tree-view-pro/src/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.test.tsx @@ -1,5 +1,5 @@ import { describeTreeView } from 'test/utils/tree-view/describeTreeView'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { fireEvent, createEvent } from '@mui/internal-test-utils'; import { DragEventTypes, MockedDataTransfer } from 'test/utils/dragAndDrop'; import { chooseActionToApply } from './useTreeViewItemsReordering.utils'; @@ -132,7 +132,7 @@ describeTreeView>( describe('onItemPositionChange prop', () => { it('should call onItemPositionChange when an item is moved', () => { - const onItemPositionChange = spy(); + const onItemPositionChange = vi.fn(); const view = render({ items: [{ id: '1' }, { id: '2' }, { id: '3' }], itemsReordering: true, @@ -140,8 +140,8 @@ describeTreeView>( }); dragEvents.fullDragSequence(view.getItemRoot('1'), view.getItemContent('2')); - expect(onItemPositionChange.callCount).to.equal(1); - expect(onItemPositionChange.lastCall.firstArg).to.deep.equal({ + expect(onItemPositionChange).toHaveBeenCalledTimes(1); + expect(onItemPositionChange.mock.calls[onItemPositionChange.mock.calls.length - 1][0]).to.deep.equal({ itemId: '1', oldPosition: { parentId: null, index: 0 }, newPosition: { parentId: '2', index: 0 }, @@ -178,7 +178,7 @@ describeTreeView>( describe('canMoveItemToNewPosition prop', () => { it('should call canMoveItemToNewPosition with the correct parameters', () => { - const canMoveItemToNewPosition = spy(); + const canMoveItemToNewPosition = vi.fn(); const view = render({ items: [{ id: '1' }, { id: '2' }, { id: '3' }], itemsReordering: true, @@ -186,7 +186,7 @@ describeTreeView>( }); dragEvents.fullDragSequence(view.getItemRoot('1'), view.getItemContent('2')); - expect(canMoveItemToNewPosition.lastCall.firstArg).to.deep.equal({ + expect(canMoveItemToNewPosition.mock.calls[canMoveItemToNewPosition.mock.calls.length - 1][0]).to.deep.equal({ itemId: '1', oldPosition: { parentId: null, index: 0 }, newPosition: { parentId: null, index: 1 }, diff --git a/packages/x-tree-view/src/internals/plugins/useTreeViewExpansion/useTreeViewExpansion.test.tsx b/packages/x-tree-view/src/internals/plugins/useTreeViewExpansion/useTreeViewExpansion.test.tsx index 70d32b406f68a..026fdaefd4e7f 100644 --- a/packages/x-tree-view/src/internals/plugins/useTreeViewExpansion/useTreeViewExpansion.test.tsx +++ b/packages/x-tree-view/src/internals/plugins/useTreeViewExpansion/useTreeViewExpansion.test.tsx @@ -84,7 +84,7 @@ describeTreeView( fireEvent.click(view.getItemContent('1')); expect(onExpandedItemsChange.callCount).to.equal(1); - expect(onExpandedItemsChange.lastCall.args[1]).to.deep.equal(['1']); + expect(onExpandedItemsChange.mock.calls[onExpandedItemsChange.mock.calls.length - 1][1]).to.deep.equal(['1']); }); it('should call the onExpandedItemsChange callback when the model is updated (add expanded item to non-empty list)', () => { @@ -102,7 +102,7 @@ describeTreeView( fireEvent.click(view.getItemContent('2')); expect(onExpandedItemsChange.callCount).to.equal(1); - expect(onExpandedItemsChange.lastCall.args[1]).to.deep.equal(['2', '1']); + expect(onExpandedItemsChange.mock.calls[onExpandedItemsChange.mock.calls.length - 1][1]).to.deep.equal(['2', '1']); }); it('should call the onExpandedItemsChange callback when the model is updated (remove expanded item)', () => { @@ -120,7 +120,7 @@ describeTreeView( fireEvent.click(view.getItemContent('1')); expect(onExpandedItemsChange.callCount).to.equal(1); - expect(onExpandedItemsChange.lastCall.args[1]).to.deep.equal([]); + expect(onExpandedItemsChange.mock.calls[onExpandedItemsChange.mock.calls.length - 1][1]).to.deep.equal([]); }); it('should warn when switching from controlled to uncontrolled', () => { @@ -299,8 +299,8 @@ describeTreeView( fireEvent.click(view.getItemContent('1')); expect(onItemExpansionToggle.callCount).to.equal(1); - expect(onItemExpansionToggle.lastCall.args[1]).to.equal('1'); - expect(onItemExpansionToggle.lastCall.args[2]).to.equal(true); + expect(onItemExpansionToggle.mock.calls[onItemExpansionToggle.mock.calls.length - 1][1]).to.equal('1'); + expect(onItemExpansionToggle.mock.calls[onItemExpansionToggle.mock.calls.length - 1][2]).to.equal(true); }); it('should call the onItemExpansionToggle callback when collapsing an item', () => { @@ -314,8 +314,8 @@ describeTreeView( fireEvent.click(view.getItemContent('1')); expect(onItemExpansionToggle.callCount).to.equal(1); - expect(onItemExpansionToggle.lastCall.args[1]).to.equal('1'); - expect(onItemExpansionToggle.lastCall.args[2]).to.equal(false); + expect(onItemExpansionToggle.mock.calls[onItemExpansionToggle.mock.calls.length - 1][1]).to.equal('1'); + expect(onItemExpansionToggle.mock.calls[onItemExpansionToggle.mock.calls.length - 1][2]).to.equal(false); }); }); @@ -338,8 +338,8 @@ describeTreeView( expect(view.isItemExpanded('1')).to.equal(true); expect(onItemExpansionToggle.callCount).to.equal(1); - expect(onItemExpansionToggle.lastCall.args[1]).to.equal('1'); - expect(onItemExpansionToggle.lastCall.args[2]).to.equal(true); + expect(onItemExpansionToggle.mock.calls[onItemExpansionToggle.mock.calls.length - 1][1]).to.equal('1'); + expect(onItemExpansionToggle.mock.calls[onItemExpansionToggle.mock.calls.length - 1][2]).to.equal(true); }); it('should collapse an expanded item when calling the setItemExpansion method with `shouldBeExpanded=false`', () => { @@ -361,8 +361,8 @@ describeTreeView( expect(view.isItemExpanded('1')).to.equal(false); expect(onItemExpansionToggle.callCount).to.equal(1); - expect(onItemExpansionToggle.lastCall.args[1]).to.equal('1'); - expect(onItemExpansionToggle.lastCall.args[2]).to.equal(false); + expect(onItemExpansionToggle.mock.calls[onItemExpansionToggle.mock.calls.length - 1][1]).to.equal('1'); + expect(onItemExpansionToggle.mock.calls[onItemExpansionToggle.mock.calls.length - 1][2]).to.equal(false); }); it('should do nothing when calling the setItemExpansion method with `shouldBeExpanded=true` on an already expanded item', () => { diff --git a/packages/x-tree-view/src/internals/plugins/useTreeViewItems/useTreeViewItems.test.tsx b/packages/x-tree-view/src/internals/plugins/useTreeViewItems/useTreeViewItems.test.tsx index 689f23f1e89cf..5020842eac30c 100644 --- a/packages/x-tree-view/src/internals/plugins/useTreeViewItems/useTreeViewItems.test.tsx +++ b/packages/x-tree-view/src/internals/plugins/useTreeViewItems/useTreeViewItems.test.tsx @@ -96,7 +96,7 @@ describeTreeView( // Check if the internal state is updated by running a range selection fireEvent.click(view.getItemContent('1')); fireEvent.click(view.getItemContent('3'), { shiftKey: true }); - expect(onSelectedItemsChange.lastCall.args[1]).to.deep.equal(['1', '3']); + expect(onSelectedItemsChange.mock.calls[onSelectedItemsChange.mock.calls.length - 1][1]).to.deep.equal(['1', '3']); }); it('should not mark an item as expandable if its children is an empty array', () => { @@ -254,11 +254,11 @@ describeTreeView( slotProps: { item: { slots: { label: spyLabel } } }, }); - spyLabel.resetHistory(); + spyLabel.mockClear(); view.setProps({ onClick: () => {} }); - const renders = spyLabel.getCalls().map((call) => call.args[0].children); + const renders = spyLabel.getCalls().map((call) => call.mock.calls[0].children); expect(renders).to.deep.equal([]); }, ); @@ -273,11 +273,11 @@ describeTreeView( slotProps: { item: { slots: { label: spyLabel } } }, }); - spyLabel.resetHistory(); + spyLabel.mockClear(); view.setProps({ selectedItems: ['1'] }); - const renders = spyLabel.getCalls().map((call) => call.args[0].children); + const renders = spyLabel.getCalls().map((call) => call.mock.calls[0].children); // 2 renders of the 1st item to remove to tabIndex={0} // 2 renders of the selected item to change its visual state @@ -297,11 +297,11 @@ describeTreeView( slotProps: { item: { slots: { label: spyLabel } } }, }); - spyLabel.resetHistory(); + spyLabel.mockClear(); view.setProps({ onClick: () => {} }); - const renders = spyLabel.getCalls().map((call) => call.args[0].children); + const renders = spyLabel.getCalls().map((call) => call.mock.calls[0].children); expect(renders).to.deep.equal([]); }, ); @@ -320,11 +320,11 @@ describeTreeView( slotProps: { item: { slots: { label: spyLabel } } }, }); - spyLabel.resetHistory(); + spyLabel.mockClear(); view.setProps({ selectedItems: ['1'] }); - const renders = spyLabel.getCalls().map((call) => call.args[0].children); + const renders = spyLabel.getCalls().map((call) => call.mock.calls[0].children); // 2 renders of the 1st item to remove to tabIndex={0} // 2 renders of the selected item to change its visual state diff --git a/packages/x-tree-view/src/internals/plugins/useTreeViewSelection/useTreeViewSelection.test.tsx b/packages/x-tree-view/src/internals/plugins/useTreeViewSelection/useTreeViewSelection.test.tsx index 3a68add330f38..ab3cb87289e7d 100644 --- a/packages/x-tree-view/src/internals/plugins/useTreeViewSelection/useTreeViewSelection.test.tsx +++ b/packages/x-tree-view/src/internals/plugins/useTreeViewSelection/useTreeViewSelection.test.tsx @@ -77,7 +77,7 @@ describeTreeView( fireEvent.click(view.getItemContent('1')); expect(onSelectedItemsChange.callCount).to.equal(1); - expect(onSelectedItemsChange.lastCall.args[1]).to.deep.equal('1'); + expect(onSelectedItemsChange.mock.calls[onSelectedItemsChange.mock.calls.length - 1][1]).to.deep.equal('1'); }); // TODO: Re-enable this test if we have a way to un-select an item in single selection. @@ -93,7 +93,7 @@ describeTreeView( fireEvent.click(view.getItemContent('1')); expect(onSelectedItemsChange.callCount).to.equal(1); - expect(onSelectedItemsChange.lastCall.args[1]).to.deep.equal([]); + expect(onSelectedItemsChange.mock.calls[onSelectedItemsChange.mock.calls.length - 1][1]).to.deep.equal([]); }); it('should call the onSelectedItemsChange callback when the model is updated (multi selection and add selected item to empty list)', () => { @@ -108,7 +108,7 @@ describeTreeView( fireEvent.click(view.getItemContent('1')); expect(onSelectedItemsChange.callCount).to.equal(1); - expect(onSelectedItemsChange.lastCall.args[1]).to.deep.equal(['1']); + expect(onSelectedItemsChange.mock.calls[onSelectedItemsChange.mock.calls.length - 1][1]).to.deep.equal(['1']); }); it('should call the onSelectedItemsChange callback when the model is updated (multi selection and add selected item to non-empty list)', () => { @@ -124,7 +124,7 @@ describeTreeView( fireEvent.click(view.getItemContent('2'), { ctrlKey: true }); expect(onSelectedItemsChange.callCount).to.equal(1); - expect(onSelectedItemsChange.lastCall.args[1]).to.deep.equal(['2', '1']); + expect(onSelectedItemsChange.mock.calls[onSelectedItemsChange.mock.calls.length - 1][1]).to.deep.equal(['2', '1']); }); it('should call the onSelectedItemsChange callback when the model is updated (multi selection and remove selected item)', () => { @@ -140,7 +140,7 @@ describeTreeView( fireEvent.click(view.getItemContent('1'), { ctrlKey: true }); expect(onSelectedItemsChange.callCount).to.equal(1); - expect(onSelectedItemsChange.lastCall.args[1]).to.deep.equal([]); + expect(onSelectedItemsChange.mock.calls[onSelectedItemsChange.mock.calls.length - 1][1]).to.deep.equal([]); }); it('should warn when switching from controlled to uncontrolled', () => { @@ -1008,8 +1008,8 @@ describeTreeView( fireEvent.click(view.getItemContent('1')); expect(onItemSelectionToggle.callCount).to.equal(1); - expect(onItemSelectionToggle.lastCall.args[1]).to.equal('1'); - expect(onItemSelectionToggle.lastCall.args[2]).to.equal(true); + expect(onItemSelectionToggle.mock.calls[onItemSelectionToggle.mock.calls.length - 1][1]).to.equal('1'); + expect(onItemSelectionToggle.mock.calls[onItemSelectionToggle.mock.calls.length - 1][2]).to.equal(true); }); it('should call the onItemSelectionToggle callback when un-selecting an item', () => { @@ -1024,8 +1024,8 @@ describeTreeView( fireEvent.click(view.getItemContent('1'), { ctrlKey: true }); expect(onItemSelectionToggle.callCount).to.equal(1); - expect(onItemSelectionToggle.lastCall.args[1]).to.equal('1'); - expect(onItemSelectionToggle.lastCall.args[2]).to.equal(false); + expect(onItemSelectionToggle.mock.calls[onItemSelectionToggle.mock.calls.length - 1][1]).to.equal('1'); + expect(onItemSelectionToggle.mock.calls[onItemSelectionToggle.mock.calls.length - 1][2]).to.equal(false); }); }); @@ -1133,9 +1133,9 @@ describeTreeView( }); expect(onItemSelectionToggle.callCount).to.equal(1); - expect(onItemSelectionToggle.lastCall.args[0]).to.equal(event); - expect(onItemSelectionToggle.lastCall.args[1]).to.equal('1'); - expect(onItemSelectionToggle.lastCall.args[2]).to.equal(true); + expect(onItemSelectionToggle.mock.calls[onItemSelectionToggle.mock.calls.length - 1][0]).to.equal(event); + expect(onItemSelectionToggle.mock.calls[onItemSelectionToggle.mock.calls.length - 1][1]).to.equal('1'); + expect(onItemSelectionToggle.mock.calls[onItemSelectionToggle.mock.calls.length - 1][2]).to.equal(true); }); it('should call call onItemSelectionToggle callback when un-selecting an item', () => { @@ -1153,9 +1153,9 @@ describeTreeView( }); expect(onItemSelectionToggle.callCount).to.equal(1); - expect(onItemSelectionToggle.lastCall.args[0]).to.equal(event); - expect(onItemSelectionToggle.lastCall.args[1]).to.equal('1'); - expect(onItemSelectionToggle.lastCall.args[2]).to.equal(false); + expect(onItemSelectionToggle.mock.calls[onItemSelectionToggle.mock.calls.length - 1][0]).to.equal(event); + expect(onItemSelectionToggle.mock.calls[onItemSelectionToggle.mock.calls.length - 1][1]).to.equal('1'); + expect(onItemSelectionToggle.mock.calls[onItemSelectionToggle.mock.calls.length - 1][2]).to.equal(false); }); }); }); diff --git a/packages/x-tree-view/src/useTreeItem/useTreeItem.test.tsx b/packages/x-tree-view/src/useTreeItem/useTreeItem.test.tsx index bfb1c41554fa3..4226a351cdc01 100644 --- a/packages/x-tree-view/src/useTreeItem/useTreeItem.test.tsx +++ b/packages/x-tree-view/src/useTreeItem/useTreeItem.test.tsx @@ -49,7 +49,7 @@ describeTreeView( fireEvent.click(view.getItemContent('1.1')); expect(onClick.callCount).to.equal(2); expect(onClick.firstCall.firstArg.target.parentElement.dataset.testid).to.equal('1.1'); - expect(onClick.lastCall.firstArg.target.parentElement.dataset.testid).to.equal('1.1'); + expect(onClick.mock.calls[onClick.mock.calls.length - 1][0].target.parentElement.dataset.testid).to.equal('1.1'); }); it('should call onClick even when the element is disabled', () => { diff --git a/test/setupVitest.ts b/test/setupVitest.ts index 57be829995545..1e16b860de5cd 100644 --- a/test/setupVitest.ts +++ b/test/setupVitest.ts @@ -5,7 +5,7 @@ import { generateTestLicenseKey, setupTestLicenseKey } from 'test/utils/testLice import { configure } from '@mui/internal-test-utils'; import { config } from 'react-transition-group'; -import sinon from 'sinon'; +import { vi } from 'vitest'; import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingDataGrid } from '@mui/x-data-grid'; import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingDataGridPro } from '@mui/x-data-grid-pro'; import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingTreeView } from '@mui/x-tree-view'; @@ -37,9 +37,8 @@ afterEach(() => { unstable_resetCleanupTrackingDataGridPro(); unstable_resetCleanupTrackingTreeView(); - // Restore Sinon default sandbox to avoid memory leak - // See https://github.com/sinonjs/sinon/issues/1866 - sinon.restore(); + // Restore all mocks to avoid memory leak + vi.restoreAllMocks(); config.disabled = false; }); diff --git a/test/utils/helperFn.ts b/test/utils/helperFn.ts index e21f407a8beeb..17cdfb6fc22ca 100644 --- a/test/utils/helperFn.ts +++ b/test/utils/helperFn.ts @@ -1,4 +1,4 @@ -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { act, screen } from '@mui/internal-test-utils'; import { gridClasses, GridRowId } from '@mui/x-data-grid'; import { unwrapPrivateAPI } from '@mui/x-data-grid/internals'; @@ -55,7 +55,7 @@ export function spyApi(api: GridApiCommon, methodName: string) { const privateApi = unwrapPrivateAPI(api); const method = privateApi[methodKey]; - const spyFn = spy((...args: any[]) => { + const spyFn = vi.fn((...args: any[]) => { return spyFn.target(...args); }) as any; spyFn.spying = true; diff --git a/test/utils/pickers/assertions.ts b/test/utils/pickers/assertions.ts index b3e0a08dc7303..fba46f2977d48 100644 --- a/test/utils/pickers/assertions.ts +++ b/test/utils/pickers/assertions.ts @@ -1,4 +1,4 @@ -import { SinonSpy } from 'sinon'; +import { MockInstance } from 'vitest'; import { cleanText, isPickerRangeType, @@ -35,14 +35,15 @@ export const expectFieldPlaceholderV6 = ( export function expectPickerChangeHandlerValue( type: PickerComponentType | PickerRangeComponentType, - spyCallback: SinonSpy, + spyCallback: MockInstance, expectedValue: any, ) { + const lastCall = spyCallback.mock.calls[spyCallback.mock.calls.length - 1]; if (isPickerRangeType(type)) { - spyCallback.lastCall.firstArg.forEach((value: any, index: number) => { + lastCall[0].forEach((value: any, index: number) => { expect(value).to.deep.equal(expectedValue[index]); }); } else { - expect(spyCallback.lastCall.firstArg).to.deep.equal(expectedValue); + expect(lastCall[0]).to.deep.equal(expectedValue); } } diff --git a/test/utils/pickers/describePicker/describePicker.tsx b/test/utils/pickers/describePicker/describePicker.tsx index 13a42e92e4182..f7f2de3f68597 100644 --- a/test/utils/pickers/describePicker/describePicker.tsx +++ b/test/utils/pickers/describePicker/describePicker.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { screen, fireEvent, createDescribe } from '@mui/internal-test-utils'; import SvgIcon, { SvgIconProps } from '@mui/material/SvgIcon'; import { DescribePickerOptions } from './describePicker.types'; @@ -60,8 +60,8 @@ function innerDescribePicker(ElementToTest: React.ElementType, options: Describe describe('Component slot: DesktopPaper', () => { it.skipIf(hasNoView || variant !== 'desktop')('should forward onClick and onTouchStart', () => { - const handleClick = spy(); - const handleTouchStart = spy(); + const handleClick = vi.fn(); + const handleTouchStart = vi.fn(); render( { it.skipIf(hasNoView || variant !== 'desktop')('should forward onClick and onTouchStart', () => { - const handleClick = spy(); - const handleTouchStart = spy(); + const handleClick = vi.fn(); + const handleTouchStart = vi.fn(); render( { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); render(); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); act(() => { [ adapterToUse.date('2018-01-02T12:00:00'), @@ -43,13 +43,13 @@ export const testTextFieldKeyboardRangeValidation: DescribeRangeValidationTestSu setValue(date, { setEndDate: index === 1 }); }); }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['invalidRange', 'invalidRange']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal(['invalidRange', 'invalidRange']); testInvalidStatus([true, true], fieldType); }); it.skipIf(!withDate)('should apply shouldDisableDate', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( { setValue(adapterToUse.date('2018-03-13'), { setEndDate: true }); }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'shouldDisableDate']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal([null, 'shouldDisableDate']); testInvalidStatus([false, true], fieldType); act(() => { setValue(adapterToUse.date('2018-03-12')); }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([ + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal([ 'shouldDisableDate', 'shouldDisableDate', ]); @@ -93,8 +93,8 @@ export const testTextFieldKeyboardRangeValidation: DescribeRangeValidationTestSu adapterToUse.isBefore(date, adapterToUse.date('2018-03-13')), }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['shouldDisableDate', null]); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal(['shouldDisableDate', null]); testInvalidStatus([true, false], fieldType); }); describe('with fake timers', () => { @@ -107,7 +107,7 @@ export const testTextFieldKeyboardRangeValidation: DescribeRangeValidationTestSu }); it('should apply disablePast', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const now = adapterToUse.date(); render(); @@ -126,29 +126,29 @@ export const testTextFieldKeyboardRangeValidation: DescribeRangeValidationTestSu setValue(past); }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['disablePast', null]); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal(['disablePast', null]); testInvalidStatus([true, false], fieldType); act(() => { setValue(past, { setEndDate: true }); }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['disablePast', 'disablePast']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal(['disablePast', 'disablePast']); testInvalidStatus([true, true], fieldType); act(() => { setValue(now); }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'disablePast']); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal([null, 'disablePast']); testInvalidStatus([false, true], fieldType); }); }); it('should apply disableFuture', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const now = adapterToUse.date(); render(); @@ -168,29 +168,29 @@ export const testTextFieldKeyboardRangeValidation: DescribeRangeValidationTestSu setValue(future, { setEndDate: true }); }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'disableFuture']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal([null, 'disableFuture']); testInvalidStatus([false, true], fieldType); act(() => { setValue(future); }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['disableFuture', 'disableFuture']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal(['disableFuture', 'disableFuture']); testInvalidStatus([true, true], fieldType); act(() => { setValue(now); }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'disableFuture']); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal([null, 'disableFuture']); testInvalidStatus([false, true], fieldType); }); it.skipIf(!withDate)('should apply minDate', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); render(); act(() => { @@ -201,29 +201,29 @@ export const testTextFieldKeyboardRangeValidation: DescribeRangeValidationTestSu ); }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minDate', 'minDate']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal(['minDate', 'minDate']); testInvalidStatus([true, true], fieldType); act(() => { setValue(adapterToUse.date('2018-03-15')); }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'minDate']); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal([null, 'minDate']); testInvalidStatus([false, true], fieldType); act(() => { setValue(adapterToUse.date('2018-03-16'), { setEndDate: true }); }); - expect(onErrorMock.callCount).to.equal(4); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, null]); + expect(onErrorMock).toHaveBeenCalledTimes(4); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal([null, null]); testInvalidStatus([false, false], fieldType); }); it.skipIf(!withDate)('should apply maxDate', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); render(); act(() => { @@ -234,21 +234,21 @@ export const testTextFieldKeyboardRangeValidation: DescribeRangeValidationTestSu ); }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'maxDate']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal([null, 'maxDate']); testInvalidStatus([false, true], fieldType); act(() => { setValue(adapterToUse.date('2018-03-16')); }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['maxDate', 'maxDate']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal(['maxDate', 'maxDate']); testInvalidStatus([true, true], fieldType); }); it.skipIf(!withTime)('should apply minTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); render( , ); @@ -262,29 +262,29 @@ export const testTextFieldKeyboardRangeValidation: DescribeRangeValidationTestSu }); }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minTime', 'minTime']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal(['minTime', 'minTime']); testInvalidStatus([true, true], fieldType); act(() => { setValue(adapterToUse.date('2018-03-10T12:10:00'), { setEndDate: true }); }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minTime', null]); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal(['minTime', null]); testInvalidStatus([true, false], fieldType); act(() => { setValue(adapterToUse.date('2018-03-10T12:05:00')); }); - expect(onErrorMock.callCount).to.equal(4); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, null]); + expect(onErrorMock).toHaveBeenCalledTimes(4); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal([null, null]); testInvalidStatus([false, false], fieldType); }); it.skipIf(!withTime)('should apply maxTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); render( , ); @@ -298,16 +298,16 @@ export const testTextFieldKeyboardRangeValidation: DescribeRangeValidationTestSu }); }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'maxTime']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal([null, 'maxTime']); testInvalidStatus([false, true], fieldType); act(() => { setValue(adapterToUse.date('2018-03-10T12:05:00')); }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['maxTime', 'maxTime']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[onErrorMock.mock.calls.length - 1][0]).to.deep.equal(['maxTime', 'maxTime']); testInvalidStatus([true, true], fieldType); }); }); diff --git a/test/utils/pickers/describeRangeValidation/testTextFieldRangeValidation.tsx b/test/utils/pickers/describeRangeValidation/testTextFieldRangeValidation.tsx index 7e6b13779afff..2fbbe2f8f594d 100644 --- a/test/utils/pickers/describeRangeValidation/testTextFieldRangeValidation.tsx +++ b/test/utils/pickers/describeRangeValidation/testTextFieldRangeValidation.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { adapterToUse, getAllFieldInputRoot } from 'test/utils/pickers'; import { vi } from 'vitest'; import { DescribeRangeValidationTestSuite } from './describeRangeValidation.types'; @@ -32,7 +32,7 @@ export const testTextFieldRangeValidation: DescribeRangeValidationTestSuite = ( describe.skipIf(!['picker', 'field'].includes(componentFamily))('text field:', () => { it('should accept single day range', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType); }); it('should not accept end date prior to start state', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['invalidRange', 'invalidRange']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[60mock.calls.length - 1][0]).to.deep.equal(['invalidRange', 'invalidRange']); testInvalidStatus([true, true], fieldType); }); it.skipIf(!withDate)('should apply shouldDisableDate', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType); setProps({ value: [adapterToUse.date('2018-03-09'), adapterToUse.date('2018-03-13')], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'shouldDisableDate']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[84mock.calls.length - 1][0]).to.deep.equal([null, 'shouldDisableDate']); testInvalidStatus([false, true], fieldType); setProps({ value: [adapterToUse.date('2018-03-12'), adapterToUse.date('2018-03-13')], }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([ + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[92mock.calls.length - 1][0]).to.deep.equal([ 'shouldDisableDate', 'shouldDisableDate', ]); @@ -101,13 +101,13 @@ export const testTextFieldRangeValidation: DescribeRangeValidationTestSuite = ( adapterToUse.isBefore(date, adapterToUse.date('2018-03-13')), }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['shouldDisableDate', null]); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[105mock.calls.length - 1][0]).to.deep.equal(['shouldDisableDate', null]); testInvalidStatus([true, false], fieldType); }); it.skipIf(!withDate)('should apply shouldDisableDate specifically on end date', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType); setProps({ value: [adapterToUse.date('2018-03-09'), adapterToUse.date('2018-03-13')], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'shouldDisableDate']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[129mock.calls.length - 1][0]).to.deep.equal([null, 'shouldDisableDate']); testInvalidStatus([false, true], fieldType); setProps({ value: [adapterToUse.date('2018-03-12'), adapterToUse.date('2018-03-13')], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'shouldDisableDate']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[137mock.calls.length - 1][0]).to.deep.equal([null, 'shouldDisableDate']); testInvalidStatus([false, true], fieldType); setProps({ @@ -143,13 +143,13 @@ export const testTextFieldRangeValidation: DescribeRangeValidationTestSuite = ( position === 'end' ? adapterToUse.isBefore(date, adapterToUse.date('2018-03-13')) : false, }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, null]); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[147mock.calls.length - 1][0]).to.deep.equal([null, null]); testInvalidStatus([false, false], fieldType); }); it.skipIf(!withDate)('should apply shouldDisableDate specifically on start date', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType); setProps({ value: [adapterToUse.date('2018-03-09'), adapterToUse.date('2018-03-13')], }); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType); @@ -177,8 +177,8 @@ export const testTextFieldRangeValidation: DescribeRangeValidationTestSuite = ( value: [adapterToUse.date('2018-03-12'), adapterToUse.date('2018-03-13')], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['shouldDisableDate', null]); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[181mock.calls.length - 1][0]).to.deep.equal(['shouldDisableDate', null]); testInvalidStatus([true, false], fieldType); setProps({ @@ -188,7 +188,7 @@ export const testTextFieldRangeValidation: DescribeRangeValidationTestSuite = ( : false, }); - expect(onErrorMock.callCount).to.equal(1); + expect(onErrorMock).toHaveBeenCalledTimes(1); testInvalidStatus([true, false], fieldType); }); @@ -202,7 +202,7 @@ export const testTextFieldRangeValidation: DescribeRangeValidationTestSuite = ( }); it('should apply disablePast', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const now = adapterToUse.date(); function WithFakeTimer(props: any) { return ; @@ -225,22 +225,22 @@ export const testTextFieldRangeValidation: DescribeRangeValidationTestSuite = ( value: [past, now], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['disablePast', null]); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[229mock.calls.length - 1][0]).to.deep.equal(['disablePast', null]); testInvalidStatus([true, false], fieldType); setProps({ value: [past, past], }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['disablePast', 'disablePast']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[237mock.calls.length - 1][0]).to.deep.equal(['disablePast', 'disablePast']); testInvalidStatus([true, true], fieldType); }); }); it('should apply disableFuture', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const now = adapterToUse.date(); function WithFakeTimer(props: any) { return ; @@ -264,21 +264,21 @@ export const testTextFieldRangeValidation: DescribeRangeValidationTestSuite = ( value: [now, future], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'disableFuture']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[268mock.calls.length - 1][0]).to.deep.equal([null, 'disableFuture']); testInvalidStatus([false, true], fieldType); setProps({ value: [future, future], }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['disableFuture', 'disableFuture']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[276mock.calls.length - 1][0]).to.deep.equal(['disableFuture', 'disableFuture']); testInvalidStatus([true, true], fieldType); }); it.skipIf(!withDate)('should apply minDate', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minDate', 'minDate']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[291mock.calls.length - 1][0]).to.deep.equal(['minDate', 'minDate']); testInvalidStatus([true, true], fieldType); setProps({ value: [adapterToUse.date('2018-03-09'), adapterToUse.date('2018-03-15')], }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minDate', null]); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[299mock.calls.length - 1][0]).to.deep.equal(['minDate', null]); testInvalidStatus([true, false], fieldType); setProps({ value: [adapterToUse.date('2018-03-16'), adapterToUse.date('2018-03-17')], }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, null]); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[307mock.calls.length - 1][0]).to.deep.equal([null, null]); testInvalidStatus([false, false], fieldType); }); it.skipIf(!withDate)('should apply minDate when only first field is filled', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minDate', null]); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[322mock.calls.length - 1][0]).to.deep.equal(['minDate', null]); testInvalidStatus([true, false], fieldType, true); setProps({ value: [adapterToUse.date('2018-03-16'), null], }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, null]); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[330mock.calls.length - 1][0]).to.deep.equal([null, null]); testInvalidStatus([false, false], fieldType, true); }); it.skipIf(!withDate)('should apply minDate when only second field is filled', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'minDate']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[345mock.calls.length - 1][0]).to.deep.equal([null, 'minDate']); testInvalidStatus([false, true], fieldType, true); setProps({ value: [null, adapterToUse.date('2018-03-16')], }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, null]); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[353mock.calls.length - 1][0]).to.deep.equal([null, null]); testInvalidStatus([false, false], fieldType, true); }); it.skipIf(!withDate)('should apply maxDate', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType); setProps({ value: [adapterToUse.date('2018-03-15'), adapterToUse.date('2018-03-17')], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'maxDate']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[375mock.calls.length - 1][0]).to.deep.equal([null, 'maxDate']); testInvalidStatus([false, true], fieldType); setProps({ value: [adapterToUse.date('2018-03-16'), adapterToUse.date('2018-03-17')], }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['maxDate', 'maxDate']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[383mock.calls.length - 1][0]).to.deep.equal(['maxDate', 'maxDate']); testInvalidStatus([true, true], fieldType); }); it.skipIf(!withTime)('should apply minTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minTime', 'minTime']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[401mock.calls.length - 1][0]).to.deep.equal(['minTime', 'minTime']); testInvalidStatus([true, true], fieldType); setProps({ value: [adapterToUse.date('2018-03-10T09:00:00'), adapterToUse.date('2018-03-10T12:05:00')], }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minTime', null]); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[409mock.calls.length - 1][0]).to.deep.equal(['minTime', null]); testInvalidStatus([true, false], fieldType); setProps({ value: [adapterToUse.date('2018-03-10T12:15:00'), adapterToUse.date('2018-03-10T18:00:00')], }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, null]); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[417mock.calls.length - 1][0]).to.deep.equal([null, null]); testInvalidStatus([false, false], fieldType); }); it.skipIf(!withTime)('should ignore date when applying minTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minTime', 'minTime']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[434mock.calls.length - 1][0]).to.deep.equal(['minTime', 'minTime']); testInvalidStatus([true, true], fieldType); setProps({ value: [adapterToUse.date('2018-03-05T15:00:00'), adapterToUse.date('2018-03-15T16:05:00')], }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, null]); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[442mock.calls.length - 1][0]).to.deep.equal([null, null]); testInvalidStatus([false, false], fieldType); }); it.skipIf(!withTime)('should apply minTime when only first field is filled', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType, true); setProps({ value: [adapterToUse.date('2018-02-01T05:00:00'), null], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minTime', null]); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[464mock.calls.length - 1][0]).to.deep.equal(['minTime', null]); testInvalidStatus([true, false], fieldType, true); }); it.skipIf(!withTime)('should apply minTime when only second field is filled', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType, true); setProps({ value: [null, adapterToUse.date('2018-02-01T05:00:00')], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'minTime']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[486mock.calls.length - 1][0]).to.deep.equal([null, 'minTime']); testInvalidStatus([false, true], fieldType, true); }); it.skipIf(!withTime)('should apply maxTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType); setProps({ value: [adapterToUse.date('2018-03-10T09:00:00'), adapterToUse.date('2018-03-10T12:05:00')], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'maxTime']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[511mock.calls.length - 1][0]).to.deep.equal([null, 'maxTime']); testInvalidStatus([false, true], fieldType); setProps({ value: [adapterToUse.date('2018-03-10T12:15:00'), adapterToUse.date('2018-03-10T18:00:00')], }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['maxTime', 'maxTime']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[519mock.calls.length - 1][0]).to.deep.equal(['maxTime', 'maxTime']); testInvalidStatus([true, true], fieldType); }); it.skipIf(!withTime)('should ignore date when applying maxTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType); setProps({ value: [adapterToUse.date('2018-03-05T15:00:00'), adapterToUse.date('2018-03-15T16:05:00')], }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['maxTime', 'maxTime']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[544mock.calls.length - 1][0]).to.deep.equal(['maxTime', 'maxTime']); testInvalidStatus([true, true], fieldType); }); // prop only available on DateTime pickers it.skipIf(!withDate || !withTime)('should apply maxDateTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); testInvalidStatus([false, false], fieldType); setProps({ maxDateTime: adapterToUse.date('2018-03-02T08:00:00') }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, 'maxTime']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[568mock.calls.length - 1][0]).to.deep.equal([null, 'maxTime']); testInvalidStatus([false, true], fieldType); setProps({ maxDateTime: adapterToUse.date('2018-03-01T05:00:00') }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['maxTime', 'maxDate']); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[574mock.calls.length - 1][0]).to.deep.equal(['maxTime', 'maxDate']); testInvalidStatus([true, true], fieldType); }); it.skipIf(!withDate || !withTime)('should apply minDateTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minDate', 'minTime']); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[592mock.calls.length - 1][0]).to.deep.equal(['minDate', 'minTime']); testInvalidStatus([true, true], fieldType); setProps({ minDateTime: adapterToUse.date('2018-03-02T08:00:00') }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.deep.equal(['minDate', null]); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[598mock.calls.length - 1][0]).to.deep.equal(['minDate', null]); testInvalidStatus([true, false], fieldType); setProps({ minDateTime: adapterToUse.date('2018-03-01T05:00:00') }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.deep.equal([null, null]); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[604mock.calls.length - 1][0]).to.deep.equal([null, null]); testInvalidStatus([false, false], fieldType); }); }); diff --git a/test/utils/pickers/describeValidation/testDayViewValidation.tsx b/test/utils/pickers/describeValidation/testDayViewValidation.tsx index e69178d63905d..e1f6111f1e0b2 100644 --- a/test/utils/pickers/describeValidation/testDayViewValidation.tsx +++ b/test/utils/pickers/describeValidation/testDayViewValidation.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { screen } from '@mui/internal-test-utils'; import { adapterToUse } from 'test/utils/pickers'; -import { SinonFakeTimers, useFakeTimers } from 'sinon'; +import { vi } from 'vitest'; import { DescribeValidationTestSuite } from './describeValidation.types'; export const testDayViewValidation: DescribeValidationTestSuite = (ElementToTest, getOptions) => { @@ -75,12 +75,12 @@ export const testDayViewValidation: DescribeValidationTestSuite = (ElementToTest describe('with fake timers', () => { // TODO: temporary for vitest. Can move to `vi.useFakeTimers` - let timer: SinonFakeTimers | null = null; + // Vitest fake timers are used; beforeEach(() => { - timer = useFakeTimers({ now: new Date(2018, 0, 1), toFake: ['Date'] }); + vi.useFakeTimers(); vi.setSystemTime(new Date(2018, 0, 1)); }); afterEach(() => { - timer?.restore(); + vi.useRealTimers(); }); it('should apply disablePast', () => { const now = adapterToUse.date(); diff --git a/test/utils/pickers/describeValidation/testMonthViewValidation.tsx b/test/utils/pickers/describeValidation/testMonthViewValidation.tsx index ad4375a80f6f8..f66dffb5c4c2b 100644 --- a/test/utils/pickers/describeValidation/testMonthViewValidation.tsx +++ b/test/utils/pickers/describeValidation/testMonthViewValidation.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { screen } from '@mui/internal-test-utils'; import { adapterToUse } from 'test/utils/pickers'; -import { SinonFakeTimers, useFakeTimers } from 'sinon'; +import { vi } from 'vitest'; import { DescribeValidationTestSuite } from './describeValidation.types'; export const testMonthViewValidation: DescribeValidationTestSuite = (ElementToTest, getOptions) => { @@ -38,12 +38,12 @@ export const testMonthViewValidation: DescribeValidationTestSuite = (ElementToTe describe('with fake timers', () => { // TODO: temporary for vitest. Can move to `vi.useFakeTimers` - let timer: SinonFakeTimers | null = null; + // Vitest fake timers are used; beforeEach(() => { - timer = useFakeTimers({ now: new Date(2018, 0, 1), toFake: ['Date'] }); + vi.useFakeTimers(); vi.setSystemTime(new Date(2018, 0, 1)); }); afterEach(() => { - timer?.restore(); + vi.useRealTimers(); }); it('should apply disablePast', () => { diff --git a/test/utils/pickers/describeValidation/testTextFieldValidation.tsx b/test/utils/pickers/describeValidation/testTextFieldValidation.tsx index a25b93272c14b..32bbd37b5fc8e 100644 --- a/test/utils/pickers/describeValidation/testTextFieldValidation.tsx +++ b/test/utils/pickers/describeValidation/testTextFieldValidation.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { TimeView } from '@mui/x-date-pickers/models'; import { adapterToUse, getFieldInputRoot } from 'test/utils/pickers'; import { vi } from 'vitest'; @@ -12,7 +12,7 @@ export const testTextFieldValidation: DescribeValidationTestSuite = (ElementToTe it.skipIf(['picker', 'field'].includes(componentFamily) && !withDate)( 'should apply shouldDisableDate', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.equal('shouldDisableYear'); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[663mock.calls.length - 1][0]).to.equal('shouldDisableYear'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); setProps({ value: adapterToUse.date('2019-03-09') }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.equal(null); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[669mock.calls.length - 1][0]).to.equal(null); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); }); // TODO: Remove when DateTimePickers will support those props it.skipIf(!withDate)('should apply shouldDisableMonth', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.equal('shouldDisableMonth'); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[685mock.calls.length - 1][0]).to.equal('shouldDisableMonth'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); setProps({ value: adapterToUse.date('2019-03-09') }); - expect(onErrorMock.callCount).to.equal(1); + expect(onErrorMock).toHaveBeenCalledTimes(1); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); setProps({ value: adapterToUse.date('2018-04-09') }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.equal(null); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[696mock.calls.length - 1][0]).to.equal(null); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); }); it.skipIf(!withTime)('should apply shouldDisableTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.equal('shouldDisableTime-hours'); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[719mock.calls.length - 1][0]).to.equal('shouldDisableTime-hours'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); setProps({ value: adapterToUse.date('2019-03-12T09:05:00') }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.equal(null); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[725mock.calls.length - 1][0]).to.equal(null); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); setProps({ value: adapterToUse.date('2018-03-12T09:10:00') }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.equal('shouldDisableTime-minutes'); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[731mock.calls.length - 1][0]).to.equal('shouldDisableTime-minutes'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); setProps({ value: adapterToUse.date('2018-03-12T09:09:00') }); - expect(onErrorMock.callCount).to.equal(4); - expect(onErrorMock.lastCall.args[0]).to.equal(null); + expect(onErrorMock).toHaveBeenCalledTimes(4); + expect(onErrorMock.mock.calls[737mock.calls.length - 1][0]).to.equal(null); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); setProps({ value: adapterToUse.date('2018-03-12T09:09:10') }); - expect(onErrorMock.callCount).to.equal(5); - expect(onErrorMock.lastCall.args[0]).to.equal('shouldDisableTime-seconds'); + expect(onErrorMock).toHaveBeenCalledTimes(5); + expect(onErrorMock.mock.calls[743mock.calls.length - 1][0]).to.equal('shouldDisableTime-seconds'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); }); @@ -151,25 +151,25 @@ export const testTextFieldValidation: DescribeValidationTestSuite = (ElementToTe return ; } - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render(); const tomorrow = adapterToUse.addDays(now, 1); const yesterday = adapterToUse.addDays(now, -1); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); setProps({ value: yesterday }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.equal('disablePast'); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[774mock.calls.length - 1][0]).to.equal('disablePast'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); setProps({ value: tomorrow }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.equal(null); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[780mock.calls.length - 1][0]).to.equal(null); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); }); }); @@ -180,30 +180,30 @@ export const testTextFieldValidation: DescribeValidationTestSuite = (ElementToTe return ; } - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render(); const tomorrow = adapterToUse.addDays(now, 1); const yesterday = adapterToUse.addDays(now, -1); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); setProps({ value: tomorrow }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.equal('disableFuture'); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[802mock.calls.length - 1][0]).to.equal('disableFuture'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); setProps({ value: yesterday }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.equal(null); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[807mock.calls.length - 1][0]).to.equal(null); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); }); it.skipIf(['picker', 'field'].includes(componentFamily) && !withDate)( 'should apply minDate', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); if (withTime) { - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.equal('minTime'); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[882mock.calls.length - 1][0]).to.equal('minTime'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); setProps({ value: adapterToUse.date('2019-06-15T13:10:00') }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.equal(null); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[888mock.calls.length - 1][0]).to.equal(null); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); } else { - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); } }, @@ -289,7 +289,7 @@ export const testTextFieldValidation: DescribeValidationTestSuite = (ElementToTe it.skipIf(['picker', 'field'].includes(componentFamily) && !withTime)( 'should apply maxTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); if (withTime) { - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); setProps({ value: adapterToUse.date('2019-06-15T13:10:00') }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.equal('maxTime'); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[915mock.calls.length - 1][0]).to.equal('maxTime'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); } else { - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); } }, ); it.skipIf(!withDate || !withTime)('should apply maxDateTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.equal('maxTime'); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[934mock.calls.length - 1][0]).to.equal('maxTime'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); // Test 5 minutes before setProps({ value: adapterToUse.date('2019-06-15T11:55:00') }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.equal(null); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[940mock.calls.length - 1][0]).to.equal(null); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); // Test 1 day before setProps({ value: adapterToUse.date('2019-06-14T20:10:00') }); - expect(onErrorMock.callCount).to.equal(2); + expect(onErrorMock).toHaveBeenCalledTimes(2); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); // Test 1 day after setProps({ value: adapterToUse.date('2019-06-16T10:00:00') }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.equal('maxDate'); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[951mock.calls.length - 1][0]).to.equal('maxDate'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); }); it.skipIf(!withDate || !withTime)('should apply minDateTime', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); // Test 5 minutes before (invalid) setProps({ value: adapterToUse.date('2019-06-15T11:55:00') }); - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.equal('minTime'); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[970mock.calls.length - 1][0]).to.equal('minTime'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); // Test 1 day before (invalid) setProps({ value: adapterToUse.date('2019-06-14T20:10:00') }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.equal('minDate'); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[976mock.calls.length - 1][0]).to.equal('minDate'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); // Test 1 day after setProps({ value: adapterToUse.date('2019-06-16T10:00:00') }); - expect(onErrorMock.callCount).to.equal(3); - expect(onErrorMock.lastCall.args[0]).to.equal(null); + expect(onErrorMock).toHaveBeenCalledTimes(3); + expect(onErrorMock.mock.calls[982mock.calls.length - 1][0]).to.equal(null); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); }); it.skipIf(['picker', 'field'].includes(componentFamily) && !withTime)( 'should apply minutesStep', () => { - const onErrorMock = spy(); + const onErrorMock = vi.fn(); const { setProps } = render( , ); if (withTime) { - expect(onErrorMock.callCount).to.equal(1); - expect(onErrorMock.lastCall.args[0]).to.equal('minutesStep'); + expect(onErrorMock).toHaveBeenCalledTimes(1); + expect(onErrorMock.mock.calls[999mock.calls.length - 1][0]).to.equal('minutesStep'); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'true'); setProps({ value: adapterToUse.date('2019-06-15T10:30:00') }); - expect(onErrorMock.callCount).to.equal(2); - expect(onErrorMock.lastCall.args[0]).to.equal(null); + expect(onErrorMock).toHaveBeenCalledTimes(2); + expect(onErrorMock.mock.calls[1005mock.calls.length - 1][0]).to.equal(null); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); } else { - expect(onErrorMock.callCount).to.equal(0); + expect(onErrorMock).toHaveBeenCalledTimes(0); expect(getFieldInputRoot()).to.have.attribute('aria-invalid', 'false'); } }, diff --git a/test/utils/pickers/describeValidation/testYearViewValidation.tsx b/test/utils/pickers/describeValidation/testYearViewValidation.tsx index 0865759e7a813..27e0e49513667 100644 --- a/test/utils/pickers/describeValidation/testYearViewValidation.tsx +++ b/test/utils/pickers/describeValidation/testYearViewValidation.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { screen } from '@mui/internal-test-utils'; import { adapterToUse } from 'test/utils/pickers'; -import { SinonFakeTimers, useFakeTimers } from 'sinon'; +import { vi } from 'vitest'; import { DescribeValidationTestSuite } from './describeValidation.types'; const queryByTextInView = (text: string) => { @@ -50,12 +50,12 @@ export const testYearViewValidation: DescribeValidationTestSuite = (ElementToTes describe('with fake timers', () => { // TODO: temporary for vitest. Can move to `vi.useFakeTimers` - let timer: SinonFakeTimers | null = null; + // Vitest fake timers are used; beforeEach(() => { - timer = useFakeTimers({ now: new Date(2018, 0, 1), toFake: ['Date'] }); + vi.useFakeTimers(); vi.setSystemTime(new Date(2018, 0, 1)); }); afterEach(() => { - timer?.restore(); + vi.useRealTimers(); }); it('should apply disablePast', () => { const now = adapterToUse.date(); diff --git a/test/utils/pickers/describeValue/testControlledUnControlled.tsx b/test/utils/pickers/describeValue/testControlledUnControlled.tsx index aa756367e3aac..d13dd7db0813a 100644 --- a/test/utils/pickers/describeValue/testControlledUnControlled.tsx +++ b/test/utils/pickers/describeValue/testControlledUnControlled.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { screen } from '@mui/internal-test-utils'; import { inputBaseClasses } from '@mui/material/InputBase'; import { @@ -116,7 +116,7 @@ export const testControlledUnControlled: DescribeValueTestSuite = ( }); it('should call onChange when updating a value defined with `props.defaultValue` and update the rendered value', () => { - const onChange = spy(); + const onChange = vi.fn(); const v7Response = renderWithProps({ enableAccessibleFieldDOMStructure: true, @@ -131,18 +131,18 @@ export const testControlledUnControlled: DescribeValueTestSuite = ( assertRenderedValue(newValue); // // TODO: Clean this exception or change the clock behavior - // expect(onChange.callCount).to.equal(getExpectedOnChangeCount(componentFamily, params)); + // expect(onChange).toHaveBeenCalledTimes(getExpectedOnChangeCount(componentFamily, params)); // if (Array.isArray(newValue)) { // newValue.forEach((value, index) => { - // expect(onChange.lastCall.args[0][index]).toEqualDateTime(value); + // expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][index]).toEqualDateTime(value); // }); // } else { - // expect(onChange.lastCall.args[0]).toEqualDateTime(newValue as any); + // expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(newValue as any); // } }); it('should call onChange when updating a value defined with `props.value`', () => { - const onChange = spy(); + const onChange = vi.fn(); const useControlledElement = (props: any) => { const [value, setValue] = React.useState(props?.value || null); @@ -166,13 +166,13 @@ export const testControlledUnControlled: DescribeValueTestSuite = ( closeMobilePicker: true, }); - expect(onChange.callCount).to.equal(getExpectedOnChangeCount(componentFamily, params)); + expect(onChange).toHaveBeenCalledTimes(getExpectedOnChangeCount(componentFamily, params)); if (Array.isArray(newValue)) { newValue.forEach((value, index) => { - expect(onChange.lastCall.args[0][index]).toEqualDateTime(value); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][index]).toEqualDateTime(value); }); } else { - expect(onChange.lastCall.args[0]).toEqualDateTime(newValue as any); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(newValue as any); } }); }); @@ -217,7 +217,7 @@ export const testControlledUnControlled: DescribeValueTestSuite = ( return; } - const handleChange = spy(); + const handleChange = vi.fn(); const v7Response = renderWithProps({ enableAccessibleFieldDOMStructure: true, @@ -226,7 +226,7 @@ export const testControlledUnControlled: DescribeValueTestSuite = ( }); v7Response.selectSection(undefined); fireUserEvent.keyPress(v7Response.getActiveSection(0), { key: 'ArrowUp' }); - expect(handleChange.callCount).to.equal(isPickerSingleInput(params) ? 1 : 0); + expect(handleChange).toHaveBeenCalledTimes(isPickerSingleInput(params) ? 1 : 0); }); it('should have correct labelledby relationship when toolbar is shown', () => { diff --git a/test/utils/pickers/describeValue/testPickerActionBar.tsx b/test/utils/pickers/describeValue/testPickerActionBar.tsx index 50afced85a05f..0aeb12f6a6ab8 100644 --- a/test/utils/pickers/describeValue/testPickerActionBar.tsx +++ b/test/utils/pickers/describeValue/testPickerActionBar.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { fireEvent, screen } from '@mui/internal-test-utils'; import { PickerRangeValue } from '@mui/x-date-pickers/internals'; import { @@ -34,9 +34,9 @@ export const testPickerActionBar: DescribeValueTestSuite = ( describe('Picker action bar', () => { describe('clear action', () => { it('should call onClose, onChange with empty value and onAccept with empty value', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( = ( // Clear the date fireEvent.click(screen.getByText(/clear/i)); - expect(onChange.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); expectPickerChangeHandlerValue(pickerParams.type, onChange, emptyValue); - expect(onAccept.callCount).to.equal(1); + expect(onAccept).toHaveBeenCalledTimes(1); expectPickerChangeHandlerValue(pickerParams.type, onAccept, emptyValue); - expect(onClose.callCount).to.equal(1); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should not call onChange or onAccept if the value is already empty value', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( = ( // Clear the date fireEvent.click(screen.getByText(/clear/i)); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(1); }); }); describe('cancel action', () => { it('should call onClose and onChange with the initial value', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { selectSection, pressKey } = renderWithProps({ enableAccessibleFieldDOMStructure: true, @@ -104,24 +104,24 @@ export const testPickerActionBar: DescribeValueTestSuite = ( // Cancel the modifications fireEvent.click(screen.getByText(/cancel/i)); - expect(onChange.callCount).to.equal( + expect(onChange).toHaveBeenCalledTimes( getExpectedOnChangeCount(componentFamily, pickerParams) + 1, ); if (isRangeType) { (values[0] as PickerRangeValue).forEach((value, index) => { - expect(onChange.lastCall.args[0][index]).toEqualDateTime(value); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][index]).toEqualDateTime(value); }); } else { - expect(onChange.lastCall.args[0]).toEqualDateTime(values[0] as any); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(values[0] as any); } - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(1); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should not call onChange if no prior value modification', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( = ( // Cancel the modifications fireEvent.click(screen.getByText(/cancel/i)); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(1); }); }); describe('confirm action', () => { it('should call onClose and onAccept with the live value', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { selectSection, pressKey } = renderWithProps({ enableAccessibleFieldDOMStructure: true, @@ -165,17 +165,17 @@ export const testPickerActionBar: DescribeValueTestSuite = ( // Accept the modifications fireEvent.click(screen.getAllByRole('button', { name: 'OK' })[0]); - expect(onChange.callCount).to.equal( + expect(onChange).toHaveBeenCalledTimes( getExpectedOnChangeCount(componentFamily, pickerParams), ); // The accepted value as already been committed, don't call onChange again - expect(onAccept.callCount).to.equal(1); - expect(onClose.callCount).to.equal(1); + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onChange, onClose and onAccept when validating the default value', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( = ( // Accept the modifications fireEvent.click(screen.getByText(/ok/i)); - expect(onChange.callCount).to.equal(1); - expect(onAccept.callCount).to.equal(1); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onClose but not onAccept when validating an already validated value', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( = ( // Accept the modifications fireEvent.click(screen.getByText(/ok/i)); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(1); }); }); @@ -231,9 +231,9 @@ export const testPickerActionBar: DescribeValueTestSuite = ( }); it("should call onClose, onChange with today's value and onAccept with today's value", () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( = ( startOfToday = adapterToUse.date(); } - expect(onChange.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); expectPickerChangeHandlerValue(pickerParams.type, onChange, startOfToday); - expect(onAccept.callCount).to.equal(1); + expect(onAccept).toHaveBeenCalledTimes(1); expectPickerChangeHandlerValue(pickerParams.type, onAccept, startOfToday); - expect(onClose.callCount).to.equal(1); + expect(onClose).toHaveBeenCalledTimes(1); }); }); }); diff --git a/test/utils/pickers/describeValue/testPickerOpenCloseLifeCycle.tsx b/test/utils/pickers/describeValue/testPickerOpenCloseLifeCycle.tsx index ac0384d06755f..6c68b929d46d8 100644 --- a/test/utils/pickers/describeValue/testPickerOpenCloseLifeCycle.tsx +++ b/test/utils/pickers/describeValue/testPickerOpenCloseLifeCycle.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { config } from 'react-transition-group'; import { fireEvent, screen, waitFor } from '@mui/internal-test-utils'; import { PickerRangeValue, PickerValidValue } from '@mui/x-date-pickers/internals'; @@ -41,25 +41,25 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite { - const onOpen = spy(); + const onOpen = vi.fn(); render(); openPicker(pickerParams); - expect(onOpen.callCount).to.equal(0); + expect(onOpen).toHaveBeenCalledTimes(0); }); it('should not open when `prop.readOnly` is true ', () => { - const onOpen = spy(); + const onOpen = vi.fn(); render(); openPicker(pickerParams); - expect(onOpen.callCount).to.equal(0); + expect(onOpen).toHaveBeenCalledTimes(0); }); it('should call onChange, onClose and onAccept (if Desktop Date Picker or Desktop Date Range Picker) when selecting a value', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { selectSection, pressKey } = renderWithProps( { @@ -73,13 +73,13 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite { - expect(onChange.lastCall.args[0][index]).toEqualDateTime(value); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][index]).toEqualDateTime(value); }); } else { - expect(onChange.lastCall.args[0]).toEqualDateTime(newValue); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(newValue); } - expect(onAccept.callCount).to.equal(!shouldCloseOnSelect ? 0 : 1); - expect(onClose.callCount).to.equal(!shouldCloseOnSelect ? 0 : 1); + expect(onAccept).toHaveBeenCalledTimes(!shouldCloseOnSelect ? 0 : 1); + expect(onClose).toHaveBeenCalledTimes(!shouldCloseOnSelect ? 0 : 1); }); it.skipIf(pickerParams.variant !== 'mobile')( @@ -114,9 +114,9 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { selectSection, pressKey } = renderWithProps( { @@ -131,13 +131,13 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite { - expect(onChange.lastCall.args[0][index]).toEqualDateTime(value); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][index]).toEqualDateTime(value); }); } else { - expect(onChange.lastCall.args[0]).toEqualDateTime(newValue); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(newValue); } - expect(onAccept.callCount).to.equal(1); - expect(onClose.callCount).to.equal(1); + expect(onAccept).toHaveBeenCalledTimes(1); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should not call onChange or onAccept when selecting the same value', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { selectSection, pressKey } = renderWithProps( { @@ -185,15 +185,15 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { selectSection, pressKey } = renderWithProps( { @@ -211,7 +211,7 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite { - expect(onChange.lastCall.args[0][index]).toEqualDateTime(value); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][index]).toEqualDateTime(value); }); } else { - expect(onChange.lastCall.args[0]).toEqualDateTime(newValue); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(newValue); } - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); // Change the value let newValueBis = setNewValue(newValue, { isOpened: true, selectSection, pressKey }); if (isRangeType) { - expect(onChange.callCount).to.equal( + expect(onChange).toHaveBeenCalledTimes( initialChangeCount + getExpectedOnChangeCount(componentFamily, pickerParams) * 2 - (pickerParams.type === 'date-time-range' || pickerParams.type === 'time-range' ? 1 : 0), @@ -243,25 +243,25 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite { - expect(onChange.lastCall.args[0][index]).toEqualDateTime(value); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][index]).toEqualDateTime(value); }); } else { - expect(onChange.callCount).to.equal( + expect(onChange).toHaveBeenCalledTimes( initialChangeCount + getExpectedOnChangeCount(componentFamily, pickerParams) - // meridiem does not change this time in case of multi section digital clock (pickerParams.type === 'time' || pickerParams.type === 'date-time' ? 1 : 0), ); - expect(onChange.lastCall.args[0]).toEqualDateTime(newValueBis); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(newValueBis); } - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); }); it('should call onClose and onAccept with the live value when pressing Escape', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { selectSection, pressKey, user } = renderWithProps( { @@ -281,25 +281,25 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite { - expect(onChange.lastCall.args[0][index]).toEqualDateTime(value); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0][index]).toEqualDateTime(value); }); } else { - expect(onChange.lastCall.args[0]).toEqualDateTime(newValue); + expect(onChange.mock.calls[onChange.mock.calls.length - 1][0]).toEqualDateTime(newValue); } - expect(onClose.callCount).to.equal(1); + expect(onClose).toHaveBeenCalledTimes(1); }); // TODO: Fix this test and enable it on mobile and date-range it.skipIf(pickerParams.variant === 'mobile' || isRangeType)( 'should call onClose when clicking outside of the picker without prior change', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { selectSection, pressKey } = renderWithProps( { @@ -346,19 +346,19 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render(); // Dismiss the picker fireEvent.keyDown(document.body, { key: 'Escape' }); - expect(onChange.callCount).to.equal(0); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onChange).toHaveBeenCalledTimes(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); }); }); @@ -443,9 +443,9 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); const { user } = renderWithProps( { @@ -463,9 +463,9 @@ export const testPickerOpenCloseLifeCycle: DescribeValueTestSuite expect(screen.queryByRole(viewWrapperRole)).to.equal(null)); }, ); diff --git a/test/utils/pickers/describeValue/testShortcuts.tsx b/test/utils/pickers/describeValue/testShortcuts.tsx index 39d4bd37d1b23..0eec6f0a82128 100644 --- a/test/utils/pickers/describeValue/testShortcuts.tsx +++ b/test/utils/pickers/describeValue/testShortcuts.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy } from 'sinon'; +import { vi } from 'vitest'; import { expectPickerChangeHandlerValue } from 'test/utils/pickers'; import { fireEvent, screen } from '@mui/internal-test-utils'; import { DescribeValueTestSuite } from './describeValue.types'; @@ -17,9 +17,9 @@ export const testShortcuts: DescribeValueTestSuite = (ElementToTe describe.skipIf(componentFamily !== 'picker')('Picker shortcuts', () => { it('should call onClose, onChange and onAccept when picking a shortcut without explicit changeImportance', async () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( = (ElementToTe fireEvent.click(shortcut); - expect(onChange.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); expectPickerChangeHandlerValue(pickerParams.type, onChange, values[1]); - expect(onAccept.callCount).to.equal(1); + expect(onAccept).toHaveBeenCalledTimes(1); expectPickerChangeHandlerValue(pickerParams.type, onAccept, values[1]); - expect(onClose.callCount).to.equal(1); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onClose and onChange when picking a shortcut with changeImportance="accept"', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( = (ElementToTe const shortcut = screen.getByRole('button', { name: 'Test shortcut' }); fireEvent.click(shortcut); - expect(onChange.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); expectPickerChangeHandlerValue(pickerParams.type, onChange, values[1]); - expect(onAccept.callCount).to.equal(1); + expect(onAccept).toHaveBeenCalledTimes(1); expectPickerChangeHandlerValue(pickerParams.type, onAccept, values[1]); - expect(onClose.callCount).to.equal(1); + expect(onClose).toHaveBeenCalledTimes(1); }); it('should call onClose and onChange when picking a shortcut with changeImportance="set"', () => { - const onChange = spy(); - const onAccept = spy(); - const onClose = spy(); + const onChange = vi.fn(); + const onAccept = vi.fn(); + const onClose = vi.fn(); render( = (ElementToTe const shortcut = screen.getByRole('button', { name: 'Test shortcut' }); fireEvent.click(shortcut); - expect(onChange.callCount).to.equal(1); + expect(onChange).toHaveBeenCalledTimes(1); expectPickerChangeHandlerValue(pickerParams.type, onChange, values[1]); - expect(onAccept.callCount).to.equal(0); - expect(onClose.callCount).to.equal(0); + expect(onAccept).toHaveBeenCalledTimes(0); + expect(onClose).toHaveBeenCalledTimes(0); }); }); }; diff --git a/test/utils/pickers/misc.ts b/test/utils/pickers/misc.ts index 040e263f82111..7e88e7b25ed9a 100644 --- a/test/utils/pickers/misc.ts +++ b/test/utils/pickers/misc.ts @@ -1,4 +1,4 @@ -import sinon from 'sinon'; +import { vi } from 'vitest'; import { MuiPickersAdapter, PickerValidDate } from '@mui/x-date-pickers/models'; import { onTestFinished } from 'vitest'; import { PickerComponentFamily } from './describe.types'; @@ -6,7 +6,7 @@ import { OpenPickerParams } from './openPicker'; export const stubMatchMedia = (matches = true) => { const original = window.matchMedia; - window.matchMedia = sinon.stub().returns({ + window.matchMedia = vi.fn().mockReturnValue({ matches, addListener: () => {}, addEventListener: () => {}, diff --git a/test/utils/scheduler/StoreSpy.tsx b/test/utils/scheduler/StoreSpy.tsx index 52f23759ce74b..a37c4dcc4b754 100644 --- a/test/utils/scheduler/StoreSpy.tsx +++ b/test/utils/scheduler/StoreSpy.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { spy as sinonSpy, SinonSpy } from 'sinon'; +import { vi, MockInstance } from 'vitest'; export function StoreSpy({ Context, @@ -8,25 +8,25 @@ export function StoreSpy({ }: { Context: React.Context; method: Extract; - onSpyReady: (sp: SinonSpy) => void; + onSpyReady: (sp: MockInstance) => void; }) { const store = React.useContext(Context); if (!store) { throw new Error('StoreSpy must be used inside the matching Provider'); } - const spyRef = React.useRef(null); + const spyRef = React.useRef(null); React.useEffect(() => { const fn = (store as any)[method]; if (typeof fn !== 'function') { throw new Error(`Method "${String(method)}" not found or is not a function on store`); } - const sp = sinonSpy(store as any, method as any); + const sp = vi.spyOn(store as any, method as any); spyRef.current = sp; onSpyReady(sp); - return () => spyRef.current?.restore?.(); + return () => spyRef.current?.mockRestore?.(); }, [store, method, onSpyReady]); return null; diff --git a/test/utils/setupFakeClock.ts b/test/utils/setupFakeClock.ts index ce17e8cbeda18..297050bb769cb 100644 --- a/test/utils/setupFakeClock.ts +++ b/test/utils/setupFakeClock.ts @@ -1,4 +1,4 @@ -import { useFakeTimers } from 'sinon'; +import { vi } from 'vitest'; declare global { interface Window { @@ -10,27 +10,26 @@ declare global { const DEFAULT_TIMESTAMP = '2014-08-18T14:11:54-05:00'; // eslint-disable-next-line import/no-mutable-exports -export let fakeClock: ReturnType | undefined; +export let fakeClock: { restore: () => void } | undefined; setupFakeClock(); export function setupFakeClock(shouldAdvanceTime = true) { restoreFakeClock(); - // eslint-disable-next-line react-hooks/rules-of-hooks - fakeClock = useFakeTimers({ - now: new Date(DEFAULT_TIMESTAMP).getTime(), - // We need to let time advance to use `useDemoData`, but on the pickers - // test it makes the tests flaky - shouldAdvanceTime, - }); + vi.useFakeTimers(); + vi.setSystemTime(new Date(DEFAULT_TIMESTAMP).getTime()); + + fakeClock = { + restore: () => vi.useRealTimers(), + }; return restoreFakeClock; } export function restoreFakeClock() { if (fakeClock) { - fakeClock.runToLast(); + vi.runAllTimers(); fakeClock.restore(); fakeClock = undefined; }