diff options
Diffstat (limited to 'spec/javascript')
| -rw-r--r-- | spec/javascript/routes/reducers/stop_points_spec.js | 588 | 
1 files changed, 147 insertions, 441 deletions
| diff --git a/spec/javascript/routes/reducers/stop_points_spec.js b/spec/javascript/routes/reducers/stop_points_spec.js index b375cdc2c..124618f9d 100644 --- a/spec/javascript/routes/reducers/stop_points_spec.js +++ b/spec/javascript/routes/reducers/stop_points_spec.js @@ -1,7 +1,17 @@  import stopPointsReducer from '../../../../app/javascript/routes/reducers/stopPoints' +import formHelper from '../../../../app/javascript/routes/form_helper' +import _ from 'lodash' + + //  _  _ ___ _    ___ ___ ___  ___ + // | || | __| |  | _ \ __| _ \/ __| + // | __ | _|| |__|  _/ _||   /\__ \ + // |_||_|___|____|_| |___|_|_\|___/ + //  let state = [] +formHelper.addInput = (...args)=>{} +  let fakeData = {    geometry: undefined,    registration_number: 'rn_test', @@ -10,34 +20,47 @@ let fakeData = {    user_objectid: 'uoid_test'  } +let update_stop_point = (stop_point, opts) => { +  return _.assign({}, stop_point, opts) +} + +let stop_point = (opts) => { +  return _.assign({}, +    { +      text: "", +      index: 0, +      edit: false, +      for_boarding: 'normal', +      for_alighting: 'normal', +      olMap: { isOpened: false, json: {} } +    }, +    opts +  ) +} + +let stop_point_1 = stop_point({text: 'first',  index: 0, stoppoint_id: 72 }) +let stop_point_2 = stop_point({text: 'second', index: 1, stoppoint_id: 73 }) +let stop_point_3 = stop_point({text: 'third',  index: 2, stoppoint_id: 74 }) + +let it_should_handle = (action, final_state, custom_state=null) => { +  it("should handle "+ action.type, () => { +    expect( +      stopPointsReducer(custom_state || state, action) +    ).toEqual( final_state ) +  }) +} + + + //  ___ ___ ___ ___ ___ + // / __| _ \ __/ __/ __| + // \__ \  _/ _| (__\__ \ + // |___/_| |___\___|___/ + // + +  describe('stops reducer', () => {    beforeEach(()=>{ -    state = [ -      { -        text: 'first', -        index: 0, -        stoppoint_id: 72, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', -        olMap: { -          isOpened: false, -          json: {} -        } -      }, -      { -        text: 'second', -        index: 1, -        stoppoint_id: 73, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', -        olMap: { -          isOpened: false, -          json: {} -        } -      } -    ] +    state = [ stop_point_1, stop_point_2, stop_point_3 ]    })    it('should return the initial state', () => { @@ -46,441 +69,124 @@ describe('stops reducer', () => {      ).toEqual([])    }) -  it('should handle ADD_STOP', () => { -    expect( -      stopPointsReducer(state, { -        type: 'ADD_STOP' -      }) -    ).toEqual( -      [ -        { -          text: 'first', -          index: 0, -          stoppoint_id: 72, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'second', -          index: 1, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: '', -          index: 2, -          edit: true, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  it_should_handle( +    {type: "ADD_STOP"}, +    [stop_point_1, stop_point_2, stop_point_3, stop_point({index: 3, edit: true})] +  ) -  it('should handle MOVE_UP_STOP', () => { -    expect( -      stopPointsReducer(state, { -        type: 'MOVE_STOP_UP', -        index: 1 -      }) -    ).toEqual( -      [ -        { -          text: 'second', -          index: 1, -          stoppoint_id: 72, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'first', -          index: 0, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  it_should_handle( +    {type: 'MOVE_STOP_UP', index: 1}, +    [ update_stop_point(stop_point_2, {index: 0}), update_stop_point(stop_point_1, {index: 1}), stop_point_3 ] +  ) -  it('should handle MOVE_DOWN_STOP', () => { -    expect( -      stopPointsReducer(state, { -        type: 'MOVE_STOP_DOWN', -        index: 0 -      }) -    ).toEqual( -      [ -        { -          text: 'second', -          index: 1, -          stoppoint_id: 72, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'first', -          index: 0, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  it_should_handle( +    {type: 'MOVE_STOP_DOWN', index: 0}, +    [ update_stop_point(stop_point_2, {index: 0}), update_stop_point(stop_point_1, {index: 1}), stop_point_3 ] +  ) -  // it('should handle DELETE_STOP', () => { -  //   expect( -  //     stopPointsReducer(state, { -  //       type: 'DELETE_STOP', -  //       index: 1 -  //     }) -  //   ).toEqual( -  //     [ -  //       { -  //         text: 'first', -  //         index: 0, -  //         stoppoint_id: 72, -  //         edit: false, -  //         for_boarding: 'normal', -  //         for_alighting: 'normal', -  //         olMap: { -  //           isOpened: false, -  //           json: {} -  //         } -  //       } -  //     ] -  //   ) -  // }) +  it_should_handle( +    {type: 'DELETE_STOP', index: 1}, +    [stop_point_1, stop_point_3] +  ) -  it('should handle UPDATE_INPUT_VALUE', () => { -    expect( -      stopPointsReducer(state, { -        type: 'UPDATE_INPUT_VALUE', -        index: 0, -        edit: false, -        text: { -          text: "new value", -          name: 'new', -          stoparea_id: 1, -          user_objectid: "1234", -          longitude: 123, -          latitude: 123, -          registration_number: '0', -          city_name: 'city', -          area_type: 'area', -          short_name: 'new', -          comment: 'newcomment' -        } -      }) -    ).toEqual( -      [ -        { -          text: 'new value', -          name: 'new', -          index: 0, -          stoppoint_id: 72, -          edit: false, -          stoparea_id: 1, -          for_boarding: 'normal', -          for_alighting: 'normal', -          user_objectid: "1234", -          longitude: 123, -          latitude: 123, -          registration_number: '0', -          city_name: 'city', -          area_type: 'area', -          short_name: 'new', -          comment: 'newcomment', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'second', -          index: 1, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  let text = { +    text: "new value", +    name: 'new', +    stoparea_id: 1, +    user_objectid: "1234", +    longitude: 123, +    latitude: 123, +    registration_number: '0', +    city_name: 'city', +    area_type: 'area', +    short_name: 'new', +    comment: 'newcomment' +  } +  it_should_handle( +    {type: 'UPDATE_INPUT_VALUE', index: 0, text: text}, +    [ +      update_stop_point(stop_point_1, text), +      stop_point_2, +      stop_point_3 +    ] +  ) -  it('should handle UPDATE_SELECT_VALUE', () => { -    expect( -      stopPointsReducer(state, { -        type :'UPDATE_SELECT_VALUE', -        select_id: 'for_boarding', -        select_value: 'prohibited', -        index: 0 -      }) -    ).toEqual( -      [ -        { -          text: 'first', -          index: 0, -          stoppoint_id: 72, -          edit: false, -          for_boarding: 'prohibited', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'second', -          index: 1, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  it_should_handle( +    {type: 'UPDATE_SELECT_VALUE', index: 0, select_id: 'for_boarding', select_value: 'prohibited'}, +    [ +      update_stop_point(stop_point_1, {for_boarding: 'prohibited'}), +      stop_point_2, +      stop_point_3 +    ] +  ) -  it('should handle TOGGLE_MAP', () => { -    expect( -      stopPointsReducer(state, { -        type: 'TOGGLE_MAP', -        index: 0 -      }) -    ).toEqual( -      [ -        { +  it_should_handle( +    {type: 'TOGGLE_MAP', index: 0}, +    [ +      update_stop_point(stop_point_1, {olMap: { +        isOpened: true, +        json: {            text: 'first',            index: 0,            stoppoint_id: 72,            edit: false,            for_boarding: 'normal',            for_alighting: 'normal', -          olMap: { -            isOpened: true, -            json: { -              text: 'first', -              index: 0, -              stoppoint_id: 72, -              edit: false, -              for_boarding: 'normal', -              for_alighting: 'normal', -              olMap: undefined -            } -          } -        }, -        { -          text: 'second', -          index: 1, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } +          olMap: undefined          } -      ] -    ) -  }) +      }}), +      stop_point_2, +      stop_point_3 +    ] +  ) -  it('should handle TOGGLE_EDIT', () => { -    expect( -      stopPointsReducer(state, { -        type: 'TOGGLE_EDIT', -        index: 0 -      }) -    ).toEqual( -      [ -        { -          text: 'first', -          index: 0, -          stoppoint_id: 72, -          edit: true, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'second', -          index: 1, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  it_should_handle( +    {type: 'TOGGLE_EDIT', index: 0}, +    [ +      update_stop_point(stop_point_1, {edit: true}), +      stop_point_2, +      stop_point_3 +    ] +  ) -  it('should handle SELECT_MARKER', () => { -    let openedMapState = [ -      { -        text: 'first', -        index: 0, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', -        olMap: { -          isOpened: true, -          json: {} -        } -      }, -      { -        text: 'second', -        index: 1, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', -        olMap: { -          isOpened: false, -          json: {} -        } +  let openedMapState = [ +    update_stop_point(stop_point_1, { +      olMap: { +        isOpened: true, +        json: {}        } -    ] -    expect( -      stopPointsReducer(openedMapState, { -        type: 'SELECT_MARKER', -        index: 0, -        data: fakeData -      }) -    ).toEqual( -      [ -        { -          text: 'first', -          index: 0, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: true, -            json: fakeData -          } -        }, -        { -          text: 'second', -          index: 1, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +    }), +    stop_point_2, +    stop_point_3 +  ] -  it('should handle UNSELECT_MARKER', () => { -    let openedMapState = [ -      { -        text: 'first', -        index: 0, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', +  it_should_handle( +    {type: 'SELECT_MARKER', index: 0, data: fakeData}, +    [ +      update_stop_point(stop_point_1, {          olMap: {            isOpened: true, -          json: {} -        } -      }, -      { -        text: 'second', -        index: 1, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', +          json: fakeData +        } +      }), +      stop_point_2, +      stop_point_3 +    ], +    openedMapState +  ) + +  it_should_handle( +    {type: 'UNSELECT_MARKER', index: 0}, +    [ +      update_stop_point(stop_point_1, {          olMap: { -          isOpened: false, +          isOpened: true,            json: {}          } -      } -    ] - -    expect( -      stopPointsReducer(openedMapState, { -        type: 'UNSELECT_MARKER', -        index: 0 -      }) -    ).toEqual( -      [ -        { -          text: 'first', -          index: 0, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: true, -            json: {} -          } -        }, -        { -          text: 'second', -          index: 1, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +      }), +      stop_point_2, +      stop_point_3 +    ], +    openedMapState +  )  }) | 
