File size: 2,379 Bytes
76a7a50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*!
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

import { mount } from '@vue/test-utils';

// translate just echoes the key.
jest.mock('CoreHome', () => ({
  translate: (key: string) => key,
}), { virtual: true });

// eslint-disable-next-line @typescript-eslint/no-var-requires
const MetricsPickerOptions = require('./MetricsPickerOptions.vue').default;

const selectableColumns = [
  { column: 'nb_visits', translation: 'Visits' },
  { column: 'nb_uniq_visitors', translation: 'Unique visitors' },
];

const selectableRows = [
  { matcher: 'Row 1', label: 'Row 1' },
  { matcher: 'Row 2', label: 'Row 2' },
];

function mountOptions(props: Record<string, unknown> = {}) {
  return mount(MetricsPickerOptions, {
    props: { selectableColumns, selectableRows, ...props },
    global: { mocks: { translate: (key: string) => key } },
  });
}

function lastSelect(wrapper: ReturnType<typeof mountOptions>) {
  const events = wrapper.emitted('select') as unknown[][];
  return events[events.length - 1][0];
}

describe('CoreVisualizations/MetricsPickerOptions.vue', () => {
  it('in single-select mode, picking an option clears any other selection and emits just that one', async () => {
    const wrapper = mountOptions({ multiselect: false, selectedColumns: ['nb_visits'] });

    await wrapper.findAll('.metrics-picker__row')[0].find('input').trigger('change');

    expect(lastSelect(wrapper)).toEqual({ columns: [], rows: ['Row 1'] });
  });

  it('in multiselect mode, selections accumulate across columns and rows', async () => {
    const wrapper = mountOptions({ multiselect: true, selectedColumns: ['nb_visits'] });

    await wrapper.findAll('.metrics-picker__column')[1].find('input').trigger('change');
    await wrapper.findAll('.metrics-picker__row')[0].find('input').trigger('change');

    expect(lastSelect(wrapper)).toEqual({
      columns: ['nb_visits', 'nb_uniq_visitors'],
      rows: ['Row 1'],
    });
  });

  it('in multiselect mode, clicking a selected option toggles it off', async () => {
    const wrapper = mountOptions({ multiselect: true, selectedColumns: ['nb_visits'] });

    await wrapper.findAll('.metrics-picker__column')[0].find('input').trigger('change');

    expect(lastSelect(wrapper)).toEqual({ columns: [], rows: [] });
  });
});