|
| 1 | +import { Params } from '@angular/router'; |
| 2 | + |
| 3 | +import { |
| 4 | + AddParameterAction, |
| 5 | + AddQueryParameterAction, |
| 6 | + ResetRouteStateAction, |
| 7 | + SetParameterAction, |
| 8 | + SetParametersAction, |
| 9 | + SetQueryParameterAction, |
| 10 | + SetQueryParametersAction, |
| 11 | +} from './route.actions'; |
| 12 | +import { |
| 13 | + routeReducer, |
| 14 | + RouteState, |
| 15 | +} from './route.reducer'; |
| 16 | + |
| 17 | +describe('routeReducer', () => { |
| 18 | + const initialState: RouteState = { |
| 19 | + queryParams: {}, |
| 20 | + params: {}, |
| 21 | + }; |
| 22 | + |
| 23 | + it('should return initial state for unknown action', () => { |
| 24 | + const state = routeReducer(undefined, { type: 'UNKNOWN' } as any); |
| 25 | + expect(state).toEqual(initialState); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('RESET', () => { |
| 29 | + it('should reset state to initialState', () => { |
| 30 | + const state: RouteState = { |
| 31 | + queryParams: { f: { author: ['Smith'] } }, |
| 32 | + params: { id: '123', configuration: 'default' }, |
| 33 | + }; |
| 34 | + const result = routeReducer(state, new ResetRouteStateAction()); |
| 35 | + expect(result).toEqual(initialState); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('SET_PARAMETERS', () => { |
| 40 | + it('should replace all params with action payload', () => { |
| 41 | + const state: RouteState = { ...initialState }; |
| 42 | + const payload: Params = { id: '123', configuration: 'default' }; |
| 43 | + const result = routeReducer(state, new SetParametersAction(payload)); |
| 44 | + expect(result.params).toEqual(payload); |
| 45 | + expect(result.queryParams).toEqual(state.queryParams); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should clear params when payload is empty', () => { |
| 49 | + const state: RouteState = { |
| 50 | + queryParams: {}, |
| 51 | + params: { id: '123' }, |
| 52 | + }; |
| 53 | + const result = routeReducer(state, new SetParametersAction({})); |
| 54 | + expect(result.params).toEqual({}); |
| 55 | + expect(result.queryParams).toEqual(state.queryParams); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('SET_QUERY_PARAMETERS', () => { |
| 60 | + it('should replace all queryParams with action payload', () => { |
| 61 | + const state: RouteState = { ...initialState }; |
| 62 | + const payload: Params = { 'f.author': ['Smith'], 'spc.page': '1' }; |
| 63 | + const result = routeReducer(state, new SetQueryParametersAction(payload)); |
| 64 | + expect(result.queryParams).toEqual(payload); |
| 65 | + expect(result.params).toEqual(state.params); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should clear queryParams when payload is empty', () => { |
| 69 | + const state: RouteState = { |
| 70 | + queryParams: { 'f.author': ['Smith'] }, |
| 71 | + params: {}, |
| 72 | + }; |
| 73 | + const result = routeReducer(state, new SetQueryParametersAction({})); |
| 74 | + expect(result.queryParams).toEqual({}); |
| 75 | + expect(result.params).toEqual(state.params); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('SET_PARAMETER', () => { |
| 80 | + it('should set a single param key-value pair', () => { |
| 81 | + const state: RouteState = { |
| 82 | + queryParams: {}, |
| 83 | + params: { id: '123' }, |
| 84 | + }; |
| 85 | + const result = routeReducer(state, new SetParameterAction('configuration', 'default')); |
| 86 | + expect(result.params).toEqual({ id: '123', configuration: 'default' }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('SET_QUERY_PARAMETER', () => { |
| 91 | + it('should set a single query param key-value pair', () => { |
| 92 | + const state: RouteState = { |
| 93 | + queryParams: { 'f.author': ['Smith'] }, |
| 94 | + params: {}, |
| 95 | + }; |
| 96 | + const result = routeReducer(state, new SetQueryParameterAction('tab', 'publications')); |
| 97 | + expect(result.queryParams).toEqual({ 'f.author': ['Smith'], tab: 'publications' }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('ADD_PARAMETER', () => { |
| 102 | + it('should append value to existing param key', () => { |
| 103 | + const state: RouteState = { |
| 104 | + queryParams: {}, |
| 105 | + params: { tag: ['a'] }, |
| 106 | + }; |
| 107 | + const result = routeReducer(state, new AddParameterAction('tag', 'b')); |
| 108 | + expect(result.params.tag).toEqual(['a', 'b']); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should create new param key with value array when key does not exist', () => { |
| 112 | + const state: RouteState = { ...initialState }; |
| 113 | + const result = routeReducer(state, new AddParameterAction('tag', 'new')); |
| 114 | + expect(result.params.tag).toEqual(['new']); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('ADD_QUERY_PARAMETER', () => { |
| 119 | + it('should append value to existing query param key', () => { |
| 120 | + const state: RouteState = { |
| 121 | + queryParams: { 'f.author': ['Smith'] }, |
| 122 | + params: {}, |
| 123 | + }; |
| 124 | + const result = routeReducer(state, new AddQueryParameterAction('f.author', 'Jones')); |
| 125 | + expect(result.queryParams['f.author']).toEqual(['Smith', 'Jones']); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should create new query param key with value array when key does not exist', () => { |
| 129 | + const state: RouteState = { ...initialState }; |
| 130 | + const result = routeReducer(state, new AddQueryParameterAction('tag', 'new')); |
| 131 | + expect(result.queryParams.tag).toEqual(['new']); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments