|
1 | 1 | import * as React from 'react';
|
2 |
| -import TestRenderer from 'react-test-renderer'; |
| 2 | +import Highcharts from 'highcharts/highstock'; |
| 3 | +import 'highcharts/modules/accessibility'; |
3 | 4 |
|
4 |
| -import XAxis from '../../../src/components/XAxis/XAxis'; |
5 |
| -import Axis from '../../../src/components/Axis'; |
6 |
| -import ChartContext from '../../../src/components/ChartContext'; |
7 |
| -import { createMockProvidedChart } from '../../test-utils'; |
| 5 | +import { render } from '@testing-library/react'; |
8 | 6 |
|
9 |
| -describe('<XAxis />', () => { |
10 |
| - let testContext; |
11 |
| - let ProvidedAxis; |
12 |
| - beforeEach(() => { |
13 |
| - testContext = {}; |
14 |
| - const { chartStubs } = createMockProvidedChart({ |
15 |
| - type: 'chart' |
16 |
| - }); |
17 |
| - testContext.chartStubs = chartStubs; |
| 7 | +import { HighchartsChart, HighchartsProvider, XAxis } from '../../../src'; |
| 8 | +import ContextSpy from '../../ContextSpy'; |
18 | 9 |
|
19 |
| - ProvidedAxis = props => ( |
20 |
| - <ChartContext.Provider value={chartStubs}> |
21 |
| - <XAxis {...props} /> |
22 |
| - </ChartContext.Provider> |
23 |
| - ); |
24 |
| - }); |
| 10 | +describe('<XAxis />', () => { |
| 11 | + it('creates an chart xaxis', () => { |
| 12 | + let chartRef = {}; |
| 13 | + const Component = () => { |
| 14 | + return ( |
| 15 | + <HighchartsProvider Highcharts={Highcharts}> |
| 16 | + <HighchartsChart> |
| 17 | + <XAxis id="myAxis"> |
| 18 | + <ContextSpy chartRef={chartRef} /> |
| 19 | + </XAxis> |
| 20 | + </HighchartsChart> |
| 21 | + </HighchartsProvider> |
| 22 | + ); |
| 23 | + }; |
25 | 24 |
|
26 |
| - it('renders an <Axis />', () => { |
27 |
| - const wrapper = new TestRenderer.create(<ProvidedAxis />); |
28 |
| - const axis = wrapper.root.findByType(Axis); |
| 25 | + render(<Component />); |
29 | 26 |
|
| 27 | + const axis = chartRef.current.object.get('myAxis'); |
30 | 28 | expect(axis).toBeDefined();
|
31 |
| - }); |
32 |
| - |
33 |
| - it('renders an <Axis isX />', () => { |
34 |
| - const wrapper = new TestRenderer.create(<ProvidedAxis />); |
35 |
| - const axis = wrapper.root.findByType(Axis); |
36 |
| - |
37 |
| - expect(axis.props).toHaveProperty('isX', true); |
| 29 | + expect(axis.isXAxis).toBeDefined(); |
38 | 30 | });
|
39 | 31 |
|
40 | 32 | it('passes other props through to <Axis />', () => {
|
41 |
| - const wrapper = new TestRenderer.create(<ProvidedAxis tickLength={1337} />); |
42 |
| - const axis = wrapper.root.findByType(Axis); |
43 |
| - |
44 |
| - expect(axis.props).toHaveProperty('tickLength', 1337); |
45 |
| - }); |
46 |
| - |
47 |
| - describe('Highcharts chart', () => { |
48 |
| - beforeEach(() => { |
49 |
| - testContext.chartStubs.type = 'chart'; |
50 |
| - }); |
51 |
| - |
52 |
| - it('renders the <Axis /> type if provided', () => { |
53 |
| - const wrapper = new TestRenderer.create( |
54 |
| - <ProvidedAxis type="logarithmic" /> |
| 33 | + let chartRef = {}; |
| 34 | + const Component = () => { |
| 35 | + return ( |
| 36 | + <HighchartsProvider Highcharts={Highcharts}> |
| 37 | + <HighchartsChart> |
| 38 | + <XAxis id="myAxis" tickLength={1337}> |
| 39 | + <ContextSpy chartRef={chartRef} /> |
| 40 | + </XAxis> |
| 41 | + </HighchartsChart> |
| 42 | + </HighchartsProvider> |
55 | 43 | );
|
56 |
| - const axis = wrapper.root.findByType(Axis); |
| 44 | + }; |
57 | 45 |
|
58 |
| - expect(axis.props).toHaveProperty('type', 'logarithmic'); |
59 |
| - }); |
60 |
| - |
61 |
| - it('renders the an <Axis type="linear" /> if no type specified', () => { |
62 |
| - const wrapper = new TestRenderer.create(<ProvidedAxis />); |
63 |
| - const axis = wrapper.root.findByType(Axis); |
64 |
| - |
65 |
| - expect(axis.props).toHaveProperty('type', 'linear'); |
66 |
| - }); |
| 46 | + render(<Component />); |
67 | 47 |
|
68 |
| - it('uses the id prop if provided', () => { |
69 |
| - const wrapper = new TestRenderer.create(<ProvidedAxis id="myXAxisId" />); |
70 |
| - const axis = wrapper.root.findByType(Axis); |
71 |
| - |
72 |
| - expect(axis.props).toHaveProperty('id', 'myXAxisId'); |
73 |
| - }); |
74 |
| - |
75 |
| - it('does not create an id if id prop not provided', () => { |
76 |
| - const wrapper = new TestRenderer.create(<ProvidedAxis />); |
77 |
| - const axis = wrapper.root.findByType(Axis); |
78 |
| - |
79 |
| - expect(axis.props.id).not.toBeDefined(); |
80 |
| - }); |
| 48 | + const axis = chartRef.current.object.get('myAxis'); |
| 49 | + expect(axis).toBeDefined(); |
| 50 | + expect(axis.userOptions.tickLength).toBe(1337); |
81 | 51 | });
|
82 | 52 |
|
83 |
| - describe('Highstock chart', () => { |
84 |
| - beforeEach(() => { |
85 |
| - testContext.chartStubs.type = 'stockChart'; |
86 |
| - }); |
87 |
| - |
| 53 | + describe('Highcharts chart', () => { |
88 | 54 | it('renders the <Axis /> type if provided', () => {
|
89 |
| - const wrapper = new TestRenderer.create( |
90 |
| - <ProvidedAxis type="logarithmic" /> |
91 |
| - ); |
92 |
| - const axis = wrapper.root.findByType(Axis); |
93 |
| - |
94 |
| - expect(axis.props).toHaveProperty('type', 'logarithmic'); |
| 55 | + let chartRef = {}; |
| 56 | + const Component = () => { |
| 57 | + return ( |
| 58 | + <HighchartsProvider Highcharts={Highcharts}> |
| 59 | + <HighchartsChart> |
| 60 | + <XAxis id="myAxis" type="logarithmic"> |
| 61 | + <ContextSpy chartRef={chartRef} /> |
| 62 | + </XAxis> |
| 63 | + </HighchartsChart> |
| 64 | + </HighchartsProvider> |
| 65 | + ); |
| 66 | + }; |
| 67 | + |
| 68 | + render(<Component />); |
| 69 | + |
| 70 | + const axis = chartRef.current.object.get('myAxis'); |
| 71 | + expect(axis).toBeDefined(); |
| 72 | + expect(axis.isXAxis).toBe(true); |
| 73 | + expect(axis.userOptions.type).toBe('logarithmic'); |
95 | 74 | });
|
96 | 75 |
|
97 |
| - it('renders the an <Axis type="datetime" /> if no type specified', () => { |
98 |
| - const wrapper = new TestRenderer.create(<ProvidedAxis />); |
99 |
| - const axis = wrapper.root.findByType(Axis); |
100 |
| - |
101 |
| - expect(axis.props).toHaveProperty('type', 'datetime'); |
102 |
| - }); |
103 |
| - |
104 |
| - it('uses the id `xAxis` even if an id prop is provided', () => { |
105 |
| - const wrapper = new TestRenderer.create(<ProvidedAxis id="myXAxisId" />); |
106 |
| - const axis = wrapper.root.findByType(Axis); |
107 |
| - |
108 |
| - expect(axis.props).toHaveProperty('id', 'xAxis'); |
109 |
| - }); |
110 |
| - |
111 |
| - it('uses the id `xAxis` if id prop not provided', () => { |
112 |
| - const wrapper = new TestRenderer.create(<ProvidedAxis />); |
113 |
| - const axis = wrapper.root.findByType(Axis); |
114 |
| - |
115 |
| - expect(axis.props).toHaveProperty('id', 'xAxis'); |
| 76 | + it('renders the an <Axis type="linear" /> if no type specified', () => { |
| 77 | + let chartRef = {}; |
| 78 | + const Component = () => { |
| 79 | + return ( |
| 80 | + <HighchartsProvider Highcharts={Highcharts}> |
| 81 | + <HighchartsChart> |
| 82 | + <XAxis id="myAxis"> |
| 83 | + <ContextSpy chartRef={chartRef} /> |
| 84 | + </XAxis> |
| 85 | + </HighchartsChart> |
| 86 | + </HighchartsProvider> |
| 87 | + ); |
| 88 | + }; |
| 89 | + |
| 90 | + render(<Component />); |
| 91 | + |
| 92 | + const axis = chartRef.current.object.get('myAxis'); |
| 93 | + expect(axis).toBeDefined(); |
| 94 | + expect(axis.userOptions.type).toBe('linear'); |
116 | 95 | });
|
117 | 96 | });
|
118 | 97 | });
|
0 commit comments