File size: 4,536 Bytes
c2b7eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import type {
  ActionCreatorWithPayload,
  ActionCreatorWithoutPayload,
  EntityAdapter,
  EntityId,
  EntityStateAdapter,
  Update,
} from '@reduxjs/toolkit'
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'

function extractReducers<T, Id extends EntityId>(
  adapter: EntityAdapter<T, Id>,
): EntityStateAdapter<T, Id> {
  const { selectId, sortComparer, getInitialState, getSelectors, ...rest } =
    adapter
  return rest
}

describe('type tests', () => {
  test('should be usable in a slice, with all the "reducer-like" functions', () => {
    type Id = string & { readonly __tag: unique symbol }
    type Entity = {
      id: Id
    }
    const adapter = createEntityAdapter<Entity>()
    const slice = createSlice({
      name: 'test',
      initialState: adapter.getInitialState(),
      reducers: {
        ...extractReducers(adapter),
      },
    })

    expectTypeOf(slice.actions.addOne).toMatchTypeOf<
      ActionCreatorWithPayload<Entity>
    >()

    expectTypeOf(slice.actions.addMany).toMatchTypeOf<
      ActionCreatorWithPayload<ReadonlyArray<Entity> | Record<string, Entity>>
    >()

    expectTypeOf(slice.actions.setAll).toMatchTypeOf<
      ActionCreatorWithPayload<ReadonlyArray<Entity> | Record<string, Entity>>
    >()

    expectTypeOf(slice.actions.removeOne).toMatchTypeOf<
      ActionCreatorWithPayload<Id>
    >()

    expectTypeOf(slice.actions.addMany).not.toMatchTypeOf<
      ActionCreatorWithPayload<Entity[] | Record<string, Entity>>
    >()

    expectTypeOf(slice.actions.setAll).not.toMatchTypeOf<
      ActionCreatorWithPayload<ReadonlyArray<Id>>
    >()

    expectTypeOf(slice.actions.removeOne).toMatchTypeOf<
      ActionCreatorWithPayload<Id>
    >()

    expectTypeOf(slice.actions.removeMany).toMatchTypeOf<
      ActionCreatorWithPayload<ReadonlyArray<Id>>
    >()

    expectTypeOf(slice.actions.removeMany).not.toMatchTypeOf<
      ActionCreatorWithPayload<EntityId[]>
    >()

    expectTypeOf(
      slice.actions.removeAll,
    ).toMatchTypeOf<ActionCreatorWithoutPayload>()

    expectTypeOf(slice.actions.updateOne).toMatchTypeOf<
      ActionCreatorWithPayload<Update<Entity, Id>>
    >()

    expectTypeOf(slice.actions.updateMany).not.toMatchTypeOf<
      ActionCreatorWithPayload<Update<Entity, Id>[]>
    >()

    expectTypeOf(slice.actions.upsertOne).toMatchTypeOf<
      ActionCreatorWithPayload<Entity>
    >()

    expectTypeOf(slice.actions.updateMany).toMatchTypeOf<
      ActionCreatorWithPayload<ReadonlyArray<Update<Entity, Id>>>
    >()

    expectTypeOf(slice.actions.upsertOne).toMatchTypeOf<
      ActionCreatorWithPayload<Entity>
    >()

    expectTypeOf(slice.actions.upsertMany).toMatchTypeOf<
      ActionCreatorWithPayload<ReadonlyArray<Entity> | Record<string, Entity>>
    >()

    expectTypeOf(slice.actions.upsertMany).not.toMatchTypeOf<
      ActionCreatorWithPayload<Entity[] | Record<string, Entity>>
    >()
  })

  test('should not be able to mix with a different EntityAdapter', () => {
    type Entity = {
      id: EntityId
      value: string
    }
    type Entity2 = {
      id: EntityId
      value2: string
    }
    const adapter = createEntityAdapter<Entity>()
    const adapter2 = createEntityAdapter<Entity2>()
    createSlice({
      name: 'test',
      initialState: adapter.getInitialState(),
      reducers: {
        addOne: adapter.addOne,
        // @ts-expect-error
        addOne2: adapter2.addOne,
      },
    })
  })

  test('should be usable in a slice with extra properties', () => {
    type Entity = { id: EntityId; value: string }
    const adapter = createEntityAdapter<Entity>()
    createSlice({
      name: 'test',
      initialState: adapter.getInitialState({ extraData: 'test' }),
      reducers: {
        addOne: adapter.addOne,
      },
    })
  })

  test('should not be usable in a slice with an unfitting state', () => {
    type Entity = { id: EntityId; value: string }
    const adapter = createEntityAdapter<Entity>()
    createSlice({
      name: 'test',
      initialState: { somethingElse: '' },
      reducers: {
        // @ts-expect-error
        addOne: adapter.addOne,
      },
    })
  })

  test('should not be able to create an adapter unless the type has an Id or an idSelector is provided', () => {
    type Entity = {
      value: string
    }
    // @ts-expect-error
    const adapter = createEntityAdapter<Entity>()
    const adapter2: EntityAdapter<Entity, Entity['value']> =
      createEntityAdapter({
        selectId: (e: Entity) => e.value,
      })
  })
})