|
1 | | -import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
2 | 2 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
3 | 3 | import { RouterTestingModule } from '@angular/router/testing'; |
4 | 4 |
|
5 | 5 | import { ToolbarComponent } from './toolbar.component'; |
6 | 6 | import { MaterialModule } from '../../../material/material.module'; |
| 7 | +import { LanguageService } from '../../services/language.service'; |
| 8 | +import { TranslateModule } from '@ngx-translate/core'; |
| 9 | +import { languagesList } from '../../data/language.data'; |
7 | 10 |
|
8 | 11 | describe('ToolbarComponent', () => { |
| 12 | + const languages = languagesList; |
9 | 13 | let component: ToolbarComponent; |
10 | 14 | let fixture: ComponentFixture<ToolbarComponent>; |
11 | | - |
12 | | - beforeEach( |
13 | | - async(() => { |
14 | | - TestBed.configureTestingModule({ |
15 | | - imports: [MaterialModule, BrowserAnimationsModule, RouterTestingModule], |
16 | | - declarations: [ToolbarComponent] |
17 | | - }).compileComponents(); |
18 | | - }) |
19 | | - ); |
| 15 | + let languageService: LanguageService; |
20 | 16 |
|
21 | 17 | beforeEach(() => { |
| 18 | + TestBed.configureTestingModule({ |
| 19 | + imports: [ |
| 20 | + MaterialModule, |
| 21 | + BrowserAnimationsModule, |
| 22 | + RouterTestingModule, |
| 23 | + TranslateModule.forRoot() |
| 24 | + ], |
| 25 | + declarations: [ToolbarComponent], |
| 26 | + providers: [LanguageService] |
| 27 | + }); |
| 28 | + |
| 29 | + languageService = TestBed.get(LanguageService); |
22 | 30 | fixture = TestBed.createComponent(ToolbarComponent); |
23 | 31 | component = fixture.componentInstance; |
| 32 | + }); |
| 33 | + |
| 34 | + it('should set languagesList and currentLang on initialization', () => { |
| 35 | + spyOn(languageService, 'getLanguagesList').and.returnValue(languages); |
| 36 | + spyOn(languageService, 'getCurrentLang').and.returnValue(languages[1]); |
| 37 | + |
24 | 38 | fixture.detectChanges(); |
| 39 | + |
| 40 | + expect(component.languagesList).toEqual(languages); |
| 41 | + expect(component.currentLang).toEqual(languages[1]); |
25 | 42 | }); |
26 | 43 |
|
27 | | - it('should create', () => { |
28 | | - expect(component).toBeTruthy(); |
| 44 | + it('should change current language on onLangSwitch', () => { |
| 45 | + spyOn(languageService, 'saveLang').and.stub(); |
| 46 | + |
| 47 | + component.onLangSwitch(languages[1]); |
| 48 | + |
| 49 | + expect(component.currentLang).toEqual(languages[1]); |
| 50 | + expect(languageService.saveLang).toHaveBeenCalledWith(languages[1]); |
29 | 51 | }); |
30 | 52 | }); |
0 commit comments