|
| 1 | +import * as d3 from 'd3'; |
| 2 | +import LineFocus from '../../../components/d3_line/line_focus'; |
| 3 | +import MultiFocus from '../../../components/d3_multi/multi_focus'; |
| 4 | +import { LIST_LAYOUT } from '../../../constants/list_layout'; |
| 5 | + |
| 6 | +// Opening a spectrum without multiplets after one with multiplets must not leave the |
| 7 | +// previous multiplet bars, peak markers and labels on the chart (#341). |
| 8 | +const buildFocus = (Focus, extra = {}) => { |
| 9 | + const svg = d3.select(document.body).append('svg'); |
| 10 | + const tags = {}; |
| 11 | + ['mpybPath', 'mpyt1Path', 'mpyt2Path', 'mpypPath'].forEach((k) => { |
| 12 | + tags[k] = svg.append('g').attr('class', k); |
| 13 | + }); |
| 14 | + const focus = Object.create(Focus.prototype); |
| 15 | + return Object.assign(focus, { |
| 16 | + svg, |
| 17 | + tags, |
| 18 | + h: 300, |
| 19 | + layout: LIST_LAYOUT.H1, |
| 20 | + scales: { x: d3.scaleLinear().domain([10, 0]).range([0, 1000]), y: d3.scaleLinear() }, |
| 21 | + shouldUpdate: {}, |
| 22 | + onClickTarget: () => {}, |
| 23 | + ...extra, |
| 24 | + }); |
| 25 | +}; |
| 26 | + |
| 27 | +const multiplet = (xL, xU) => ({ |
| 28 | + xExtent: { xL, xU }, |
| 29 | + mpyType: 'd', |
| 30 | + peaks: [{ x: xL + 0.01, y: 1 }, { x: xU - 0.01, y: 1 }], |
| 31 | +}); |
| 32 | +const stack = [multiplet(7.9, 8.0), multiplet(7.1, 7.2)]; |
| 33 | +const count = (focus) => ['.mpyb', '.mpyp', '.mpyt1', '.mpyt2'] |
| 34 | + .reduce((n, sel) => n + focus.svg.selectAll(sel).size(), 0); |
| 35 | + |
| 36 | +describe('drawMtply clears the multiplicity layers when there is nothing to draw', () => { |
| 37 | + afterEach(() => { document.body.innerHTML = ''; }); |
| 38 | + |
| 39 | + it('LineFocus: a stack, then an empty stack', () => { |
| 40 | + const focus = buildFocus(LineFocus); |
| 41 | + focus.drawMtply({ selectedIdx: 0, multiplicities: [{ stack, smExtext: false, shift: 0 }] }); |
| 42 | + expect(focus.svg.selectAll('.mpyt1').size()).toEqual(2); |
| 43 | + |
| 44 | + focus.drawMtply({ selectedIdx: 0, multiplicities: [{ stack: [], smExtext: false, shift: 0 }] }); |
| 45 | + expect(count(focus)).toEqual(0); |
| 46 | + }); |
| 47 | + |
| 48 | + it('LineFocus: a stack, then a non-NMR layout (multiplicity disabled)', () => { |
| 49 | + const focus = buildFocus(LineFocus); |
| 50 | + const mtplySt = { selectedIdx: 0, multiplicities: [{ stack, smExtext: false, shift: 0 }] }; |
| 51 | + focus.drawMtply(mtplySt); |
| 52 | + focus.layout = LIST_LAYOUT.IR; |
| 53 | + focus.drawMtply(mtplySt); |
| 54 | + expect(count(focus)).toEqual(0); |
| 55 | + }); |
| 56 | + |
| 57 | + it('MultiFocus: a stack, then an empty stack', () => { |
| 58 | + const focus = buildFocus(MultiFocus, { jcampIdx: 0 }); |
| 59 | + focus.drawMtply({ multiplicities: [{ stack, smExtext: false, shift: 0 }] }); |
| 60 | + expect(focus.svg.selectAll('.mpyt1').size()).toEqual(2); |
| 61 | + |
| 62 | + focus.drawMtply({ multiplicities: [{ stack: [], smExtext: false, shift: 0 }] }); |
| 63 | + expect(count(focus)).toEqual(0); |
| 64 | + }); |
| 65 | +}); |
0 commit comments