|
1 | 1 | import { FieldState, FormState, ArrayFormState, TransformedState, DebouncedState, DebouncedFieldState } from '.' |
2 | | -import { defaultDelay, delay } from './testUtils' |
| 2 | +import { assertType, defaultDelay, delay } from './testUtils' |
3 | 3 |
|
4 | 4 | describe('FieldState', () => { |
5 | 5 | it('should be newable', () => { |
@@ -126,3 +126,63 @@ describe('Composition', () => { |
126 | 126 | expect(hostState.error).toBe('empty hostname') |
127 | 127 | }) |
128 | 128 | }) |
| 129 | + |
| 130 | +interface HostInput { |
| 131 | + hostname: string | null |
| 132 | + port: number | null |
| 133 | +} |
| 134 | + |
| 135 | +function parseHost(input: string): HostInput { |
| 136 | + const [hostname, portStr] = input.split(':') |
| 137 | + const port = parseInt(portStr, 10) |
| 138 | + return { hostname, port } |
| 139 | +} |
| 140 | + |
| 141 | +function stringifyHost(host: HostInput) { |
| 142 | + const suffix = (host.port == null || Number.isNaN(host.port)) ? '' : `:${host.port}` |
| 143 | + return host.hostname + suffix |
| 144 | +} |
| 145 | + |
| 146 | +function createRawHostState(host: HostInput) { |
| 147 | + const hostnameState = new FieldState<string | null>(host.hostname).withValidator<string>( |
| 148 | + v => !v && 'empty hostname' |
| 149 | + ) |
| 150 | + const portState = new FieldState<number | null, number>(host.port) |
| 151 | + return new FormState({ |
| 152 | + hostname: hostnameState, |
| 153 | + port: portState |
| 154 | + }) |
| 155 | +} |
| 156 | + |
| 157 | +function createDebouncedHostState(hostStr: string) { |
| 158 | + const host = parseHost(hostStr) |
| 159 | + const rawState = createRawHostState(host) |
| 160 | + const state = new DebouncedState( |
| 161 | + new TransformedState(rawState, stringifyHost, parseHost), |
| 162 | + defaultDelay |
| 163 | + ).withValidator( |
| 164 | + v => !v && 'empty' |
| 165 | + ) |
| 166 | + return state |
| 167 | +} |
| 168 | + |
| 169 | +describe('safeValue', () => { |
| 170 | + it('should work well', () => { |
| 171 | + const state = new FieldState<string | null>('foo').withValidator<string>( |
| 172 | + v => v == null && 'empty' |
| 173 | + ) |
| 174 | + assertType<string>(state.safeValue) |
| 175 | + }) |
| 176 | + it('should work well with multiple validators', () => { |
| 177 | + const state = new FieldState<string | number | null>('foo').withValidator<string | number>( |
| 178 | + v => v == null && 'empty' |
| 179 | + ).withValidator<string>( |
| 180 | + v => typeof v !== 'string' && 'not string' |
| 181 | + ) |
| 182 | + assertType<string>(state.safeValue) |
| 183 | + }) |
| 184 | + it('should work well with complex states', () => { |
| 185 | + const rawHostState = createRawHostState({ hostname: 'foo', port: 80 }) |
| 186 | + assertType<{ hostname: string, port: number }>(rawHostState.safeValue) |
| 187 | + }) |
| 188 | +}) |
0 commit comments