From a3feb9307e416fc8ef368b3f186ad7ce217d58ff Mon Sep 17 00:00:00 2001 From: RoXoM Date: Mon, 9 Oct 2023 00:22:32 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E8=A1=A5=E5=85=A8=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AMapMap/__tests__/AMapMap.test.tsx | 4 + .../AMapMarker/__tests__/AMapMarker.test.tsx | 288 ++++++++++++++++++ 2 files changed, 292 insertions(+) create mode 100644 src/components/AMapMarker/__tests__/AMapMarker.test.tsx diff --git a/src/components/AMapMap/__tests__/AMapMap.test.tsx b/src/components/AMapMap/__tests__/AMapMap.test.tsx index e60d893..3c3b245 100644 --- a/src/components/AMapMap/__tests__/AMapMap.test.tsx +++ b/src/components/AMapMap/__tests__/AMapMap.test.tsx @@ -25,6 +25,10 @@ jest.mock('../../../hooks/useAMapAPI', () => ({ })); describe('AMapMap', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + afterEach(cleanup); test('render without error', () => { diff --git a/src/components/AMapMarker/__tests__/AMapMarker.test.tsx b/src/components/AMapMarker/__tests__/AMapMarker.test.tsx new file mode 100644 index 0000000..d9f4106 --- /dev/null +++ b/src/components/AMapMarker/__tests__/AMapMarker.test.tsx @@ -0,0 +1,288 @@ +import * as React from 'react'; +import { render, cleanup } from '@testing-library/react'; + +import useAMapPluginInstance from '../../../hooks/useAMapPluginInstance'; + +import AMapMarker from '../AMapMarker'; + +jest.mock('../../../hooks/useAMapPluginInstance', () => ({ + esModule: true, + default: jest.fn((__, cb) => { + cb({ + Marker: jest.fn(), + }, {}); + }), +})); + +describe('AMapMarker Component', () => { + const mockPosition: [number, number] = [118, 23]; + beforeEach(() => { + jest.clearAllMocks(); + }); + afterEach(cleanup); + + test('renders without crashing', () => { + expect(() => { + render(); + }).not.toThrowError(); + expect(useAMapPluginInstance).toHaveBeenCalledWith('Marker', expect.any(Function)); + }); + + test('renders without crashing when instance is null', () => { + (useAMapPluginInstance as jest.Mock).mockReturnValue(null); + expect(() => { + render(); + }).not.toThrowError(); + }); + + test('set title', () => { + const mockInstance = { + setTitle: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + render(); + + expect(mockInstance.setTitle).toHaveBeenCalledWith('123'); + }); + + test('set icon', () => { + const mockInstance = { + setIcon: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + const icon = ''; + render(); + + expect(mockInstance.setIcon).toHaveBeenCalledWith(icon); + }); + + test('set label', () => { + const mockInstance = { + setLabel: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + const label = { + content: '123', + offset: [0, 10], + direction: 'center', + }; + render(); + + expect(mockInstance.setLabel).toHaveBeenCalledWith(label); + }); + + test('set clickable', () => { + const mockInstance = { + setClickable: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + render(); + + expect(mockInstance.setClickable).toHaveBeenCalledWith(true); + }); + + test('set draggable', () => { + const mockInstance = { + setDraggable: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + render(); + + expect(mockInstance.setDraggable).toHaveBeenCalledWith(true); + }); + + test('set cursor', () => { + const mockInstance = { + setCursor: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + render(); + + expect(mockInstance.setCursor).toHaveBeenCalledWith(''); + }); + + test('set ext-data', () => { + const mockInstance = { + setExtData: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + const extData = { id: 1 }; + render(); + + expect(mockInstance.setExtData).toHaveBeenCalledWith(extData); + }); + + test('set anchor', () => { + const mockInstance = { + setAnchor: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + render(); + + expect(mockInstance.setAnchor).toHaveBeenCalledWith('center'); + }); + + test('set offset', () => { + const mockInstance = { + setOffset: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + const offset: [number, number] = [1, 1]; + render(); + + expect(mockInstance.setOffset).toHaveBeenCalledWith(offset); + }); + + test('set angle', () => { + const mockInstance = { + setAngle: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + render(); + + expect(mockInstance.setAngle).toHaveBeenCalledWith(45); + }); + + test('set size', () => { + const mockInstance = { + setSize: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + const size: [number, number] = [1, 1]; + render(); + + expect(mockInstance.setSize).toHaveBeenCalledWith(size); + }); + + test('set z-index', () => { + const mockInstance = { + setzIndex: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + render(); + + expect(mockInstance.setzIndex).toHaveBeenCalledWith(2); + }); + + test('set content', () => { + const mockInstance = { + setContent: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + render(); + + expect(mockInstance.setContent).toHaveBeenCalledWith('content'); + }); + + test('set to invisible', () => { + const mockInstance = { + show: jest.fn(), + hide: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + const { rerender } = render(); + + expect(mockInstance.show).toBeCalled(); + + rerender(); + + expect(mockInstance.hide).toBeCalled(); + }); + + test('bind event correctly', () => { + const mockInstance = { + on: jest.fn(), + off: jest.fn(), + }; + (useAMapPluginInstance as jest.Mock).mockReturnValue(mockInstance); + + const onShow = jest.fn(); + const onHide = jest.fn(); + const onClick = jest.fn(); + const onDBLClick = jest.fn(); + const onRightClick = jest.fn(); + const onMousemove = jest.fn(); + const onMousedown = jest.fn(); + const onMouseup = jest.fn(); + const onMouseover = jest.fn(); + const onMouseout = jest.fn(); + const onTouchstart = jest.fn(); + const onTouchmove = jest.fn(); + const onTouchend = jest.fn(); + const onDragstart = jest.fn(); + const onDragging = jest.fn(); + const onDragend = jest.fn(); + const onMoving = jest.fn(); + const onMoveEnd = jest.fn(); + const onMoveAlong = jest.fn(); + + const { unmount } = render( + , + ); + + expect(mockInstance.on).toBeCalledTimes(19); + expect(mockInstance.on).toBeCalledWith('show', onShow); + expect(mockInstance.on).toBeCalledWith('hide', onHide); + expect(mockInstance.on).toBeCalledWith('click', onClick); + expect(mockInstance.on).toBeCalledWith('dblclick', onDBLClick); + expect(mockInstance.on).toBeCalledWith('rightclick', onRightClick); + expect(mockInstance.on).toBeCalledWith('mousemove', onMousemove); + expect(mockInstance.on).toBeCalledWith('mousedown', onMousedown); + expect(mockInstance.on).toBeCalledWith('mouseup', onMouseup); + expect(mockInstance.on).toBeCalledWith('mouseover', onMouseover); + expect(mockInstance.on).toBeCalledWith('mouseout', onMouseout); + expect(mockInstance.on).toBeCalledWith('touchstart', onTouchstart); + expect(mockInstance.on).toBeCalledWith('touchmove', onTouchmove); + expect(mockInstance.on).toBeCalledWith('touchend', onTouchend); + expect(mockInstance.on).toBeCalledWith('dragstart', onDragstart); + expect(mockInstance.on).toBeCalledWith('dragging', onDragging); + expect(mockInstance.on).toBeCalledWith('dragend', onDragend); + expect(mockInstance.on).toBeCalledWith('moving', onMoving); + expect(mockInstance.on).toBeCalledWith('moveend', onMoveEnd); + expect(mockInstance.on).toBeCalledWith('movealong', onMoveAlong); + + unmount(); + + expect(mockInstance.on).toBeCalledTimes(19); + expect(mockInstance.off).toBeCalledWith('show', onShow); + expect(mockInstance.off).toBeCalledWith('hide', onHide); + expect(mockInstance.off).toBeCalledWith('click', onClick); + expect(mockInstance.off).toBeCalledWith('dblclick', onDBLClick); + expect(mockInstance.off).toBeCalledWith('rightclick', onRightClick); + expect(mockInstance.off).toBeCalledWith('mousemove', onMousemove); + expect(mockInstance.off).toBeCalledWith('mousedown', onMousedown); + expect(mockInstance.off).toBeCalledWith('mouseup', onMouseup); + expect(mockInstance.off).toBeCalledWith('mouseover', onMouseover); + expect(mockInstance.off).toBeCalledWith('mouseout', onMouseout); + expect(mockInstance.off).toBeCalledWith('touchstart', onTouchstart); + expect(mockInstance.off).toBeCalledWith('touchmove', onTouchmove); + expect(mockInstance.off).toBeCalledWith('touchend', onTouchend); + expect(mockInstance.off).toBeCalledWith('dragstart', onDragstart); + expect(mockInstance.off).toBeCalledWith('dragging', onDragging); + expect(mockInstance.off).toBeCalledWith('dragend', onDragend); + expect(mockInstance.off).toBeCalledWith('moving', onMoving); + expect(mockInstance.off).toBeCalledWith('moveend', onMoveEnd); + expect(mockInstance.off).toBeCalledWith('movealong', onMoveAlong); + }); +});