From f32ccb30235c91cca0b6d5b511e0fa500671b94a Mon Sep 17 00:00:00 2001 From: muhammad-ammar Date: Wed, 23 Oct 2024 12:09:40 +0500 Subject: [PATCH] test: add unittests --- src/components/Admin/AdminSearchForm.jsx | 5 +- src/components/Admin/AdminSearchForm.test.jsx | 33 ++++++++ src/components/Admin/index.jsx | 1 + src/data/reducers/enterpriseBudgets.test.js | 80 +++++++++++++++++++ 4 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 src/data/reducers/enterpriseBudgets.test.js diff --git a/src/components/Admin/AdminSearchForm.jsx b/src/components/Admin/AdminSearchForm.jsx index 0d4a6e347c..e93626f166 100644 --- a/src/components/Admin/AdminSearchForm.jsx +++ b/src/components/Admin/AdminSearchForm.jsx @@ -57,9 +57,6 @@ class AdminSearchForm extends React.Component { budget_uuid: event.target.value, page: 1, }; - if (event.target.value === '') { - updateParams.budget_uuid = ''; - } updateUrl(navigate, location.pathname, updateParams); } @@ -209,7 +206,7 @@ class AdminSearchForm extends React.Component { /> this.onBudgetSelect(e)} diff --git a/src/components/Admin/AdminSearchForm.test.jsx b/src/components/Admin/AdminSearchForm.test.jsx index 7a0d944ebf..dada76b682 100644 --- a/src/components/Admin/AdminSearchForm.test.jsx +++ b/src/components/Admin/AdminSearchForm.test.jsx @@ -5,6 +5,7 @@ import { IntlProvider } from '@edx/frontend-platform/i18n'; import AdminSearchForm from './AdminSearchForm'; import SearchBar from '../SearchBar'; +import { updateUrl } from '../../utils'; jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), @@ -12,6 +13,10 @@ jest.mock('react-router-dom', () => ({ useNavigate: jest.fn(), })); +jest.mock('../../utils', () => ({ + updateUrl: jest.fn(), +})); + const DEFAULT_PROPS = { searchEnrollmentsList: () => {}, searchParams: {}, @@ -48,4 +53,32 @@ describe('', () => { expect(spy).toHaveBeenCalledTimes(1); }); }); + + it('select the correct budget', () => { + const budgetUUID = '8d6503dd-e40d-42b8-442b-37dd4c5450e3'; + const budgets = [{ + subsidy_access_policy_uuid: budgetUUID, + subsidy_access_policy_display_name: 'Everything', + }]; + const props = { + ...DEFAULT_PROPS, + budgets, + location: { pathname: '/admin/learners' }, + }; + const wrapper = mount( + , + ); + const selectElement = wrapper.find('.budgets-dropdown select'); + + selectElement.simulate('change', { target: { value: budgetUUID } }); + expect(updateUrl).toHaveBeenCalled(); + expect(updateUrl).toHaveBeenCalledWith( + undefined, + '/admin/learners', + { + budget_uuid: budgetUUID, + page: 1, + }, + ); + }); }); diff --git a/src/components/Admin/index.jsx b/src/components/Admin/index.jsx index 1fa1ac8b6d..062824d99a 100644 --- a/src/components/Admin/index.jsx +++ b/src/components/Admin/index.jsx @@ -586,6 +586,7 @@ Admin.defaultProps = { table: null, insightsLoading: false, insights: null, + budgets: [], }; Admin.propTypes = { diff --git a/src/data/reducers/enterpriseBudgets.test.js b/src/data/reducers/enterpriseBudgets.test.js new file mode 100644 index 0000000000..66e111c404 --- /dev/null +++ b/src/data/reducers/enterpriseBudgets.test.js @@ -0,0 +1,80 @@ +import enterpriseBudgets from './enterpriseBudgets'; +import { + FETCH_ENTERPRISE_BUDGETS_REQUEST, + FETCH_ENTERPRISE_BUDGETS_SUCCESS, + FETCH_ENTERPRISE_BUDGETS_FAILURE, + CLEAR_ENTERPRISE_BUDGETS, +} from '../constants/enterpriseBudgets'; + +const initialState = { + loading: false, + error: null, + budgets: null, +}; + +const mockBudgetsData = [{ + subsidy_access_policy_uuid: '8d6503dd-e40d-42b8-442b-37dd4c5450e3', + subsidy_access_policy_display_name: 'Everything', +}]; + +describe('enterpriseBudgets reducer', () => { + it('has initial state', () => { + expect(enterpriseBudgets(undefined, {})).toEqual(initialState); + }); + + it('updates fetch budgets request state', () => { + const expected = { + ...initialState, + loading: true, + error: null, + }; + expect(enterpriseBudgets(undefined, { + type: FETCH_ENTERPRISE_BUDGETS_REQUEST, + })).toEqual(expected); + }); + + it('updates fetch budgets success state', () => { + const expected = { + ...initialState, + loading: false, + budgets: mockBudgetsData, + error: null, + }; + expect(enterpriseBudgets(undefined, { + type: FETCH_ENTERPRISE_BUDGETS_SUCCESS, + payload: { data: mockBudgetsData }, + })).toEqual(expected); + }); + + it('updates fetch budgets failure state', () => { + const error = Error('Network Request'); + const expected = { + ...initialState, + loading: false, + error, + budgets: null, + }; + expect(enterpriseBudgets(undefined, { + type: FETCH_ENTERPRISE_BUDGETS_FAILURE, + payload: { error }, + })).toEqual(expected); + }); + + it('updates clear budgets state', () => { + const state = enterpriseBudgets(undefined, { + type: FETCH_ENTERPRISE_BUDGETS_SUCCESS, + payload: { data: mockBudgetsData }, + }); + + const expected = { + ...initialState, + loading: false, + error: null, + budgets: null, + }; + + expect(enterpriseBudgets(state, { + type: CLEAR_ENTERPRISE_BUDGETS, + })).toEqual(expected); + }); +});