diff --git a/src/components/OntologyBrowser/Test/store.test.ts b/src/components/OntologyBrowser/Test/store.test.ts index dc214b3dd..5ff6e27aa 100644 --- a/src/components/OntologyBrowser/Test/store.test.ts +++ b/src/components/OntologyBrowser/Test/store.test.ts @@ -151,12 +151,21 @@ describe('Phenotype Store', () => { }); it('/removeSameTerms should prohibit selecting multiple times the same term', () => { - const selecteKeys = ['ONE-two-three (123)']; - const targetKeys = ['two-ONE-three (123)', 'five-six-seven (567)', 'one-zero-zero (100)']; + let selecteKeys = ['ONE-two-three (123)']; + let targetKeys = ['two-ONE-three (123)', 'five-six-seven (567)', 'one-zero-zero (100)']; - const res = removeSameTerms(selecteKeys, targetKeys); - expect(res.sort()).toEqual( - ['ONE-two-three (123)', 'five-six-seven (567)', 'one-zero-zero (100)'].sort(), + //not expected to change + const res_one = removeSameTerms(selecteKeys, targetKeys); + expect(res_one.sort()).toEqual( + ['two-ONE-three (123)', 'five-six-seven (567)', 'one-zero-zero (100)'].sort(), + ); + selecteKeys = ['ONE-two-three (123)', 'two-ONE-three (123)']; + targetKeys = ['five-six-seven (567)', 'one-zero-zero (100)']; + + //expected to add last item change + const res_two = removeSameTerms(selecteKeys, targetKeys); + expect(res_two.sort()).toEqual( + ['two-ONE-three (123)', 'five-six-seven (567)', 'one-zero-zero (100)'].sort(), ); }); diff --git a/src/components/OntologyBrowser/index.tsx b/src/components/OntologyBrowser/index.tsx index b8152f1de..7af504e1e 100644 --- a/src/components/OntologyBrowser/index.tsx +++ b/src/components/OntologyBrowser/index.tsx @@ -243,7 +243,7 @@ class OntologyModal extends React.Component { return ; } if (direction === 'left' && treeSource) { - const checkedKeys = [...selectedKeys, ...removeSameTerms(selectedKeys, targetKeys)]; + const checkedKeys = [...removeSameTerms(selectedKeys, targetKeys)]; return ( str.replace('.', '__'); const termRegex = new RegExp('[^-]+$'); -//pattern of term with escaped parentheses -const termPattern = (term: string) => new RegExp(`.*${term?.replace(/(?=[()])/g, '\\')}$`); - export const removeSameTerms = (selectedKeys: string[], targetKeys: string[]) => { - let updatedTargetKeys = targetKeys; - - selectedKeys.forEach((t) => { - const match = t.match(termRegex); - if (match) { - const term = match.pop(); + let allSelectedAndChecked = {}; - if (term) { - const pattern = termPattern(term); - updatedTargetKeys = updatedTargetKeys.filter((t) => !pattern.test(t)); - } - } + selectedKeys.concat(targetKeys).forEach((t) => { + allSelectedAndChecked = { ...allSelectedAndChecked, [`${t.match(termRegex)}`]: t }; }); - return [...updatedTargetKeys, ...selectedKeys]; + + return [...Object.values(allSelectedAndChecked)]; }; export const selectSameTerms = (selectedKeys: string[], tree: TreeNode[] | undefined) => {