diff --git a/webpack/ForemanTasks/Components/TasksDashboard/__tests__/TasksDashboard.test.js b/webpack/ForemanTasks/Components/TasksDashboard/__tests__/TasksDashboard.test.js index e00b9463c..0d359795c 100644 --- a/webpack/ForemanTasks/Components/TasksDashboard/__tests__/TasksDashboard.test.js +++ b/webpack/ForemanTasks/Components/TasksDashboard/__tests__/TasksDashboard.test.js @@ -1,13 +1,100 @@ -import { testComponentSnapshotsWithFixtures } from '@theforeman/test'; +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { TASKS_DASHBOARD_AVAILABLE_TIMES } from '../TasksDashboardConstants'; +import { getQueryFromUrl } from '../TasksDashboardHelper'; import TasksDashboard from '../TasksDashboard'; -const fixtures = { - 'render without Props': { history: {} }, - /** fixtures, props for the component */ -}; +jest.mock('../TasksDashboardHelper', () => ({ + ...jest.requireActual('../TasksDashboardHelper'), + getQueryFromUrl: jest.fn(() => ({})), +})); + +jest.mock( + '../Components/TasksCardsGrid/Components/TasksDonutChart/TasksDonutChart', + () => { + const React = require('react'); + const Stub = () =>
; + Stub.displayName = 'TasksDonutChart'; + + return Stub; + } +); describe('TasksDashboard', () => { - describe('rendering', () => - testComponentSnapshotsWithFixtures(TasksDashboard, fixtures)); + const cardIds = [ + 'running-tasks-card', + 'paused-tasks-card', + 'stopped-tasks-card', + 'scheduled-tasks-card', + ]; + + beforeEach(() => { + jest.clearAllMocks(); + getQueryFromUrl.mockReturnValue({}); + }); + + it('renders the dashboard grid with time picker and task cards', () => { + const { container } = render(); + + expect(container.querySelector('.tasks-dashboard-grid')).toBeInTheDocument(); + expect(screen.getByText('With focus on last')).toBeInTheDocument(); + expect( + screen.getByRole('button', { name: /24h/i }) + ).toBeInTheDocument(); + + cardIds.forEach(id => { + expect(container.querySelector(`#${id}`)).toBeInTheDocument(); + }); + }); + + it('initializes the dashboard and fetches the summary on mount', () => { + const initializeDashboard = jest.fn(); + const fetchTasksSummary = jest.fn(); + + render( + + ); + + expect(initializeDashboard).toHaveBeenCalledWith({ + time: undefined, + query: {}, + }); + expect(fetchTasksSummary).toHaveBeenCalledWith( + TASKS_DASHBOARD_AVAILABLE_TIMES.H24, + '' + ); + }); + + it('fetches the summary again when time changes', () => { + const fetchTasksSummary = jest.fn(); + + const { rerender } = render( + + ); + + fetchTasksSummary.mockClear(); + + rerender( + + ); + + expect(fetchTasksSummary).toHaveBeenCalledWith( + TASKS_DASHBOARD_AVAILABLE_TIMES.H12, + '' + ); + }); }); diff --git a/webpack/ForemanTasks/Components/TasksDashboard/__tests__/__snapshots__/TasksDashboard.test.js.snap b/webpack/ForemanTasks/Components/TasksDashboard/__tests__/__snapshots__/TasksDashboard.test.js.snap deleted file mode 100644 index 2ac85d8bd..000000000 --- a/webpack/ForemanTasks/Components/TasksDashboard/__tests__/__snapshots__/TasksDashboard.test.js.snap +++ /dev/null @@ -1,51 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`TasksDashboard rendering render without Props 1`] = ` -
- - - -
-`;