Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter for printing categories #946

Merged
merged 4 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions static/js/formatters-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,24 @@ export function getYoutubeUrl(videos = []) {
: null;
return youtubeVideoUrl;
}

/**
* construct a list of displayable category names based on given category ids from liveAPI
* and a mapping of category ids to names.
*
* @param {string[]} categoryIds category ids from liveAPI
* @param {Object[]} categoryMap mapping of category ids to names
tmeyer2115 marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} categoryMap[].id id of a category entry
* @param {string} categoryMap[].category name of a category entry
* @returns {string[]} a list of category names
*/
export function getCategoryNames(categoryIds, categoryMap) {
if (!categoryIds || !categoryMap) {
return [];
}
return categoryIds.reduce((list, id) => {
const categoryEntry = categoryMap.find(category => category.id === id);
categoryEntry ? list.push(categoryEntry.category) : console.error(`Unable to find category name for id ${id}.`);
return list;
}, []);
}
6 changes: 4 additions & 2 deletions static/js/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
generateCTAFieldTypeLink,
price,
highlightField,
getYoutubeUrl
getYoutubeUrl,
getCategoryNames
} from './formatters-internal.js';
import * as CustomFormatters from './formatters-custom.js';

Expand Down Expand Up @@ -62,7 +63,8 @@ let Formatters = {
generateCTAFieldTypeLink,
price,
highlightField,
getYoutubeUrl
getYoutubeUrl,
getCategoryNames
};
Formatters = Object.assign(Formatters, CustomFormatters);

Expand Down
53 changes: 53 additions & 0 deletions tests/static/js/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,57 @@ describe('Formatters', () => {
expect(actual).toEqual(expected);
});
});

describe('getCategoryNames', () => {
const categoryMap = [
{
"id": "1",
"category": "Neurology"
},
{
"id": "2",
"category": "Dermatology"
},
{
"id": "3",
"category": "Psychiatry"
},
{
"id": "4",
"category": "Surgery"
}
];

it('Handle undefined categoryIds and categoryMap', () => {
let categoryNames = Formatters.getCategoryNames(null, categoryMap);
expect(categoryNames).toEqual([]);
categoryNames = Formatters.getCategoryNames(['1'], null);
expect(categoryNames).toEqual([]);
});

it('return empty list for no matching category names', () => {
const categoryIds = ['5', '0'];
const consoleWarn = jest.spyOn(console, 'error')
.mockImplementation();
const categoryNames = Formatters.getCategoryNames(categoryIds, categoryMap);
expect(categoryNames).toEqual([]);
expect(consoleWarn).toHaveBeenCalledTimes(2);
console.error.mockClear();
});

it('return a list of matching category names', () => {
const categoryIds = ['1', '3'];
const categoryNames = Formatters.getCategoryNames(categoryIds, categoryMap);
expect(categoryNames).toEqual(['Neurology', 'Psychiatry']);
});

it('return a list of category names given non-matching and matching ids', () => {
const categoryIds = ['1', '10', '4'];
const consoleWarn = jest.spyOn(console, 'error')
.mockImplementation();
const categoryNames = Formatters.getCategoryNames(categoryIds, categoryMap);
expect(categoryNames).toEqual(['Neurology', 'Surgery']);
expect(consoleWarn).toHaveBeenCalledTimes(1);
});
});
yen-tt marked this conversation as resolved.
Show resolved Hide resolved
});