Skip to content

Releases: ctrlplusb/easy-peasy

v2.3.1

03 Jun 16:51
Compare
Choose a tag to compare

Patches

  • Fixes support for index signatures on model state in Typescript: fa05637

Thanks @jmyrland for all the help with this!

v2.3.0

03 Jun 16:53
Compare
Choose a tag to compare

Minor Changes

  • Adds support to provide custom name to store for redux dev tools: 03b6fbb

v2.2.1

20 May 07:53
Compare
Choose a tag to compare

Patches

  • Upgrades deps: 1e8b51c
  • Fixes useStore hook when mapping derived state with funcs: e61753f

v2.2.0

13 May 13:51
Compare
Choose a tag to compare

Minor Changes

  • Adds new APIs allow manual triggering of listeners: #133

Patches


store.triggerListener(listener, action, payload)

Allows you to trigger a listen registration of your model with the provided action.

Arguments

  • listener (Listen, required)

    The listener instance to trigger.

  • action (Action | Thunk | string, required)

    The action to fire against the listener. Any registered handlers within the listener registration for the given action type will be fired.

  • payload (any)

    The payload to provide for the action.

Returns

A promise that resolves when all listener handlers have completed.

Examples

Given the following store definition:

const model = {
  session: {
    user: undefined,
    registerSession: action((state, payload) => {
      state.user = payload;
    }),
    logIn: thunk(async (actions, payload) => {
      const user = await loginService(payload);
      actions.registerSession(user);
    }),
  },
  audit: {
    logs: [],
	sessionHistory: [],
	logSessionHistory: action((state, payload) => {
      state.sessionHistory.push(payload);
	}),
    listeners: on => {
      on(model.session.registerSession, thunk((actions, payload) => {
        actions.logSessionHistory(payload.username);
      });
      on(model.session.logIn, action((state, payload) => {
        state.logs.push(`Authenticated ${payload.username} against server`);        
      });
    }
  }
}

const store = createStore(model);

We can trigger the listeners defined on our "audit" model like so:

store.triggerListener(
  models.audit.listeners, // The listeners to trigger
  model.session.logIn, // The action to trigger them with
  { username: 'bob' } // The payload for the action
);

// assert the changes
expect(store.getState().audit.logs).toEqual(['Authenticated bob against server']);

If the handler defined within your listeners is a thunk you should resolve the promise that is returned by the triggerListener call before asserting any changes:

store.triggerListener(
  models.audit.listeners, // The listeners to trigger
  model.session.registerSession, // The action to trigger them with
  { username: 'bob' } // The payload for the action
).then(() => {
    // assert the changes
    expect(store.getState().audit.sessionHistory).toEqual(['bob']);
});

You can also use the mockActions configuration to assert the thunk handler in isolation:

import { actionName } from 'easy-peasy';

const store = createStore(model, { mockActions: true });

store.triggerListener(
  models.audit.listeners, // The listeners to trigger
  model.session.registerSession, // The action to trigger them with
  { username: 'bob' } // The payload for the action
).then(() => {
    // assert the changes
    expect(store.getMockedActions()).toEqual([
        { type: actionName(models.audit.logSessionHistory), payload: 'bob' }
    ]);
});

store.triggerListeners(action, payload)

Allows you to trigger all registered listeners across the store that are listening to the provided action.

Arguments

  • action (Action | Thunk | string, required)

    The action to fire that will trigger listeners that are targeting it.

  • payload (any)

    The payload to provide.

Returns

A promise that resolves when all fired listeners have completed.

v2.1.6

20 Apr 22:35
Compare
Choose a tag to compare

Patches

  • Updates type definitions to support TS 3.4: 8564b52

Closes #143 #149 #151

v2.1.5

31 Mar 11:43
Compare
Choose a tag to compare

Patches

  • Fixes nested empty models not being represented in state: 8102944

v2.1.4

31 Mar 11:31
Compare
Choose a tag to compare

Patches

  • Fixes initial state loading when initial state doesn't match model (7518557)

v2.1.3

28 Mar 17:56
Compare
Choose a tag to compare

Patches

  • Splits out tests into multiple test suite files: d39e447
  • Optimises selectors so that they will only get run when their branch of state changes: 8f0c850

v2.1.2

27 Mar 15:54
Compare
Choose a tag to compare

Patches

  • Fix thunk getStoreState docs: 67f656b
  • Updates use store hook to have a window of opportunity to deal with stale state / props: #138
  • Fixes action listeners timing and undefined action logging: #140
  • Bumps version: 38022dc

v2.1.1

21 Mar 13:32
Compare
Choose a tag to compare

Patches

  • Update README according to easy-peasy v2 API: 8aa68e9
  • Attach mockActions middleware as the last one.: 54a07c1

Thanks @ivosh for the help! ❤️