Skip to content

Commit

Permalink
feat: [2U-530] added alphabetically sorting for course list (#58)
Browse files Browse the repository at this point in the history
* feat: added alphabetically sorting for course list

* refactor: sorted all tabs

* refactor: added tests
  • Loading branch information
PKulkoRaccoonGang authored Aug 21, 2023
1 parent 079261b commit 5880bfb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/studio-home/tabs-section/archived-tab/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import React from 'react';
import PropTypes from 'prop-types';

import CardItem from '../../card-item';
import { sortAlphabeticallyArray } from '../utils';

const ArchivedTab = ({ archivedCoursesData }) => (
<div className="courses-tab">
{archivedCoursesData.map(({
{sortAlphabeticallyArray(archivedCoursesData).map(({
courseKey, displayName, lmsLink, org, rerunLink, number, run, url,
}) => (
<CardItem
Expand Down
3 changes: 2 additions & 1 deletion src/studio-home/tabs-section/courses-tab/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';

import CardItem from '../../card-item';
import { sortAlphabeticallyArray } from '../utils';
import DefaultSection from './default-section';

const CoursesTab = ({ coursesDataItems }) => {
Expand All @@ -11,7 +12,7 @@ const CoursesTab = ({ coursesDataItems }) => {
);
}

return coursesDataItems.map(({
return sortAlphabeticallyArray(coursesDataItems).map(({
courseKey, displayName, lmsLink, org, rerunLink, number, run, url,
}) => (
<CardItem
Expand Down
3 changes: 2 additions & 1 deletion src/studio-home/tabs-section/libraries-tab/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import React from 'react';
import PropTypes from 'prop-types';

import CardItem from '../../card-item';
import { sortAlphabeticallyArray } from '../utils';

const LibrariesTab = ({ libraries }) => (
<div className="courses-tab">
{libraries.map(({
{sortAlphabeticallyArray(libraries).map(({
displayName, org, number, url,
}) => (
<CardItem
Expand Down
12 changes: 12 additions & 0 deletions src/studio-home/tabs-section/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Alphabetical sorting for arrays of courses and libraries.
*
* @param {array} arr - Array of courses or libraries.
* @returns {array} - An array of alphabetically sorted courses or libraries.
*/
const sortAlphabeticallyArray = (arr) => [...arr]
.sort((firstArrayData, secondArrayData) => firstArrayData
.displayName.localeCompare(secondArrayData.displayName));

// eslint-disable-next-line import/prefer-default-export
export { sortAlphabeticallyArray };
26 changes: 26 additions & 0 deletions src/studio-home/tabs-section/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { sortAlphabeticallyArray } from './utils';

const testData = [
{ displayName: 'Apple' },
{ displayName: 'Orange' },
{ displayName: 'Banana' },
];

describe('sortAlphabeticallyArray', () => {
it('sortAlphabeticallyArray sorts array alphabetically', () => {
const sortedData = sortAlphabeticallyArray(testData);

expect(sortedData).toEqual([
{ displayName: 'Apple' },
{ displayName: 'Banana' },
{ displayName: 'Orange' },
]);
});

it('sortAlphabeticallyArray does not mutate the original array', () => {
const originalData = [...testData];
sortAlphabeticallyArray(testData);

expect(testData).toEqual(originalData);
});
});

0 comments on commit 5880bfb

Please sign in to comment.