Skip to content

Commit 53e6b2b

Browse files
committed
Add createSliceReducerBuild Vitest setup and TS transform test
1 parent 4f46686 commit 53e6b2b

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

packages/rtk-codemods/transforms/createSliceReducerBuilder/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# createSliceReducerBuilder
22

3+
Rewrites uses of Redux Toolkit's `createSlice` API to use the "builder callback" syntax for the `reducers` field, to make it easier to add prepared reducers and thunks inside of `createSlice`.
4+
5+
Note that unlike the `createReducerBuilder` and `createSliceBuilder` transforms (which both were fixes for deprecated/removed overloads), this is entirely optional. You do not _need_ to apply this to an entire codebase unless you specifically want to. Otherwise, feel free to apply to to specific slice files as needed.
6+
7+
Should work with both JS and TS files.
38

49
## Usage
510

@@ -13,6 +18,7 @@ yarn global add @reduxjs/rtk-codemods
1318
```
1419

1520
## Local Usage
21+
1622
```
1723
node ./bin/cli.js createSliceReducerBuilder path/of/files/ or/some**/*glob.js
1824
```
@@ -23,4 +29,4 @@ node ./bin/cli.js createSliceReducerBuilder path/of/files/ or/some**/*glob.js
2329
<!--FIXTURES_TOC_END-->
2430

2531
<!--FIXTURES_CONTENT_START-->
26-
<!--FIXTURES_CONTENT_END-->
32+
<!--FIXTURES_CONTENT_END-->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const aSlice = createSlice({
2+
name: 'name',
3+
initialState: todoAdapter.getInitialState(),
4+
reducers: {
5+
property: () => {},
6+
method(state, action: PayloadAction<Todo>) {
7+
todoAdapter.addOne(state, action);
8+
},
9+
identifier: todoAdapter.removeOne,
10+
preparedProperty: {
11+
prepare: (todo: Todo) => ({ payload: { id: nanoid(), ...todo } }),
12+
reducer: () => {}
13+
},
14+
preparedMethod: {
15+
prepare(todo: Todo) {
16+
return { payload: { id: nanoid(), ...todo } }
17+
},
18+
reducer(state, action: PayloadAction<Todo>) {
19+
todoAdapter.addOne(state, action);
20+
}
21+
},
22+
preparedIdentifier: {
23+
prepare: withPayload(),
24+
reducer: todoAdapter.setMany
25+
},
26+
}
27+
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const aSlice = createSlice({
2+
name: 'name',
3+
initialState: todoAdapter.getInitialState(),
4+
5+
reducers: (create) => ({
6+
property: create.reducer(() => {}),
7+
8+
method: create.reducer((state, action: PayloadAction<Todo>) => {
9+
todoAdapter.addOne(state, action);
10+
}),
11+
12+
identifier: create.reducer(todoAdapter.removeOne),
13+
preparedProperty: create.preparedReducer((todo: Todo) => ({ payload: { id: nanoid(), ...todo } }), () => {}),
14+
15+
preparedMethod: create.preparedReducer((todo: Todo) => {
16+
return { payload: { id: nanoid(), ...todo } }
17+
}, (state, action: PayloadAction<Todo>) => {
18+
todoAdapter.addOne(state, action);
19+
}),
20+
21+
preparedIdentifier: create.preparedReducer(withPayload(), todoAdapter.setMany)
22+
})
23+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import path from 'path';
2+
import transform, { parser } from './index';
3+
4+
import { runTransformTest } from '../../transformTestUtils';
5+
6+
runTransformTest(
7+
'createSliceReducerBuilder',
8+
transform,
9+
parser,
10+
path.join(__dirname, '__testfixtures__')
11+
);

0 commit comments

Comments
 (0)