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 f32ccb3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 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,
},
);
});
});
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 f32ccb3

Please sign in to comment.