Skip to content

Instantly share code, notes, and snippets.

@splincode
Last active May 17, 2020 19:26
Show Gist options
  • Save splincode/ef2244b67a6211fcd23942f954ba7770 to your computer and use it in GitHub Desktop.
Save splincode/ef2244b67a6211fcd23942f954ba7770 to your computer and use it in GitHub Desktop.
import { TestBed } from '@angular/core/testing';
export interface ZooStateModel {
feedAnimals: string[];
}
export class FeedAnimals {
public static type: string = '[FeedAnimals]: action';
constructor(public animalsToFeed: string[]) {}
}
@State<ZooStateModel>({
name: 'zoo',
defaults: {
feedAnimals: []
}
})
@Injectable()
export class ZooState {
@Action(FeedAnimals)
feedAnimals(ctx: StateContext<ZooStateModel>, action: FeedAnimals) {
const state = ctx.getState();
ctx.setState({
...state,
feedAnimals: [...state.feedAnimals, action.animalsToFeed]
});
}
}
describe('Zoo', () => {
let store: Store;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgxsModule.forRoot([ZooState])]
});
store = TestBed.inject(Store);
});
it('it toggles feed', () => {
store.dispatch(new FeedAnimals([ 'leaves', 'bark' ]));
const feed = store.selectSnapshot(state => state.zoo.feedAnimals);
expect(feed).toBe([ 'leaves', 'bark' ]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment