Skip to content

Commit

Permalink
Add tests for Redux selectors (#180)
Browse files Browse the repository at this point in the history
* test: add selector tests

* refactor: remove unused typeOfSelectedAssignment

* chore: bump version to 1.4.26

Co-authored-by: Ben Warzeski <[email protected]>
  • Loading branch information
nsprenkle and muselesscreator authored May 11, 2021
1 parent 0c242ab commit 807a57d
Show file tree
Hide file tree
Showing 11 changed files with 728 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@edx/frontend-app-gradebook",
"version": "1.4.25",
"version": "1.4.26",
"description": "edx editable gradebook-ui to manipulate grade overrides on subsections",
"repository": {
"type": "git",
Expand Down
17 changes: 17 additions & 0 deletions src/data/selectors/assignmentTypes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import selectors from './assignmentTypes';

describe('areGradesFrozen', () => {
it('selects areGradesFrozen from state', () => {
const testValue = 'THX 1138';
const areGradesFrozen = selectors.areGradesFrozen({ assignmentTypes: { areGradesFrozen: testValue } });
expect(areGradesFrozen).toEqual(testValue);
});
});

describe('allAssignmentTypes', () => {
it('returns assignment types', () => {
const testAssignmentTypes = ['assignment', 'labs'];
const allAssignmentTypes = selectors.allAssignmentTypes({ assignmentTypes: { results: testAssignmentTypes } });
expect(allAssignmentTypes).toEqual(testAssignmentTypes);
});
});
49 changes: 49 additions & 0 deletions src/data/selectors/cohorts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import selectors from './cohorts';

const testCohorts = [
{
id: '1',
name: 'Cohort 1',
},
{
id: '9000',
name: 'Cohort 9000',
},
];

const nonMatchingId = '9001';

const testState = { cohorts: { results: testCohorts } };

describe('getCohortById', () => {
it('returns cohort when a match is found', () => {
const cohort = selectors.getCohortById(testState, testCohorts[0].id);
expect(cohort).toEqual(testCohorts[0]);
});
it('returns undefined when no match is found', () => {
const cohort = selectors.getCohortById(testState, nonMatchingId);
expect(cohort).toEqual(undefined);
});
});

describe('getCohortNameById', () => {
it('returns a cohort name when cohort matching ID is found', () => {
const cohortName = selectors.getCohortNameById(testState, testCohorts[1].id);
expect(cohortName).toEqual(testCohorts[1].name);
});
it('returns undefined when no matching cohort is found', () => {
const cohortName = selectors.getCohortNameById(testState, nonMatchingId);
expect(cohortName).toEqual(undefined);
});
});

describe('allCohorts', () => {
it('selects cohorts from state', () => {
const allCohorts = selectors.allCohorts(testState);
expect(allCohorts).toEqual(testCohorts);
});
it('returns an empty array when no cohort results present in state', () => {
const allCohorts = selectors.allCohorts({ cohorts: {} });
expect(allCohorts).toEqual([]);
});
});
11 changes: 0 additions & 11 deletions src/data/selectors/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ const selectableAssignmentLabels = (state) => (
selectableAssignments(state).map(chooseRelevantAssignmentData)
);

const typeOfSelectedAssignment = (state) => {
const selectedAssignmentLabel = allFilters(state).assignment;
const sectionBreakdown = (state.grades.results[0] || {}).section_breakdown || [];
const selectedAssignment = sectionBreakdown.find(
({ label }) => label === selectedAssignmentLabel,
);
return selectedAssignment && selectedAssignment.category;
};

const simpleSelectors = simpleSelectorFactory(
({ filters }) => filters,
[
Expand All @@ -61,11 +52,9 @@ const selectors = {
...simpleSelectors,
selectedAssignmentId,
selectedAssignmentLabel,

selectableAssignmentLabels,
selectableAssignments,
allFilters,
typeOfSelectedAssignment,
chooseRelevantAssignmentData,
getAssignmentsFromResultsSubstate,
};
Expand Down
137 changes: 137 additions & 0 deletions src/data/selectors/filters.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import selectors from './filters';

const selectedAssignmentInfo = {
category: 'Homework',
id: 'block-v1:edX+type@sequential+block@abcde',
label: 'HW 01',
subsectionLabel: 'Example Week 1: Getting Started',
};

const filters = {
assignment: selectedAssignmentInfo,
assignmentGradeMax: '100',
assignmentGradeMin: '0',
assignmentType: 'Homework',
cohort: 'Spring Term',
courseGradeMax: '100',
courseGradeMin: '0',
includeCourseRoleMembers: false,
track: 'masters',
};

const noFilters = {
assignment: undefined,
assignmentGradeMax: '100',
assignmentGradeMin: '0',
assignmentType: 'All',
cohort: '',
courseGradeMax: '100',
courseGradeMin: '0',
includeCourseRoleMembers: false,
track: '',
};

const sectionBreakdowns = [
{
subsection_name: 'Example Week 1: Getting Started',
score_earned: 1,
score_possible: 1,
percent: 1,
displayed_value: '1.00',
grade_description: '(0.00/0.00)',
module_id: 'block-v1:edX+type@sequential+block@abcde',
label: 'HW 01',
category: 'Homework',
},
{
subsection_name: 'Example Week 2: Going Deeper',
score_earned: 1,
score_possible: 42,
percent: 0,
displayed_value: '0.02',
grade_description: '(0.00/0.00)',
module_id: 'block-v1:edX+type@sequential+block@bcdef',
label: 'LAB 01',
category: 'Labs',
},
];

const gradesData = { results: [{ section_breakdown: sectionBreakdowns }] };

const testState = {
filters,
grades: gradesData,
};

describe('allFilters', () => {
it('selects all filters from state', () => {
const allFilters = selectors.allFilters(testState);
expect(allFilters).toEqual(filters);
});
it('returns an empty object when no filters are in state', () => {
const allFilters = selectors.allFilters({});
expect(allFilters).toEqual({});
});
});

describe('selectedAssignmentId', () => {
it('gets filtered assignment ID when available', () => {
const assignmentId = selectors.selectedAssignmentId(testState);
expect(assignmentId).toEqual(filters.assignment.id);
});
it('returns undefined when assignment ID unavailable', () => {
const assignmentId = selectors.selectedAssignmentId({ filters: { assignment: undefined } });
expect(assignmentId).toEqual(undefined);
});
});

describe('selectedAssignmentLabel', () => {
it('gets filtered assignment label when available', () => {
const assignmentLabel = selectors.selectedAssignmentLabel(testState);
expect(assignmentLabel).toEqual(filters.assignment.label);
});
it('returns undefined when assignment label is unavailable', () => {
const assignmentLabel = selectors.selectedAssignmentLabel({ filters: { assignment: undefined } });
expect(assignmentLabel).toEqual(undefined);
});
});

describe('selectableAssignmentLabels', () => {
it('gets assignment data for sections matching selected type filters', () => {
const selectableAssignmentLabels = selectors.selectableAssignmentLabels(testState);
expect(selectableAssignmentLabels).toEqual([filters.assignment]);
});
});

describe('selectableAssignments', () => {
it('returns all graded assignments when no assignment type filtering is applied', () => {
const selectableAssignments = selectors.selectableAssignments({ grades: gradesData, filters: noFilters });
expect(selectableAssignments).toEqual(sectionBreakdowns);
});
it('gets assignments of the selected category when assignment type filtering is applied', () => {
const selectableAssignments = selectors.selectableAssignments(testState);
expect(selectableAssignments).toEqual([sectionBreakdowns[0]]);
});
});

describe('chooseRelevantAssignmentData', () => {
it('maps label, subsection, category, and ID from assignment data', () => {
const assignmentData = selectors.chooseRelevantAssignmentData(sectionBreakdowns[0]);
expect(assignmentData).toEqual(selectedAssignmentInfo);
});
});

describe('getAssignmentsFromResultsSubstate', () => {
it('gets section breakdowns from state', () => {
const assignments = selectors.getAssignmentsFromResultsSubstate(gradesData.results);
expect(assignments).toEqual(sectionBreakdowns);
});
it('returns an empty array when results are not supplied', () => {
const assignments = selectors.getAssignmentsFromResultsSubstate([]);
expect(assignments).toEqual([]);
});
it('returns an empty array when section breakdowns are not supplied', () => {
const assignments = selectors.getAssignmentsFromResultsSubstate([{}]);
expect(assignments).toEqual([]);
});
});
Loading

0 comments on commit 807a57d

Please sign in to comment.