Skip to content

Commit

Permalink
feat(reducers): create new status state object if none exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixeladed committed Oct 21, 2020
1 parent 745fe35 commit 629fcd6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { getSettings } from './settings';
*/
export const handlePending = <Data, Returned, ThunkArg, ThunkApiConfig>(
asyncThunk: AsyncThunk<Returned, ThunkArg, ThunkApiConfig>
) => (state: AsyncState<Data>) => {
) => (state: Partial<AsyncState<Data>>) => {
if (!state.status) {
state.status = {};
}

const { typePrefix } = asyncThunk;
const currentStatus =
state.status[typePrefix] || getDefaultStatus(typePrefix);
Expand All @@ -27,7 +31,11 @@ export const handlePending = <Data, Returned, ThunkArg, ThunkApiConfig>(
*/
export const handleFulfilled = <Data, Returned, ThunkArg, ThunkApiConfig>(
asyncThunk: AsyncThunk<Returned, ThunkArg, ThunkApiConfig>
) => (state: AsyncState<Data>) => {
) => (state: Partial<AsyncState<Data>>) => {
if (!state.status) {
state.status = {};
}

const { typePrefix } = asyncThunk;
const currentStatus =
state.status[typePrefix] || getDefaultStatus(typePrefix);
Expand All @@ -48,9 +56,13 @@ export const handleFulfilled = <Data, Returned, ThunkArg, ThunkApiConfig>(
export const handleRejected = <Data, Returned, ThunkArg, ThunkApiConfig>(
asyncThunk: AsyncThunk<Returned, ThunkArg, ThunkApiConfig>
) => (
state: AsyncState<Data>,
state: Partial<AsyncState<Data>>,
action: ReturnType<typeof asyncThunk['rejected']>
) => {
if (!state.status) {
state.status = {};
}

const { typePrefix } = asyncThunk;
const currentStatus =
state.status[typePrefix] || getDefaultStatus(typePrefix);
Expand All @@ -72,7 +84,11 @@ export const handleRejected = <Data, Returned, ThunkArg, ThunkApiConfig>(
*/
export const handleReset = <Data, Returned, ThunkArg, ThunkApiConfig>(
asyncThunk: AsyncThunk<Returned, ThunkArg, ThunkApiConfig>
) => (state: AsyncState<Data>) => {
) => (state: Partial<AsyncState<Data>>) => {
if (!state.status) {
state.status = {};
}

const { typePrefix } = asyncThunk;
const newStatus = getDefaultStatus(typePrefix);
state.status[typePrefix] = newStatus;
Expand All @@ -81,6 +97,6 @@ export const handleReset = <Data, Returned, ThunkArg, ThunkApiConfig>(
/**
* Reset the status of all async thunks
*/
export const resetAllStatuses = <Data>(state: AsyncState<Data>) => {
export const resetAllStatuses = <Data>(state: Partial<AsyncState<Data>>) => {
state.status = {};
};
12 changes: 12 additions & 0 deletions test/reducers/handleFulfilled.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,16 @@ describe('handleFulfilled', () => {
Date.parse(state.status[thunk.typePrefix]?.lastLoaded!)
).toBeTruthy();
});

it('creates a status state object is none exist', () => {
const adapter = createAsyncAdapter();
const thunk = createAsyncThunk('thunk', () => {});

const state: Partial<AsyncState<{}>> = {
data: {},
};

adapter.handleFulfilled(thunk)(state);
expect(state.status[thunk.typePrefix]).toBeTruthy();
});
});
12 changes: 12 additions & 0 deletions test/reducers/handlePending.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,16 @@ describe('handlePending', () => {
adapter.handlePending(thunk)(state);
expect(state.status[thunk.typePrefix]?.lastLoaded).toBe(lastLoaded);
});

it('creates a status state object is none exist', () => {
const adapter = createAsyncAdapter();
const thunk = createAsyncThunk('thunk', () => {});

const state: Partial<AsyncState<{}>> = {
data: {},
};

adapter.handlePending(thunk)(state);
expect(state.status[thunk.typePrefix]).toBeTruthy();
});
});
14 changes: 14 additions & 0 deletions test/reducers/handleRejected.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,18 @@ describe('handleRejected', () => {
adapter.handleRejected(thunk)(state, action);
expect(state.status[thunk.typePrefix]?.lastLoaded).toBeUndefined();
});

it('creates a status state object is none exist', () => {
const adapter = createAsyncAdapter();
const thunk = createAsyncThunk('thunk', () => {});
const error = new Error();
const action = thunk.rejected(error, 'request id');

const state: Partial<AsyncState<{}>> = {
data: {},
};

adapter.handleRejected(thunk)(state, action);
expect(state.status[thunk.typePrefix]).toBeTruthy();
});
});
12 changes: 12 additions & 0 deletions test/reducers/handleReset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ describe('handleReset', () => {
getDefaultStatus(thunk.typePrefix)
);
});

it('creates a status state object is none exist', () => {
const adapter = createAsyncAdapter();
const thunk = createAsyncThunk('thunk', () => {});

const state: Partial<AsyncState<{}>> = {
data: {},
};

adapter.handleReset(thunk)(state);
expect(state.status[thunk.typePrefix]).toBeTruthy();
});
});

0 comments on commit 629fcd6

Please sign in to comment.