|
1 | 1 | import React from 'react'; |
2 | | -import { MemoryRouter } from 'react-router-dom'; |
3 | | -import renderer from 'react-test-renderer'; |
| 2 | +import { createMemoryHistory } from 'history'; |
| 3 | +import { Router } from 'react-router-dom'; |
4 | 4 | import { IntlProvider } from '@edx/frontend-platform/i18n'; |
5 | 5 | import configureMockStore from 'redux-mock-store'; |
6 | 6 | import thunk from 'redux-thunk'; |
7 | 7 | import { Provider } from 'react-redux'; |
| 8 | +import { act } from '@testing-library/react'; |
| 9 | +import { mount } from 'enzyme'; |
8 | 10 |
|
9 | 11 | import CompletedLearnersTable from '.'; |
| 12 | +import useCompletedLearners from './data/hooks/useCompletedLearners'; |
| 13 | +import { PAGE_SIZE } from '../../data/constants/table'; |
| 14 | +import { mockCompletedLearners, mockEmptyLearners } from './data/tests/constants'; |
| 15 | + |
| 16 | +// Mock the hooks |
| 17 | +jest.mock('./data/hooks/useCompletedLearners', () => jest.fn()); |
| 18 | +jest.mock('../Admin/TableDataContext', () => ({ |
| 19 | + useTableData: () => ({ |
| 20 | + setTableHasData: jest.fn(), |
| 21 | + }), |
| 22 | +})); |
10 | 23 |
|
11 | 24 | const mockStore = configureMockStore([thunk]); |
12 | | -const enterpriseId = 'test-enterprise'; |
13 | | -const store = mockStore({ |
14 | | - portalConfiguration: { |
15 | | - enterpriseId, |
16 | | - }, |
17 | | - table: { |
18 | | - 'completed-learners': { |
19 | | - data: { |
20 | | - results: [], |
21 | | - current_page: 1, |
22 | | - num_pages: 1, |
23 | | - }, |
24 | | - ordering: null, |
25 | | - loading: false, |
26 | | - error: null, |
27 | | - }, |
28 | | - }, |
29 | | -}); |
30 | 25 |
|
31 | | -const CompletedLearnersWrapper = props => ( |
32 | | - <MemoryRouter> |
33 | | - <IntlProvider locale="en"> |
34 | | - <Provider store={store}> |
35 | | - <CompletedLearnersTable |
36 | | - {...props} |
37 | | - /> |
38 | | - </Provider> |
39 | | - </IntlProvider> |
40 | | - </MemoryRouter> |
41 | | -); |
| 26 | +// Mock implementations |
| 27 | +const mockFetchData = jest.fn().mockResolvedValue({}); |
| 28 | +const mockFetchDataImmediate = jest.fn(); |
42 | 29 |
|
43 | 30 | describe('CompletedLearnersTable', () => { |
44 | | - it('renders empty state correctly', () => { |
45 | | - const tree = renderer |
46 | | - .create(( |
47 | | - <CompletedLearnersWrapper /> |
48 | | - )) |
49 | | - .toJSON(); |
50 | | - expect(tree).toMatchSnapshot(); |
| 31 | + const enterpriseId = 'test-enterprise-id'; |
| 32 | + const tableId = 'completed-learners'; |
| 33 | + |
| 34 | + const store = mockStore({ |
| 35 | + portalConfiguration: { |
| 36 | + enterpriseId, |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + const defaultProps = { |
| 41 | + id: tableId, |
| 42 | + }; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + // Setup default mock implementation |
| 46 | + useCompletedLearners.mockReturnValue({ |
| 47 | + isLoading: false, |
| 48 | + data: mockCompletedLearners, |
| 49 | + fetchData: mockFetchData, |
| 50 | + fetchDataImmediate: mockFetchDataImmediate, |
| 51 | + hasData: true, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + jest.clearAllMocks(); |
| 57 | + }); |
| 58 | + |
| 59 | + const CompletedLearnersTableWrapper = (props = {}) => { |
| 60 | + const history = createMemoryHistory(); |
| 61 | + return ( |
| 62 | + <Router location={history.location} navigator={history}> |
| 63 | + <IntlProvider locale="en"> |
| 64 | + <Provider store={store}> |
| 65 | + <CompletedLearnersTable {...defaultProps} {...props} /> |
| 66 | + </Provider> |
| 67 | + </IntlProvider> |
| 68 | + </Router> |
| 69 | + ); |
| 70 | + }; |
| 71 | + |
| 72 | + it('renders the table with learner data', () => { |
| 73 | + const wrapper = mount(<CompletedLearnersTableWrapper />); |
| 74 | + |
| 75 | + // Check if table exists |
| 76 | + const table = wrapper.find('DataTable'); |
| 77 | + expect(table.exists()).toBe(true); |
| 78 | + |
| 79 | + // Verify DataTable props |
| 80 | + expect(table.prop('id')).toBe(tableId); |
| 81 | + expect(table.prop('data')).toEqual(mockCompletedLearners.results); |
| 82 | + expect(table.prop('itemCount')).toBe(mockCompletedLearners.itemCount); |
| 83 | + expect(table.prop('pageCount')).toBe(mockCompletedLearners.pageCount); |
| 84 | + expect(table.prop('isLoading')).toBe(false); |
| 85 | + |
| 86 | + // Verify columns are correctly configured |
| 87 | + expect(table.prop('columns').length).toBe(2); |
| 88 | + expect(table.prop('columns')[0].accessor).toBe('userEmail'); |
| 89 | + expect(table.prop('columns')[1].accessor).toBe('completedCourses'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('renders empty table when no data is available', () => { |
| 93 | + useCompletedLearners.mockReturnValue({ |
| 94 | + isLoading: false, |
| 95 | + data: mockEmptyLearners, |
| 96 | + fetchData: mockFetchData, |
| 97 | + fetchDataImmediate: mockFetchDataImmediate, |
| 98 | + hasData: false, |
| 99 | + }); |
| 100 | + |
| 101 | + const wrapper = mount(<CompletedLearnersTableWrapper />); |
| 102 | + |
| 103 | + const table = wrapper.find('DataTable'); |
| 104 | + expect(table.exists()).toBe(true); |
| 105 | + expect(table.prop('data')).toEqual([]); |
| 106 | + expect(table.prop('itemCount')).toBe(0); |
| 107 | + expect(table.prop('pageCount')).toBe(0); |
| 108 | + }); |
| 109 | + |
| 110 | + it('shows loading state when data is being fetched', () => { |
| 111 | + useCompletedLearners.mockReturnValue({ |
| 112 | + isLoading: true, |
| 113 | + data: mockEmptyLearners, |
| 114 | + fetchData: mockFetchData, |
| 115 | + fetchDataImmediate: mockFetchDataImmediate, |
| 116 | + hasData: false, |
| 117 | + }); |
| 118 | + |
| 119 | + const wrapper = mount(<CompletedLearnersTableWrapper />); |
| 120 | + |
| 121 | + const table = wrapper.find('DataTable'); |
| 122 | + expect(table.prop('isLoading')).toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + it('fetches data immediately on mount', () => { |
| 126 | + mount(<CompletedLearnersTableWrapper />); |
| 127 | + |
| 128 | + expect(mockFetchDataImmediate).toHaveBeenCalledTimes(1); |
| 129 | + expect(mockFetchDataImmediate).toHaveBeenCalledWith( |
| 130 | + { |
| 131 | + pageIndex: 0, |
| 132 | + pageSize: PAGE_SIZE, |
| 133 | + sortBy: [], |
| 134 | + }, |
| 135 | + true, |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + it('uses URL query parameters for initial page', () => { |
| 140 | + const history = createMemoryHistory(); |
| 141 | + history.push(`?${tableId}-page=3`); // Set page 3 in URL |
| 142 | + |
| 143 | + const wrapper = mount( |
| 144 | + <Router location={history.location} navigator={history}> |
| 145 | + <IntlProvider locale="en"> |
| 146 | + <Provider store={store}> |
| 147 | + <CompletedLearnersTable {...defaultProps} /> |
| 148 | + </Provider> |
| 149 | + </IntlProvider> |
| 150 | + </Router>, |
| 151 | + ); |
| 152 | + |
| 153 | + const table = wrapper.find('DataTable'); |
| 154 | + // Check that initialState has pageIndex set to 2 (0-based index for page 3) |
| 155 | + expect(table.prop('initialState').pageIndex).toBe(2); |
| 156 | + |
| 157 | + // Check that fetchDataImmediate was called with pageIndex 2 |
| 158 | + expect(mockFetchDataImmediate).toHaveBeenCalledWith( |
| 159 | + expect.objectContaining({ |
| 160 | + pageIndex: 2, |
| 161 | + }), |
| 162 | + true, |
| 163 | + ); |
| 164 | + }); |
| 165 | + |
| 166 | + it('updates URL when page changes', async () => { |
| 167 | + const history = createMemoryHistory(); |
| 168 | + jest.spyOn(history, 'push'); |
| 169 | + |
| 170 | + const wrapper = mount( |
| 171 | + <Router location={history.location} navigator={history}> |
| 172 | + <IntlProvider locale="en"> |
| 173 | + <Provider store={store}> |
| 174 | + <CompletedLearnersTable {...defaultProps} /> |
| 175 | + </Provider> |
| 176 | + </IntlProvider> |
| 177 | + </Router>, |
| 178 | + ); |
| 179 | + |
| 180 | + // Simulate page change by calling fetchData prop with new table state |
| 181 | + const table = wrapper.find('DataTable'); |
| 182 | + await act(async () => { |
| 183 | + await table.prop('fetchData')({ |
| 184 | + pageIndex: 2, // Navigate to page 3 (0-indexed) |
| 185 | + pageSize: PAGE_SIZE, |
| 186 | + sortBy: [], |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + // Check that fetchData was called |
| 191 | + expect(mockFetchData).toHaveBeenCalledWith({ |
| 192 | + pageIndex: 2, |
| 193 | + pageSize: PAGE_SIZE, |
| 194 | + sortBy: [], |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + it('renders UserEmail component correctly', () => { |
| 199 | + const wrapper = mount(<CompletedLearnersTableWrapper />); |
| 200 | + |
| 201 | + // Find columns in the DataTable props |
| 202 | + const columns = wrapper.find('DataTable').prop('columns'); |
| 203 | + const emailColumn = columns.find(col => col.accessor === 'userEmail'); |
| 204 | + |
| 205 | + // Test the Cell renderer with a sample row |
| 206 | + const testRow = { |
| 207 | + original: { |
| 208 | + |
| 209 | + }, |
| 210 | + }; |
| 211 | + |
| 212 | + const emailCell = mount( |
| 213 | + <IntlProvider locale="en"> |
| 214 | + {emailColumn.Cell({ row: testRow })} |
| 215 | + </IntlProvider>, |
| 216 | + ); |
| 217 | + |
| 218 | + expect(emailCell.find('[data-hj-suppress]').text()).toBe('[email protected]'); |
| 219 | + }); |
| 220 | + |
| 221 | + it('renders completedCourses column correctly', () => { |
| 222 | + const wrapper = mount(<CompletedLearnersTableWrapper />); |
| 223 | + |
| 224 | + // Find columns in the DataTable props |
| 225 | + const columns = wrapper.find('DataTable').prop('columns'); |
| 226 | + const completedCoursesColumn = columns.find(col => col.accessor === 'completedCourses'); |
| 227 | + expect(completedCoursesColumn).toBeDefined(); |
| 228 | + |
| 229 | + // Verify column header text |
| 230 | + expect(completedCoursesColumn.Header).toBe('Total Course Completed Count'); |
51 | 231 | }); |
52 | 232 | }); |
0 commit comments