Skip to content

Commit 8ea176d

Browse files
committed
feat: add image snap test
1 parent 9aba8f2 commit 8ea176d

7 files changed

+283
-9
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,6 @@ lib/
7777

7878
# Docusaurus (when switching from docs branches to code branches)
7979
.docusaurus/
80+
81+
# jest
82+
coverage/

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"eslint-config-prettier": "^8.5.0",
7070
"eslint-plugin-prettier": "^4.0.0",
7171
"jest": "^29.7.0",
72+
"jest-image-snapshot": "^6.4.0",
7273
"pod-install": "^0.1.0",
7374
"prettier": "^2.0.5",
7475
"react": "18.2.0",
@@ -78,6 +79,7 @@
7879
"react-native-svg": "14.1.0",
7980
"react-test-renderer": "^18.2.0",
8081
"release-it": "^15.0.0",
82+
"sharp": "^0.33.2",
8183
"typescript": "^5.0.4",
8284
"zrender": "^5.5.0"
8385
},

src/__tests__/chart.test.tsx

+38-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
/**
22
* @jest-environment @shopify/react-native-skia/jestEnv.mjs
33
*/
4+
declare global {
5+
namespace jest {
6+
interface Matchers<R> {
7+
toMatchImageSnapshot(): R;
8+
}
9+
}
10+
}
411
import React, { useEffect, useRef } from 'react';
512
import { Dimensions } from 'react-native';
613
import { render } from '@testing-library/react-native';
@@ -9,6 +16,19 @@ import SVGComponent from '../svgChart';
916
import { SVGRenderer } from '../SVGRenderer';
1017
import * as echarts from 'echarts/core';
1118
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+
1232
import {
1333
TitleComponent,
1434
TooltipComponent,
@@ -21,9 +41,10 @@ echarts.use([
2141
SVGRenderer,
2242
BarChart,
2343
]);
44+
2445
const Components = [SkiaComponent, SVGComponent];
2546
const E_HEIGHT = 250;
26-
const E_WIDTH = Dimensions.get('screen').width;//750
47+
const E_WIDTH = Dimensions.get('screen').width; //750
2748
const option = {
2849
xAxis: {
2950
type: 'category',
@@ -39,7 +60,8 @@ const option = {
3960
},
4061
],
4162
};
42-
function Chart({ option, Component }: any) {
63+
64+
function Chart({ option, Component, getDataURL }: any) {
4365
const ref = useRef<any>(null);
4466
useEffect(() => {
4567
let chart: any;
@@ -51,17 +73,29 @@ function Chart({ option, Component }: any) {
5173
height: E_HEIGHT,
5274
});
5375
chart.setOption(option);
76+
getDataURL?.(chart.getDataURL());
5477
}
5578
return () => chart?.dispose();
5679
}, [option]);
5780

5881
return <Component ref={ref} />;
5982
}
83+
6084
Components.forEach((Component) => {
6185
describe(`${Component.displayName} Chart` || 'unknown', () => {
62-
it('renders correctly', () => {
86+
it('renders correctly', (done) => {
6387
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+
/>
6599
);
66100
expect(toJSON()).toMatchSnapshot();
67101
});

src/skiaChart.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function SkiaComponent(
5555
const [width, setWidth] = useState<number>(initialWidth ?? 0);
5656
const [height, setHeight] = useState<number>(initialHeight ?? 0);
5757
const zrenderId = useRef<number>();
58-
const canvasRef = useCanvasRef();
58+
const canvasRef = useCanvasRef?.();
5959

6060
const dispatchEvents = useCallback<DispatchEvents>(
6161
(types, nativeEvent, eventArgs) => {
@@ -88,7 +88,7 @@ function SkiaComponent(
8888
zrenderId.current = id;
8989
},
9090
makeImageSnapshot: () => {
91-
const image = canvasRef.current?.makeImageSnapshot();
91+
const image = canvasRef?.current?.makeImageSnapshot();
9292
return image ? `data:image/png;base64,${image.encodeToBase64()}` : null;
9393
}
9494
},

0 commit comments

Comments
 (0)