File tree 9 files changed +134
-13
lines changed
9 files changed +134
-13
lines changed Original file line number Diff line number Diff line change
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports [` actions > api > form should debounce numerous requests to plan a trip 1` ] = `
4
+ Array [
5
+ Array [
6
+ Object {
7
+ " type" : " FORM_CHANGED" ,
8
+ } ,
9
+ ],
10
+ Array [
11
+ Object {
12
+ " type" : " FORM_CHANGED" ,
13
+ } ,
14
+ ],
15
+ Array [
16
+ Object {
17
+ " type" : " FORM_CHANGED" ,
18
+ } ,
19
+ ],
20
+ Array [
21
+ Object {
22
+ " type" : " FORM_CHANGED" ,
23
+ } ,
24
+ ],
25
+ Array [
26
+ Object {
27
+ " type" : " FORM_CHANGED" ,
28
+ } ,
29
+ ],
30
+ Array [
31
+ Object {
32
+ " type" : " FORM_CHANGED" ,
33
+ } ,
34
+ ],
35
+ Array [
36
+ Object {
37
+ " type" : " FORM_CHANGED" ,
38
+ } ,
39
+ ],
40
+ Array [
41
+ Object {
42
+ " type" : " FORM_CHANGED" ,
43
+ } ,
44
+ ],
45
+ Array [
46
+ Object {
47
+ " type" : " FORM_CHANGED" ,
48
+ } ,
49
+ ],
50
+ Array [
51
+ Object {
52
+ " type" : " FORM_CHANGED" ,
53
+ } ,
54
+ ],
55
+ Array [
56
+ [Function],
57
+ ],
58
+ ]
59
+ ` ;
Original file line number Diff line number Diff line change 2
2
3
3
import nock from 'nock'
4
4
5
- import { planTrip } from '../../lib/actions/api '
5
+ import { timeoutPromise } from '../test-utils '
6
6
7
- function timeoutPromise ( ms ) {
8
- return new Promise ( ( resolve , reject ) => {
9
- setTimeout ( resolve , ms )
10
- } )
11
- }
7
+ import { planTrip } from '../../lib/actions/api'
12
8
13
9
describe ( 'actions > api' , ( ) => {
14
10
describe ( '> planTrip' , ( ) => {
Original file line number Diff line number Diff line change
1
+ /* globals describe, expect, it, jest */
2
+
3
+ import { timeoutPromise } from '../test-utils'
4
+
5
+ import { formChanged } from '../../lib/actions/form'
6
+
7
+ describe ( 'actions > api > form' , ( ) => {
8
+ it ( 'should debounce numerous requests to plan a trip' , async ( ) => {
9
+ const defaultState = {
10
+ otp : {
11
+ config : {
12
+ api : {
13
+ host : 'http://mock-host.com' ,
14
+ path : '/api' ,
15
+ port : 80
16
+ } ,
17
+ autoPlan : true ,
18
+ debouncePlanTimeMs : 500
19
+ } ,
20
+ currentQuery : {
21
+ from : { lat : 12 , lon : 34 } ,
22
+ to : { lat : 34 , lon : 12 }
23
+ } ,
24
+ searches : [ ]
25
+ }
26
+ }
27
+
28
+ const formChangedAction = formChanged ( )
29
+
30
+ const mockDispatch = jest . fn ( )
31
+ for ( var i = 0 ; i < 10 ; i ++ ) {
32
+ formChangedAction ( mockDispatch , ( ) => defaultState )
33
+ }
34
+
35
+ // wait for request to complete
36
+ await timeoutPromise ( 1000 )
37
+
38
+ expect ( mockDispatch . mock . calls ) . toMatchSnapshot ( )
39
+ } )
40
+ } )
Original file line number Diff line number Diff line change
1
+ export function timeoutPromise ( ms ) {
2
+ return new Promise ( ( resolve , reject ) => {
3
+ setTimeout ( resolve , ms )
4
+ } )
5
+ }
Original file line number Diff line number Diff line change @@ -28,9 +28,9 @@ export function planTrip (customOtpQueryBuilder) {
28
28
const queryBuilderFn = customOtpQueryBuilder || otpState . config . customOtpQueryBuilder || constructPlanQuery
29
29
const url = queryBuilderFn ( otpState . config . api , otpState . currentQuery )
30
30
// setURLSearch(url)
31
- let response , json
31
+ let json
32
32
try {
33
- response = await fetch ( url )
33
+ const response = await fetch ( url )
34
34
if ( response . status >= 400 ) {
35
35
const error = new Error ( 'Received error from server' )
36
36
error . response = response
Original file line number Diff line number Diff line change
1
+ import debounce from 'lodash.debounce'
1
2
import { createAction } from 'redux-actions'
2
3
3
4
import { planTrip } from './api'
@@ -37,12 +38,25 @@ export function setTime (payload) {
37
38
}
38
39
}
39
40
41
+ let debouncedPlanTrip // store as variable here, so it can be reused.
42
+ let lastDebouncePlanTimeMs
43
+
40
44
export function formChanged ( ) {
41
45
return function ( dispatch , getState ) {
42
46
dispatch ( changingForm ( ) )
43
- // TODO: make auto-plan trip a configurable
44
- if ( queryIsValid ( getState ( ) . otp ) ) {
45
- dispatch ( planTrip ( ) )
47
+ const otpState = getState ( ) . otp
48
+ const { autoPlan, debouncePlanTimeMs} = otpState . config
49
+
50
+ // check if a trip plan should be made
51
+ if ( autoPlan && queryIsValid ( otpState ) ) {
52
+ // trip plan should be made
53
+
54
+ // check if debouncing function needs to be (re)created
55
+ if ( ! debouncedPlanTrip || lastDebouncePlanTimeMs !== debouncePlanTimeMs ) {
56
+ debouncedPlanTrip = debounce ( ( ) => dispatch ( planTrip ( ) ) , debouncePlanTimeMs )
57
+ lastDebouncePlanTimeMs = debouncePlanTimeMs
58
+ }
59
+ debouncedPlanTrip ( )
46
60
}
47
61
}
48
62
}
Original file line number Diff line number Diff line change @@ -22,9 +22,11 @@ function createOtpReducer (config, initialQuery) {
22
22
defaultQuery . mode = defaultQuery . mode || config . modes [ 0 ] || 'TRANSIT,WALK'
23
23
// populate query by merging any provided query params w/ the default params
24
24
const currentQuery = Object . assign ( defaultQuery , initialQuery )
25
- console . log ( currentQuery )
26
25
const initialState = {
27
- config,
26
+ config : Object . assign ( {
27
+ autoPlan : true ,
28
+ debouncePlanTimeMs : 0
29
+ } , config ) ,
28
30
currentQuery,
29
31
searches : [ ] ,
30
32
activeSearch : 0
Original file line number Diff line number Diff line change 36
36
"immutability-helper" : " ^2.1.1" ,
37
37
"isomorphic-fetch" : " ^2.2.1" ,
38
38
"leaflet" : " ^1.0.3" ,
39
+ "lodash.debounce" : " ^4.0.8" ,
39
40
"moment" : " ^2.17.1" ,
40
41
"object-path" : " ^0.11.3" ,
41
42
"qs" : " ^6.3.0" ,
Original file line number Diff line number Diff line change @@ -4306,6 +4306,10 @@ lodash.assign@^4.0.3, lodash.assign@^4.0.6:
4306
4306
version "4.2.0"
4307
4307
resolved "https://registry.yarnpkg.com/lodash.assign/-/lodash.assign-4.2.0.tgz#0d99f3ccd7a6d261d19bdaeb9245005d285808e7"
4308
4308
4309
+ lodash.debounce@^4.0.8 :
4310
+ version "4.0.8"
4311
+ resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
4312
+
4309
4313
lodash.isarguments@^3.0.0 :
4310
4314
version "3.1.0"
4311
4315
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"
You can’t perform that action at this time.
0 commit comments