|
| 1 | +import { Component, Input, input } from '@angular/core'; |
| 2 | +import { BehaviorSubject } from 'rxjs'; |
| 3 | +import { RoutedComponentInputBinder } from '@nativescript/angular/lib/legacy/router/router-component-input-binder'; |
| 4 | +import type { PageRouterOutlet } from '@nativescript/angular/lib/legacy/router/page-router-outlet'; |
| 5 | + |
| 6 | +@Component({ template: '', standalone: true }) |
| 7 | +class TestComponent { |
| 8 | + @Input() name: string; |
| 9 | + @Input() id: string; |
| 10 | + @Input() language: string; |
| 11 | +} |
| 12 | + |
| 13 | +@Component({ template: '', standalone: true }) |
| 14 | +class SignalInputComponent { |
| 15 | + name = input<string>(); |
| 16 | + id = input<string>(); |
| 17 | +} |
| 18 | + |
| 19 | +function createMockOutlet( |
| 20 | + component: any, |
| 21 | + options?: { |
| 22 | + params?: Record<string, string>; |
| 23 | + queryParams?: Record<string, string>; |
| 24 | + data?: Record<string, any>; |
| 25 | + }, |
| 26 | +) { |
| 27 | + const params$ = new BehaviorSubject(options?.params ?? {}); |
| 28 | + const queryParams$ = new BehaviorSubject(options?.queryParams ?? {}); |
| 29 | + const data$ = new BehaviorSubject(options?.data ?? {}); |
| 30 | + |
| 31 | + const setInputSpy = jasmine.createSpy('setInput'); |
| 32 | + const activatedRoute = { |
| 33 | + params: params$.asObservable(), |
| 34 | + queryParams: queryParams$.asObservable(), |
| 35 | + data: data$.asObservable(), |
| 36 | + component, |
| 37 | + }; |
| 38 | + |
| 39 | + const outlet = { |
| 40 | + isActivated: true, |
| 41 | + activatedRoute, |
| 42 | + activatedComponentRef: { setInput: setInputSpy }, |
| 43 | + } as unknown as PageRouterOutlet; |
| 44 | + |
| 45 | + return { outlet, params$, queryParams$, data$, setInputSpy, activatedRoute }; |
| 46 | +} |
| 47 | + |
| 48 | +describe('RoutedComponentInputBinder', () => { |
| 49 | + it('binds route params to component inputs', () => { |
| 50 | + const binder = new RoutedComponentInputBinder({ queryParams: true }); |
| 51 | + const { outlet, setInputSpy } = createMockOutlet(TestComponent, { |
| 52 | + params: { name: 'test-name' }, |
| 53 | + }); |
| 54 | + |
| 55 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 56 | + |
| 57 | + expect(setInputSpy).toHaveBeenCalledWith('name', 'test-name'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('binds query params to component inputs', () => { |
| 61 | + const binder = new RoutedComponentInputBinder({ queryParams: true }); |
| 62 | + const { outlet, setInputSpy } = createMockOutlet(TestComponent, { |
| 63 | + queryParams: { id: '42' }, |
| 64 | + }); |
| 65 | + |
| 66 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 67 | + |
| 68 | + expect(setInputSpy).toHaveBeenCalledWith('id', '42'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('binds route data to component inputs', () => { |
| 72 | + const binder = new RoutedComponentInputBinder({ queryParams: true }); |
| 73 | + const { outlet, setInputSpy } = createMockOutlet(TestComponent, { |
| 74 | + data: { name: 'from-data' }, |
| 75 | + }); |
| 76 | + |
| 77 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 78 | + |
| 79 | + expect(setInputSpy).toHaveBeenCalledWith('name', 'from-data'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('data takes priority over params, params over queryParams', () => { |
| 83 | + const binder = new RoutedComponentInputBinder({ queryParams: true }); |
| 84 | + const { outlet, setInputSpy } = createMockOutlet(TestComponent, { |
| 85 | + queryParams: { name: 'from-query' }, |
| 86 | + params: { name: 'from-params' }, |
| 87 | + data: { name: 'from-data' }, |
| 88 | + }); |
| 89 | + |
| 90 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 91 | + |
| 92 | + const nameCall = setInputSpy.calls.allArgs().find((args: any[]) => args[0] === 'name'); |
| 93 | + expect(nameCall[1]).toBe('from-data'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('params take priority over queryParams', () => { |
| 97 | + const binder = new RoutedComponentInputBinder({ queryParams: true }); |
| 98 | + const { outlet, setInputSpy } = createMockOutlet(TestComponent, { |
| 99 | + queryParams: { name: 'from-query' }, |
| 100 | + params: { name: 'from-params' }, |
| 101 | + }); |
| 102 | + |
| 103 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 104 | + |
| 105 | + const nameCall = setInputSpy.calls.allArgs().find((args: any[]) => args[0] === 'name'); |
| 106 | + expect(nameCall[1]).toBe('from-params'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('does not bind query params when queryParams option is false', () => { |
| 110 | + const binder = new RoutedComponentInputBinder({ queryParams: false }); |
| 111 | + const { outlet, setInputSpy } = createMockOutlet(TestComponent, { |
| 112 | + queryParams: { name: 'from-query' }, |
| 113 | + }); |
| 114 | + |
| 115 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 116 | + |
| 117 | + const nameCall = setInputSpy.calls.allArgs().find((args: any[]) => args[0] === 'name'); |
| 118 | + expect(nameCall[1]).toBeUndefined(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('sets unmatched inputs to undefined with alwaysUndefined behavior (default)', () => { |
| 122 | + const binder = new RoutedComponentInputBinder({}); |
| 123 | + const { outlet, setInputSpy } = createMockOutlet(TestComponent, { |
| 124 | + params: { name: 'test' }, |
| 125 | + }); |
| 126 | + |
| 127 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 128 | + |
| 129 | + const idCall = setInputSpy.calls.allArgs().find((args: any[]) => args[0] === 'id'); |
| 130 | + expect(idCall).toBeTruthy(); |
| 131 | + expect(idCall[1]).toBeUndefined(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('does not set unmatched inputs with undefinedIfStale when key was never seen', () => { |
| 135 | + const binder = new RoutedComponentInputBinder({ unmatchedInputBehavior: 'undefinedIfStale' }); |
| 136 | + const { outlet, setInputSpy } = createMockOutlet(TestComponent, { |
| 137 | + params: { name: 'test' }, |
| 138 | + }); |
| 139 | + |
| 140 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 141 | + |
| 142 | + const idCall = setInputSpy.calls.allArgs().find((args: any[]) => args[0] === 'id'); |
| 143 | + expect(idCall).toBeUndefined(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('sets previously seen keys to undefined with undefinedIfStale behavior', async () => { |
| 147 | + const binder = new RoutedComponentInputBinder({ unmatchedInputBehavior: 'undefinedIfStale' }); |
| 148 | + const { outlet, setInputSpy, params$ } = createMockOutlet(TestComponent, { |
| 149 | + params: { name: 'test', id: '1' }, |
| 150 | + }); |
| 151 | + |
| 152 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 153 | + |
| 154 | + const idCallBefore = setInputSpy.calls.allArgs().find((args: any[]) => args[0] === 'id'); |
| 155 | + expect(idCallBefore[1]).toBe('1'); |
| 156 | + |
| 157 | + setInputSpy.calls.reset(); |
| 158 | + params$.next({ name: 'test' }); |
| 159 | + await Promise.resolve(); |
| 160 | + |
| 161 | + const idCallAfter = setInputSpy.calls.allArgs().find((args: any[]) => args[0] === 'id'); |
| 162 | + expect(idCallAfter).toBeTruthy(); |
| 163 | + expect(idCallAfter[1]).toBeUndefined(); |
| 164 | + }); |
| 165 | + |
| 166 | + it('reacts to param changes', async () => { |
| 167 | + const binder = new RoutedComponentInputBinder({}); |
| 168 | + const { outlet, setInputSpy, params$ } = createMockOutlet(TestComponent, { |
| 169 | + params: { name: 'initial' }, |
| 170 | + }); |
| 171 | + |
| 172 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 173 | + expect(setInputSpy).toHaveBeenCalledWith('name', 'initial'); |
| 174 | + |
| 175 | + setInputSpy.calls.reset(); |
| 176 | + params$.next({ name: 'updated' }); |
| 177 | + await Promise.resolve(); |
| 178 | + |
| 179 | + expect(setInputSpy).toHaveBeenCalledWith('name', 'updated'); |
| 180 | + }); |
| 181 | + |
| 182 | + it('unsubscribes from route data', () => { |
| 183 | + const binder = new RoutedComponentInputBinder({}); |
| 184 | + const { outlet, setInputSpy, params$ } = createMockOutlet(TestComponent, { |
| 185 | + params: { name: 'test' }, |
| 186 | + }); |
| 187 | + |
| 188 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 189 | + setInputSpy.calls.reset(); |
| 190 | + |
| 191 | + binder.unsubscribeFromRouteData(outlet); |
| 192 | + params$.next({ name: 'after-unsub' }); |
| 193 | + |
| 194 | + expect(setInputSpy).not.toHaveBeenCalled(); |
| 195 | + }); |
| 196 | + |
| 197 | + it('re-subscribes when bindActivatedRouteToOutletComponent is called again', () => { |
| 198 | + const binder = new RoutedComponentInputBinder({}); |
| 199 | + const { outlet, setInputSpy, params$ } = createMockOutlet(TestComponent, { |
| 200 | + params: { name: 'first' }, |
| 201 | + }); |
| 202 | + |
| 203 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 204 | + setInputSpy.calls.reset(); |
| 205 | + |
| 206 | + params$.next({ name: 'second' }); |
| 207 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 208 | + |
| 209 | + const nameCall = setInputSpy.calls.allArgs().find((args: any[]) => args[0] === 'name'); |
| 210 | + expect(nameCall[1]).toBe('second'); |
| 211 | + }); |
| 212 | + |
| 213 | + it('unsubscribes when outlet is deactivated mid-stream', async () => { |
| 214 | + const binder = new RoutedComponentInputBinder({}); |
| 215 | + const { outlet, setInputSpy, params$ } = createMockOutlet(TestComponent, { |
| 216 | + params: { name: 'test' }, |
| 217 | + }); |
| 218 | + |
| 219 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 220 | + setInputSpy.calls.reset(); |
| 221 | + |
| 222 | + (outlet as any).isActivated = false; |
| 223 | + params$.next({ name: 'after-deactivate' }); |
| 224 | + await Promise.resolve(); |
| 225 | + |
| 226 | + expect(setInputSpy).not.toHaveBeenCalledWith('name', 'after-deactivate'); |
| 227 | + }); |
| 228 | + |
| 229 | + it('works with signal inputs', () => { |
| 230 | + const binder = new RoutedComponentInputBinder({}); |
| 231 | + const { outlet, setInputSpy } = createMockOutlet(SignalInputComponent, { |
| 232 | + params: { name: 'signal-test' }, |
| 233 | + }); |
| 234 | + |
| 235 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 236 | + |
| 237 | + expect(setInputSpy).toHaveBeenCalledWith('name', 'signal-test'); |
| 238 | + }); |
| 239 | + |
| 240 | + it('combines values from all sources', () => { |
| 241 | + const binder = new RoutedComponentInputBinder({ queryParams: true }); |
| 242 | + const { outlet, setInputSpy } = createMockOutlet(TestComponent, { |
| 243 | + queryParams: { language: 'en' }, |
| 244 | + params: { name: 'test-name' }, |
| 245 | + data: { id: 'data-id' }, |
| 246 | + }); |
| 247 | + |
| 248 | + binder.bindActivatedRouteToOutletComponent(outlet); |
| 249 | + |
| 250 | + expect(setInputSpy).toHaveBeenCalledWith('name', 'test-name'); |
| 251 | + expect(setInputSpy).toHaveBeenCalledWith('id', 'data-id'); |
| 252 | + expect(setInputSpy).toHaveBeenCalledWith('language', 'en'); |
| 253 | + }); |
| 254 | +}); |
0 commit comments