1+ /* eslint-disable @typescript-eslint/ban-ts-comment */
12import React , { createRef } from 'react'
23import { createRoot } from 'react-dom/client'
3- import { create } from 'react-test-renderer'
4+ import { create , act } from 'react-test-renderer'
45import LineChart from '../../src/plots/line'
56import { LineOptions , Plot as BasePlot } from '@antv/g2plot'
67
8+ let div
9+ let root
10+
11+ beforeEach ( ( ) => {
12+ div = document . createElement ( 'div' )
13+ document . body . appendChild ( div )
14+ act ( ( ) => {
15+ root = createRoot ( div )
16+ } )
17+ } )
18+
19+ afterEach ( ( ) => {
20+ act ( ( ) => {
21+ root . unmount ( )
22+ } )
23+ document . body . removeChild ( div )
24+ div = null
25+ } )
26+
727describe ( 'LineChart' , ( ) => {
828 test ( 'render without crashed' , ( ) => {
9- let div = document . createElement ( 'div' )
10- const root = createRoot ( div )
11- root . render ( < LineChart data = { [ ] } /> )
12- root . unmount ( )
13- div = null
29+ act ( ( ) => {
30+ root . render ( < LineChart data = { [ ] } /> )
31+ } )
1432 } )
1533
1634 test ( 'object ref should be assigned' , ( ) => {
1735 const ref = createRef < HTMLDivElement | null > ( )
1836 const chartRef = createRef < BasePlot < LineOptions > | null > ( )
19- const div = document . createElement ( 'div' )
20- const root = createRoot ( div )
21- root . render ( < LineChart data = { [ ] } ref = { ref } chartRef = { chartRef } /> )
22- root . unmount ( )
37+ act ( ( ) => {
38+ root . render ( < LineChart data = { [ ] } ref = { ref } chartRef = { chartRef } /> )
39+ } )
2340 expect ( ref . current ) . toBeDefined ( )
2441 expect ( chartRef . current ) . toBeDefined ( )
2542 } )
@@ -28,23 +45,21 @@ describe('LineChart', () => {
2845 const onReady = ( plot : BasePlot < LineOptions > ) => {
2946 expect ( plot ) . toBeDefined ( )
3047 }
31- const div = document . createElement ( 'div' )
32- const root = createRoot ( div )
33- root . render ( < LineChart data = { [ ] } onReady = { onReady } /> )
34- root . unmount ( )
48+ act ( ( ) => {
49+ root . render ( < LineChart data = { [ ] } onReady = { onReady } /> )
50+ } )
3551 } )
3652
3753 test ( 'function ref should be called' , ( ) => {
38- // let chart
54+ let chart
3955 const getChart = ( instance ) => {
40- expect ( instance ) . toBeTruthy ( )
56+ chart = instance
4157 }
42- const div = document . createElement ( 'div' )
43- const root = createRoot ( div )
44- root . render ( < LineChart data = { [ ] } chartRef = { getChart } /> )
45- root . unmount ( )
58+ act ( ( ) => {
59+ root . render ( < LineChart data = { [ ] } chartRef = { getChart } /> )
60+ } )
4661
47- // expect(chart).toBeDefined()
62+ expect ( chart ) . toBeDefined ( )
4863 } )
4964
5065 test ( 'test update config and data' , ( ) => {
@@ -70,19 +85,29 @@ describe('LineChart', () => {
7085 } ,
7186 } ,
7287 }
73- const div = document . createElement ( 'div' )
74- const root = createRoot ( div )
75- root . render ( < LineChart { ...config } data = { null } /> )
76-
77- root . render ( < LineChart { ...config } data = { [ ] } autoFit /> )
88+ act ( ( ) => {
89+ // @ts -ignore
90+ root . render ( < LineChart { ...config } data = { null } /> )
91+ } )
7892
79- root . render ( < LineChart { ...config } data = { null } autoFit /> )
93+ act ( ( ) => {
94+ root . render ( < LineChart { ...config } data = { [ ] } autoFit /> )
95+ } )
8096
81- root . render ( < LineChart { ...config } autoFit /> )
82- root . render ( < LineChart { ...config } autoFit data = { [ ] } /> )
83- root . render ( < LineChart { ...config } data = { [ ] } autoFit /> )
97+ act ( ( ) => {
98+ // @ts -ignore
99+ root . render ( < LineChart { ...config } data = { null } autoFit /> )
100+ } )
84101
85- root . unmount ( )
102+ act ( ( ) => {
103+ root . render ( < LineChart { ...config } autoFit /> )
104+ } )
105+ act ( ( ) => {
106+ root . render ( < LineChart { ...config } autoFit data = { [ ] } /> )
107+ } )
108+ act ( ( ) => {
109+ root . render ( < LineChart { ...config } data = { [ ] } autoFit /> )
110+ } )
86111 } )
87112
88113 test ( 'lifecycle' , ( ) => {
0 commit comments