Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
varshamenon4 committed Aug 20, 2024
1 parent 76611f8 commit cf3156f
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/pages/ExamsPage/components/AllowanceListActions.test.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { render, screen } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';

import { useDeleteAllowance, useEditAllowance } from '../hooks';
import AllowanceListActions from './AllowanceListActions';
import * as reduxHooks from '../../../data/redux/hooks';

// const mockEditAllowance = jest.fn();
const mockClearRequest = jest.fn();
const mockRequestError = jest.fn();

Expand All @@ -16,6 +15,7 @@ const mockAllowance = {
allowanceType: 'Minutes',
extraTimeMins: 45,
};
const newExtraTimeMins = 60;

jest.mock('../hooks', () => ({
useDeleteAllowance: jest.fn(),
Expand All @@ -33,7 +33,6 @@ jest.unmock('react');
describe('AllowanceListActions', () => {
beforeEach(() => {
jest.resetAllMocks();
// hooks.useEditAllowance().mockReturnValue(mockEditAllowance);
reduxHooks.useRequestError.mockReturnValue(mockRequestError);
reduxHooks.useClearRequest.mockReturnValue(mockClearRequest);
});
Expand All @@ -50,7 +49,6 @@ describe('AllowanceListActions', () => {
expect(screen.getByText('Are you sure you want to delete this allowance?')).toBeInTheDocument();
});

// todo: figure out why this isn't working
it('should open the edit modal when the edit icon is clicked', () => {
render(<AllowanceListActions allowance={mockAllowance} />);
screen.getByTestId('edit-allowance-icon').click();
Expand Down Expand Up @@ -87,17 +85,17 @@ describe('AllowanceListActions', () => {
});

describe('edit modal', () => {
// todo: figure out why this fails
it('should edit the allowance when the save button is clicked', () => {
const mockEditAllowance = jest.fn();
useEditAllowance.mockReturnValue(mockEditAllowance);
render(<AllowanceListActions allowance={mockAllowance} />);
screen.getByTestId('edit-allowance-icon').click();
fireEvent.change(screen.getByTestId('extra-time-mins'), { target: { value: newExtraTimeMins } });
screen.getByText('Save').click();
const expectedData = {
username: mockAllowance.username,
exam_id: mockAllowance.examId,
extra_time_mins: mockAllowance.extraTimeMins,
extra_time_mins: newExtraTimeMins,
};
expect(mockEditAllowance).toHaveBeenCalledWith(mockAllowance.id, expectedData, expect.any(Function));
});
Expand All @@ -118,14 +116,14 @@ describe('AllowanceListActions', () => {
expect(screen.queryByTestId('edit-allowance-header')).not.toBeInTheDocument();
});

// todo: figure out why this fails
it('should close the modal when the edit is successful', () => {
const mockEditAllowance = jest.fn();
useEditAllowance.mockReturnValue(mockEditAllowance);
render(<AllowanceListActions allowance={mockAllowance} />);
screen.getByTestId('edit-allowance-icon').click();
fireEvent.change(screen.getByTestId('extra-time-mins'), { target: { value: newExtraTimeMins } });
screen.getByText('Save').click();
// expect(mockEditAllowance.mock.calls).toBe(1);
mockEditAllowance.mock.calls[0][2]();
expect(screen.queryByTestId('edit-allowance-header')).not.toBeInTheDocument();
});
});
Expand Down

0 comments on commit cf3156f

Please sign in to comment.