Skip to content

Commit 91fd5ee

Browse files
committed
feat(api): add another way of customizing otp query
This option allows setting the custom builder function in the config when creating the reducer’s initial state. When using all of the reducers together they make calls to the planTrip action, but there wasn’t really a way to pass in a custom function that way. For now it seems like the only way to do this could be to set something in the config of the state. This PR includes the work done in #12.
1 parent 5f3382a commit 91fd5ee

File tree

3 files changed

+116
-42
lines changed

3 files changed

+116
-42
lines changed

__tests__/actions/__snapshots__/api.js.snap

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`actions > api should make a query to OTP 1`] = `"/api/plan?arriveBy=false&fromPlace=12%2C34&showIntermediateStops=true&toPlace=34%2C12"`;
3+
exports[`actions > api > planTrip should make a query to OTP with customOtpQueryBuilder in state config 1`] = `"/api/plan?from=here&to=there"`;
44

5-
exports[`actions > api should make a query to OTP 2`] = `
5+
exports[`actions > api > planTrip should make a query to OTP with customOtpQueryBuilder in state config 2`] = `
6+
Array [
7+
Array [
8+
Object {
9+
"type": "PLAN_REQUEST",
10+
},
11+
],
12+
Array [
13+
Object {
14+
"payload": Object {
15+
"fake": "response",
16+
},
17+
"type": "PLAN_RESPONSE",
18+
},
19+
],
20+
]
21+
`;
22+
23+
exports[`actions > api > planTrip should make a query to OTP with customOtpQueryBuilder sent to action 1`] = `"/api/plan?from=here&to=there"`;
24+
25+
exports[`actions > api > planTrip should make a query to OTP with customOtpQueryBuilder sent to action 2`] = `
26+
Array [
27+
Array [
28+
Object {
29+
"type": "PLAN_REQUEST",
30+
},
31+
],
32+
Array [
33+
Object {
34+
"payload": Object {
35+
"fake": "response",
36+
},
37+
"type": "PLAN_RESPONSE",
38+
},
39+
],
40+
]
41+
`;
42+
43+
exports[`actions > api > planTrip should make a query to OTP with default settings 1`] = `"/api/plan?arriveBy=false&fromPlace=12%2C34&showIntermediateStops=true&toPlace=34%2C12"`;
44+
45+
exports[`actions > api > planTrip should make a query to OTP with default settings 2`] = `
646
Array [
747
Array [
848
Object {

__tests__/actions/api.js

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,82 @@ function timeoutPromise (ms) {
1010
})
1111
}
1212

13-
const planTripAction = planTrip()
14-
1513
describe('actions > api', () => {
16-
it('should make a query to OTP', async () => {
17-
nock('http://mock-host.com')
18-
.get(/api\/plan/)
19-
.reply(200, {
20-
fake: 'response'
21-
})
22-
.on('request', (req, interceptor) => {
23-
expect(req.path).toMatchSnapshot()
24-
})
14+
describe('> planTrip', () => {
15+
const defaultState = {
16+
otp: {
17+
config: {
18+
api: {
19+
host: 'http://mock-host.com',
20+
path: '/api',
21+
port: 80
22+
}
23+
},
24+
currentQuery: {
25+
from: { lat: 12, lon: 34 },
26+
to: { lat: 34, lon: 12 }
27+
},
28+
searches: []
29+
}
30+
}
2531

26-
const mockDispatch = jest.fn()
27-
planTripAction(mockDispatch, () => {
28-
return {
29-
otp: {
30-
config: {
31-
api: {
32-
host: 'http://mock-host.com',
33-
path: '/api',
34-
port: 80
35-
}
36-
},
37-
currentQuery: {
38-
from: {
39-
lat: 12,
40-
lon: 34
41-
},
42-
to: {
43-
lat: 34,
44-
lon: 12
45-
}
32+
const customOtpQueryBuilder = () => {
33+
return `http://mock-host.com/api/plan?from=here&to=there`
34+
}
35+
36+
const stateWithCustomBuilderFunction = {
37+
otp: {
38+
config: {
39+
api: {
40+
host: 'http://mock-host.com',
41+
path: '/api',
42+
port: 80
4643
},
47-
searches: []
48-
}
44+
customOtpQueryBuilder
45+
},
46+
currentQuery: {
47+
from: { lat: 12, lon: 34 },
48+
to: { lat: 34, lon: 12 }
49+
},
50+
searches: []
4951
}
50-
})
52+
}
5153

52-
// wait for request to complete
53-
await timeoutPromise(100)
54+
const testCases = [{
55+
state: defaultState,
56+
title: 'default settings'
57+
}, {
58+
customOtpQueryBuilder,
59+
state: defaultState,
60+
title: 'customOtpQueryBuilder sent to action'
61+
}, {
62+
state: stateWithCustomBuilderFunction,
63+
title: 'customOtpQueryBuilder in state config'
64+
}]
5465

55-
expect(mockDispatch.mock.calls).toMatchSnapshot()
66+
testCases.forEach((testCase) => {
67+
it(`should make a query to OTP with ${testCase.title}`, async () => {
68+
const planTripAction = planTrip(testCase.customOtpQueryBuilder)
69+
70+
nock('http://mock-host.com')
71+
.get(/api\/plan/)
72+
.reply(200, {
73+
fake: 'response'
74+
})
75+
.on('request', (req, interceptor) => {
76+
expect(req.path).toMatchSnapshot()
77+
})
78+
79+
const mockDispatch = jest.fn()
80+
planTripAction(mockDispatch, () => {
81+
return testCase.state
82+
})
83+
84+
// wait for request to complete
85+
await timeoutPromise(100)
86+
87+
expect(mockDispatch.mock.calls).toMatchSnapshot()
88+
})
89+
})
5690
})
5791
})

lib/actions/api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (typeof (fetch) === 'undefined') {
88
require('isomorphic-fetch')
99
}
1010

11-
import { hasValidLocation } from '../util/state'
11+
import { queryIsValid } from '../util/state'
1212

1313
export const receivedPlanResponse = createAction('PLAN_RESPONSE')
1414
export const requestPlanResponse = createAction('PLAN_REQUEST')
@@ -22,9 +22,9 @@ export function planTrip (customOtpQueryBuilder) {
2222
console.log('query hasn\'t changed')
2323
return
2424
}
25-
if (!hasValidLocation(otpState, 'from') || !hasValidLocation(otpState, 'to')) return // TODO: replace with isQueryValid?
25+
if (!queryIsValid(otpState)) return
2626
dispatch(requestPlanResponse())
27-
const queryBuilderFn = customOtpQueryBuilder || constructPlanQuery
27+
const queryBuilderFn = customOtpQueryBuilder || otpState.config.customOtpQueryBuilder || constructPlanQuery
2828
const url = queryBuilderFn(otpState.config.api, otpState.currentQuery)
2929
// setURLSearch(url)
3030
fetch(url)

0 commit comments

Comments
 (0)