Last active
May 17, 2020 19:26
-
-
Save splincode/ef2244b67a6211fcd23942f954ba7770 to your computer and use it in GitHub Desktop.
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
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