Completed
Push — master ( 984c1e...16ffbc )
by Alejandro
19s queued 10s
created

test/visits/helpers/GraphCard.test.js   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 93
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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