1
1
/**
2
2
* @jest -environment @shopify/react-native-skia/jestEnv.mjs
3
3
*/
4
+ declare global {
5
+ namespace jest {
6
+ interface Matchers < R > {
7
+ toMatchImageSnapshot ( ) : R ;
8
+ }
9
+ }
10
+ }
4
11
import React , { useEffect , useRef } from 'react' ;
5
12
import { Dimensions } from 'react-native' ;
6
13
import { render } from '@testing-library/react-native' ;
@@ -9,6 +16,19 @@ import SVGComponent from '../svgChart';
9
16
import { SVGRenderer } from '../SVGRenderer' ;
10
17
import * as echarts from 'echarts/core' ;
11
18
import { BarChart } from 'echarts/charts' ;
19
+ const sharp = require ( 'sharp' ) ;
20
+
21
+ const { toMatchImageSnapshot } = require ( 'jest-image-snapshot' ) ;
22
+
23
+ function getSVGImage ( svg : string ) {
24
+ const svgString = decodeURIComponent (
25
+ svg . replace ( 'data:image/svg+xml;charset=UTF-8,' , '' )
26
+ ) ;
27
+ return sharp ( Buffer . from ( svgString ) ) . png ( ) . toBuffer ( ) ;
28
+ }
29
+
30
+ expect . extend ( { toMatchImageSnapshot } ) ;
31
+
12
32
import {
13
33
TitleComponent ,
14
34
TooltipComponent ,
@@ -21,10 +41,11 @@ echarts.use([
21
41
SVGRenderer ,
22
42
BarChart ,
23
43
] ) ;
44
+
24
45
const Components = [ SkiaComponent , SVGComponent ] ;
25
46
const E_HEIGHT = 250 ;
26
- const E_WIDTH = Dimensions . get ( 'screen' ) . width ; //750
27
- const option = {
47
+ const E_WIDTH = Dimensions . get ( 'screen' ) . width ; //750
48
+ const OPTION = {
28
49
xAxis : {
29
50
type : 'category' ,
30
51
data : [ 'Mon' , 'Tue' ] ,
@@ -39,7 +60,8 @@ const option = {
39
60
} ,
40
61
] ,
41
62
} ;
42
- function Chart ( { option, Component } : any ) {
63
+
64
+ function Chart ( { option, Component, getDataURL } : any ) {
43
65
const ref = useRef < any > ( null ) ;
44
66
useEffect ( ( ) => {
45
67
let chart : any ;
@@ -51,17 +73,29 @@ function Chart({ option, Component }: any) {
51
73
height : E_HEIGHT ,
52
74
} ) ;
53
75
chart . setOption ( option ) ;
76
+ getDataURL ?.( chart . getDataURL ( ) ) ;
54
77
}
55
78
return ( ) => chart ?. dispose ( ) ;
56
- } , [ option ] ) ;
79
+ } , [ option , getDataURL ] ) ;
57
80
58
81
return < Component ref = { ref } /> ;
59
82
}
83
+
60
84
Components . forEach ( ( Component ) => {
61
85
describe ( `${ Component . displayName } Chart` || 'unknown' , ( ) => {
62
- it ( 'renders correctly' , ( ) => {
86
+ it ( 'renders correctly' , ( done ) => {
63
87
const { toJSON } = render (
64
- < Chart option = { option } Component = { Component } />
88
+ < Chart
89
+ option = { OPTION }
90
+ Component = { Component }
91
+ getDataURL = { ( data : string ) => {
92
+ expect ( data ) . toBeDefined ( ) ;
93
+ getSVGImage ( data ) . then ( ( d : Buffer ) => {
94
+ expect ( d ) . toMatchImageSnapshot ( ) ;
95
+ done ( ) ;
96
+ } ) ;
97
+ } }
98
+ />
65
99
) ;
66
100
expect ( toJSON ( ) ) . toMatchSnapshot ( ) ;
67
101
} ) ;
0 commit comments