ngxs-labs / async-storage-plugin

⏱ WIP: Async Storage Plugin
MIT License
34 stars 12 forks source link

Plugin saves state A to storage when state B (should not be stored) was changed. #154

Open lomchik opened 4 years ago

lomchik commented 4 years ago

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');
  }
}