--- /dev/null
+import { createAction } from '@ngrx/store';
+
+export const increment = createAction('[Counter Component] Increment');
+export const decrement = createAction('[Counter Component] Decrement');
+export const reset = createAction('[Counter Component] Reset');
MetaReducer
} from '@ngrx/store';
import { environment } from '../../environments/environment';
+import * as fromVorgang from './vorgang';
export interface State {
}
export const reducers: ActionReducerMap<State> = {
-
+ vorgang: fromVorgang.reducer
};
--- /dev/null
+import { createReducer, on } from '@ngrx/store';
+import { increment, decrement, reset } from '../actions/vorgang';
+
+export const initialState = 0;
+
+const REDUCER = createReducer(initialState,
+ on(increment, state => state + 1),
+ on(decrement, state => state - 1),
+ on(reset, state => 0),
+);
+
+export function reducer(state, action) {
+ return REDUCER(state, action);
+}
-<p>{{vorgang}}</p>
+<button id="increment" (click)="increment()">Increment</button>
+
+<div>Vorgang: <span id="vorgang">{{ vorgang$ | async }}</span></div>
+
+<button id="decrement" (click)="decrement()">Decrement</button>
+
+<button id="reset" (click)="reset()">Reset Counter</button>
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+import { provideMockStore, MockStore } from '@ngrx/store/testing';
import { VorgangComponent } from './vorgang.component';
describe('VorgangComponent', () => {
let component: VorgangComponent;
let fixture: ComponentFixture<VorgangComponent>;
+ let mockStore: MockStore;
+
+ const initialState = { vorgang: 0 };
beforeEach(async(() => {
TestBed.configureTestingModule({
- declarations: [ VorgangComponent ]
+ declarations: [ VorgangComponent ],
+ providers: [ provideMockStore({ initialState }) ]
})
.compileComponents();
+
+ mockStore = TestBed.inject(MockStore);
}));
beforeEach(() => {
const fixture = TestBed.createComponent(VorgangComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
- expect(compiled.querySelector('p').textContent).toContain('Hello world!');
+ expect(compiled.querySelector('#vorgang').textContent).toContain('0');
});
});
import { Component, OnInit } from '@angular/core';
+import { Store, select } from '@ngrx/store';
+import { Observable } from 'rxjs';
+import { increment, decrement, reset } from '../actions/vorgang';
@Component({
selector: 'app-vorgang',
})
export class VorgangComponent implements OnInit {
- vorgang = "Hello world!";
+ vorgang$: Observable<number>;
- constructor() { }
+ constructor(private store: Store<{ vorgang: number }>) {
+ this.vorgang$ = store.pipe(select('vorgang'));
+ }
ngOnInit(): void {
}
+ increment() {
+ this.store.dispatch(increment());
+ }
+
+ decrement() {
+ this.store.dispatch(decrement());
+ }
+
+ reset() {
+ this.store.dispatch(reset());
+ }
}