-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1818 from reduxjs/bugfix/1817-rtkq-selectors
- Loading branch information
Showing
4 changed files
with
79 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' | ||
|
||
import { createSelector, configureStore } from '@reduxjs/toolkit' | ||
import { expectExactType } from './helpers' | ||
|
||
describe('buildSelector', () => { | ||
test.skip('buildSelector typetest', () => { | ||
interface Todo { | ||
userId: number | ||
id: number | ||
title: string | ||
completed: boolean | ||
} | ||
|
||
type Todos = Array<Todo> | ||
|
||
const exampleApi = createApi({ | ||
reducerPath: 'api', | ||
baseQuery: fetchBaseQuery({ | ||
baseUrl: 'https://jsonplaceholder.typicode.com', | ||
}), | ||
endpoints: (build) => ({ | ||
getTodos: build.query<Todos, string>({ | ||
query: () => '/todos', | ||
}), | ||
}), | ||
}) | ||
|
||
const exampleQuerySelector = exampleApi.endpoints.getTodos.select('/') | ||
|
||
const todosSelector = createSelector( | ||
[exampleQuerySelector], | ||
(queryState) => { | ||
return queryState?.data?.[0] ?? ({} as Todo) | ||
} | ||
) | ||
const firstTodoTitleSelector = createSelector( | ||
[todosSelector], | ||
(todo) => todo?.title | ||
) | ||
|
||
const store = configureStore({ | ||
reducer: { | ||
[exampleApi.reducerPath]: exampleApi.reducer, | ||
}, | ||
}) | ||
|
||
const todoTitle = firstTodoTitleSelector(store.getState()) | ||
|
||
// This only compiles if we carried the types through | ||
const upperTitle = todoTitle.toUpperCase() | ||
expectExactType<string>(upperTitle) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters