-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Description
I have added unit test on this
Test failed with error Error: Expected spy setItem not to have been called
:
import { TestBed } from '@angular/core/testing';
import { NgxsAsyncStoragePluginModule } from '@ngxs-labs/async-storage-plugin';
import { Action, NgxsModule, State, StateContext, Store } from '@ngxs/store';
describe('save to storage on any state change', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NgxsModule.forRoot([
StateToStore, InMemoryState
]),
NgxsAsyncStoragePluginModule.forRoot(KeyValueStorageMock, {
key: [StateToStore.name]
})
],
providers: [KeyValueStorageMock]
});
});
it(`does not save ${StateToStore.name} when ${UpdateInMemoryState.name} dispatched`, async () => {
const store = TestBed.get(Store) as Store;
await store.dispatch(new UpdateInMemoryState).toPromise();
expect(setItemSpy).not.toHaveBeenCalled();
});
});
const setItemSpy = jasmine.createSpy('setItem');
class KeyValueStorageMock {
getItem() {}
setItem() {
return setItemSpy();
}
}
@State<string>({
defaults: 'default',
name: StateToStore.name
})
class StateToStore {
constructor() {}
}
class UpdateInMemoryState {
static type = 'UpdateInMemoryStore';
constructor() {}
}
@State<string>({
defaults: '',
name: InMemoryState.name
})
class InMemoryState {
constructor() {}
@Action(UpdateInMemoryState)
update(ctx: StateContext<string>) {
ctx.setState('some value');
}
}
Metadata
Metadata
Assignees
Labels
No labels