Skip to content

Commit 0c3c41d

Browse files
authored
Merge pull request #1818 from reduxjs/bugfix/1817-rtkq-selectors
2 parents 4bb0c5c + 17f033e commit 0c3c41d

File tree

4 files changed

+79
-14
lines changed

4 files changed

+79
-14
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ jobs:
7979
fail-fast: false
8080
matrix:
8181
node: ['14.x']
82-
ts: ['3.9', '4.0', '4.1', '4.2', '4.3', , '4.4', '4.5', 'next']
82+
ts: ['3.9', '4.0', '4.1', '4.2', '4.3', '4.4', '4.5', 'next']
8383
steps:
8484
- name: Checkout repo
8585
uses: actions/checkout@v2

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,9 @@ export type QueryArgFrom<D extends BaseEndpointDefinition<any, any, any>> =
464464
export type ResultTypeFrom<D extends BaseEndpointDefinition<any, any, any>> =
465465
D extends BaseEndpointDefinition<any, any, infer RT> ? RT : unknown
466466

467-
export type ReducerPathFrom<D extends EndpointDefinition<any, any, any, any>> =
468-
D extends EndpointDefinition<any, any, any, infer RP> ? RP : unknown
467+
export type ReducerPathFrom<
468+
D extends EndpointDefinition<any, any, any, any, any>
469+
> = D extends EndpointDefinition<any, any, any, any, infer RP> ? RP : unknown
469470

470471
export type TagTypesFrom<D extends EndpointDefinition<any, any, any, any>> =
471472
D extends EndpointDefinition<any, any, infer RP, any> ? RP : unknown
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
2+
3+
import { createSelector, configureStore } from '@reduxjs/toolkit'
4+
import { expectExactType } from './helpers'
5+
6+
describe('buildSelector', () => {
7+
test.skip('buildSelector typetest', () => {
8+
interface Todo {
9+
userId: number
10+
id: number
11+
title: string
12+
completed: boolean
13+
}
14+
15+
type Todos = Array<Todo>
16+
17+
const exampleApi = createApi({
18+
reducerPath: 'api',
19+
baseQuery: fetchBaseQuery({
20+
baseUrl: 'https://jsonplaceholder.typicode.com',
21+
}),
22+
endpoints: (build) => ({
23+
getTodos: build.query<Todos, string>({
24+
query: () => '/todos',
25+
}),
26+
}),
27+
})
28+
29+
const exampleQuerySelector = exampleApi.endpoints.getTodos.select('/')
30+
31+
const todosSelector = createSelector(
32+
[exampleQuerySelector],
33+
(queryState) => {
34+
return queryState?.data?.[0] ?? ({} as Todo)
35+
}
36+
)
37+
const firstTodoTitleSelector = createSelector(
38+
[todosSelector],
39+
(todo) => todo?.title
40+
)
41+
42+
const store = configureStore({
43+
reducer: {
44+
[exampleApi.reducerPath]: exampleApi.reducer,
45+
},
46+
})
47+
48+
const todoTitle = firstTodoTitleSelector(store.getState())
49+
50+
// This only compiles if we carried the types through
51+
const upperTitle = todoTitle.toUpperCase()
52+
expectExactType<string>(upperTitle)
53+
})
54+
})

packages/toolkit/src/query/tests/optimisticUpdates.test.tsx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ beforeEach(() => baseQuery.mockReset())
1414
const api = createApi({
1515
baseQuery: (...args: any[]) => {
1616
const result = baseQuery(...args)
17-
if ('then' in result)
17+
if (typeof result === 'object' && 'then' in result)
1818
return result
1919
.then((data: any) => ({ data, meta: 'meta' }))
2020
.catch((e: any) => ({ error: e }))
@@ -131,11 +131,16 @@ describe('basic lifecycle', () => {
131131

132132
describe('updateQueryData', () => {
133133
test('updates cache values, can apply inverse patch', async () => {
134-
baseQuery.mockResolvedValueOnce({
135-
id: '3',
136-
title: 'All about cheese.',
137-
contents: 'TODO',
138-
})
134+
baseQuery
135+
.mockResolvedValueOnce({
136+
id: '3',
137+
title: 'All about cheese.',
138+
contents: 'TODO',
139+
})
140+
// TODO I have no idea why the query is getting called multiple times,
141+
// but passing an additional mocked value (_any_ value)
142+
// seems to silence some annoying "got an undefined result" logging
143+
.mockResolvedValueOnce(42)
139144
const { result } = renderHook(() => api.endpoints.post.useQuery('3'), {
140145
wrapper: storeRef.wrapper,
141146
})
@@ -180,11 +185,14 @@ describe('updateQueryData', () => {
180185
})
181186

182187
test('does not update non-existing values', async () => {
183-
baseQuery.mockResolvedValueOnce({
184-
id: '3',
185-
title: 'All about cheese.',
186-
contents: 'TODO',
187-
})
188+
baseQuery
189+
.mockImplementationOnce(async () => ({
190+
id: '3',
191+
title: 'All about cheese.',
192+
contents: 'TODO',
193+
}))
194+
.mockResolvedValueOnce(42)
195+
188196
const { result } = renderHook(() => api.endpoints.post.useQuery('3'), {
189197
wrapper: storeRef.wrapper,
190198
})
@@ -234,6 +242,7 @@ describe('full integration', () => {
234242
title: 'Meanwhile, this changed server-side.',
235243
contents: 'Delicious cheese!',
236244
})
245+
.mockResolvedValueOnce(42)
237246
const { result } = renderHook(
238247
() => ({
239248
query: api.endpoints.post.useQuery('3'),
@@ -283,6 +292,7 @@ describe('full integration', () => {
283292
title: 'Meanwhile, this changed server-side.',
284293
contents: 'TODO',
285294
})
295+
.mockResolvedValueOnce(42)
286296

287297
const { result } = renderHook(
288298
() => ({

0 commit comments

Comments
 (0)