1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
1
2
import React , { createRef } from 'react'
2
3
import { createRoot } from 'react-dom/client'
3
- import { create } from 'react-test-renderer'
4
+ import { create , act } from 'react-test-renderer'
4
5
import LineChart from '../../src/plots/line'
5
6
import { LineOptions , Plot as BasePlot } from '@antv/g2plot'
6
7
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
+
7
27
describe ( 'LineChart' , ( ) => {
8
28
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
+ } )
14
32
} )
15
33
16
34
test ( 'object ref should be assigned' , ( ) => {
17
35
const ref = createRef < HTMLDivElement | null > ( )
18
36
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
+ } )
23
40
expect ( ref . current ) . toBeDefined ( )
24
41
expect ( chartRef . current ) . toBeDefined ( )
25
42
} )
@@ -28,23 +45,21 @@ describe('LineChart', () => {
28
45
const onReady = ( plot : BasePlot < LineOptions > ) => {
29
46
expect ( plot ) . toBeDefined ( )
30
47
}
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
+ } )
35
51
} )
36
52
37
53
test ( 'function ref should be called' , ( ) => {
38
- // let chart
54
+ let chart
39
55
const getChart = ( instance ) => {
40
- expect ( instance ) . toBeTruthy ( )
56
+ chart = instance
41
57
}
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
+ } )
46
61
47
- // expect(chart).toBeDefined()
62
+ expect ( chart ) . toBeDefined ( )
48
63
} )
49
64
50
65
test ( 'test update config and data' , ( ) => {
@@ -70,19 +85,29 @@ describe('LineChart', () => {
70
85
} ,
71
86
} ,
72
87
}
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
+ } )
78
92
79
- root . render ( < LineChart { ...config } data = { null } autoFit /> )
93
+ act ( ( ) => {
94
+ root . render ( < LineChart { ...config } data = { [ ] } autoFit /> )
95
+ } )
80
96
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
+ } )
84
101
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
+ } )
86
111
} )
87
112
88
113
test ( 'lifecycle' , ( ) => {
0 commit comments