Skip to content

Commit

Permalink
test: add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ammar committed Oct 23, 2024
1 parent 9cb29b0 commit 78cb995
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 16 deletions.
5 changes: 1 addition & 4 deletions src/components/Admin/AdminSearchForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -209,7 +206,7 @@ class AdminSearchForm extends React.Component {
/>
</Form.Label>
<Form.Control
className="w-100"
className="w-100 budgets-dropdown"
as="select"
value={searchBudgetQuery}
onChange={e => this.onBudgetSelect(e)}
Expand Down
33 changes: 33 additions & 0 deletions src/components/Admin/AdminSearchForm.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ 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'),
useLocation: jest.fn(),
useNavigate: jest.fn(),
}));

jest.mock('../../utils', () => ({
updateUrl: jest.fn(),
}));

const DEFAULT_PROPS = {
searchEnrollmentsList: () => {},
searchParams: {},
Expand Down Expand Up @@ -48,4 +53,32 @@ describe('<AdminSearchForm />', () => {
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(
<AdminSearchFormWrapper {...props} />,
);
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,
},
);
});
});
24 changes: 12 additions & 12 deletions src/components/Admin/__snapshots__/Admin.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,7 @@ exports[`<Admin /> renders correctly with dashboard analytics data renders # cou
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -3462,7 +3462,7 @@ exports[`<Admin /> renders correctly with dashboard analytics data renders # of
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -5087,7 +5087,7 @@ exports[`<Admin /> renders correctly with dashboard analytics data renders # of
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -6712,7 +6712,7 @@ exports[`<Admin /> renders correctly with dashboard analytics data renders colla
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -8337,7 +8337,7 @@ exports[`<Admin /> renders correctly with dashboard analytics data renders full
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -9962,7 +9962,7 @@ exports[`<Admin /> renders correctly with dashboard analytics data renders inact
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -11587,7 +11587,7 @@ exports[`<Admin /> renders correctly with dashboard analytics data renders inact
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -13212,7 +13212,7 @@ exports[`<Admin /> renders correctly with dashboard analytics data renders learn
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -14837,7 +14837,7 @@ exports[`<Admin /> renders correctly with dashboard analytics data renders regis
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -16462,7 +16462,7 @@ exports[`<Admin /> renders correctly with dashboard analytics data renders top a
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -18136,7 +18136,7 @@ exports[`<Admin /> renders correctly with dashboard insights data renders dashbo
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down Expand Up @@ -21571,7 +21571,7 @@ exports[`<Admin /> renders correctly with no dashboard insights data 1`] = `
Filter by budget
</label>
<div
className="pgn__form-control-decorator-group w-100"
className="pgn__form-control-decorator-group w-100 budgets-dropdown"
>
<select
className="form-control"
Expand Down
1 change: 1 addition & 0 deletions src/components/Admin/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ Admin.defaultProps = {
table: null,
insightsLoading: false,
insights: null,
budgets: [],
};

Admin.propTypes = {
Expand Down
80 changes: 80 additions & 0 deletions src/data/reducers/enterpriseBudgets.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 78cb995

Please sign in to comment.