Skip to content

Commit

Permalink
fix: loading circle + tests (#22)
Browse files Browse the repository at this point in the history
* fix: loading circle + tests

* chore: nit

* fix: abiding react hook rules + tests
  • Loading branch information
ilee2u authored Feb 5, 2024
1 parent 348ee1e commit 4a68fbb
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/pages/ExamsPage/components/ResetExamAttemptModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import messages from '../messages';
const ResetExamAttemptModal = ({ username, examName, attemptId }) => {
const [isOpen, open, close] = useToggle(false);
const resetExamAttempt = useDeleteExamAttempt();
const getRequestStatus = useButtonStateFromRequestStatus();
const { formatMessage } = useIntl();
const deleteExamAttemptRequestStatus = useButtonStateFromRequestStatus(constants.RequestKeys.deleteExamAttempt);

const ResetButtonProps = {
labels: {
Expand Down Expand Up @@ -63,7 +63,7 @@ const ResetExamAttemptModal = ({ username, examName, attemptId }) => {
data-testid="reset-stateful-button"
// The state of this button is updated based on the request status of the deleteExamAttempt
// api function. The change of the button's label is based on VerifyButtonProps
state={getRequestStatus(constants.RequestKeys.deleteExamAttempt)}
state={deleteExamAttemptRequestStatus()}
{...ResetButtonProps}
variant="primary"
onClick={e => { // eslint-disable-line no-unused-vars
Expand Down
6 changes: 3 additions & 3 deletions src/pages/ExamsPage/components/ReviewExamAttemptModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ const ReviewExamAttemptModal = ({
}) => {
const [isOpen, open, close] = useToggle(false);
const modifyExamAttempt = useModifyExamAttempt();
const getRequestStatus = useButtonStateFromRequestStatus();
const { currentExam } = useExamsData();
const { formatMessage } = useIntl();
const modifyExamAttemptRequestStatus = useButtonStateFromRequestStatus(constants.RequestKeys.modifyExamAttempt);

const getButton = (status) => {
if (ReviewRequiredStatuses.includes(status)) {
Expand Down Expand Up @@ -134,7 +134,7 @@ const ReviewExamAttemptModal = ({
<StatefulButton
// The state of this button is updated based on the request status of the modifyExamAttempt
// api function. The change of the button's label is based on VerifyButtonProps.
state={getRequestStatus(constants.RequestKeys.modifyExamAttempt)}
state={modifyExamAttemptRequestStatus()}
{...VerifyButtonProps}
variant="success"
onClick={async e => { // eslint-disable-line no-unused-vars
Expand All @@ -146,7 +146,7 @@ const ReviewExamAttemptModal = ({
&& (
<StatefulButton
// See above comment in the other StatefulButton to understand how this works
state={getRequestStatus(constants.RequestKeys.modifyExamAttempt)}
state={modifyExamAttemptRequestStatus()}
{...RejectButtonProps}
variant="danger"
onClick={async e => { // eslint-disable-line no-unused-vars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('ReviewExamAttemptModal', () => {
// Using queryByText here allows the function to throw
expect(screen.queryByText('Update review status')).not.toBeInTheDocument();
});
it('Clicking the Verify button displays the correct label based on the request state', () => {
it('Clicking the Verify button displays the correct label based on the request state', async () => {
hooks.useButtonStateFromRequestStatus.mockReturnValue(() => 'pending'); // for testing button label state
render(reviewModal());
screen.getByText('Review Required').click();
Expand Down
2 changes: 1 addition & 1 deletion src/pages/ExamsPage/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSelector, useDispatch } from 'react-redux';

import { RequestKeys } from 'data/constants';

import * as reduxHooks from 'data/redux/hooks';
import * as reduxHooks from 'data/redux/hooks/requests';

import * as api from './data/api';
import * as selectors from './data/selectors';
Expand Down
13 changes: 5 additions & 8 deletions src/pages/ExamsPage/hooks.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import * as reduxHooks from 'data/redux/hooks';
import * as reduxHooks from 'data/redux/hooks/requests';

import * as constants from 'data/constants';
import * as api from './data/api';
Expand All @@ -13,7 +13,7 @@ jest.mock('react-redux', () => ({
useSelector: () => mockUseSelector,
}));

jest.mock('data/redux/hooks', () => ({
jest.mock('data/redux/hooks/requests', () => ({
useMakeNetworkRequest: jest.fn(),
useRequestIsPending: jest.fn(),
useRequestError: jest.fn(),
Expand Down Expand Up @@ -175,20 +175,17 @@ describe('ExamsPage hooks', () => {
it('returns empty string if no request made', () => {
reduxHooks.useRequestIsPending.mockReturnValue(false);
reduxHooks.useRequestError.mockReturnValue(false);
const getRequestStatus = hooks.useButtonStateFromRequestStatus(constants.modifyExamAttempt);
expect(getRequestStatus()).toBe('');
expect(hooks.useButtonStateFromRequestStatus(constants.RequestKeys.modifyExamAttempt)()).toBe('');
});
it('returns pending if request is pending', () => {
reduxHooks.useRequestIsPending.mockReturnValue(true);
reduxHooks.useRequestError.mockReturnValue(false);
const getRequestStatus = hooks.useButtonStateFromRequestStatus(constants.modifyExamAttempt);
expect(getRequestStatus()).toBe('pending');
expect(hooks.useButtonStateFromRequestStatus(constants.RequestKeys.modifyExamAttempt)()).toBe('pending');
});
it('returns error if request errors', () => {
reduxHooks.useRequestIsPending.mockReturnValue(false);
reduxHooks.useRequestError.mockReturnValue(true);
const getRequestStatus = hooks.useButtonStateFromRequestStatus(constants.modifyExamAttempt);
expect(getRequestStatus()).toBe('error');
expect(hooks.useButtonStateFromRequestStatus(constants.RequestKeys.modifyExamAttempt)()).toBe('error');
});
});
});

0 comments on commit 4a68fbb

Please sign in to comment.