From f75d55439f856b4f7e2dfffee3d2fbf2f2bcd4f6 Mon Sep 17 00:00:00 2001 From: Lan Le Date: Thu, 17 Aug 2023 15:57:35 +0200 Subject: [PATCH] fix: hide NMRium button on non-NMR layouts Co-authored-by: Lan Le Refs: #1460 --- .../analysesTab/SampleDetailsContainersAux.js | 12 +- app/packs/src/utilities/SpectraHelper.js | 17 +- .../SampleDetailsContainersAux.spec.js | 105 +++++++ .../packs/src/utilities/SpectraHelper.spec.js | 263 ++++++++++++++++++ 4 files changed, 382 insertions(+), 15 deletions(-) create mode 100644 spec/javascripts/packs/src/apps/mydb/elements/details/samples/analysesTab/SampleDetailsContainersAux.spec.js create mode 100644 spec/javascripts/packs/src/utilities/SpectraHelper.spec.js diff --git a/app/packs/src/apps/mydb/elements/details/samples/analysesTab/SampleDetailsContainersAux.js b/app/packs/src/apps/mydb/elements/details/samples/analysesTab/SampleDetailsContainersAux.js index cc2c926564..4439ad078b 100644 --- a/app/packs/src/apps/mydb/elements/details/samples/analysesTab/SampleDetailsContainersAux.js +++ b/app/packs/src/apps/mydb/elements/details/samples/analysesTab/SampleDetailsContainersAux.js @@ -10,7 +10,7 @@ import { stopBubble } from 'src/utilities/DomHelper'; import ImageModal from 'src/components/common/ImageModal'; import SpectraActions from 'src/stores/alt/actions/SpectraActions'; import LoadingActions from 'src/stores/alt/actions/LoadingActions'; -import { BuildSpcInfos, JcampIds, BuildSpcInfosForNMRDisplayer } from 'src/utilities/SpectraHelper'; +import { BuildSpcInfos, JcampIds, BuildSpcInfosForNMRDisplayer, isNMRKind } from 'src/utilities/SpectraHelper'; import { hNmrCheckMsg, cNmrCheckMsg, msCheckMsg, instrumentText } from 'src/utilities/ElementUtils'; import { contentToText } from 'src/utilities/quillFormat'; import UIStore from 'src/stores/alt/stores/UIStore'; @@ -56,13 +56,6 @@ const qCheckMsg = (sample, container) => { return ''; }; -const isNMRKind = (container) => { - if (container.extended_metadata.kind) { - return container.extended_metadata.kind.includes('NMR'); - } - return false; -} - const SpectraEditorBtn = ({ sample, spcInfos, hasJcamp, hasChemSpectra, toggleSpectraModal, confirmRegenerate, confirmRegenerateEdited, hasEditedJcamp, @@ -305,8 +298,7 @@ const headerBtnGroup = ( } const { hasChemSpectra, hasNmriumWrapper } = UIStore.getState(); - // const hasNMRium = isNMRKind(container) && hasNmriumWrapper; - const hasNMRium = hasNmriumWrapper; + const hasNMRium = isNMRKind(container) && hasNmriumWrapper; return (
diff --git a/app/packs/src/utilities/SpectraHelper.js b/app/packs/src/utilities/SpectraHelper.js index fdb96328ac..44776f335d 100644 --- a/app/packs/src/utilities/SpectraHelper.js +++ b/app/packs/src/utilities/SpectraHelper.js @@ -66,7 +66,7 @@ const extractJcampWithFailedFiles = (container) => { const isJcamp = acceptables.indexOf(ext.toLowerCase()) >= 0; const isApp = [ 'idle', 'queueing', 'done', - 'backup', 'image','non_jcamp', + 'backup', 'image', 'non_jcamp', ].indexOf(att.aasm_state) < 0; if (isJcamp && isApp) { const file = Object.assign({}, att, { @@ -90,7 +90,7 @@ const extractNMRiumFiles = (container) => { const fns = att.filename.split('.'); const ext = fns[fns.length - 1]; const isNMRium = ext.toLowerCase() === 'nmrium'; - + if (isNMRium) { const file = Object.assign({}, att, { idDt: dt.id, @@ -122,7 +122,7 @@ const BuildSpcInfos = (sample, container) => { const files = extractJcampFiles(container); if (files.length < 1) return []; const idAe = extractAnalysesId(sample, container); - return files.map(file => ( + return files.map((file) => ( { value: null, label: file.filename, @@ -143,7 +143,7 @@ const BuildSpcInfosForNMRDisplayer = (sample, container) => { files.push(...nmriumFiles); if (files.length < 1) return []; const idAe = extractAnalysesId(sample, container); - return files.map(file => ( + return files.map((file) => ( { value: null, label: file.filename, @@ -157,4 +157,11 @@ const BuildSpcInfosForNMRDisplayer = (sample, container) => { )); }; -export { BuildSpcInfos, BuildSpcInfosForNMRDisplayer, JcampIds }; // eslint-disable-line +const isNMRKind = (container) => { + if (!(container && container.extended_metadata && container.extended_metadata.kind)) return false; + const { extended_metadata } = container; // eslint-disable-line + const { kind } = extended_metadata; // eslint-disable-line + return kind.toLowerCase().includes('nuclear magnetic resonance'); +}; + +export { BuildSpcInfos, BuildSpcInfosForNMRDisplayer, JcampIds, isNMRKind }; // eslint-disable-line diff --git a/spec/javascripts/packs/src/apps/mydb/elements/details/samples/analysesTab/SampleDetailsContainersAux.spec.js b/spec/javascripts/packs/src/apps/mydb/elements/details/samples/analysesTab/SampleDetailsContainersAux.spec.js new file mode 100644 index 0000000000..a057d8c8a7 --- /dev/null +++ b/spec/javascripts/packs/src/apps/mydb/elements/details/samples/analysesTab/SampleDetailsContainersAux.spec.js @@ -0,0 +1,105 @@ +import React from 'react'; +import expect from 'expect'; +import Enzyme, { mount, shallow } from 'enzyme'; +import Adapter from '@wojtekmaj/enzyme-adapter-react-17'; +import { + describe, it, beforeEach +} from 'mocha'; + +import { + HeaderDeleted, HeaderNormal +} from 'src/apps/mydb/elements/details/samples/analysesTab/SampleDetailsContainersAux'; + +import Container from 'src/models/Container'; +import Sample from 'src/models/Sample'; + +Enzyme.configure({ adapter: new Adapter() }); + +describe('SampleDetailsContainersAux', () => { + describe('HeaderDeleted', () => { + describe('Render without edit mode', () => { + let container; + beforeEach(() => { + container = Container.buildEmpty(); + container.name = 'Just a string'; + }); + + it('Render without kind and status', () => { + const wrapper = shallow(); + expect(wrapper.html()) + .toEqual(`
${container.name}
`); + }); + + it('Render with kind', () => { + container.extended_metadata.kind = 'Just a kind string'; + const wrapper = shallow(); + expect(wrapper.html()) + .toEqual(`
${container.name} - Type: ${container.extended_metadata.kind}
`); + }); + + it('Render with status', () => { + container.extended_metadata.status = 'Just a status string'; + const wrapper = shallow(); + expect(wrapper.html()) + .toEqual(`
${container.name} - Status: ${container.extended_metadata.status}
`); + }); + + it('Render with kind and status', () => { + container.extended_metadata.kind = 'Just a kind string'; + container.extended_metadata.status = 'Just a status string'; + const wrapper = shallow(); + expect(wrapper.html()) + .toEqual(`
${container.name} - Type: ${container.extended_metadata.kind} - Status: ${container.extended_metadata.status}
`); + }); + }); + + describe('Render with edit mode', () => { + const container = Container.buildEmpty(); + container.name = 'Just a string'; + + it('Render without kind and status', () => { + const wrapper = shallow(); + expect(wrapper.html()) + .toEqual(`
${container.name}
`); + }); + + it('Check on click instance', () => { + const testOnClick = () => {}; + const wrapper = mount(); + const button = wrapper.find('button'); + const onClickProp = button.prop('onClick'); + expect(onClickProp).toBeInstanceOf(Function); + }); + }); + }); + + describe('HeaderNormal', () => { + describe('Render without edit mode', () => { + let container; let + sample; + + beforeEach(() => { + container = Container.buildEmpty(); + container.name = 'Just a string'; + sample = Sample.buildEmpty(); + }); + + it('Render without status', () => { + const wrapper = shallow(); + const statusWrapper = shallow(
+ Status: + {' '} + {container.extended_metadata.status} + {' '} + + {' '} +       + {' '} + +
); + expect(wrapper.html()) + .toEqual(`
${container.name}
Type:
${statusWrapper.html()}
Content:
`); + }); + }); + }); +}); diff --git a/spec/javascripts/packs/src/utilities/SpectraHelper.spec.js b/spec/javascripts/packs/src/utilities/SpectraHelper.spec.js new file mode 100644 index 0000000000..96cc18f756 --- /dev/null +++ b/spec/javascripts/packs/src/utilities/SpectraHelper.spec.js @@ -0,0 +1,263 @@ +import expect from 'expect'; +import { + describe, it, beforeEach +} from 'mocha'; +import { + isNMRKind, BuildSpcInfosForNMRDisplayer, + JcampIds, BuildSpcInfos +} from 'src/utilities/SpectraHelper'; +import Sample from 'src/models/Sample'; +import Container from 'src/models/Container'; + +describe('SpectraHelper', () => { + describe('isNMRKind', () => { + describe('container is null or undefined', () => { + it('container is null', () => { + expect(isNMRKind(null)).toEqual(false); + }); + + it('container is undefined', () => { + expect(isNMRKind(undefined)).toEqual(false); + }); + }); + + describe('container is neither not null nor undefined', () => { + it('container does not has extended_metadata', () => { + const container = 'Just a random value'; + expect(isNMRKind(container)).toEqual(false); + }); + + describe('container has extended_metadata', () => { + it('metadata does not has kind value', () => { + const container = { extended_metadata: {} }; + expect(isNMRKind(container)).toEqual(false); + }); + + it('metadata has kind value but it is not NMR type', () => { + const container = { extended_metadata: { kind: 'Mass spectrum' } }; + expect(isNMRKind(container)).toEqual(false); + }); + + it('it is NMR type', () => { + const container = { extended_metadata: { kind: '1H nuclear magnetic resonance spectroscopy (1H NMR)' } }; + expect(isNMRKind(container)).toEqual(true); + }); + }); + }); + }); + + describe('BuildSpcInfosForNMRDisplayer', () => { + describe('sample or container is null or undefined', () => { + it('sample is null or undefined', () => { + const specInfo1 = BuildSpcInfosForNMRDisplayer(null, 'just a random value'); + expect(specInfo1).toEqual([]); + + const specInfo2 = BuildSpcInfosForNMRDisplayer(undefined, 'just a random value'); + expect(specInfo2).toEqual([]); + }); + + it('container is null or undefined', () => { + const specInfo1 = BuildSpcInfosForNMRDisplayer('just a random value', null); + expect(specInfo1).toEqual([]); + + const specInfo2 = BuildSpcInfosForNMRDisplayer('just a random value', undefined); + expect(specInfo2).toEqual([]); + }); + }); + + describe('it does not has any file to process', () => { + it('container does not has any attachment', () => { + const container = { children: [{ attachments: [] }] }; + const specInfo = BuildSpcInfosForNMRDisplayer('just a random value', container); + expect(specInfo).toEqual([]); + }); + + it('container does not has any attachment as jcamp', () => { + const container = { children: [{ attachments: [{ filename: 'testfile.txt' }] }] }; + const specInfo = BuildSpcInfosForNMRDisplayer('just a random value', container); + expect(specInfo).toEqual([]); + }); + + it('container has a attachment as jcamp but aasm_state is in failed or processing state', () => { + const states = ['idle', 'queueing', 'done', 'backup', 'image', 'non_jcamp']; + states.forEach((state) => { + const container = { children: [{ attachments: [{ filename: 'testfile.dx', aasm_state: state }] }] }; + const specInfo = BuildSpcInfosForNMRDisplayer('just a random value', container); + expect(specInfo).toEqual([]); + }); + }); + }); + + describe('it has file to be processed', () => { + it('get spectra info', () => { + const sample = Sample.buildEmpty(); + const analyses = Container.buildEmpty(); + analyses.container_type = 'analyses'; + + const analysis = Container.buildEmpty(); + analysis.container_type = 'analysis'; + + analyses.children.push(analysis); + sample.container.children.push(analyses); + + const file1 = { + dt: { id: '1001' }, id: '001', filename: 'testfile.dx', aasm_state: 'state' + }; + const file2 = { + dt: { id: '1001' }, id: '002', filename: 'testfile.nmrium', aasm_state: 'state' + }; + const listFile = [file1, file2]; + const container = { id: analysis.id, children: [{ attachments: listFile }] }; + const specInfo = BuildSpcInfosForNMRDisplayer(sample, container); + + const expectedValue = listFile.map((file) => ({ + value: null, + label: file.filename, + title: sample.short_label, + idSp: sample.id, + idAe: analyses.id, + idAi: container.id, + idDt: file.idDt, + idx: file.id, + })); + expect(specInfo).toEqual(expectedValue); + }); + }); + }); + + describe('JcampIds', () => { + describe('Container does not have any child', () => { + it('children is empty', () => { + const container = Container.buildEmpty(); + const listJcampIds = JcampIds(container); + const expectedValue = { orig: [], gene: [], edited: [] }; + expect(listJcampIds).toEqual(expectedValue); + }); + }); + + describe('Container does not have jcamp file', () => { + const container = Container.buildEmpty(); + const attachments = [{ filename: 'testfile.txt' }]; + container.children.push({ attachments }); + + const listJcampIds = JcampIds(container); + const expectedValue = { orig: [], gene: [], edited: [] }; + expect(listJcampIds).toEqual(expectedValue); + }); + + describe('Container has jcamp files', () => { + let container; + + beforeEach(() => { + container = Container.buildEmpty(); + }); + + it('Only has original files', () => { + const attachments = [{ id: 1, filename: 'testfile.dx' }]; + container.children.push({ attachments }); + + const listJcampIds = JcampIds(container); + const expectedValue = { orig: [1], gene: [], edited: [] }; + expect(listJcampIds).toEqual(expectedValue); + }); + + it('Only has edidted and generated files', () => { + const attachments = [{ id: 1, filename: 'testfile.peak.dx' }, { id: 2, filename: 'testfile.edit.dx' }]; + container.children.push({ attachments }); + + const listJcampIds = JcampIds(container); + const expectedValue = { orig: [], gene: [1, 2], edited: [1, 2] }; + expect(listJcampIds).toEqual(expectedValue); + }); + + it('Has original edidted and generated files', () => { + const attachments = [ + { id: 1, filename: 'testfile.dx' }, + { id: 2, filename: 'testfile.peak.dx' }, + { id: 3, filename: 'testfile.edit.dx' }]; + container.children.push({ attachments }); + + const listJcampIds = JcampIds(container); + const expectedValue = { orig: [1], gene: [2, 3], edited: [2, 3] }; + expect(listJcampIds).toEqual(expectedValue); + }); + }); + }); + + describe('BuildSpcInfos', () => { + describe('sample or container is null or undefined', () => { + it('sample is null or undefined', () => { + const specInfo1 = BuildSpcInfos(null, 'just a random value'); + expect(specInfo1).toEqual([]); + + const specInfo2 = BuildSpcInfos(undefined, 'just a random value'); + expect(specInfo2).toEqual([]); + }); + + it('container is null or undefined', () => { + const specInfo1 = BuildSpcInfos('just a random value', null); + expect(specInfo1).toEqual([]); + + const specInfo2 = BuildSpcInfos('just a random value', undefined); + expect(specInfo2).toEqual([]); + }); + }); + + describe('it does not has any file to process', () => { + it('container does not has any attachment', () => { + const container = { children: [{ attachments: [] }] }; + const specInfo = BuildSpcInfos('just a random value', container); + expect(specInfo).toEqual([]); + }); + + it('container does not has any attachment as jcamp', () => { + const container = { children: [{ attachments: [{ filename: 'testfile.txt' }] }] }; + const specInfo = BuildSpcInfos('just a random value', container); + expect(specInfo).toEqual([]); + }); + + it('container has a attachment as jcamp but aasm_state is in failed or processing state', () => { + const states = ['idle', 'queueing', 'done', 'backup', 'image', 'failure', 'non_jcamp']; + states.forEach((state) => { + const container = { children: [{ attachments: [{ filename: 'testfile.dx', aasm_state: state }] }] }; + const specInfo = BuildSpcInfos('just a random value', container); + expect(specInfo).toEqual([]); + }); + }); + }); + + describe('it has file to be processed', () => { + it('get spectra info', () => { + const sample = Sample.buildEmpty(); + const analyses = Container.buildEmpty(); + analyses.container_type = 'analyses'; + + const analysis = Container.buildEmpty(); + analysis.container_type = 'analysis'; + + analyses.children.push(analysis); + sample.container.children.push(analyses); + + const file = { + dt: { id: '1001' }, id: '001', filename: 'testfile.dx', aasm_state: 'state' + }; + const container = { id: analysis.id, children: [{ attachments: [file] }] }; + const specInfo = BuildSpcInfos(sample, container); + + const expectedValue = [ + { + value: null, + label: 'testfile.dx', + title: sample.short_label, + idSp: sample.id, + idAe: analyses.id, + idAi: container.id, + idDt: file.idDt, + idx: file.id, + } + ]; + expect(specInfo).toEqual(expectedValue); + }); + }); + }); +});