Skip to content

Commit 489bacc

Browse files
committed
test: update test case
1 parent cfb6f33 commit 489bacc

File tree

8 files changed

+450
-547
lines changed

8 files changed

+450
-547
lines changed

.babelrc

-4
This file was deleted.

__tests__/plots/line.spec.tsx

+56-31
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
12
import React, { createRef } from 'react'
23
import { createRoot } from 'react-dom/client'
3-
import { create } from 'react-test-renderer'
4+
import { create, act } from 'react-test-renderer'
45
import LineChart from '../../src/plots/line'
56
import { 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+
727
describe('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', () => {

babel.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
presets: ['@opd/babel-preset-component'],
3+
ignore: ['src/.umi/**/*.ts'],
4+
}

jest.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ module.exports = {
55
'./__tests__/setups/worker.js',
66
'./__tests__/setups/url.js',
77
],
8-
transformIgnorePatterns: ['/node_modules/', '/src/.umi/'],
8+
transformIgnorePatterns: ['/node_modules/!d3-*', '/src/.umi/'],
99
collectCoverageFrom: [
1010
'./src/**/*.{ts,tsx}',
1111
'!./**/*.d.ts',
1212
'!src/.umi/**/*.ts',
1313
],
1414
coverageReporters: ['json', 'text', 'lcov', 'clover', 'cobertura'],
1515
testEnvironment: 'jsdom',
16+
globals: {
17+
IS_REACT_ACT_ENVIRONMENT: true,
18+
},
1619
}

0 commit comments

Comments
 (0)