-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
217 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/framework/theme/components/search/search.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import createSpy = jasmine.createSpy; | ||
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { | ||
NbLayoutModule, | ||
NbSearchComponent, | ||
NbSearchFieldComponent, | ||
NbSearchModule, | ||
NbSearchService, | ||
NbThemeModule, | ||
NbLayoutComponent, | ||
} from '@nebular/theme'; | ||
|
||
describe('NbSearchFieldComponent', () => { | ||
let fixture: ComponentFixture<NbSearchFieldComponent>; | ||
let searchFieldComponent: NbSearchFieldComponent; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
RouterTestingModule.withRoutes([]), | ||
NbThemeModule.forRoot(), | ||
NbSearchModule, | ||
], | ||
}); | ||
|
||
fixture = TestBed.createComponent(NbSearchFieldComponent); | ||
searchFieldComponent = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should emit input event when typing in input', fakeAsync(() => { | ||
const inputSpy = createSpy('inputSpy'); | ||
searchFieldComponent.searchInput.subscribe(inputSpy); | ||
|
||
const input: HTMLInputElement = fixture.componentInstance.inputElement.nativeElement; | ||
input.dispatchEvent(new Event('input')); | ||
tick(); | ||
|
||
expect(inputSpy).toHaveBeenCalledTimes(1); | ||
})); | ||
|
||
it('should emit input event with input value', fakeAsync(() => { | ||
const inputValue = 'input text'; | ||
const inputSpy = createSpy('inputSpy'); | ||
searchFieldComponent.searchInput.subscribe(inputSpy); | ||
|
||
const input: HTMLInputElement = fixture.componentInstance.inputElement.nativeElement; | ||
input.value = inputValue; | ||
input.dispatchEvent(new Event('input')); | ||
tick(); | ||
|
||
expect(inputSpy).toHaveBeenCalledWith(inputValue); | ||
})); | ||
}); | ||
|
||
describe('NbSearchComponent', () => { | ||
let fixture: ComponentFixture<NbSearchComponent>; | ||
let searchComponent: NbSearchComponent; | ||
|
||
function getSearchFieldComponent(): NbSearchFieldComponent { | ||
return fixture.debugElement.query(By.directive(NbSearchFieldComponent)).componentInstance; | ||
} | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
RouterTestingModule.withRoutes([]), | ||
NbThemeModule.forRoot(), | ||
NbLayoutModule, | ||
NbSearchModule, | ||
], | ||
}); | ||
|
||
TestBed.createComponent(NbLayoutComponent); | ||
|
||
fixture = TestBed.createComponent(NbSearchComponent); | ||
searchComponent = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should listen to search field input event', fakeAsync(() => { | ||
searchComponent.openSearch(); | ||
fixture.detectChanges(); | ||
tick(); | ||
|
||
const inputSpy = spyOn(searchComponent, 'emitInput'); | ||
const searchFieldComponent: NbSearchFieldComponent = getSearchFieldComponent(); | ||
|
||
searchFieldComponent.searchInput.emit(); | ||
tick(); | ||
expect(inputSpy).toHaveBeenCalledTimes(1); | ||
})); | ||
|
||
it(`should propagate search event to search service`, fakeAsync(() => { | ||
searchComponent.openSearch(); | ||
fixture.detectChanges(); | ||
tick(); | ||
const searchFieldComponent: NbSearchFieldComponent = getSearchFieldComponent(); | ||
|
||
const searchService = TestBed.get(NbSearchService); | ||
const searchInput = spyOn(searchService, 'searchInput').and.callThrough(); | ||
const searchTerm = 'search term'; | ||
|
||
searchFieldComponent.searchInput.emit(searchTerm); | ||
tick(); | ||
|
||
expect(searchInput).toHaveBeenCalledTimes(1); | ||
expect(searchInput).toHaveBeenCalledWith(searchTerm, undefined); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/framework/theme/components/search/search.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import createSpy = jasmine.createSpy; | ||
import objectContaining = jasmine.objectContaining; | ||
import { NbSearchService } from '@nebular/theme'; | ||
|
||
describe('NbSearchService', () => { | ||
it('should emit onSearchInput when searchInput called', () => { | ||
const term = 'term'; | ||
const tag = 'tag'; | ||
const searchService = new NbSearchService(); | ||
const onSearchInputSpy = createSpy('onSearchInput'); | ||
searchService.onSearchInput().subscribe(onSearchInputSpy); | ||
|
||
searchService.searchInput(term, tag); | ||
|
||
expect(onSearchInputSpy).toHaveBeenCalledTimes(1); | ||
expect(onSearchInputSpy.calls.first().args).toEqual(objectContaining([{ term, tag }])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/playground/without-layout/search/search-with-input-event.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import {NbSearchService} from '@nebular/theme'; | ||
import {debounceTime, distinctUntilChanged} from 'rxjs/operators'; | ||
|
||
@Component({ | ||
selector: 'nb-search-with-input-event', | ||
template: ` | ||
<nb-layout> | ||
<nb-layout-header fixed> | ||
<nb-search type="rotate-layout" tag="search-input-event"></nb-search> | ||
</nb-layout-header> | ||
<nb-sidebar> | ||
</nb-sidebar> | ||
<nb-layout-column> | ||
<nb-card accent="info"> | ||
<nb-card-header>You searched for:</nb-card-header> | ||
<nb-card-body> | ||
<h3>{{ value || '-' }}</h3> | ||
</nb-card-body> | ||
</nb-card> | ||
</nb-layout-column> | ||
</nb-layout> | ||
`, | ||
}) | ||
export class SearchWithInputEventComponent implements OnInit { | ||
value: string; | ||
constructor(private searchService: NbSearchService) { | ||
} | ||
|
||
ngOnInit() { | ||
this.searchService.onSearchInput().pipe( | ||
debounceTime(300), | ||
distinctUntilChanged(), | ||
).subscribe((data: { term: string, tag: string }) => { | ||
this.value = data.term; | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters