From c13a4728e8df16f7faba81bc056d9e9d08b8fd93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Or=C5=82=C3=B3w?= <adrian.orlow@fishbrain.com> Date: Mon, 27 Nov 2023 16:45:21 +0100 Subject: [PATCH] test: add tests for reaction drawer utils --- .../utils/getFilteredReferences.test.ts | 34 ++++ .../utils/getFilteredReferences.ts | 4 +- .../utils/getGroupedReferences.test.ts | 89 +++++++++ src/models/mocks/referencesMock.ts | 178 ++++++++++++++++++ src/utils/array/groupBy.test.ts | 85 +++++++++ 5 files changed, 389 insertions(+), 1 deletion(-) create mode 100644 src/components/Map/Drawer/ReactionDrawer/utils/getFilteredReferences.test.ts create mode 100644 src/components/Map/Drawer/ReactionDrawer/utils/getGroupedReferences.test.ts create mode 100644 src/models/mocks/referencesMock.ts create mode 100644 src/utils/array/groupBy.test.ts diff --git a/src/components/Map/Drawer/ReactionDrawer/utils/getFilteredReferences.test.ts b/src/components/Map/Drawer/ReactionDrawer/utils/getFilteredReferences.test.ts new file mode 100644 index 00000000..3d887fbe --- /dev/null +++ b/src/components/Map/Drawer/ReactionDrawer/utils/getFilteredReferences.test.ts @@ -0,0 +1,34 @@ +import { + REFERENCES_MOCK_ALL_INVALID, + REFERENCES_MOCK_ALL_VALID, +} from '@/models/mocks/referencesMock'; +import { Reaction } from '@/types/models'; +import { ReferenceFiltered } from '../ReactionDrawer.types'; +import { getFilteredReferences } from './getFilteredReferences'; + +describe('getFilteredReferences - subUtil', () => { + const cases: [Pick<Reaction, 'references'>, ReferenceFiltered[]][] = [ + [ + { + references: REFERENCES_MOCK_ALL_VALID, + }, + REFERENCES_MOCK_ALL_VALID as ReferenceFiltered[], + ], + [ + { + references: REFERENCES_MOCK_ALL_INVALID, + }, + [], + ], + [ + { + references: [...REFERENCES_MOCK_ALL_VALID, ...REFERENCES_MOCK_ALL_INVALID], + }, + REFERENCES_MOCK_ALL_VALID as ReferenceFiltered[], + ], + ]; + + it.each(cases)('should return valid filtered references', (reaction, result) => { + expect(getFilteredReferences(reaction)).toStrictEqual(result); + }); +}); diff --git a/src/components/Map/Drawer/ReactionDrawer/utils/getFilteredReferences.ts b/src/components/Map/Drawer/ReactionDrawer/utils/getFilteredReferences.ts index c466650a..10ef8b9f 100644 --- a/src/components/Map/Drawer/ReactionDrawer/utils/getFilteredReferences.ts +++ b/src/components/Map/Drawer/ReactionDrawer/utils/getFilteredReferences.ts @@ -1,7 +1,9 @@ import { Reaction } from '@/types/models'; import { ReferenceFiltered } from '../ReactionDrawer.types'; -export const getFilteredReferences = (reaction: Reaction | undefined): ReferenceFiltered[] => +type InputReaction = Pick<Reaction, 'references'>; + +export const getFilteredReferences = (reaction: InputReaction | undefined): ReferenceFiltered[] => (reaction?.references || []).filter( (ref): ref is ReferenceFiltered => ref.link !== null && ref.link !== undefined, ); diff --git a/src/components/Map/Drawer/ReactionDrawer/utils/getGroupedReferences.test.ts b/src/components/Map/Drawer/ReactionDrawer/utils/getGroupedReferences.test.ts new file mode 100644 index 00000000..465c54f8 --- /dev/null +++ b/src/components/Map/Drawer/ReactionDrawer/utils/getGroupedReferences.test.ts @@ -0,0 +1,89 @@ +import { ReferenceFiltered } from '../ReactionDrawer.types'; +import { getGroupedReferences } from './getGroupedReferences'; + +describe('getGroupedReferences - util', () => { + const singleReference = { + link: 'https://www.ncbi.nlm.nih.gov/pubmed/24448649', + article: { + title: + 'The nutrient-responsive transcription factor TFE3 promotes autophagy, lysosomal biogenesis, and clearance of cellular debris.', + authors: [ + 'Martina JA', + ' Diab HI', + ' Lishu L', + ' Jeong-A L', + ' Patange S', + ' Raben N', + ' Puertollano R.', + ], + journal: 'Science signaling', + year: 2014, + link: 'https://www.ncbi.nlm.nih.gov/pubmed/24448649', + pubmedId: '24448649', + citationCount: 321, + }, + type: 'PUBMED', + resource: '24448649', + id: 154973, + annotatorClassName: '', + }; + + const cases = [ + [[], []], + [ + [singleReference], + [ + { + source: '', + references: [singleReference], + }, + ], + ], + [ + [ + { + ...singleReference, + annotatorClassName: 'source1', + }, + { + ...singleReference, + annotatorClassName: 'source1', + }, + { + ...singleReference, + annotatorClassName: 'source2', + }, + ], + [ + { + source: 'source1', + references: [ + { + ...singleReference, + annotatorClassName: 'source1', + }, + { + ...singleReference, + annotatorClassName: 'source1', + }, + ], + }, + { + source: 'source2', + references: [ + { + ...singleReference, + annotatorClassName: 'source2', + }, + ], + }, + ], + ], + ]; + + it.each(cases)('should return correct grouped references', (references, referencesGrouped) => + expect(getGroupedReferences(references as ReferenceFiltered[])).toMatchObject( + referencesGrouped, + ), + ); +}); diff --git a/src/models/mocks/referencesMock.ts b/src/models/mocks/referencesMock.ts new file mode 100644 index 00000000..f1e2a5d8 --- /dev/null +++ b/src/models/mocks/referencesMock.ts @@ -0,0 +1,178 @@ +import { FIRST } from '@/constants/common'; +import { Reference } from '@/types/models'; + +export const REFERENCES_MOCK_ALL_VALID: Reference[] = [ + { + link: 'https://www.ncbi.nlm.nih.gov/pubmed/24448649', + article: { + title: + 'The nutrient-responsive transcription factor TFE3 promotes autophagy, lysosomal biogenesis, and clearance of cellular debris.', + authors: [ + 'Martina JA', + ' Diab HI', + ' Lishu L', + ' Jeong-A L', + ' Patange S', + ' Raben N', + ' Puertollano R.', + ], + journal: 'Science signaling', + year: 2014, + link: 'https://www.ncbi.nlm.nih.gov/pubmed/24448649', + pubmedId: '24448649', + citationCount: 321, + }, + type: 'PUBMED', + resource: '24448649', + id: 154973, + annotatorClassName: '', + }, + { + link: 'https://www.ncbi.nlm.nih.gov/pubmed/27299292', + article: { + title: + 'Transcription factor EB: from master coordinator of lysosomal pathways to candidate therapeutic target in degenerative storage diseases.', + authors: ['Sardiello M.'], + journal: 'Annals of the New York Academy of Sciences', + year: 2016, + link: 'https://www.ncbi.nlm.nih.gov/pubmed/27299292', + pubmedId: '27299292', + citationCount: 66, + }, + type: 'PUBMED', + resource: '27299292', + id: 154974, + annotatorClassName: '', + }, + { + link: 'https://www.ncbi.nlm.nih.gov/pubmed/24448649', + article: { + title: + 'The nutrient-responsive transcription factor TFE3 promotes autophagy, lysosomal biogenesis, and clearance of cellular debris.', + authors: [ + 'Martina JA', + ' Diab HI', + ' Lishu L', + ' Jeong-A L', + ' Patange S', + ' Raben N', + ' Puertollano R.', + ], + journal: 'Science signaling', + year: 2014, + link: 'https://www.ncbi.nlm.nih.gov/pubmed/24448649', + pubmedId: '24448649', + citationCount: 321, + }, + type: 'PUBMED', + resource: '24448649', + id: 154973, + annotatorClassName: 'source1', + }, + { + link: 'https://www.ncbi.nlm.nih.gov/pubmed/24448649', + article: { + title: + 'Transcription factor EB: from master coordinator of lysosomal pathways to candidate therapeutic target in degenerative storage diseases.', + authors: ['Sardiello M.'], + journal: 'Annals of the New York Academy of Sciences', + year: 2016, + link: 'https://www.ncbi.nlm.nih.gov/pubmed/27299292', + pubmedId: '27299292', + citationCount: 66, + }, + type: 'PUBMED', + resource: '27299292', + id: 154974, + annotatorClassName: 'source2', + }, +]; + +export const REFERENCES_MOCK_ALL_INVALID: Reference[] = [ + { + link: null, + article: { + title: + 'The nutrient-responsive transcription factor TFE3 promotes autophagy, lysosomal biogenesis, and clearance of cellular debris.', + authors: [ + 'Martina JA', + ' Diab HI', + ' Lishu L', + ' Jeong-A L', + ' Patange S', + ' Raben N', + ' Puertollano R.', + ], + journal: 'Science signaling', + year: 2014, + link: 'https://www.ncbi.nlm.nih.gov/pubmed/24448649', + pubmedId: '24448649', + citationCount: 321, + }, + type: 'PUBMED', + resource: '24448649', + id: 154973, + annotatorClassName: '', + }, + { + link: null, + article: { + title: + 'Transcription factor EB: from master coordinator of lysosomal pathways to candidate therapeutic target in degenerative storage diseases.', + authors: ['Sardiello M.'], + journal: 'Annals of the New York Academy of Sciences', + year: 2016, + link: 'https://www.ncbi.nlm.nih.gov/pubmed/27299292', + pubmedId: '27299292', + citationCount: 66, + }, + type: 'PUBMED', + resource: '27299292', + id: 154974, + annotatorClassName: '', + }, + { + link: null, + article: { + title: + 'The nutrient-responsive transcription factor TFE3 promotes autophagy, lysosomal biogenesis, and clearance of cellular debris.', + authors: [ + 'Martina JA', + ' Diab HI', + ' Lishu L', + ' Jeong-A L', + ' Patange S', + ' Raben N', + ' Puertollano R.', + ], + journal: 'Science signaling', + year: 2014, + link: 'https://www.ncbi.nlm.nih.gov/pubmed/24448649', + pubmedId: '24448649', + citationCount: 321, + }, + type: 'PUBMED', + resource: '24448649', + id: 154973, + annotatorClassName: 'source1', + }, + { + link: null, + article: { + title: + 'Transcription factor EB: from master coordinator of lysosomal pathways to candidate therapeutic target in degenerative storage diseases.', + authors: ['Sardiello M.'], + journal: 'Annals of the New York Academy of Sciences', + year: 2016, + link: 'https://www.ncbi.nlm.nih.gov/pubmed/27299292', + pubmedId: '27299292', + citationCount: 66, + }, + type: 'PUBMED', + resource: '27299292', + id: 154974, + annotatorClassName: 'source2', + }, +]; + +export const SINGLE_VALID_REFERENCE = REFERENCES_MOCK_ALL_VALID[FIRST]; diff --git a/src/utils/array/groupBy.test.ts b/src/utils/array/groupBy.test.ts new file mode 100644 index 00000000..32bbc32a --- /dev/null +++ b/src/utils/array/groupBy.test.ts @@ -0,0 +1,85 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { groupBy } from './groupBy'; + +interface InputObject { + title: string; + value: number; +} + +describe('groupBy - util', () => { + const cases: [InputObject[], (value: InputObject) => string, { [key: string]: InputObject[] }][] = + [ + [ + [ + { + title: 'GROUP_1', + value: 1, + }, + { + title: 'GROUP_1', + value: 2, + }, + { + title: 'GROUP_1', + value: 3, + }, + { + title: 'GROUP_2', + value: 1, + }, + { + title: 'GROUP_3', + value: 1, + }, + ], + (obj): string => obj.title, + { + GROUP_1: [ + { title: 'GROUP_1', value: 1 }, + { title: 'GROUP_1', value: 2 }, + { title: 'GROUP_1', value: 3 }, + ], + GROUP_2: [{ title: 'GROUP_2', value: 1 }], + GROUP_3: [{ title: 'GROUP_3', value: 1 }], + }, + ], + [ + [ + { + title: '1', + value: 1, + }, + { + title: '1', + value: 2, + }, + { + title: '1', + value: 3, + }, + { + title: '2', + value: 1, + }, + { + title: '3', + value: 1, + }, + ], + (obj): string => obj.value.toString(), + { + '1': [ + { title: '1', value: 1 }, + { title: '2', value: 1 }, + { title: '3', value: 1 }, + ], + '2': [{ title: '1', value: 2 }], + '3': [{ title: '1', value: 3 }], + }, + ], + ]; + + it.each(cases)('should return valid data basing on predicate', (input, predicate, output) => { + expect(groupBy(input as any[], predicate)).toStrictEqual(output); + }); +}); -- GitLab