|
| 1 | +/* eslint-disable import/no-extraneous-dependencies */ |
| 2 | +import React from 'react'; |
| 3 | +import { BrowserRouter as Router } from 'react-router-dom'; |
| 4 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 5 | +import { IntlProvider } from '@edx/frontend-platform/i18n'; |
| 6 | +import { QueryClientProvider } from '@tanstack/react-query'; |
| 7 | +import AnalyticsTable from './AnalyticsTable'; |
| 8 | +import { useEnterpriseAnalyticsData, usePaginatedData } from '../data/hooks'; |
| 9 | +import EnterpriseDataApiService from '../../../data/services/EnterpriseDataApiService'; |
| 10 | +import { queryClient } from '../../test/testUtils'; |
| 11 | +import { analyticsDataTableKeys } from '../data/constants'; |
| 12 | +import '@testing-library/jest-dom'; |
| 13 | + |
| 14 | +// Mock hooks |
| 15 | +jest.mock('../data/hooks', () => ({ |
| 16 | + useEnterpriseAnalyticsData: jest.fn(), |
| 17 | + usePaginatedData: jest.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +// Mock service |
| 21 | +jest.mock('../../../data/services/EnterpriseDataApiService', () => ({ |
| 22 | + getAnalyticsCSVDownloadURL: jest.fn(() => '/mock.csv'), |
| 23 | +})); |
| 24 | + |
| 25 | +const defaultProps = { |
| 26 | + name: 'leaderboardTable', |
| 27 | + tableColumns: [], |
| 28 | + tableTitle: 'Leaderboard Table', |
| 29 | + entityId: 'entity-1', |
| 30 | + enterpriseId: 'enterprise-1', |
| 31 | + startDate: '2023-01-01', |
| 32 | + endDate: '2023-02-01', |
| 33 | + trackCsvDownloadClick: jest.fn(), |
| 34 | +}; |
| 35 | + |
| 36 | +function renderWithIntl(ui) { |
| 37 | + return render( |
| 38 | + <Router> |
| 39 | + <QueryClientProvider client={queryClient()}> |
| 40 | + <IntlProvider locale="en"> |
| 41 | + {ui} |
| 42 | + </IntlProvider> |
| 43 | + </QueryClientProvider> |
| 44 | + </Router>, |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +describe('AnalyticsTable', () => { |
| 49 | + beforeEach(() => { |
| 50 | + jest.clearAllMocks(); |
| 51 | + usePaginatedData.mockReturnValue({ |
| 52 | + data: [], |
| 53 | + itemCount: 0, |
| 54 | + pageCount: 1, |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + test('disables CSV button when data.results is empty', () => { |
| 59 | + useEnterpriseAnalyticsData.mockReturnValue({ |
| 60 | + data: { results: [] }, |
| 61 | + isFetching: false, |
| 62 | + }); |
| 63 | + |
| 64 | + renderWithIntl(<AnalyticsTable {...defaultProps} />); |
| 65 | + |
| 66 | + const button = screen.getByRole('link', { name: /download/i }); |
| 67 | + |
| 68 | + expect(button).toHaveClass('disabled'); |
| 69 | + }); |
| 70 | + |
| 71 | + test('enables CSV button when data.results has items', () => { |
| 72 | + useEnterpriseAnalyticsData.mockReturnValue({ |
| 73 | + data: { results: [{ id: 1 }] }, |
| 74 | + isFetching: false, |
| 75 | + }); |
| 76 | + |
| 77 | + renderWithIntl(<AnalyticsTable {...defaultProps} />); |
| 78 | + |
| 79 | + const button = screen.getByRole('link', { name: /download/i }); |
| 80 | + |
| 81 | + expect(button).not.toHaveClass('disabled'); |
| 82 | + }); |
| 83 | + |
| 84 | + test('CSV download URL is generated', () => { |
| 85 | + useEnterpriseAnalyticsData.mockReturnValue({ |
| 86 | + data: { results: [] }, |
| 87 | + isFetching: false, |
| 88 | + }); |
| 89 | + |
| 90 | + renderWithIntl(<AnalyticsTable {...defaultProps} />); |
| 91 | + |
| 92 | + expect(EnterpriseDataApiService.getAnalyticsCSVDownloadURL).toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + test('CSV download URL includes correct csvDownloadOptions', () => { |
| 96 | + useEnterpriseAnalyticsData.mockReturnValue({ |
| 97 | + data: { results: [{ id: 1 }] }, |
| 98 | + isFetching: false, |
| 99 | + }); |
| 100 | + |
| 101 | + const props = { |
| 102 | + ...defaultProps, |
| 103 | + courseType: 'special', |
| 104 | + course: { value: 'course-123', label: 'Course 123' }, |
| 105 | + budgetUUID: 'budget-xyz', |
| 106 | + groupUUID: 'group-abc', |
| 107 | + }; |
| 108 | + |
| 109 | + renderWithIntl(<AnalyticsTable {...props} />); |
| 110 | + |
| 111 | + expect(EnterpriseDataApiService.getAnalyticsCSVDownloadURL).toHaveBeenCalledWith( |
| 112 | + analyticsDataTableKeys[props.name], |
| 113 | + props.enterpriseId, |
| 114 | + { |
| 115 | + start_date: props.startDate, |
| 116 | + end_date: props.endDate, |
| 117 | + course_type: props.courseType, |
| 118 | + course_key: props.course.value, |
| 119 | + budget_uuid: props.budgetUUID, |
| 120 | + group_uuid: props.groupUUID, |
| 121 | + }, |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + test('clicking CSV button triggers trackCsvDownloadClick', () => { |
| 126 | + useEnterpriseAnalyticsData.mockReturnValue({ |
| 127 | + data: { results: [{ id: 1 }] }, |
| 128 | + isFetching: false, |
| 129 | + }); |
| 130 | + |
| 131 | + renderWithIntl(<AnalyticsTable {...defaultProps} />); |
| 132 | + |
| 133 | + const link = screen.getByRole('link', { name: /download/i }); |
| 134 | + |
| 135 | + fireEvent.click(link); |
| 136 | + |
| 137 | + expect(defaultProps.trackCsvDownloadClick).toHaveBeenCalledWith('entity-1'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments