Skip to content

Commit de82d4d

Browse files
authored
Merge pull request #2152 from val1984/patch-1
2 parents 1751eb9 + 038ce67 commit de82d4d

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ describe('createListenerMiddleware', () => {
10981098
expect(takeResult).toBe(null)
10991099
})
11001100

1101-
test("take resolves to [A, CurrentState, PreviousState] if the timeout is provided but doesn't expires", async () => {
1101+
test("take resolves to [A, CurrentState, PreviousState] if the timeout is provided but doesn't expire", async () => {
11021102
const store = configureStore({
11031103
reducer: counterSlice.reducer,
11041104
middleware: (gDM) => gDM().prepend(middleware),
@@ -1122,6 +1122,43 @@ describe('createListenerMiddleware', () => {
11221122
expect(takeResult).toEqual([increment(), stateCurrent, stateBefore])
11231123
})
11241124

1125+
test("take resolves to `[A, CurrentState, PreviousState] | null` if a possibly undefined timeout parameter is provided", async () => {
1126+
const store = configureStore({
1127+
reducer: counterSlice.reducer,
1128+
middleware: (gDM) => gDM().prepend(middleware),
1129+
})
1130+
1131+
type ExpectedTakeResultType = readonly [ReturnType<typeof increment>, CounterState, CounterState] | null
1132+
1133+
let timeout: number | undefined = undefined
1134+
let done = false
1135+
1136+
const startAppListening = startListening as TypedStartListening<CounterState>
1137+
startAppListening({
1138+
predicate: incrementByAmount.match,
1139+
effect: async (_, listenerApi) => {
1140+
const stateBefore = listenerApi.getState()
1141+
1142+
let takeResult = await listenerApi.take(increment.match, timeout)
1143+
const stateCurrent = listenerApi.getState()
1144+
expect(takeResult).toEqual([increment(), stateCurrent, stateBefore])
1145+
1146+
timeout = 1
1147+
takeResult = await listenerApi.take(increment.match, timeout)
1148+
expect(takeResult).toBeNull()
1149+
1150+
expectType<ExpectedTakeResultType>(takeResult)
1151+
1152+
done = true
1153+
},
1154+
})
1155+
store.dispatch(incrementByAmount(1))
1156+
store.dispatch(increment())
1157+
1158+
await delay(25)
1159+
expect(done).toBe(true);
1160+
})
1161+
11251162
test('condition method resolves promise when the predicate succeeds', async () => {
11261163
const store = configureStore({
11271164
reducer: counterSlice.reducer,

packages/toolkit/src/listenerMiddleware/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ export interface TakePattern<State> {
363363
<Predicate extends AnyListenerPredicate<State>>(
364364
predicate: Predicate,
365365
timeout?: number | undefined
366-
): Promise<[AnyAction, State, State] | null>
366+
): TakePatternOutputWithTimeout<State, Predicate>
367367
}
368368

369369
/** @public */

0 commit comments

Comments
 (0)