|
| 1 | +import { renderHook } from '@testing-library/react-hooks' |
| 2 | +import { Map, Shape } from 'azure-maps-control' |
| 3 | +import { drawing } from 'azure-maps-drawing-tools' |
| 4 | +import React, { useContext } from 'react' |
| 5 | +import { IAzureDrawingManagerStatefulProviderProps } from '../types' |
| 6 | +import { AzureMapsContext, AzureMapsProvider } from './AzureMapContext' |
| 7 | +import { AzureMapDrawingManagerProvider, AzureMapDrawingManagerContext } from './AzureMapDrawingManagerContext' |
| 8 | + |
| 9 | +const mapContextProps = { |
| 10 | + mapRef: null, |
| 11 | + isMapReady: false, |
| 12 | + setMapReady: jest.fn(), |
| 13 | + removeMapRef: jest.fn(), |
| 14 | + setMapRef: jest.fn() |
| 15 | +} |
| 16 | + |
| 17 | +const mapRef = new Map('fake', {}) |
| 18 | + |
| 19 | +const useContextConsumer = () => { |
| 20 | + const drawingManagerContext = useContext(AzureMapDrawingManagerContext) |
| 21 | + return drawingManagerContext |
| 22 | +} |
| 23 | + |
| 24 | +const wrapWithDrawingManagerContext = (props: IAzureDrawingManagerStatefulProviderProps) => ({ |
| 25 | + children |
| 26 | +}: { |
| 27 | + children?: any |
| 28 | +}) => { |
| 29 | + return ( |
| 30 | + <AzureMapsContext.Provider |
| 31 | + value={{ |
| 32 | + ...mapContextProps, |
| 33 | + mapRef |
| 34 | + }} |
| 35 | + > |
| 36 | + <AzureMapDrawingManagerProvider {...{ ...props }}>{children}</AzureMapDrawingManagerProvider> |
| 37 | + </AzureMapsContext.Provider> |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +describe('AzureMapDataSourceProvider tests', () => { |
| 42 | + it('should add drawing manager datasource to map ref on map ready', () => { |
| 43 | + mapRef.sources.add = jest.fn() |
| 44 | + const { result } = renderHook(() => useContextConsumer(), { |
| 45 | + wrapper: wrapWithDrawingManagerContext({ options: {} }) |
| 46 | + }) |
| 47 | + |
| 48 | + expect(mapRef.sources.add).toHaveBeenCalledWith(result.current.drawingManagerRef?.getSource()) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should add passed events to the map with DrawingManager target', () => { |
| 52 | + (mapRef.events.add as any) = jest.fn((eventType, targetOrCallback, callback) => { |
| 53 | + if(callback){ |
| 54 | + callback() |
| 55 | + } else { |
| 56 | + // for ready event |
| 57 | + targetOrCallback() |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + const drawingmodechanged = (mode: drawing.DrawingMode) => {} |
| 62 | + const drawingstarted = (shape: Shape) => {} |
| 63 | + const { result } = renderHook(() => useContextConsumer(), { |
| 64 | + wrapper: wrapWithDrawingManagerContext({ options: {}, events: { drawingstarted, drawingmodechanged }}) |
| 65 | + }) |
| 66 | + |
| 67 | + expect(mapRef.events.add).toHaveBeenCalledWith( |
| 68 | + 'drawingmodechanged', |
| 69 | + result.current.drawingManagerRef, |
| 70 | + drawingmodechanged |
| 71 | + ) |
| 72 | + |
| 73 | + expect(mapRef.events.add).toHaveBeenCalledWith( |
| 74 | + 'drawingstarted', |
| 75 | + result.current.drawingManagerRef, |
| 76 | + drawingstarted |
| 77 | + ) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should discard drawing event when it is undefined', () => { |
| 81 | + mapRef.events.add = jest.fn((eventName: any, callback: any) => callback()) |
| 82 | + const { result } = renderHook(() => useContextConsumer(), { |
| 83 | + wrapper: wrapWithDrawingManagerContext({ options: {}, events: { drawingstarted: undefined }}) |
| 84 | + }) |
| 85 | + |
| 86 | + // only ready event handler should have been added |
| 87 | + expect(mapRef.events.add).toHaveBeenCalledWith('ready', expect.any(Function)) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should add toolbar control to map ref on map ready', () => { |
| 91 | + mapRef.controls.add = jest.fn() |
| 92 | + const { result } = renderHook(() => useContextConsumer(), { |
| 93 | + wrapper: wrapWithDrawingManagerContext({ options: { toolbar: { position: 'top-right' }}}) |
| 94 | + }) |
| 95 | + |
| 96 | + expect(mapRef.controls.add).toBeCalledTimes(1) |
| 97 | + }) |
| 98 | +}) |
0 commit comments