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

How to mock store in components #14

Open
blopez2010 opened this issue Feb 12, 2018 · 5 comments
Open

How to mock store in components #14

blopez2010 opened this issue Feb 12, 2018 · 5 comments

Comments

@blopez2010
Copy link

I've being looking around how to mock components in order to be tested, do you have an idea? https://github.com/rintoj/statex/blob/master/examples/todo-ng-ts/src/app/list/list.component.ts

Since you are doing a new instance of the store, it isn't quite easy to test it.

Thanks!

@blopez2010
Copy link
Author

This is how I did it but I'm really not confortable with, if you have a better way please let us know :).

Love Statex man!!

Implementation:

private loadRooms(rooms: Room[], roomsNumber?: number) {
    if (!isUndefined(roomsNumber)) {
      if (roomsNumber > rooms.length) {
        this.dispatchedAction = new AddRoomsAction(roomsNumber);
      } else if (roomsNumber < rooms.length) {
        this.dispatchedAction = new RemoveRoomsAction(roomsNumber);
      }

      this.dispatchedAction.dispatch();
    }
  }

Unit test:

    it('should remove rooms', () => {
      const numberOfRoomsControl = {
        value: 0
      };

      component.rooms = [
        {
          id: '',
          name: '',
          ordinal: 3,
          isNew: true
        }
      ];

      component.onNumberOfRoomsChange(numberOfRoomsControl);
      expect(
        component.dispatchedAction instanceof RemoveRoomsAction
      ).toBeTruthy();
    });

@rintoj
Copy link
Owner

rintoj commented Feb 12, 2018

@blopez2010, give me couple of hours. I will upload a test case for list component

@rintoj
Copy link
Owner

rintoj commented Feb 12, 2018

@blopez2010, StateX is configured for detached store and view. Therefore when you unit test a view you must only test for appropriate actions being dispatched. What happens when an action is trigged must be tested under store's test case, not here.

This example should help you understand how to test that.

https://github.com/rintoj/statex/blob/master/examples/todo-ng-ts/src/app/list/list.component.spec.ts

@blopez2010
Copy link
Author

Thanks @rintoj for the example!

@frulo
Copy link

frulo commented Aug 30, 2018

I have some problems with the example above. What I forgot is to reset the state before each test:
initialize( {} )

an other possibility is the jasmin spy returns a correct state. example:
let toggleTodoActionCall = jasmine.createSpy('spy').and.returnValue(new ReplaceableState({}));

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

3 participants