Legitcode / tests

Chainable, easy to read, React testing library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add wrapper for testing Alt stores

dphaener opened this issue · comments

@zackify Per our conversation:

store:

import alt from '../alt';
import BaseStore from './base_store';
import immutable from 'alt/utils/ImmutableUtil';
import Immutable from 'immutable';
import actions from '../actions/edit_subscription_actions';

@immutable
export class EditSubscriptionStore extends BaseStore {
  constructor() {
    super();
    this.bindActions(actions);
  }

  updateAlsoEnergy(response) {

  }

  revokeMembership(response) {
    let index = this.state.get('members').findIndex((x) => {
      return Immutable.Map(x).getIn(['data', 'id']) === response.data.id;
    });

    this.setState(this.state.updateIn(['members'], (x) => {
      return x.set(index, response);
    }));
  }

  activateMembership(response) {
    let index = this.state.get('members').findIndex((x) => {
      return Immutable.Map(x).getIn(['data', 'id']) === response.data.id;
    });


    this.setState(this.state.updateIn(['members'], (x) => {
      return x.set(index, response);
    }));
  }

  setSavingState(state) {
    this.setState({ savingStatus: state });
  }
}

export default alt.createStore(EditSubscriptionStore, 'EditSubscriptionStore');

dphaener [11:32 PM]
test:

import alt from '../../../app/assets/javascripts/alt';
import { expect } from 'chai';
import AltTestingUtils from 'alt/utils/AltTestingUtils';

import { EditSubscriptionStore } from '../../../app/assets/javascripts/stores/edit_subscription_store';

describe('EditSubscriptionStore', () => {
  describe('#revokeMembership', () => {
    let members = [
      {
        data: {
          id: "foo",
          attributes: {
            status: "active"
          }
        }
      },
      {
        data: {
          id: "bar",
          attributes: {
            status: "active"
          }
        }
      }
    ];

    it('should update the members list with the updated member', () => {
      let response = {
        data: {
          id: "foo",
          attributes: {
            status: "revoked"
          }
        }
      };

      let unwrappedStore = AltTestingUtils.makeStoreTestable(alt, EditSubscriptionStore);

      unwrappedStore.setInitialState({ members: members });
      setTimeout(() => {
        unwrappedStore.revokeMembership(response);

        let members = unwrappedStore.state.get('members'),
            updatedMember = members[0];

        expect(updateMember.attributes.status).to.equal("revoked");
      }, 5000);

    });
  });
});

dphaener [11:33 PM]
So, Alt does have some test utils. Which is cool. But, things are still happening asynchronously, so setting initial state needs to happen, and then everything else has to happen in a promise, instead of the super hacky set timeout

dphaener [11:34 PM]
That test passes, but it’s dumb