test/visits/helpers/DefaultChart.test.js   A
last analyzed

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 99
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 89
mnd 1
bc 1
fnc 0
dl 0
loc 99
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React from 'react';
2
import { shallow } from 'enzyme';
3
import { Doughnut, HorizontalBar } from 'react-chartjs-2';
4
import { keys, values } from 'ramda';
5
import DefaultChart from '../../../src/visits/helpers/DefaultChart';
6
7
describe('<DefaultChart />', () => {
8
  let wrapper;
9
  const stats = {
10
    foo: 123,
11
    bar: 456,
12
  };
13
14
  afterEach(() => wrapper && wrapper.unmount());
15
16
  it('renders Doughnut when is not a bar chart', () => {
17
    wrapper = shallow(<DefaultChart title="The chart" stats={stats} />);
18
    const doughnut = wrapper.find(Doughnut);
19
    const horizontal = wrapper.find(HorizontalBar);
20
    const cols = wrapper.find('.col-sm-12');
21
22
    expect(doughnut).toHaveLength(1);
23
    expect(horizontal).toHaveLength(0);
24
25
    const { labels, datasets } = doughnut.prop('data');
26
    const [{ title, data, backgroundColor, borderColor }] = datasets;
27
    const { legend, legendCallback, scales } = doughnut.prop('options');
28
29
    expect(title).toEqual('The chart');
30
    expect(labels).toEqual(keys(stats));
31
    expect(data).toEqual(values(stats));
32
    expect(datasets).toHaveLength(1);
33
    expect(backgroundColor).toEqual([
34
      '#97BBCD',
35
      '#F7464A',
36
      '#46BFBD',
37
      '#FDB45C',
38
      '#949FB1',
39
      '#57A773',
40
      '#414066',
41
      '#08B2E3',
42
      '#B6C454',
43
      '#DCDCDC',
44
      '#463730',
45
    ]);
46
    expect(borderColor).toEqual('white');
47
    expect(legend).toEqual({ display: false });
48
    expect(typeof legendCallback).toEqual('function');
49
    expect(scales).toBeUndefined();
50
    expect(cols).toHaveLength(2);
51
  });
52
53
  it('renders HorizontalBar when is not a bar chart', () => {
54
    wrapper = shallow(<DefaultChart isBarChart title="The chart" stats={stats} />);
55
    const doughnut = wrapper.find(Doughnut);
56
    const horizontal = wrapper.find(HorizontalBar);
57
    const cols = wrapper.find('.col-sm-12');
58
59
    expect(doughnut).toHaveLength(0);
60
    expect(horizontal).toHaveLength(1);
61
62
    const { datasets: [{ backgroundColor, borderColor }] } = horizontal.prop('data');
63
    const { legend, legendCallback, scales } = horizontal.prop('options');
64
65
    expect(backgroundColor).toEqual('rgba(70, 150, 229, 0.4)');
66
    expect(borderColor).toEqual('rgba(70, 150, 229, 1)');
67
    expect(legend).toEqual({ display: false });
68
    expect(legendCallback).toEqual(false);
69
    expect(scales).toEqual({
70
      xAxes: [
71
        {
72
          ticks: { beginAtZero: true, precision: 0 },
73
          stacked: true,
74
        },
75
      ],
76
      yAxes: [{ stacked: true }],
77
    });
78
    expect(cols).toHaveLength(1);
79
  });
80
81
  it.each([
82
    [{ foo: 23 }, [ 100, 456 ], [ 23, 0 ]],
83
    [{ foo: 50 }, [ 73, 456 ], [ 50, 0 ]],
84
    [{ bar: 45 }, [ 123, 411 ], [ 0, 45 ]],
85
    [{ bar: 20, foo: 13 }, [ 110, 436 ], [ 13, 20 ]],
86
    [ undefined, [ 123, 456 ], undefined ],
87
  ])('splits highlighted data from regular data', (highlightedStats, expectedData, expectedHighlightedData) => {
88
    wrapper = shallow(<DefaultChart isBarChart title="The chart" stats={stats} highlightedStats={highlightedStats} />);
89
    const horizontal = wrapper.find(HorizontalBar);
90
91
    const { datasets: [{ data, label }, highlightedData ] } = horizontal.prop('data');
92
93
    expect(label).toEqual(highlightedStats ? 'Non-selected' : 'Visits');
94
    expect(data).toEqual(expectedData);
95
    expectedHighlightedData && expect(highlightedData.data).toEqual(expectedHighlightedData);
96
    !expectedHighlightedData && expect(highlightedData).toBeUndefined();
97
  });
98
});
99