From c6d8cf7a16dfea47f87826527b17d3ceb10ce36f Mon Sep 17 00:00:00 2001 From: RoXoM Date: Sat, 28 Oct 2023 13:25:44 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E7=A7=BB=E9=99=A4=E6=9C=AA=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/AMapPolygonEditor.test.tsx | 221 ------------------ .../__tests__/AMapPolylineEditor.test.tsx | 186 --------------- 2 files changed, 407 deletions(-) delete mode 100644 src/components/AMapPolygonEditor/__tests__/AMapPolygonEditor.test.tsx delete mode 100644 src/components/AMapPolylineEditor/__tests__/AMapPolylineEditor.test.tsx diff --git a/src/components/AMapPolygonEditor/__tests__/AMapPolygonEditor.test.tsx b/src/components/AMapPolygonEditor/__tests__/AMapPolygonEditor.test.tsx deleted file mode 100644 index 7279f39..0000000 --- a/src/components/AMapPolygonEditor/__tests__/AMapPolygonEditor.test.tsx +++ /dev/null @@ -1,221 +0,0 @@ -import * as React from 'react'; -import { render, cleanup } from '@testing-library/react'; - -import useAMap from '../../../hooks/useAMap'; -import useAMapPluginInstance from '../../../hooks/useAMapPluginInstance'; - -import AMapPolygonEditor from '../AMapPolygonEditor'; - -const mockMapInstance = { - on: jest.fn(), - off: jest.fn(), - getAllOverlays: jest.fn(), -}; - -const mockContext = { - __AMAP__: null, - __AMAP_UI__: null, - map: mockMapInstance, -}; - -jest.mock('../../../hooks/useAMap', () => ({ - esModule: true, - default: jest.fn(() => mockContext), -})); - -const mockInstance = { - setTarget: jest.fn(), - on: jest.fn(), - off: jest.fn(), - open: jest.fn(), - close: jest.fn(), - setAdsorbPolygons: jest.fn(), - clearAdsorbPolygons: jest.fn(), -}; - -jest.mock('../../../hooks/useAMapPluginInstance', () => ({ - esModule: true, - default: jest.fn((__, cb) => { - cb({ - PolygonEditor: jest.fn(), - }, {}); - return mockInstance; - }), -})); - -describe('AMapPolygonEditor', () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - afterEach(cleanup); - - test('renders without error', () => { - expect(() => { - render(); - }).not.toThrowError(); - expect(useAMapPluginInstance).toBeCalledWith('PolygonEditor', expect.any(Function)); - }); - - test('renders without error when map instance is null', () => { - (useAMap as jest.Mock).mockReturnValueOnce({}); - expect(() => { - render(); - }).not.toThrowError(); - }); - - test('renders without error when instance is null', () => { - (useAMapPluginInstance as jest.Mock).mockReturnValueOnce(null); - expect(() => { - render(); - }).not.toThrowError(); - }); - - test('sets target correctly', () => { - const computeTarget = jest.fn((arr) => arr?.[0]); - const mockOverlays = [{}]; - mockMapInstance.getAllOverlays.mockReturnValueOnce(mockOverlays); - render(); - expect(mockMapInstance.getAllOverlays).toBeCalledWith('polygon'); - expect(computeTarget).toBeCalledWith(mockOverlays); - expect(mockInstance.setTarget).toBeCalledWith(mockOverlays[0]); - }); - - test('sets target correctly when map.getAllOverlays returns an empty array', () => { - const computeTarget = jest.fn((arr) => arr?.[0]); - const computeAdsorbPolygons = jest.fn((arr) => arr); - mockMapInstance.getAllOverlays.mockReturnValueOnce([]); - render(); - expect(mockMapInstance.getAllOverlays).toBeCalledWith('polygon'); - expect(computeTarget).toBeCalledWith([]); - expect(computeAdsorbPolygons).toBeCalledWith([]); - expect(mockInstance.setTarget).toBeCalledWith(null); - }); - - test('sets target correctly when map.getAllOverlays returns null', () => { - const computeTarget = jest.fn((arr) => arr?.[0]); - const computeAdsorbPolygons = jest.fn((arr) => arr); - mockMapInstance.getAllOverlays.mockReturnValueOnce(null); - render(); - expect(mockMapInstance.getAllOverlays).toBeCalledWith('polygon'); - expect(computeTarget).toBeCalledWith([]); - expect(computeAdsorbPolygons).toBeCalledWith([]); - expect(mockInstance.setTarget).toBeCalledWith(null); - }); - - test('triggers onChange correctly', () => { - const onChange = jest.fn(); - const computeTarget = jest.fn(); - const eventRepo = new Map>(); - mockInstance.on.mockImplementation((type, cb) => { - if (!eventRepo.has(type)) { - eventRepo.set(type, new Set()); - } - eventRepo.get(type)!.add(cb); - }); - mockInstance.off.mockImplementation((type, cb) => { - if (!eventRepo.has(type)) return; - eventRepo.get(type)!.delete(cb); - }); - - const { rerender } = render( - , - ); - - const mockEvent = {}; - eventRepo.get('add')?.forEach((fn) => fn(mockEvent)); - eventRepo.get('addnode')?.forEach((fn) => fn(mockEvent)); - eventRepo.get('adjust')?.forEach((fn) => fn(mockEvent)); - eventRepo.get('removenode')?.forEach((fn) => fn(mockEvent)); - expect(onChange).toHaveBeenCalledTimes(4); - expect(onChange).toBeCalledWith(mockEvent); - - rerender(); - - // 未设置 onChange - eventRepo.get('add')?.forEach((fn) => fn(mockEvent)); - // reset mock - mockMapInstance.on.mockReset(); - // Write your test code here - }); - - test('closes and opens correctly', () => { - // Test case 1: disabled is true, curInstance.close() is called - const computeTarget = jest.fn(); - const { rerender } = render(); - expect(mockInstance.open).toHaveBeenCalled(); - // Test case 2: disabled is false, curInstance.open() is called - rerender(); - // Write your test code here - expect(mockInstance.close).toHaveBeenCalled(); - }); - - test('bind and unbind events correctly', () => { - const computeTarget = jest.fn(); - const { unmount } = render(); - expect(mockInstance.on).toBeCalledWith('add', expect.any(Function)); - expect(mockInstance.on).toBeCalledWith('addnode', expect.any(Function)); - expect(mockInstance.on).toBeCalledWith('adjust', expect.any(Function)); - expect(mockInstance.on).toBeCalledWith('removenode', expect.any(Function)); - - unmount(); - - expect(mockInstance.off).toBeCalledWith('add', expect.any(Function)); - expect(mockInstance.off).toBeCalledWith('addnode', expect.any(Function)); - expect(mockInstance.off).toBeCalledWith('adjust', expect.any(Function)); - expect(mockInstance.off).toBeCalledWith('removenode', expect.any(Function)); - }); - - test('polygon added or removed then re-compute target', () => { - const computeTarget = jest.fn(); - const eventRepo = new Map>(); - mockMapInstance.on.mockImplementation((type, cb) => { - if (!eventRepo.has(type)) { - eventRepo.set(type, new Set()); - } - eventRepo.get(type)!.add(cb); - }); - render(); - - // modified overlay is not polygon - eventRepo.get('overlaysRemoved')?.forEach((fn) => fn([{ className: 'Overlay.Polyline' }])); - expect(computeTarget).toBeCalledTimes(1); // not called again - - const mockEvent = { className: 'Overlay.Polygon' }; - eventRepo.get('overlaysAdded')?.forEach((fn) => fn(mockEvent)); - expect(computeTarget).toBeCalledTimes(2); - eventRepo.get('overlaysRemoved')?.forEach((fn) => fn([mockEvent, { className: 'Overlay.Polygon' }])); - expect(computeTarget).toBeCalledTimes(3); - - // reset mock - mockMapInstance.on.mockReset(); - }); - - test('custom computeAdsorbPolygons returns falsy', () => { - const computeTarget = jest.fn(); - const computeAdsorbPolygons = jest.fn(() => null); - - render(); - - expect(mockInstance.clearAdsorbPolygons).toBeCalled(); - }); - - test('set computeAdsorbPolygons to null', () => { - const computeTarget = jest.fn(); - - render(); - - expect(mockInstance.clearAdsorbPolygons).toBeCalled(); - }); -}); diff --git a/src/components/AMapPolylineEditor/__tests__/AMapPolylineEditor.test.tsx b/src/components/AMapPolylineEditor/__tests__/AMapPolylineEditor.test.tsx deleted file mode 100644 index 6f65db0..0000000 --- a/src/components/AMapPolylineEditor/__tests__/AMapPolylineEditor.test.tsx +++ /dev/null @@ -1,186 +0,0 @@ -import * as React from 'react'; -import { render, cleanup } from '@testing-library/react'; - -import useAMap from '../../../hooks/useAMap'; -import useAMapPluginInstance from '../../../hooks/useAMapPluginInstance'; - -import AMapPolylineEditor from '../AMapPolylineEditor'; - -const mockMapInstance = { - on: jest.fn(), - off: jest.fn(), - getAllOverlays: jest.fn(), -}; - -const mockContext = { - __AMAP__: null, - __AMAP_UI__: null, - map: mockMapInstance, -}; - -jest.mock('../../../hooks/useAMap', () => ({ - esModule: true, - default: jest.fn(() => mockContext), -})); - -const mockInstance = { - setTarget: jest.fn(), - on: jest.fn(), - off: jest.fn(), - open: jest.fn(), - close: jest.fn(), -}; - -jest.mock('../../../hooks/useAMapPluginInstance', () => ({ - esModule: true, - default: jest.fn((__, cb) => { - cb({ - PolylineEditor: jest.fn(), - }, {}); - return mockInstance; - }), -})); - -describe('AMapPolylineEditor', () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - afterEach(cleanup); - - test('renders without error', () => { - expect(() => { - render(); - }).not.toThrowError(); - expect(useAMapPluginInstance).toBeCalledWith('PolylineEditor', expect.any(Function)); - }); - - test('renders without error when map instance is null', () => { - (useAMap as jest.Mock).mockReturnValueOnce({}); - expect(() => { - render(); - }).not.toThrowError(); - }); - - test('renders without error when instance is null', () => { - (useAMapPluginInstance as jest.Mock).mockReturnValueOnce(null); - expect(() => { - render(); - }).not.toThrowError(); - }); - - test('sets target correctly', () => { - const computeTarget = jest.fn((arr) => arr?.[0]); - const mockOverlays = [{}]; - mockMapInstance.getAllOverlays.mockReturnValueOnce(mockOverlays); - render(); - expect(mockMapInstance.getAllOverlays).toBeCalledWith('polyline'); - expect(computeTarget).toBeCalledWith(mockOverlays); - expect(mockInstance.setTarget).toBeCalledWith(mockOverlays[0]); - }); - - test('sets target correctly when map.getAllOverlays returns an empty array', () => { - const computeTarget = jest.fn((arr) => arr?.[0]); - mockMapInstance.getAllOverlays.mockReturnValueOnce([]); - render(); - expect(mockMapInstance.getAllOverlays).toBeCalledWith('polyline'); - expect(computeTarget).toBeCalledWith([]); - expect(mockInstance.setTarget).toBeCalledWith(null); - }); - - test('sets target correctly when map.getAllOverlays returns null', () => { - const computeTarget = jest.fn((arr) => arr?.[0]); - mockMapInstance.getAllOverlays.mockReturnValueOnce(null); - render(); - expect(mockMapInstance.getAllOverlays).toBeCalledWith('polyline'); - expect(computeTarget).toBeCalledWith([]); - expect(mockInstance.setTarget).toBeCalledWith(null); - }); - - test('triggers onChange correctly', () => { - const onChange = jest.fn(); - const computeTarget = jest.fn(); - const eventRepo = new Map>(); - mockInstance.on.mockImplementation((type, cb) => { - if (!eventRepo.has(type)) { - eventRepo.set(type, new Set()); - } - eventRepo.get(type)!.add(cb); - }); - mockInstance.off.mockImplementation((type, cb) => { - if (!eventRepo.has(type)) return; - eventRepo.get(type)!.delete(cb); - }); - - const { rerender } = render( - , - ); - - const mockEvent = {}; - eventRepo.get('add')?.forEach((fn) => fn(mockEvent)); - eventRepo.get('addnode')?.forEach((fn) => fn(mockEvent)); - eventRepo.get('adjust')?.forEach((fn) => fn(mockEvent)); - eventRepo.get('removenode')?.forEach((fn) => fn(mockEvent)); - expect(onChange).toHaveBeenCalledTimes(4); - expect(onChange).toBeCalledWith(mockEvent); - - rerender(); - - // 未设置 onChange - eventRepo.get('add')?.forEach((fn) => fn(mockEvent)); - // reset mock - mockMapInstance.on.mockReset(); - // Write your test code here - }); - - test('closes and opens correctly', () => { - // Test case 1: disabled is true, curInstance.close() is called - const computeTarget = jest.fn(); - const { rerender } = render(); - expect(mockInstance.open).toHaveBeenCalled(); - // Test case 2: disabled is false, curInstance.open() is called - rerender(); - // Write your test code here - expect(mockInstance.close).toHaveBeenCalled(); - }); - - test('bind and unbind events correctly', () => { - const computeTarget = jest.fn(); - const { unmount } = render(); - expect(mockInstance.on).toBeCalledWith('add', expect.any(Function)); - expect(mockInstance.on).toBeCalledWith('addnode', expect.any(Function)); - expect(mockInstance.on).toBeCalledWith('adjust', expect.any(Function)); - expect(mockInstance.on).toBeCalledWith('removenode', expect.any(Function)); - - unmount(); - - expect(mockInstance.off).toBeCalledWith('add', expect.any(Function)); - expect(mockInstance.off).toBeCalledWith('addnode', expect.any(Function)); - expect(mockInstance.off).toBeCalledWith('adjust', expect.any(Function)); - expect(mockInstance.off).toBeCalledWith('removenode', expect.any(Function)); - }); - - test('polyline added or removed then re-compute target', () => { - const computeTarget = jest.fn(); - const eventRepo = new Map>(); - mockMapInstance.on.mockImplementation((type, cb) => { - if (!eventRepo.has(type)) { - eventRepo.set(type, new Set()); - } - eventRepo.get(type)!.add(cb); - }); - render(); - - // modified overlay is not polyline - eventRepo.get('overlaysRemoved')?.forEach((fn) => fn([{ className: 'Overlay.Polygon' }])); - expect(computeTarget).toBeCalledTimes(1); // not called again - - const mockEvent = { className: 'Overlay.Polyline' }; - eventRepo.get('overlaysAdded')?.forEach((fn) => fn(mockEvent)); - expect(computeTarget).toBeCalledTimes(2); - eventRepo.get('overlaysRemoved')?.forEach((fn) => fn([mockEvent, { className: 'Overlay.Polygon' }])); - expect(computeTarget).toBeCalledTimes(3); - - // reset mock - mockMapInstance.on.mockReset(); - }); -});