Skip to content

Commit e45c04a

Browse files
authored
Merge pull request #128 from Dataport/fix/geo-location-tests
Add tests for the actions of @polar/plugin-geo-location
2 parents bd42e89 + 624937b commit e45c04a

File tree

2 files changed

+197
-1
lines changed

2 files changed

+197
-1
lines changed

packages/plugins/GeoLocation/src/store/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ const actions: PolarActionTree<GeoLocationState, GeoLocationGetters> = {
161161
},
162162
printPositioningFailed(
163163
{ dispatch, getters: { toastAction } },
164-
boundaryErrorOccurred
164+
boundaryErrorOccurred: string
165165
) {
166166
if (toastAction) {
167167
const toast = boundaryErrorOccurred
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import Geolocation from 'ol/Geolocation.js'
2+
import { makeStoreModule } from '../src/store/index'
3+
4+
describe('plugin-geolocation', () => {
5+
describe('store', () => {
6+
describe('actions', () => {
7+
let consoleErrorSpy
8+
let consoleLogSpy
9+
let actionContext
10+
let commit
11+
let dispatch
12+
let storeModule
13+
14+
beforeEach(() => {
15+
consoleErrorSpy = jest.fn()
16+
consoleLogSpy = jest.fn()
17+
jest.spyOn(console, 'error').mockImplementation(consoleErrorSpy)
18+
jest.spyOn(console, 'log').mockImplementation(consoleLogSpy)
19+
commit = jest.fn()
20+
dispatch = jest.fn()
21+
actionContext = {
22+
commit,
23+
dispatch,
24+
getters: {
25+
geolocation: null,
26+
configuredEpsg: 'EPSG:4326',
27+
position: [100, 100],
28+
},
29+
}
30+
storeModule = makeStoreModule()
31+
})
32+
afterEach(jest.restoreAllMocks)
33+
34+
describe('onError', () => {
35+
const error = { message: 'uhoh' }
36+
it('should dispatch a toast if the toastAction is configured', () => {
37+
actionContext.getters.toastAction = 'actionName'
38+
39+
storeModule.actions.onError(actionContext, error)
40+
41+
expect(commit.mock.calls.length).toEqual(2)
42+
expect(commit.mock.calls[0]).toEqual(['setIsGeolocationDenied', true])
43+
expect(commit.mock.calls[1]).toEqual(['setTracking', false])
44+
expect(dispatch.mock.calls.length).toEqual(2)
45+
expect(dispatch.mock.calls[0]).toEqual([
46+
'actionName',
47+
{
48+
type: 'error',
49+
text: 'common:plugins.geoLocation.button.tooltip.locationAccessDenied',
50+
},
51+
{ root: true },
52+
])
53+
expect(dispatch.mock.calls[1]).toEqual(['removeMarker'])
54+
expect(consoleErrorSpy.mock.calls.length).toEqual(1)
55+
expect(consoleErrorSpy.mock.calls[0]).toEqual([
56+
'@polar/plugin-geo-location',
57+
error.message,
58+
])
59+
})
60+
it('should log an additional error if the toastAction is not configured', () => {
61+
storeModule.actions.onError(actionContext, error)
62+
63+
expect(commit.mock.calls.length).toEqual(2)
64+
expect(commit.mock.calls[0]).toEqual(['setIsGeolocationDenied', true])
65+
expect(commit.mock.calls[1]).toEqual(['setTracking', false])
66+
expect(dispatch.mock.calls.length).toEqual(1)
67+
expect(dispatch.mock.calls[0]).toEqual(['removeMarker'])
68+
expect(consoleErrorSpy.mock.calls.length).toEqual(2)
69+
expect(consoleErrorSpy.mock.calls[0]).toEqual([
70+
'@polar/plugin-geo-location: Location access denied by user.',
71+
])
72+
expect(consoleErrorSpy.mock.calls[1]).toEqual([
73+
'@polar/plugin-geo-location',
74+
error.message,
75+
])
76+
})
77+
})
78+
describe('printPositioningFailed', () => {
79+
it('should dispatch a toast for a boundaryError if the toastAction is configured and the given parameter has a relevant value', () => {
80+
actionContext.getters.toastAction = 'actionName'
81+
82+
storeModule.actions.printPositioningFailed(
83+
actionContext,
84+
'boundaryError'
85+
)
86+
87+
expect(dispatch.mock.calls.length).toEqual(1)
88+
expect(dispatch.mock.calls[0]).toEqual([
89+
'actionName',
90+
{
91+
type: 'error',
92+
text: 'plugins.geoLocation.toast.boundaryError',
93+
},
94+
{ root: true },
95+
])
96+
expect(consoleErrorSpy.mock.calls.length).toEqual(0)
97+
expect(consoleLogSpy.mock.calls.length).toEqual(0)
98+
})
99+
it('should dispatch a toast for a generic not in boundary error if the toastAction is configured and the given parameter does not have a relevant value', () => {
100+
actionContext.getters.toastAction = 'actionName'
101+
102+
storeModule.actions.printPositioningFailed(actionContext, '')
103+
104+
expect(dispatch.mock.calls.length).toEqual(1)
105+
expect(dispatch.mock.calls[0]).toEqual([
106+
'actionName',
107+
{
108+
type: 'info',
109+
text: 'plugins.geoLocation.toast.notInBoundary',
110+
timeout: 10000,
111+
},
112+
{ root: true },
113+
])
114+
expect(consoleErrorSpy.mock.calls.length).toEqual(0)
115+
expect(consoleLogSpy.mock.calls.length).toEqual(0)
116+
})
117+
it('should log only an error for a boundaryError if the toastAction is not configured and the given parameter has a relevant value', () => {
118+
storeModule.actions.printPositioningFailed(
119+
actionContext,
120+
'boundaryError'
121+
)
122+
123+
expect(dispatch.mock.calls.length).toEqual(0)
124+
expect(consoleErrorSpy.mock.calls.length).toEqual(1)
125+
expect(consoleErrorSpy.mock.calls[0]).toEqual([
126+
'Checking boundary layer failed.',
127+
])
128+
expect(consoleLogSpy.mock.calls.length).toEqual(0)
129+
})
130+
it('should log only an error for a generic not in boundary error if the toastAction is not configured and the given parameter does not have a relevant value', () => {
131+
storeModule.actions.printPositioningFailed(actionContext, '')
132+
133+
expect(dispatch.mock.calls.length).toEqual(0)
134+
expect(consoleErrorSpy.mock.calls.length).toEqual(0)
135+
expect(consoleLogSpy.mock.calls.length).toEqual(1)
136+
expect(consoleLogSpy.mock.calls[0]).toEqual([
137+
'User position outside of boundary layer.',
138+
])
139+
})
140+
})
141+
describe('track', () => {
142+
it('instantiate the OpenLayers GeoLocation object and commit it to the store if the geolocation was not denied and the GeoLocation object has not been set yet', () => {
143+
actionContext.getters.isGeolocationDenied = false
144+
145+
storeModule.actions.track(actionContext)
146+
147+
expect(commit.mock.calls.length).toEqual(2)
148+
expect(commit.mock.calls[0][0]).toEqual('setGeolocation')
149+
expect(commit.mock.calls[0][1] instanceof Geolocation).toEqual(true)
150+
expect(commit.mock.calls[0][1].getTracking()).toEqual(true)
151+
expect(commit.mock.calls[0][1].getProjection().getCode()).toEqual(
152+
'EPSG:4326'
153+
)
154+
expect(commit.mock.calls[1]).toEqual(['setTracking', true])
155+
expect(dispatch.mock.calls.length).toEqual(0)
156+
})
157+
it('trigger the action to reposition the location if the geolocation was not denied and the geolocation has been instantiated already', () => {
158+
actionContext.getters.isGeolocationDenied = false
159+
actionContext.getters.geolocation = { on: jest.fn() }
160+
161+
storeModule.actions.track(actionContext)
162+
163+
expect(commit.mock.calls.length).toEqual(1)
164+
expect(commit.mock.calls[0]).toEqual(['setTracking', true])
165+
expect(dispatch.mock.calls.length).toEqual(1)
166+
expect(dispatch.mock.calls[0]).toEqual(['positioning'])
167+
})
168+
it('should dispatch the onError action if the geolocation was denied', () => {
169+
actionContext.getters.isGeolocationDenied = true
170+
171+
storeModule.actions.track(actionContext)
172+
173+
expect(commit.mock.calls.length).toEqual(0)
174+
expect(dispatch.mock.calls.length).toEqual(1)
175+
expect(dispatch.mock.calls[0]).toEqual(['onError'])
176+
})
177+
})
178+
describe('untrack', () => {
179+
it('should reset all relevant fields in the store, remove the marker and stop tracking', () => {
180+
const setTracking = jest.fn()
181+
actionContext.getters.geolocation = { setTracking }
182+
183+
storeModule.actions.untrack(actionContext)
184+
185+
expect(setTracking.mock.calls.length).toEqual(1)
186+
expect(setTracking.mock.calls[0]).toEqual([false])
187+
expect(commit.mock.calls.length).toEqual(2)
188+
expect(commit.mock.calls[0]).toEqual(['setTracking', false])
189+
expect(commit.mock.calls[1]).toEqual(['setGeolocation', null])
190+
expect(dispatch.mock.calls.length).toEqual(1)
191+
expect(dispatch.mock.calls[0]).toEqual(['removeMarker'])
192+
})
193+
})
194+
})
195+
})
196+
})

0 commit comments

Comments
 (0)