Created
October 4, 2020 09:56
-
-
Save mmitou/b1c1060228337534bdeff89a1c802e67 to your computer and use it in GitHub Desktop.
ngrx test example
This file contains hidden or 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
| import { reducer, initialState } from './counter-control.reducer'; | |
| import * as CounterControlActions from './counter-control.actions'; | |
| describe('CounterControl Reducer', () => { | |
| describe('an unknown action', () => { | |
| it('should return the previous state', () => { | |
| const action = {} as any; | |
| const result = reducer(initialState, action); | |
| expect(result).toEqual(initialState); | |
| }); | |
| it('should return the current state', () => { | |
| const result = reducer( | |
| initialState, | |
| CounterControlActions.loadCounterControls() | |
| ); | |
| expect(result).toEqual(initialState); | |
| }); | |
| it('should return the incremented state', () => { | |
| const result = reducer( | |
| initialState, | |
| CounterControlActions.incrementCounter() | |
| ); | |
| expect(result.counter).toBe(initialState.counter + 1); | |
| }); | |
| }); | |
| }); |
This file contains hidden or 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
| import * as fromCounterControl from './counter-control.reducer'; | |
| import { | |
| selectCounterControlState, | |
| selectCounter | |
| } from './counter-control.selectors'; | |
| describe('CounterControl Selectors', () => { | |
| it('should select the feature state', () => { | |
| const result = selectCounterControlState({ | |
| [fromCounterControl.counterControlFeatureKey]: | |
| fromCounterControl.initialState | |
| }); | |
| expect(result).toEqual(fromCounterControl.initialState); | |
| }); | |
| it('should select the counter', () => { | |
| const n = selectCounter.projector({ counter: 2 }); | |
| expect(n).toEqual(2); | |
| }); | |
| }); |
This file contains hidden or 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
| import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
| import { MemoizedSelector } from '@ngrx/store'; | |
| import { provideMockStore, MockStore } from '@ngrx/store/testing'; | |
| import { By } from '@angular/platform-browser'; | |
| import { CounterViewerComponent } from './counter-viewer.component'; | |
| import { selectCounter } from '../store/counter-control.selectors'; | |
| import { State } from '../store/counter-control.reducer'; | |
| import { counterControlFeatureKey } from '../store/counter-control.reducer'; | |
| describe('CounterViewerComponent', () => { | |
| let component: CounterViewerComponent; | |
| let fixture: ComponentFixture<CounterViewerComponent>; | |
| let mockStore: MockStore; | |
| let mockSelectCounter: MemoizedSelector<State, number>; | |
| beforeEach(async () => { | |
| await TestBed.configureTestingModule({ | |
| declarations: [CounterViewerComponent], | |
| providers: [ | |
| provideMockStore({ | |
| initialState: { [counterControlFeatureKey]: { counter: 0 } } | |
| }) | |
| ] | |
| }).compileComponents(); | |
| }); | |
| beforeEach(() => { | |
| fixture = TestBed.createComponent(CounterViewerComponent); | |
| component = fixture.componentInstance; | |
| fixture.detectChanges(); | |
| mockStore = TestBed.inject(MockStore); | |
| mockSelectCounter = mockStore.overrideSelector(selectCounter, 0); | |
| }); | |
| it('should create', () => { | |
| expect(component).toBeTruthy(); | |
| }); | |
| it('should return hello', () => { | |
| expect(component.message).toBe('hello'); | |
| }); | |
| it('should have hello: 1', () => { | |
| mockSelectCounter.setResult(1); | |
| mockStore.refreshState(); | |
| fixture.detectChanges(); | |
| const elm = fixture.debugElement.query(By.css('p')); | |
| expect(elm.nativeElement.innerText).toBe('hello: 1'); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment