|
1 | | -import { shallowMount } from '@vue/test-utils'; |
2 | | -import Chart from '@/index'; |
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | +import VueHighchart from '@/index'; |
| 3 | +import { wrap } from 'highcharts'; |
3 | 4 |
|
4 | | -describe('HelloWorld.vue', () => { |
5 | | - it('renders props.msg when passed', () => { |
6 | | - const msg = 'new message'; |
7 | | - const wrapper = shallowMount(Chart, { |
8 | | - props: { msg }, |
| 5 | +const defaultOptions = { |
| 6 | + series: [{ |
| 7 | + name: 'Test Series', |
| 8 | + data: [1, 2, 3], |
| 9 | + }], |
| 10 | +}; |
| 11 | + |
| 12 | +describe('VueHighchart', () => { |
| 13 | + describe('Plugin', () => { |
| 14 | + it('should install when used as a plugin', () => { |
| 15 | + const componentStub = jest.fn(); |
| 16 | + |
| 17 | + const appMock = { |
| 18 | + component: componentStub, |
| 19 | + }; |
| 20 | + |
| 21 | + expect(VueHighchart).toHaveProperty('install'); |
| 22 | + |
| 23 | + VueHighchart.install(appMock); |
| 24 | + |
| 25 | + expect(componentStub).toHaveBeenCalledWith(VueHighchart.name, VueHighchart); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('Component', () => { |
| 30 | + let wrapper = null; |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + if (wrapper) { |
| 34 | + wrapper.unmount(); |
| 35 | + wrapper = null; |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + it('should have the correct props', () => { |
| 40 | + wrapper = mount(VueHighchart, { |
| 41 | + props: { |
| 42 | + options: { ...defaultOptions }, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + expect(wrapper.props()).toEqual({ |
| 47 | + type: 'chart', |
| 48 | + options: { ...defaultOptions }, |
| 49 | + redrawOnUpdate: true, |
| 50 | + oneToOneUpdate: false, |
| 51 | + animateOnUpdate: true, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should expose the rendered highcharts instance', () => { |
| 56 | + wrapper = mount(VueHighchart, { |
| 57 | + props: { |
| 58 | + options: { ...defaultOptions }, |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + expect(wrapper.vm.chart).toBeDefined(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should render a highchart chart with the expected data', () => { |
| 66 | + wrapper = mount(VueHighchart, { |
| 67 | + props: { |
| 68 | + options: { ...defaultOptions }, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + const { vm } = wrapper; |
| 73 | + |
| 74 | + expect(wrapper.get('.vue-highcharts')).toBeTruthy(); |
| 75 | + expect(wrapper.get('.highcharts-container')).toBeTruthy(); |
| 76 | + |
| 77 | + expect(vm.chart.series[0].yData).toEqual(wrapper.props().options.series[0].data); |
| 78 | + expect(vm.chart.series[0].name).toEqual(wrapper.props().options.series[0].name); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should emit a rendered event when the chart is rendered', () => { |
| 82 | + wrapper = mount(VueHighchart, { |
| 83 | + props: { |
| 84 | + options: { ...defaultOptions }, |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + expect(wrapper.emitted()).toHaveProperty('rendered'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should emit an updated event when the chart is updated due to options changes', async () => { |
| 92 | + const options = { ...defaultOptions }; |
| 93 | + |
| 94 | + wrapper = mount(VueHighchart, { |
| 95 | + props: { |
| 96 | + options, |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(wrapper.get('.highcharts-container')).toBeTruthy(); |
| 101 | + |
| 102 | + await wrapper.setProps({ |
| 103 | + options: { |
| 104 | + series: [{ |
| 105 | + name: 'Test Series 2', |
| 106 | + data: [1, 2, 3, 4], |
| 107 | + }], |
| 108 | + }, |
| 109 | + }); |
| 110 | + |
| 111 | + expect(wrapper.emitted()).toHaveProperty('updated'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should emit a destroyed event when the highcharts instance is destroyed', async () => { |
| 115 | + const options = { ...defaultOptions }; |
| 116 | + |
| 117 | + wrapper = mount(VueHighchart, { |
| 118 | + props: { |
| 119 | + options, |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + expect(wrapper.get('.highcharts-container')).toBeTruthy(); |
| 124 | + |
| 125 | + wrapper.unmount(); |
| 126 | + |
| 127 | + expect(wrapper.emitted()).toHaveProperty('destroyed'); |
9 | 128 | }); |
10 | | - expect(wrapper.text()).toMatch(msg); |
11 | 129 | }); |
12 | 130 | }); |
0 commit comments