| 1 | import { createApi } from '@reduxjs/toolkit/query'
|
|---|
| 2 |
|
|---|
| 3 | const baseQuery = (args?: any) => ({ data: args })
|
|---|
| 4 |
|
|---|
| 5 | const api = createApi({
|
|---|
| 6 | baseQuery,
|
|---|
| 7 | tagTypes: ['Banana', 'Bread'],
|
|---|
| 8 | endpoints: (build) => ({
|
|---|
| 9 | getBanana: build.query<unknown, number>({
|
|---|
| 10 | query(id) {
|
|---|
| 11 | return { url: `banana/${id}` }
|
|---|
| 12 | },
|
|---|
| 13 | providesTags: ['Banana'],
|
|---|
| 14 | }),
|
|---|
| 15 | getBananas: build.query<unknown, void>({
|
|---|
| 16 | query() {
|
|---|
| 17 | return { url: 'bananas' }
|
|---|
| 18 | },
|
|---|
| 19 | providesTags: ['Banana'],
|
|---|
| 20 | }),
|
|---|
| 21 | getBread: build.query<unknown, number>({
|
|---|
| 22 | query(id) {
|
|---|
| 23 | return { url: `bread/${id}` }
|
|---|
| 24 | },
|
|---|
| 25 | providesTags: ['Bread'],
|
|---|
| 26 | }),
|
|---|
| 27 | }),
|
|---|
| 28 | })
|
|---|
| 29 |
|
|---|
| 30 | describe('type tests', () => {
|
|---|
| 31 | it('should allow for an array of string TagTypes', () => {
|
|---|
| 32 | api.util.invalidateTags(['Banana', 'Bread'])
|
|---|
| 33 | })
|
|---|
| 34 |
|
|---|
| 35 | it('should allow for an array of full TagTypes descriptions', () => {
|
|---|
| 36 | api.util.invalidateTags([{ type: 'Banana' }, { type: 'Bread', id: 1 }])
|
|---|
| 37 | })
|
|---|
| 38 |
|
|---|
| 39 | it('should allow for a mix of full descriptions as well as plain strings', () => {
|
|---|
| 40 | api.util.invalidateTags(['Banana', { type: 'Bread', id: 1 }])
|
|---|
| 41 | })
|
|---|
| 42 |
|
|---|
| 43 | it('should error when using non-existing TagTypes', () => {
|
|---|
| 44 | // @ts-expect-error
|
|---|
| 45 | api.util.invalidateTags(['Missing Tag'])
|
|---|
| 46 | })
|
|---|
| 47 |
|
|---|
| 48 | it('should error when using non-existing TagTypes in the full format', () => {
|
|---|
| 49 | // @ts-expect-error
|
|---|
| 50 | api.util.invalidateTags([{ type: 'Missing' }])
|
|---|
| 51 | })
|
|---|
| 52 |
|
|---|
| 53 | it('should allow pre-fetching for an endpoint that takes an arg', () => {
|
|---|
| 54 | api.util.prefetch('getBanana', 5, { force: true })
|
|---|
| 55 | api.util.prefetch('getBanana', 5, { force: false })
|
|---|
| 56 | api.util.prefetch('getBanana', 5, { ifOlderThan: false })
|
|---|
| 57 | api.util.prefetch('getBanana', 5, { ifOlderThan: 30 })
|
|---|
| 58 | api.util.prefetch('getBanana', 5, {})
|
|---|
| 59 | })
|
|---|
| 60 |
|
|---|
| 61 | it('should error when pre-fetching with the incorrect arg type', () => {
|
|---|
| 62 | // @ts-expect-error arg should be number, not string
|
|---|
| 63 | api.util.prefetch('getBanana', '5', { force: true })
|
|---|
| 64 | })
|
|---|
| 65 |
|
|---|
| 66 | it('should allow pre-fetching for an endpoint with a void arg', () => {
|
|---|
| 67 | api.util.prefetch('getBananas', undefined, { force: true })
|
|---|
| 68 | api.util.prefetch('getBananas', undefined, { force: false })
|
|---|
| 69 | api.util.prefetch('getBananas', undefined, { ifOlderThan: false })
|
|---|
| 70 | api.util.prefetch('getBananas', undefined, { ifOlderThan: 30 })
|
|---|
| 71 | api.util.prefetch('getBananas', undefined, {})
|
|---|
| 72 | })
|
|---|
| 73 |
|
|---|
| 74 | it('should error when pre-fetching with a defined arg when expecting void', () => {
|
|---|
| 75 | // @ts-expect-error arg should be void, not number
|
|---|
| 76 | api.util.prefetch('getBananas', 5, { force: true })
|
|---|
| 77 | })
|
|---|
| 78 |
|
|---|
| 79 | it('should error when pre-fetching for an incorrect endpoint name', () => {
|
|---|
| 80 | // @ts-expect-error endpoint name does not exist
|
|---|
| 81 | api.util.prefetch('getPomegranates', undefined, { force: true })
|
|---|
| 82 | })
|
|---|
| 83 | })
|
|---|