Skip to content

Commit

Permalink
test: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanttV committed Feb 28, 2024
1 parent 9e41247 commit 343da29
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 34 deletions.
24 changes: 12 additions & 12 deletions src/containers/ListView/SubmissionsTable.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,26 @@ describe('SubmissionsTable component', () => {
expect(columns[3]).toEqual({
Header: messages.learnerSubmissionDate.defaultMessage,
accessor: submissionFields.dateSubmitted,
Cell: el.instance.children[0].props.columns[1].Cell,
Cell: el.instance.children[0].props.columns[3].Cell,
disableFilters: true,
});
});
test('grade column', () => {
expect(columns[4]).toEqual({
Header: messages.grade.defaultMessage,
accessor: submissionFields.score,
Cell: el.instance.children[0].props.columns[2].Cell,
Cell: el.instance.children[0].props.columns[4].Cell,
disableFilters: true,
});
});
test('grading status column', () => {
expect(columns[5]).toEqual({
Header: messages.gradingStatus.defaultMessage,
accessor: submissionFields.gradingStatus,
Cell: el.instance.children[0].props.columns[3].Cell,
Cell: el.instance.children[0].props.columns[5].Cell,
Filter: MultiSelectDropdownFilter,
filter: 'includesValue',
filterChoices: el.instance.children[0].props.columns[3].filterChoices,
filterChoices: el.instance.children[0].props.columns[5].filterChoices,
});
});
});
Expand All @@ -251,26 +251,26 @@ describe('SubmissionsTable component', () => {
expect(columns[3]).toEqual({
Header: messages.teamSubmissionDate.defaultMessage,
accessor: submissionFields.dateSubmitted,
Cell: el.instance.children[0].props.columns[1].Cell,
Cell: el.instance.children[0].props.columns[3].Cell,
disableFilters: true,
});
});
test('grade column', () => {
expect(columns[4]).toEqual({
Header: messages.grade.defaultMessage,
accessor: submissionFields.score,
Cell: el.instance.children[0].props.columns[2].Cell,
Cell: el.instance.children[0].props.columns[4].Cell,
disableFilters: true,
});
});
test('grading status column', () => {
expect(columns[5]).toEqual({
Header: messages.gradingStatus.defaultMessage,
accessor: submissionFields.gradingStatus,
Cell: el.instance.children[0].props.columns[3].Cell,
Cell: el.instance.children[0].props.columns[5].Cell,
Filter: MultiSelectDropdownFilter,
filter: 'includesValue',
filterChoices: el.instance.children[0].props.columns[3].filterChoices,
filterChoices: el.instance.children[0].props.columns[5].filterChoices,
});
});
});
Expand Down Expand Up @@ -397,24 +397,24 @@ describe('SubmissionsTable component', () => {
const fakeDate = 16131215154955;
const fakeDateString = 'test-date-string';
const mock = jest.spyOn(Date.prototype, 'toLocaleString').mockReturnValue(fakeDateString);
expect(el.instance.children[0].props.columns[1].Cell({ value: fakeDate })).toEqual(fakeDateString);
expect(el.instance.children[0].props.columns[3].Cell({ value: fakeDate })).toEqual(fakeDateString);
mock.mockRestore();
});
});
describe('formatGrade method', () => {
it('returns "-" if grade is null', () => {
expect(el.instance.children[0].props.columns[2].Cell({ value: null })).toEqual('-');
expect(el.instance.children[0].props.columns[4].Cell({ value: null })).toEqual('-');
});
it('returns <pointsEarned>/<pointsPossible> if grade exists', () => {
expect(
el.instance.children[0].props.columns[2].Cell({ value: { pointsEarned: 1, pointsPossible: 10 } }),
el.instance.children[0].props.columns[4].Cell({ value: { pointsEarned: 1, pointsPossible: 10 } }),
).toEqual('1/10');
});
});
describe('formatStatus method', () => {
it('returns a StatusBadge with the given status', () => {
const status = 'graded';
expect(el.instance.children[0].props.columns[3].Cell({ value: 'graded' })).toEqual(
expect(el.instance.children[0].props.columns[5].Cell({ value: 'graded' })).toEqual(
<StatusBadge status={status} />,
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { shallow } from 'enzyme';
import { shallow } from '@edx/react-unit-test-utils';
import { formatMessage } from 'testUtils';
import {
DataTable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import api from 'data/services/lms/api';
import { useFeedbackList } from './hooks';

jest.mock('data/services/lms/api', () => ({
getFeedbackList: jest.fn(),
getFeedbackFromList: jest.fn(),
getFeedbackToList: jest.fn(),
}));

describe('ReviewProblemStepsContent hooks', () => {
Expand All @@ -14,13 +15,13 @@ describe('ReviewProblemStepsContent hooks', () => {
const mockAssessments = [
{
assessmentId: 1,
assesmentDate: '2024-01-01',
assessmentDate: '2024-01-01',
scorerEmail: '[email protected]',
scorerName: 'John Doe',
scorerUsername: 'johndoe123',
feedback: 'Great work!',
problemStep: 'Step 1',
assesmentScores: [
assessmentScores: [
{
criterionName: 'Criterion 1',
scoreEarned: 8,
Expand Down Expand Up @@ -61,34 +62,60 @@ describe('ReviewProblemStepsContent hooks', () => {

test('should change feedbackListType', async () => {
const mockResponse = { assessments: mockAssessments };
api.getFeedbackList.mockResolvedValue(mockResponse);
api.getFeedbackFromList.mockResolvedValue(mockResponse);

const { result, waitForNextUpdate } = renderHook(() => useFeedbackList('some-uuid'));

act(() => result.current.setFeedbackListType('received'));

await act(async () => {
result.current.getFeedbackListApi();
await waitForNextUpdate();
});

expect(api.getFeedbackFromList).toHaveBeenCalledWith('some-uuid');
expect(result.current.isLoadingFeedbackList).toBe(false);
expect(result.current.feedbackList).toEqual(expectedFormattedAssessments);
expect(result.current.feedbackListError).toBeNull();
});

test('successful API call getFeedbackFromList', async () => {
const mockResponse = { assessments: mockAssessments };
api.getFeedbackFromList.mockResolvedValue(mockResponse);

const { result, waitForNextUpdate } = renderHook(() => useFeedbackList('some-uuid'));

act(() => result.current.setFeedbackListType('given'));
act(() => {
result.current.getFeedbackListApi();
});

expect(api.getFeedbackFromList).toHaveBeenCalledWith('some-uuid');
expect(result.current.isLoadingFeedbackList).toBe(true);
expect(result.current.feedbackList).toEqual([]);
expect(result.current.feedbackListError).toBeNull();

await act(async () => {
result.current.getFeedbackListApi();
await waitForNextUpdate();
});

expect(api.getFeedbackList).toHaveBeenCalledWith('some-uuid', 'given');
expect(api.getFeedbackFromList).toHaveBeenCalledWith('some-uuid');
expect(result.current.isLoadingFeedbackList).toBe(false);
expect(result.current.feedbackList).toEqual(expectedFormattedAssessments);
expect(result.current.feedbackListError).toBeNull();
});

test('successful API call', async () => {
test('successful API call getFeedbackToList', async () => {
const mockResponse = { assessments: mockAssessments };
api.getFeedbackList.mockResolvedValue(mockResponse);
api.getFeedbackToList.mockResolvedValue(mockResponse);

const { result, waitForNextUpdate } = renderHook(() => useFeedbackList('some-uuid'));

act(() => {
result.current.getFeedbackListApi();
});

expect(api.getFeedbackList).toHaveBeenCalledWith('some-uuid', 'received');
expect(api.getFeedbackToList).toHaveBeenCalledWith('some-uuid');
expect(result.current.isLoadingFeedbackList).toBe(true);
expect(result.current.feedbackList).toEqual([]);
expect(result.current.feedbackListError).toBeNull();
Expand All @@ -98,23 +125,23 @@ describe('ReviewProblemStepsContent hooks', () => {
await waitForNextUpdate();
});

expect(api.getFeedbackList).toHaveBeenCalledWith('some-uuid', 'received');
expect(api.getFeedbackToList).toHaveBeenCalledWith('some-uuid');
expect(result.current.isLoadingFeedbackList).toBe(false);
expect(result.current.feedbackList).toEqual(expectedFormattedAssessments);
expect(result.current.feedbackListError).toBeNull();
});

test('fail API call', async () => {
test('fail API call getFeedbackFromList', async () => {
const mockError = new Error('Error fetching data');
api.getFeedbackList.mockRejectedValue(mockError);
api.getFeedbackFromList.mockRejectedValue(mockError);

const { result, waitForNextUpdate } = renderHook(() => useFeedbackList('some-uuid'));

act(() => {
result.current.getFeedbackListApi();
});

expect(api.getFeedbackList).toHaveBeenCalledWith('some-uuid', 'received');
expect(api.getFeedbackFromList).toHaveBeenCalledWith('some-uuid');
expect(result.current.isLoadingFeedbackList).toBe(true);
expect(result.current.feedbackListError).toBeNull();

Expand All @@ -123,7 +150,7 @@ describe('ReviewProblemStepsContent hooks', () => {
await waitForNextUpdate();
});

expect(api.getFeedbackList).toHaveBeenCalledWith('some-uuid', 'received');
expect(api.getFeedbackFromList).toHaveBeenCalledWith('some-uuid');
expect(result.current.isLoadingFeedbackList).toBe(false);
expect(result.current.feedbackListError).toBe('Error fetching data');
});
Expand All @@ -139,7 +166,8 @@ describe('ReviewProblemStepsContent hooks', () => {
expect(result.current.isLoadingFeedbackList).toBe(false);
}, { timeout: 500 });

expect(api.getFeedbackList).not.toHaveBeenCalled();
expect(api.getFeedbackFromList).not.toHaveBeenCalled();
expect(api.getFeedbackToList).not.toHaveBeenCalled();
expect(result.current.feedbackList).toEqual([]);
expect(result.current.feedbackListError).toBeNull();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ describe('assessmentTableFormat', () => {
const inputAssessmentData = [
{
assessmentId: 1,
assesmentDate: '2023-11-17',
assessmentDate: '2023-11-17',
scorerEmail: '[email protected]',
scorerName: 'Scorer 1',
scorerUsername: 'scorer1',
feedback: 'Good work!',
problemStep: 'Problem Step 1',
assesmentScores: [
assessmentScores: [
{ criterionName: 'Criterion 1', scoreEarned: 8, scoreType: 'High' },
{ criterionName: 'Criterion 2', scoreEarned: 6, scoreType: 'Medium' },
],
Expand Down
21 changes: 17 additions & 4 deletions src/data/services/lms/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jest.mock('data/constants/app', () => ({
const gradeData = 'test-grade-data';
const submissionUUID = 'test-submission-uuid';
const submissionUUIDs = ['some', 'submission', 'uuid'];
const assessmentType = 'received';

const methodKeys = StrictDict({
get: 'get',
Expand Down Expand Up @@ -152,12 +151,26 @@ describe('lms service api methods', () => {
});
});

describe('getFeedbackList', () => {
describe('getFeedbackFromList', () => {
testAPI({
promise: api.getFeedbackList(submissionUUID, assessmentType),
promise: api.getFeedbackFromList(submissionUUID),
method: methodKeys.get,
expected: {
urlKey: urlKeys.getFeedbackSubmissionsUrl,
urlKey: urlKeys.assessmentsFeedbackFromUrl,
urlParams: {
[paramKeys.oraLocation]: locationId(),
[paramKeys.submissionUUID]: submissionUUID,
},
},
});
});

describe('getFeedbackToList', () => {
testAPI({
promise: api.getFeedbackToList(submissionUUID),
method: methodKeys.get,
expected: {
urlKey: urlKeys.assessmentsFeedbackToUrl,
urlParams: {
[paramKeys.oraLocation]: locationId(),
[paramKeys.submissionUUID]: submissionUUID,
Expand Down

0 comments on commit 343da29

Please sign in to comment.