Skip to content

Commit fe88a2f

Browse files
committed
fix: unit tests and minor tweaks
1 parent cc63dd7 commit fe88a2f

9 files changed

+257
-7
lines changed

__mocks__/azure-maps-control.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
module.exports = {
22
Map: jest.fn(() => ({
33
controls: {
4-
add: jest.fn()
4+
add: jest.fn(),
5+
remove: jest.fn()
56
},
67
events: {
78
add: jest.fn((eventName, callback = () => {}) => {
89
callback()
9-
})
10+
}),
11+
remove: jest.fn()
1012
},
1113
imageSprite: {
1214
add: jest.fn(),

__mocks__/azure-maps-drawing-tools.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,48 @@
1+
const atlas = require('azure-maps-control')
2+
13
module.exports = {
2-
control: {},
3-
drawing: {}
4+
control: {
5+
DrawingToolbar: jest.fn(options => ({
6+
setOptions: jest.fn()
7+
}))
8+
},
9+
drawing: {
10+
DrawingManager: jest.fn((map, options) => {
11+
let toolbar;
12+
const dataSource = new atlas.source.DataSource()
13+
map.sources.add(dataSource)
14+
return {
15+
setOptions: jest.fn(options => {
16+
if(!toolbar){
17+
map.controls.add(options.toolbar)
18+
} else if (toolbar !== options.toolbar){
19+
map.control.remove(toolbar)
20+
map.control.add(options.toolbar)
21+
}
22+
toolbar = options.toolbar
23+
}),
24+
getOptions: jest.fn(() => ({})),
25+
dispose: jest.fn(),
26+
getLayers: jest.fn(() => ({
27+
lineLayer: {
28+
layer: 'LineLayer',
29+
setOptions: jest.fn()
30+
},
31+
polygonLayer: {
32+
layer: 'PolygonLayer',
33+
setOptions: jest.fn()
34+
},
35+
polygonOutlineLayer: {
36+
layer: 'LineLayer',
37+
setOptions: jest.fn()
38+
},
39+
pointLayer: {
40+
layer: 'SymbolLayer',
41+
setOptions: jest.fn()
42+
}
43+
})),
44+
getSource: jest.fn(() => dataSource)
45+
}
46+
})
47+
}
448
}

assets/coverage.png

-82 KB
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
import { render } from '@testing-library/react'
3+
import { AzureMapDrawingLayerProvider } from './AzureMapDrawingLayerContext'
4+
5+
describe('AzureMapDrawingLayerProvider', () => {
6+
it('should create and render AzureMapDrawingLayerProvider', () => {
7+
const { container } = render(<AzureMapDrawingLayerProvider type="polygonLayer" options={{fillColor: 'green'}}/>)
8+
expect(container).toMatchSnapshot()
9+
})
10+
})
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
})

src/contexts/AzureMapDrawingManagerContext.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const AzureMapDrawingManagerStatefulProvider = ({
5656
setDrawingManagerRef(drawingManager)
5757
setDataSourceRef(drawingManager.getSource())
5858
setToolbarOnceReadyRef(toolbar)
59-
59+
6060
// register drawing events
6161
for (const eventType in events) {
6262
const handler = events[eventType as IAzureDrawingManagerEventType]
@@ -90,7 +90,9 @@ const AzureMapDrawingManagerStatefulProvider = ({
9090
useEffect(() => {
9191
if(drawingManagerRef && options){
9292
drawingManagerRef.setOptions({ ...options, toolbar: toolbarOnceReadyRef })
93-
toolbarOnceReadyRef?.setOptions(options.toolbar)
93+
}
94+
if(toolbarOnceReadyRef && options && options.toolbar){
95+
toolbarOnceReadyRef.setOptions(options.toolbar)
9496
}
9597
}, [drawingManagerRef, options, toolbarOnceReadyRef])
9698

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`AzureMapDrawingLayerProvider should create and render AzureMapDrawingLayerProvider 1`] = `<div />`;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import atlas, { source, layer } from 'azure-maps-control'
2+
import { ReactNode } from 'react'
3+
import { renderHook } from '@testing-library/react-hooks'
4+
import { useAzureMapLayer } from './useAzureMapLayer'
5+
import { Map } from 'azure-maps-control'
6+
import { drawing } from 'azure-maps-drawing-tools'
7+
import React from 'react'
8+
import { AzureMapsContext } from '../contexts/AzureMapContext'
9+
import { AzureMapDrawingManagerContext } from '../contexts/AzureMapDrawingManagerContext'
10+
import { IAzureLayerStatefulProviderProps, LayerType } from '../types'
11+
import { useAzureMapDrawingLayer } from './useAzureMapDrawingLayer'
12+
13+
const mapContextProps = {
14+
mapRef: null,
15+
isMapReady: false,
16+
setMapReady: jest.fn(),
17+
removeMapRef: jest.fn(),
18+
setMapRef: jest.fn()
19+
}
20+
const mapRef = new Map('fake', {})
21+
22+
const wrapWithAzureMapContext = ({ children }: { children?: ReactNode | null }) => {
23+
const drawingManagerRef = new drawing.DrawingManager(mapRef, {})
24+
return (
25+
<AzureMapsContext.Provider
26+
value={{
27+
...mapContextProps,
28+
mapRef
29+
}}
30+
>
31+
<AzureMapDrawingManagerContext.Provider
32+
value={{ drawingManagerRef }}
33+
>
34+
{children}
35+
</AzureMapDrawingManagerContext.Provider>
36+
</AzureMapsContext.Provider>
37+
)
38+
}
39+
40+
describe('userMapDrawingLayer tests', () => {
41+
it('should create standard layer and set options', () => {
42+
const options = {iconOptions: { image: 'marker-blue' }}
43+
const { result } = renderHook(
44+
() =>
45+
useAzureMapDrawingLayer({
46+
type: 'pointLayer',
47+
options
48+
}),
49+
{ wrapper: wrapWithAzureMapContext }
50+
)
51+
52+
expect((result.current.layerRef as unknown as { layer: string }).layer).toBe("SymbolLayer")
53+
expect(result.current.layerRef?.setOptions).toBeCalledWith(options)
54+
})
55+
56+
it('should handle add events to layerRef', () => {
57+
mapRef.events.add = jest.fn()
58+
const options = {iconOptions: { image: 'marker-blue' }}
59+
const events = { click: () => {} }
60+
renderHook(
61+
() =>
62+
useAzureMapDrawingLayer({
63+
type: 'pointLayer',
64+
options,
65+
events
66+
}),
67+
{ wrapper: wrapWithAzureMapContext }
68+
)
69+
expect(mapRef.events.add).toHaveBeenCalledWith('click', expect.any(Object), events.click)
70+
})
71+
72+
it('should handle add lifeCycleEvents to layerRef', () => {
73+
mapRef.events.add = jest.fn()
74+
const lifecycleEvents = { onCreate: () => {} }
75+
const options = {iconOptions: { image: 'marker-blue' }}
76+
renderHook(
77+
() =>
78+
useAzureMapDrawingLayer({
79+
type: 'pointLayer',
80+
options,
81+
lifecycleEvents
82+
}),
83+
{ wrapper: wrapWithAzureMapContext }
84+
)
85+
expect(mapRef.events.add).toHaveBeenCalledWith(
86+
'onCreate',
87+
expect.any(Object),
88+
lifecycleEvents.onCreate
89+
)
90+
})
91+
})

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ export interface IAzureMapDrawingManagerProps {
339339
}
340340

341341
export interface IAzureDrawingManagerStatefulProviderProps {
342-
options: Omit<DrawingManagerOptions, 'toolbar'> & { 'toolbar': DrawingToolbarOptions }
342+
options: Omit<DrawingManagerOptions, 'toolbar'> & { 'toolbar'?: DrawingToolbarOptions }
343343
events?: IAzureDrawingManagerEvent
344344
children?:
345345
| Array<IAzureDataSourceChildren | null>

0 commit comments

Comments
 (0)