|
| 1 | +/** |
| 2 | + * @format |
| 3 | + */ |
| 4 | + |
| 5 | +import 'react-native'; |
| 6 | +import React from 'react'; |
| 7 | +import ReactNativeFusionCharts from '../src/FusionCharts.js'; |
| 8 | +import { data } from '../testData/null_data.js'; |
| 9 | +import { schema } from '../testData/null_schema.js'; |
| 10 | + |
| 11 | +// Note: import explicitly to use the types shipped with jest. |
| 12 | +import { it } from '@jest/globals'; |
| 13 | + |
| 14 | +// Note: test renderer must be required after react-native. |
| 15 | +import renderer from 'react-test-renderer'; |
| 16 | +import { render } from '@testing-library/react-native'; |
| 17 | + |
| 18 | +jest.mock('react-native-webview', () => { |
| 19 | + const MockWebView = jest.requireActual('react-native').View; |
| 20 | + |
| 21 | + return { |
| 22 | + __esModule: true, |
| 23 | + WebView: MockWebView, |
| 24 | + default: MockWebView, |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +jest.mock('react-native-fs', () => ({ |
| 29 | + default: () => jest.fn() // or any mocked component instead of native view, |
| 30 | +})); |
| 31 | + |
| 32 | +jest.mock('@notifee/react-native', () => ({ |
| 33 | + default: () => jest.fn() // or any mocked component instead of native view, |
| 34 | +})); |
| 35 | + |
| 36 | +jest.mock('react-native-share', () => ({ |
| 37 | + default: () => jest.fn() // or any mocked component instead of native view, |
| 38 | +})); |
| 39 | + |
| 40 | +jest.mock('@react-native-camera-roll/camera-roll', () => ({ |
| 41 | + default: () => jest.fn() // or any mocked component instead of native view, |
| 42 | +})); |
| 43 | + |
| 44 | + |
| 45 | +it('Chart renders without crashing', () => { |
| 46 | + const chartData = [ |
| 47 | + { label: "Venezuela", value: "250" }, |
| 48 | + { label: "Saudi", value: "260" }, |
| 49 | + { label: "Canada", value: "180" }, |
| 50 | + { label: "Iran", value: "140" }, |
| 51 | + { label: "Russia", value: "115" }, |
| 52 | + { label: "UAE", value: "100" }, |
| 53 | + { label: "US", value: "30" }, |
| 54 | + { label: "China", value: "30" }, |
| 55 | + ]; |
| 56 | + const chartConfig = { |
| 57 | + type: "column2D", |
| 58 | + width: "300", |
| 59 | + height: "400", |
| 60 | + dataFormat: "json", |
| 61 | + dataSource: { |
| 62 | + chart: { |
| 63 | + caption: "Countries With Most Oil Reserves [2017-18]", |
| 64 | + subCaption: "In MMbbl = One Million barrels", |
| 65 | + xAxisName: "Country", |
| 66 | + yAxisName: "Reserves (MMbbl)", |
| 67 | + numberSuffix: "K", |
| 68 | + theme: "fusion", |
| 69 | + exportEnabled: 1 // to enable the export chart functionality |
| 70 | + }, |
| 71 | + data: chartData |
| 72 | + } |
| 73 | + }; |
| 74 | + const { toJSON } = render( |
| 75 | + <ReactNativeFusionCharts |
| 76 | + chartConfig={chartConfig} |
| 77 | + />); |
| 78 | + expect(toJSON()).toMatchSnapshot(); // Check if the component renders correctly |
| 79 | +}); |
| 80 | + |
| 81 | + |
| 82 | +it('TimeSeries chart renders without crashing', () => { |
| 83 | + let startTime = performance.now(); |
| 84 | + |
| 85 | + const chartConfig = { |
| 86 | + type: 'timeseries', |
| 87 | + width: '100%', |
| 88 | + height: '500', |
| 89 | + dataFormat: 'json', |
| 90 | + dataSource: { |
| 91 | + chart: {}, |
| 92 | + data: null, |
| 93 | + caption: { |
| 94 | + text: "Pollution Report of Yatcha Street" |
| 95 | + }, |
| 96 | + subcaption: { |
| 97 | + text: "An industrial town" |
| 98 | + }, |
| 99 | + yaxis: [{ |
| 100 | + plot: [{ |
| 101 | + value: "Pollution", |
| 102 | + "connectnulldata": "true", |
| 103 | + type: 'line' |
| 104 | + |
| 105 | + }], |
| 106 | + title: "Pollution Concentration (in ppm)", |
| 107 | + min: "130", |
| 108 | + |
| 109 | + referenceline: [{ |
| 110 | + label: "Controlled Temperature", |
| 111 | + value: "150" |
| 112 | + }] |
| 113 | + }] |
| 114 | + }, |
| 115 | + schemaJson: schema, |
| 116 | + dataJson: data |
| 117 | + }; |
| 118 | + |
| 119 | + const events = { |
| 120 | + dataplotclick: (e, a) => { |
| 121 | + alert(`You clicked on ${e.data.categoryLabel}`); |
| 122 | + }, |
| 123 | + "rendered": (e, a) => { |
| 124 | + let endTime = performance.now() |
| 125 | + |
| 126 | + console.log(`Call to doSomething took ${endTime - startTime} milliseconds.`); |
| 127 | + }, |
| 128 | + "referencelineclick": function (ev) { |
| 129 | + alert('reference line clicked') |
| 130 | + }, |
| 131 | + } |
| 132 | + const modules = ['timeseries']; |
| 133 | + |
| 134 | + |
| 135 | + const { toJSON } = render( |
| 136 | + <ReactNativeFusionCharts |
| 137 | + chartConfig={chartConfig} |
| 138 | + events={events} |
| 139 | + modules={modules} |
| 140 | + />); |
| 141 | + expect(toJSON()).toMatchSnapshot(); // Check if the component renders correctly |
| 142 | +}); |
0 commit comments