|
| 1 | +import { HttpClientTestingModule } from "@angular/common/http/testing"; |
| 2 | +import { Component, DebugElement } from "@angular/core"; |
| 3 | +import { ComponentFixture, TestBed } from "@angular/core/testing"; |
| 4 | +import { FormControl, FormsModule, ReactiveFormsModule } from "@angular/forms"; |
| 5 | +import { By } from "@angular/platform-browser"; |
| 6 | +import { BrowserDynamicTestingModule } |
| 7 | + from "@angular/platform-browser-dynamic/testing"; |
| 8 | +import { RouterTestingModule } from "@angular/router/testing"; |
| 9 | +import { GlobalStorage, USER_SERVICE, UserConfigurationService } |
| 10 | + from "@batch-flask/core"; |
| 11 | +import { |
| 12 | + I18nTestingModule, |
| 13 | + MockGlobalStorage, |
| 14 | + MockUserConfigurationService |
| 15 | +} from "@batch-flask/core/testing"; |
| 16 | +import { ElectronTestingModule } from "@batch-flask/electron/testing"; |
| 17 | +import { ButtonsModule, DialogService, FormModule, SelectModule } |
| 18 | + from "@batch-flask/ui"; |
| 19 | +import { AuthService, BatchExplorerService } from "app/services"; |
| 20 | +import { BehaviorSubject } from "rxjs"; |
| 21 | +import { FileGroupPickerComponent } from "./file-group-picker.component"; |
| 22 | +import { FileGroupPickerModule } from "./file-group-picker.module"; |
| 23 | + |
| 24 | +@Component({ |
| 25 | + template: `<bl-file-group-picker [formControl]="control"></bl-file-group-picker>`, |
| 26 | +}) |
| 27 | +class TestComponent { |
| 28 | + public control = new FormControl(); |
| 29 | +} |
| 30 | + |
| 31 | +describe("FileGroupPickerComponent", () => { |
| 32 | + let testComponent: TestComponent; |
| 33 | + let component: FileGroupPickerComponent; |
| 34 | + let fixture: ComponentFixture<TestComponent>; |
| 35 | + let de: DebugElement; |
| 36 | + |
| 37 | + const userServiceSpy = { currentUser: new BehaviorSubject(null) }; |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + await TestBed.configureTestingModule({ |
| 41 | + imports: [ |
| 42 | + FormModule, |
| 43 | + FormsModule, |
| 44 | + ReactiveFormsModule, |
| 45 | + SelectModule, |
| 46 | + RouterTestingModule, |
| 47 | + I18nTestingModule, |
| 48 | + HttpClientTestingModule, |
| 49 | + ButtonsModule, |
| 50 | + ElectronTestingModule, |
| 51 | + BrowserDynamicTestingModule, |
| 52 | + FileGroupPickerModule |
| 53 | + ], |
| 54 | + declarations: [TestComponent], |
| 55 | + providers: [ |
| 56 | + { provide: BatchExplorerService, useValue: {} }, |
| 57 | + { provide: UserConfigurationService, useValue: |
| 58 | + new MockUserConfigurationService({}) }, |
| 59 | + { provide: AuthService, useValue: userServiceSpy }, |
| 60 | + { provide: GlobalStorage, useValue: new MockGlobalStorage() }, |
| 61 | + { provide: USER_SERVICE, useValue: userServiceSpy }, |
| 62 | + { provide: DialogService, useValue: {} }, |
| 63 | + ] |
| 64 | + }).compileComponents(); |
| 65 | + |
| 66 | + fixture = TestBed.createComponent(TestComponent); |
| 67 | + testComponent = fixture.componentInstance; |
| 68 | + de = fixture.debugElement.query(By.css("bl-file-group-picker")); |
| 69 | + component = fixture.debugElement.query(By.css("bl-file-group-picker")) |
| 70 | + .componentInstance; |
| 71 | + fixture.detectChanges(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should pick an existing value", () => { |
| 75 | + const testValue = 'existingValue'; |
| 76 | + component.fileGroupPicked(testValue); |
| 77 | + fixture.detectChanges(); |
| 78 | + expect(component.value.value).toEqual(testValue); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should create a new file group on null value", () => { |
| 82 | + const spy = spyOn(component, "createFileGroup"); |
| 83 | + const testValue = null; |
| 84 | + component.fileGroupPicked(testValue); |
| 85 | + fixture.detectChanges(); |
| 86 | + expect(spy).toHaveBeenCalledOnce(); |
| 87 | + }); |
| 88 | + |
| 89 | + afterEach(() => { |
| 90 | + fixture.destroy(); |
| 91 | + }); |
| 92 | +}); |
0 commit comments