|
| 1 | +import {Injectable} from '@angular/core'; |
| 2 | +import {TestBed, inject, async, ComponentFixture} from '@angular/core/testing'; |
| 3 | +import {Router, RouterModule} from '@angular/router'; |
| 4 | +import {MaterialModule} from '@angular/material'; |
| 5 | +import {ReactiveFormsModule, FormControl} from '@angular/forms'; |
| 6 | + |
| 7 | +import {DocumentationItems, DocItem} from '../../documentation-items/documentation-items'; |
| 8 | +import {SearchBar} './searchbar'; |
| 9 | + |
| 10 | +const mockRouter = { |
| 11 | + navigate: jasmine.createSpy('navigate'), |
| 12 | + navigateByUrl: jasmine.createSpy('navigateByUrl'); |
| 13 | +}; |
| 14 | + |
| 15 | +const testDocItem = { |
| 16 | + id: 'test-doc-item', |
| 17 | + name: 'TestingExample', |
| 18 | + examples: ['test-examples'] |
| 19 | +}; |
| 20 | + |
| 21 | + |
| 22 | +describe('SearchBar', () => { |
| 23 | + let fixture: ComponentFixture<SearchBar>; |
| 24 | + let component: SearchBar; |
| 25 | + |
| 26 | + beforeEach(async(() => { |
| 27 | + TestBed.configureTestingModule({ |
| 28 | + imports: [RouterModule, ReactiveFormsModule, MaterialModule], |
| 29 | + declarations: [SearchBar], |
| 30 | + providers: [ |
| 31 | + {provide: DocumentationItems, useClass: MockDocumentationItems}, |
| 32 | + {provide: Router, useValue: mockRouter}, |
| 33 | + ], |
| 34 | + }); |
| 35 | + |
| 36 | + TestBed.compileComponents(); |
| 37 | + fixture = TestBed.createComponent(SearchBar); |
| 38 | + component = fixture.componentInstance; |
| 39 | + component.searchControl = new FormControl(''); |
| 40 | + fixture.detectChanges(); |
| 41 | + })); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + component._router.navigateByUrl.calls.reset(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should toggle isExpanded', () => { |
| 48 | + expect(component._isExpanded).toBe(false); |
| 49 | + component.toggleIsExpanded(); |
| 50 | + expect(component._isExpanded).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('Filter Search Suggestions', () => { |
| 54 | + it('should return all items matching search query', () => { |
| 55 | + const query = 'testing'; |
| 56 | + const result = component.filterSearchSuggestions(query); |
| 57 | + expect(result).toEqual([testDocItem]); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return empty list if no items match', () => { |
| 61 | + const query = 'does not exist'; |
| 62 | + const result = component.filterSearchSuggestions(query); |
| 63 | + expect(result).toEqual([]); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('Navigate', () => { |
| 68 | + |
| 69 | + it('should take an id and navigate to the given route', () => { |
| 70 | + component.navigate('button-toggle'); |
| 71 | + expect(component._router.navigateByUrl).toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should not navigate if no id is given', () => { |
| 75 | + component.navigate(''); |
| 76 | + expect(component._router.navigateByUrl).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should show a snackbar error', () => { |
| 81 | + spyOn(component._snackBar, 'open'); |
| 82 | + component._showError(); |
| 83 | + expect(component._snackBar.open).toHaveBeenCalled(); |
| 84 | + expect(component._snackBar.open).toHaveBeenCalledWith( |
| 85 | + 'No search results found.', |
| 86 | + null, {duration: 3000}); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return the proper display value for form control', () => { |
| 90 | + const result = component.displayFn(testDocItem); |
| 91 | + expect(result).toEqual(testDocItem.name); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | + |
| 96 | +class MockDocumentationItems extends DocumentationItems { |
| 97 | + getAllItems(): DocItem[] { return [testDocItem]; } |
| 98 | +} |
| 99 | + |
0 commit comments