|
1 |
| -import { ParamTypes } from '../src/index'; |
| 1 | +import { ParamTypes, UIRouter } from '../src/index'; |
| 2 | +import { ParamTypeDefinition } from '../src/params'; |
| 3 | +import { StateObject } from '../src/state'; |
2 | 4 |
|
3 | 5 | describe('parameters', () => {
|
4 | 6 | let types;
|
@@ -32,4 +34,88 @@ describe('parameters', () => {
|
32 | 34 | expect(dateType.equals(date1, date3)).toBeFalsy();
|
33 | 35 | });
|
34 | 36 | });
|
| 37 | + |
| 38 | + describe('from a custom type', () => { |
| 39 | + let router: UIRouter = null; |
| 40 | + let state: StateObject = null; |
| 41 | + |
| 42 | + const customTypeA: ParamTypeDefinition = { |
| 43 | + encode: val => (val ? 'true' : 'false'), |
| 44 | + decode: str => (str === 'true' ? true : str === 'false' ? false : undefined), |
| 45 | + dynamic: false, |
| 46 | + equals: (a, b) => a === b, |
| 47 | + inherit: true, |
| 48 | + is: val => typeof val === 'boolean', |
| 49 | + pattern: /(?:true|false)/, |
| 50 | + raw: true, |
| 51 | + }; |
| 52 | + |
| 53 | + const customTypeB: ParamTypeDefinition = { |
| 54 | + encode: val => (val ? 'true' : 'false'), |
| 55 | + decode: str => (str === 'true' ? true : str === 'false' ? false : undefined), |
| 56 | + dynamic: true, |
| 57 | + equals: (a, b) => a === b, |
| 58 | + inherit: false, |
| 59 | + is: val => typeof val === 'boolean', |
| 60 | + pattern: /(?:true|false)/, |
| 61 | + raw: false, |
| 62 | + }; |
| 63 | + |
| 64 | + describe('as a simple path parameter', () => { |
| 65 | + beforeEach(() => { |
| 66 | + router = new UIRouter(); |
| 67 | + router.urlService.config.type('customTypeA', customTypeA); |
| 68 | + router.urlService.config.type('customTypeB', customTypeB); |
| 69 | + |
| 70 | + state = router.stateRegistry.register({ |
| 71 | + name: 'state', |
| 72 | + url: '/{paramA:customTypeA}/{paramB:customTypeB}', |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should use `dynamic` from the custom type', () => { |
| 77 | + expect(state.parameter('paramA').dynamic).toEqual(customTypeA.dynamic); |
| 78 | + expect(state.parameter('paramB').dynamic).toEqual(customTypeB.dynamic); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should use `inherit` from the custom type', () => { |
| 82 | + expect(state.parameter('paramA').inherit).toEqual(customTypeA.inherit); |
| 83 | + expect(state.parameter('paramB').inherit).toEqual(customTypeB.inherit); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should use `raw` from the custom type', () => { |
| 87 | + expect(state.parameter('paramA').raw).toEqual(customTypeA.raw); |
| 88 | + expect(state.parameter('paramB').raw).toEqual(customTypeB.raw); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('as an array path parameter', () => { |
| 93 | + beforeEach(() => { |
| 94 | + router = new UIRouter(); |
| 95 | + router.urlService.config.type('customTypeA', customTypeA); |
| 96 | + router.urlService.config.type('customTypeB', customTypeB); |
| 97 | + |
| 98 | + state = router.stateRegistry.register({ |
| 99 | + name: 'state', |
| 100 | + url: '/{paramA[]:customTypeA}/{paramB[]:customTypeB}', |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should use `dynamic` from the custom type', () => { |
| 105 | + expect(state.parameter('paramA[]').dynamic).toEqual(customTypeA.dynamic); |
| 106 | + expect(state.parameter('paramB[]').dynamic).toEqual(customTypeB.dynamic); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should use `inherit` from the custom type', () => { |
| 110 | + expect(state.parameter('paramA[]').inherit).toEqual(customTypeA.inherit); |
| 111 | + expect(state.parameter('paramB[]').inherit).toEqual(customTypeB.inherit); |
| 112 | + }); |
| 113 | + |
| 114 | + // Test for https://github.com/ui-router/core/issues/178 |
| 115 | + it('should use `raw` from the custom type', () => { |
| 116 | + expect(state.parameter('paramA[]').raw).toEqual(customTypeA.raw); |
| 117 | + expect(state.parameter('paramB[]').raw).toEqual(customTypeB.raw); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
35 | 121 | });
|
0 commit comments