forked from PaulLeCam/react-leaflet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapComponent.js
105 lines (83 loc) · 2.49 KB
/
MapComponent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* global describe, expect, it, jest */
import Leaflet from 'leaflet'
import React, { createRef, Component } from 'react'
import { renderIntoDocument } from 'react-dom/test-utils'
import MapComponent from '../src/MapComponent'
describe('MapComponent', () => {
class TestComponent extends MapComponent {
constructor(props) {
super(props)
this.leafletElement = Leaflet.map('test')
}
render() {
return null
}
}
it('exposes a `leafletElement` getter', () => {
const component = renderIntoDocument(<TestComponent />)
expect(component.leafletElement._container).toBeDefined()
})
it('binds the event', () => {
const callback = jest.fn()
const component = renderIntoDocument(<TestComponent onClick={callback} />)
component.fireLeafletEvent('click')
expect(callback.mock.calls.length).toBe(1)
})
it('unbinds the event', () => {
const callback = jest.fn()
class EventComponent extends Component {
constructor(props) {
super(props)
this.state = { bindEvent: true }
this.ref = createRef()
}
dontBind() {
this.setState({ bindEvent: false })
}
fire() {
this.ref.current.fireLeafletEvent('click')
}
render() {
return this.state.bindEvent ? (
<TestComponent onClick={callback} ref={this.ref} />
) : (
<TestComponent ref={this.ref} />
)
}
}
const component = renderIntoDocument(<EventComponent />)
component.fire()
expect(callback.mock.calls.length).toBe(1)
component.dontBind()
component.fire()
expect(callback.mock.calls.length).toBe(1)
})
it('replaces the event', () => {
const callback1 = jest.fn()
const callback2 = jest.fn()
class EventComponent extends Component {
constructor(props) {
super(props)
this.state = { cb: callback1 }
this.ref = createRef()
}
replaceCallback() {
this.setState({ cb: callback2 })
}
fire() {
this.ref.current.fireLeafletEvent('click')
}
render() {
return <TestComponent onClick={this.state.cb} ref={this.ref} />
}
}
const component = renderIntoDocument(<EventComponent />)
component.fire()
expect(callback1.mock.calls.length).toBe(1)
expect(callback2.mock.calls.length).toBe(0)
component.replaceCallback()
component.fire()
expect(callback1.mock.calls.length).toBe(1)
expect(callback2.mock.calls.length).toBe(1)
})
})