Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lomchik opened this issue May 8, 2020 · 0 comments

Comments

@lomchik
Copy link

lomchik commented May 8, 2020

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');
  }
}
@lomchik lomchik changed the title Plugin saves state A to storage even state B (should not be stored) was changed. Plugin saves state A to storage when state B (should not be stored) was changed. May 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant