|
1 | 1 | import { Component, DebugElement } from '@angular/core'; |
2 | 2 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
3 | | -import { FormsModule } from '@angular/forms'; |
| 3 | +import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms'; |
4 | 4 | import { By } from '@angular/platform-browser'; |
5 | 5 |
|
| 6 | +import { KbqButton } from '@koobiq/components/button'; |
| 7 | +import { KbqInput } from '@koobiq/components/input'; |
6 | 8 | import { KbqSearchExpandable, KbqSearchExpandableModule } from '@koobiq/components/search-expandable'; |
7 | 9 |
|
8 | 10 | describe('KbqSearchExpandable', () => { |
9 | 11 | let debugElement: DebugElement; |
10 | 12 | let nativeElement: HTMLElement; |
11 | | - let fixture: ComponentFixture<TestApp>; |
| 13 | + let fixture: ComponentFixture<TestSearchExpandable>; |
12 | 14 |
|
13 | 15 | beforeEach(async () => { |
14 | 16 | await TestBed.configureTestingModule({ |
15 | | - imports: [KbqSearchExpandableModule, TestApp] |
| 17 | + imports: [KbqSearchExpandableModule, TestSearchExpandable, TestSearchExpandableWithFormControl] |
16 | 18 | }).compileComponents(); |
17 | 19 | }); |
18 | 20 |
|
19 | 21 | beforeEach(() => { |
20 | | - fixture = TestBed.createComponent(TestApp); |
| 22 | + fixture = TestBed.createComponent(TestSearchExpandable); |
21 | 23 | debugElement = fixture.debugElement.query(By.directive(KbqSearchExpandable)); |
22 | 24 | nativeElement = debugElement.nativeElement; |
23 | 25 | fixture.detectChanges(); |
@@ -51,17 +53,71 @@ describe('KbqSearchExpandable', () => { |
51 | 53 | expect(nativeElement.querySelectorAll('.kbq-search-expandable__button').length).toBe(0); |
52 | 54 | expect(nativeElement.querySelectorAll('.kbq-search-expandable__search').length).toBe(1); |
53 | 55 | }); |
| 56 | + |
| 57 | + it('disabled state', () => { |
| 58 | + expect(debugElement.query(By.directive(KbqInput))).toBe(null); |
| 59 | + expect(debugElement.query(By.directive(KbqButton)).nativeElement.hasAttribute('disabled')).toBe(false); |
| 60 | + |
| 61 | + fixture.componentInstance.disabled = true; |
| 62 | + fixture.detectChanges(); |
| 63 | + expect(debugElement.query(By.directive(KbqButton)).nativeElement.hasAttribute('disabled')).toBe(true); |
| 64 | + |
| 65 | + fixture.componentInstance.openedState = true; |
| 66 | + fixture.detectChanges(); |
| 67 | + |
| 68 | + expect(debugElement.query(By.directive(KbqButton))).toBe(null); |
| 69 | + expect(debugElement.query(By.directive(KbqInput)).nativeElement.hasAttribute('disabled')).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('with formControl', () => { |
| 73 | + let fixture: ComponentFixture<TestSearchExpandableWithFormControl>; |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + fixture = TestBed.createComponent(TestSearchExpandableWithFormControl); |
| 77 | + debugElement = fixture.debugElement.query(By.directive(KbqSearchExpandable)); |
| 78 | + nativeElement = debugElement.nativeElement; |
| 79 | + fixture.detectChanges(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('disabled state', () => { |
| 83 | + expect(debugElement.query(By.directive(KbqInput))).toBe(null); |
| 84 | + expect(debugElement.componentInstance.disabled).toBe(false); |
| 85 | + |
| 86 | + fixture.componentInstance.searchControl.disable(); |
| 87 | + fixture.componentInstance.openedState = true; |
| 88 | + fixture.detectChanges(); |
| 89 | + |
| 90 | + expect(debugElement.componentInstance.disabled).toBe(true); |
| 91 | + expect(debugElement.query(By.directive(KbqButton))).toBe(null); |
| 92 | + expect(debugElement.query(By.directive(KbqInput)).nativeElement.hasAttribute('disabled')).toBe(true); |
| 93 | + }); |
| 94 | + }); |
54 | 95 | }); |
55 | 96 |
|
56 | 97 | @Component({ |
57 | 98 | standalone: true, |
58 | 99 | selector: 'test-app', |
59 | 100 | imports: [KbqSearchExpandableModule, FormsModule], |
60 | 101 | template: ` |
61 | | - <kbq-search-expandable [isOpened]="openedState" [(ngModel)]="search" /> |
| 102 | + <kbq-search-expandable [isOpened]="openedState" [disabled]="disabled" [(ngModel)]="search" /> |
62 | 103 | ` |
63 | 104 | }) |
64 | | -class TestApp { |
| 105 | +class TestSearchExpandable { |
65 | 106 | openedState: boolean = false; |
| 107 | + disabled: boolean = false; |
66 | 108 | search: string; |
67 | 109 | } |
| 110 | + |
| 111 | +@Component({ |
| 112 | + standalone: true, |
| 113 | + selector: 'test-app-search-expandable-with-form-control', |
| 114 | + imports: [KbqSearchExpandableModule, ReactiveFormsModule], |
| 115 | + template: ` |
| 116 | + <kbq-search-expandable [isOpened]="openedState" [formControl]="searchControl" /> |
| 117 | + ` |
| 118 | +}) |
| 119 | +class TestSearchExpandableWithFormControl { |
| 120 | + searchControl = new FormControl<string>(''); |
| 121 | + |
| 122 | + openedState: boolean = false; |
| 123 | +} |
0 commit comments