Skip to content

Commit

Permalink
fix: set default error on empty helpers.fail() call. (#860)
Browse files Browse the repository at this point in the history
  • Loading branch information
scascar authored Sep 18, 2023
1 parent 827f1df commit 56a5f82
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function createThunkActionsCreator(def, _r) {
let failure = null;

const fail = (_failure) => {
failure = _failure;
failure = _failure || new Error();
};

const result = _r.dispatch(() => def.thunkHandler(payload, fail));
Expand Down
18 changes: 18 additions & 0 deletions tests/listener-actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ it('listening to a successful or failed thunk, firing an action', async () => {
helpers.fail('💩');
return undefined;
}
if (payload === 8) {
helpers.fail();
return undefined;
}
return 'foo';
}),
},
Expand Down Expand Up @@ -162,6 +166,20 @@ it('listening to a successful or failed thunk, firing an action', async () => {
expect(actualTarget.result).toBeUndefined();
expect(actualTarget.error).toBe('💩');
expect(store.getState().audit.logs).toEqual(['Added 10', 'Failed to add 7']);

// ACT
await store.getActions().math.add(8);

// ASSERT
expect(actualTarget.type).toBe('@thunk.math.add(fail)');
expect(actualTarget.payload).toBe(8);
expect(actualTarget.result).toBeUndefined();
expect(actualTarget.error).toStrictEqual(new Error());
expect(store.getState().audit.logs).toEqual([
'Added 10',
'Failed to add 7',
'Failed to add 8',
]);
});

it('listening to a failed thunk', async () => {
Expand Down

0 comments on commit 56a5f82

Please sign in to comment.