-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests): for settings component #244
- Loading branch information
shootermv
committed
Jun 24, 2018
1 parent
e253554
commit e5d65ed
Showing
2 changed files
with
98 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,37 +1,100 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
// angular | ||
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
// 3D | ||
import { Store } from '@ngrx/store'; | ||
import { MatSlideToggle } from '@angular/material'; | ||
|
||
// angular testing stuff | ||
import { | ||
async, | ||
ComponentFixture, | ||
TestBed, | ||
inject | ||
} from '@angular/core/testing'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
|
||
import { CoreModule } from '@app/core'; | ||
import { SharedModule } from '@app/shared'; | ||
|
||
// relativr stuff | ||
import { SettingsComponent } from './settings.component'; | ||
import { | ||
SettingsState, | ||
ActionSettingsChangeTheme, | ||
ActionSettingsChangeAutoNightMode | ||
} from '../settings.reducer'; | ||
|
||
import { TestStore } from '@testing/utils'; | ||
describe('SettingsComponent', () => { | ||
let component: SettingsComponent; | ||
let fixture: ComponentFixture<SettingsComponent>; | ||
let store: TestStore<SettingsState>; | ||
let dispatchSpy; | ||
|
||
const getSelectArrow = () => | ||
fixture.debugElement.query(By.css('.mat-select-trigger')); | ||
const getOptions = () => fixture.debugElement.queryAll(By.css('mat-option')); | ||
|
||
beforeEach( | ||
async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
NoopAnimationsModule, | ||
RouterTestingModule, | ||
CoreModule, | ||
SharedModule | ||
], | ||
declarations: [SettingsComponent] | ||
imports: [NoopAnimationsModule, RouterTestingModule, SharedModule], | ||
declarations: [SettingsComponent], | ||
providers: [{ provide: Store, useClass: TestStore }] | ||
}).compileComponents(); | ||
}) | ||
); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(SettingsComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
beforeEach( | ||
inject([Store], (testStore: TestStore<SettingsState>) => { | ||
store = testStore; | ||
store.setState({ | ||
theme: 'DEFAULT-THEME', | ||
autoNightMode: true, | ||
pageAnimations: true, | ||
pageAnimationsDisabled: true, | ||
elementsAnimations: true | ||
}); | ||
fixture = TestBed.createComponent(SettingsComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}) | ||
); | ||
|
||
it('should be created', () => { | ||
expect(component).toBeTruthy(); | ||
expect(component.settings.theme).toBe('DEFAULT-THEME'); | ||
expect(component.settings.autoNightMode).toBeTruthy(); | ||
}); | ||
|
||
it('should dispatch change theme action on theme selection', () => { | ||
dispatchSpy = spyOn(store, 'dispatch'); | ||
getSelectArrow().triggerEventHandler('click', {}); | ||
|
||
fixture.detectChanges(); | ||
|
||
getOptions()[1].triggerEventHandler('click', {}); | ||
|
||
fixture.detectChanges(); | ||
|
||
expect(dispatchSpy).toHaveBeenCalledTimes(2); | ||
expect(dispatchSpy).toHaveBeenCalledWith( | ||
new ActionSettingsChangeTheme({ theme: 'LIGHT-THEME' }) | ||
); | ||
}); | ||
|
||
it('should dispatch change auto night mode on night mode toggle', () => { | ||
dispatchSpy = spyOn(store, 'dispatch'); | ||
const componentDebug = fixture.debugElement; | ||
const slider = componentDebug.queryAll(By.directive(MatSlideToggle))[0]; | ||
|
||
slider.triggerEventHandler('change', { checked: false }); | ||
fixture.detectChanges(); | ||
|
||
expect(dispatchSpy).toHaveBeenCalledTimes(2); | ||
expect(dispatchSpy).toHaveBeenCalledWith( | ||
new ActionSettingsChangeAutoNightMode({ autoNightMode: false }) | ||
); | ||
}); | ||
}); |