|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 19 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 20 | +import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; |
| 21 | +import {MatSelectHarness} from '@angular/material/select/testing'; |
| 22 | +import {By} from '@angular/platform-browser'; |
| 23 | +import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; |
| 24 | +import {Router} from '@angular/router'; |
| 25 | +import {RouterTestingModule} from '@angular/router/testing'; |
| 26 | +import {routerReducer, RouterReducerState, StoreRouterConnectingModule} from '@ngrx/router-store'; |
| 27 | +import {createFeatureSelector, Store, StoreModule} from '@ngrx/store'; |
| 28 | +import {UrlParamKey} from 'src/app/common/types'; |
| 29 | +import {appendConfigIndexToKey} from 'src/app/common/utils'; |
| 30 | +import {InputTypeId} from 'src/app/data_model/input_type'; |
| 31 | +import {AppState} from 'src/app/store/state'; |
| 32 | + |
| 33 | +import {InputSelector} from './input_selector.component'; |
| 34 | +import {InputSelectorModule} from './input_selector.module'; |
| 35 | + |
| 36 | +describe('InputSelector', () => { |
| 37 | + let store: Store<AppState>; |
| 38 | + let router: Router; |
| 39 | + let fixture: ComponentFixture<InputSelector>; |
| 40 | + let loader: HarnessLoader; |
| 41 | + const selectRouter = createFeatureSelector<RouterReducerState>('router'); |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + TestBed.configureTestingModule({ |
| 45 | + declarations: [ |
| 46 | + InputSelector, |
| 47 | + ], |
| 48 | + imports: [ |
| 49 | + InputSelectorModule, |
| 50 | + BrowserAnimationsModule, |
| 51 | + RouterTestingModule, |
| 52 | + StoreModule.forRoot({ |
| 53 | + 'router': routerReducer, |
| 54 | + }), |
| 55 | + StoreRouterConnectingModule.forRoot(), |
| 56 | + ], |
| 57 | + }); |
| 58 | + |
| 59 | + router = TestBed.inject(Router); |
| 60 | + store = TestBed.inject(Store); |
| 61 | + fixture = TestBed.createComponent(InputSelector); |
| 62 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should select the correct input type encoded in URL', fakeAsync(() => { |
| 66 | + // Verify that the default selection is the first one in the inputTypes |
| 67 | + // array. |
| 68 | + fixture.componentInstance.configIndex = 1; |
| 69 | + // Need 2 rounds of change detections (one for initial data binding and |
| 70 | + // one for navigation) to properly update the UI |
| 71 | + detectChanges(2); |
| 72 | + const selectEle: HTMLElement = |
| 73 | + fixture.debugElement.query(By.css('mat-select')).nativeElement; |
| 74 | + expect(selectEle.textContent) |
| 75 | + .toBe(fixture.componentInstance.inputTypes[0].label); |
| 76 | + |
| 77 | + // After updating the url with inputTypeId = CUSTOM_VALUE, verify that |
| 78 | + // the input type selector has the correct one selected. |
| 79 | + router.navigate([], { |
| 80 | + queryParams: { |
| 81 | + [appendConfigIndexToKey( |
| 82 | + UrlParamKey.SELECTED_INPUT_TYPE_ID, |
| 83 | + fixture.componentInstance.configIndex)]: |
| 84 | + `${InputTypeId.CUSTOM_VALUE}`, |
| 85 | + } |
| 86 | + }); |
| 87 | + detectChanges(); |
| 88 | + |
| 89 | + const inputType = fixture.componentInstance.inputTypes.find( |
| 90 | + inputType => inputType.id === InputTypeId.CUSTOM_VALUE)!; |
| 91 | + expect(selectEle.textContent).toBe(inputType.label); |
| 92 | + })); |
| 93 | + |
| 94 | + it('should correctly encode the selected input type in URL', fakeAsync(() => { |
| 95 | + fixture.componentInstance.configIndex = 1; |
| 96 | + detectChanges(2); |
| 97 | + |
| 98 | + // Click the "Random" in the input type selector. |
| 99 | + const selectInputType = async () => { |
| 100 | + const selector = await loader.getHarness(MatSelectHarness); |
| 101 | + await selector.open(); |
| 102 | + const options = await selector.getOptions({text: 'Random'}); |
| 103 | + await options[0].click(); |
| 104 | + }; |
| 105 | + |
| 106 | + selectInputType(); |
| 107 | + tick(); |
| 108 | + |
| 109 | + // Verify that the router store has the correct query param for the |
| 110 | + // selected input type id. |
| 111 | + let curRouterState!: RouterReducerState; |
| 112 | + store.select(selectRouter).subscribe(routerState => { |
| 113 | + curRouterState = routerState; |
| 114 | + }); |
| 115 | + tick(); |
| 116 | + |
| 117 | + expect(curRouterState.state.root.queryParams[appendConfigIndexToKey( |
| 118 | + UrlParamKey.SELECTED_INPUT_TYPE_ID, |
| 119 | + fixture.componentInstance.configIndex)]) |
| 120 | + .toBe(`${InputTypeId.RANDOM}`); |
| 121 | + })); |
| 122 | + |
| 123 | + const detectChanges = (count = 1) => { |
| 124 | + for (let i = 0; i < count; i++) { |
| 125 | + tick(); |
| 126 | + fixture.detectChanges(); |
| 127 | + } |
| 128 | + }; |
| 129 | +}); |
0 commit comments