aboutsummaryrefslogtreecommitdiffstats
path: root/spec/javascript/vehicle_journeys/reducers/pagination_spec.js
blob: 352997c7c3df53b4a126b6a20783800831053bb4 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import reducer from '../../../../app/javascript/vehicle_journeys/reducers/pagination'

const diff = 1
let state = {
  page : 2,
  totalCount : 25,
  stateChanged: false,
  perPage: 12
}
let pagination = Object.assign({}, state)
const dispatch = function(){}

describe('pagination reducer, given parameters allowing page change', () => {

  it('should return the initial state', () => {
    expect(
      reducer(undefined, {})
    ).toEqual({})
  })

  it('should handle RECEIVE_TOTAL_COUNT', () => {
    expect(
      reducer(state, {
        type: 'RECEIVE_TOTAL_COUNT',
        total: 1
      })
    ).toEqual(Object.assign({}, state, {totalCount: 1}))
  })

  it('should handle RESET_PAGINATION', () => {
    expect(
      reducer(state, {
        type: 'RESET_PAGINATION',
      })
    ).toEqual(Object.assign({}, state, {page: 1}))
  })

  it('should handle UPDATE_TOTAL_COUNT', () => {
    expect(
      reducer(state, {
        type: 'UPDATE_TOTAL_COUNT',
        diff: 1
      })
    ).toEqual(Object.assign({}, state, {totalCount: 24}))
  })

  it('should handle GO_TO_NEXT_PAGE and change state', () => {
    expect(
      reducer(state, {
        type: 'GO_TO_NEXT_PAGE',
        dispatch,
        pagination,
        nextPage : true
      })
    ).toEqual(Object.assign({}, state, {page : state.page + 1, stateChanged: false}))
  })

  it('should return GO_TO_PREVIOUS_PAGE and change state', () => {
    expect(
      reducer(state, {
        type: 'GO_TO_PREVIOUS_PAGE',
        dispatch,
        pagination,
        nextPage : false
      })
    ).toEqual(Object.assign({}, state, {page : state.page - 1, stateChanged: false}))
  })

  it('should handle UPDATE_TIME', () => {
    const val = '33', subIndex = 0, index = 0, timeUnit = 'minute', isDeparture = true, isArrivalsToggled = true
    expect(
      reducer(state, {
        type: 'UPDATE_TIME',
        val,
        subIndex,
        index,
        timeUnit,
        isDeparture,
        isArrivalsToggled
      })
    ).toEqual(Object.assign({}, state, {stateChanged: true}))
  })

})


describe('pagination reducer, given parameters not allowing to go to previous page', () => {

  beforeEach(()=>{
    state.page = 1
    pagination.page = 1
  })

  it('should return GO_TO_PREVIOUS_PAGE and not change state', () => {
    expect(
      reducer(state, {
        type: 'GO_TO_PREVIOUS_PAGE',
        dispatch,
        pagination,
        nextPage : false
      })
    ).toEqual(state)
  })
})

describe('pagination reducer, given parameters not allowing to go to next page', () => {

  beforeEach(()=>{
    state.page = 3
    pagination.page = 3
  })

  it('should return GO_TO_NEXT_PAGE and not change state', () => {
    expect(
      reducer(state, {
        type: 'GO_TO_NEXT_PAGE',
        dispatch,
        pagination,
        nextPage : true
      })
    ).toEqual(state)
  })
})