Skip to content

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

@lomchik

Description

@lomchik

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions