Skip to content

Commit d07c47b

Browse files
committed
Add more scenarios to test for createSliceBuilder
1 parent 17f43b5 commit d07c47b

File tree

8 files changed

+145
-14
lines changed

8 files changed

+145
-14
lines changed

packages/rtk-codemods/transforms/createReducerBuilder/__testfixtures__/basic-ts.input.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const todoInitialState = todoAdapter.getInitialState()
1212

1313
export type TodoSliceState = typeof todoInitialState
1414

15+
const { addOne } = todoAdapter
16+
1517
createReducer(todoInitialState, {
1618
[todoAdded1a]: (state: TodoSliceState, action: PayloadAction<string>) => {
1719
// stuff
@@ -29,8 +31,8 @@ createReducer(todoInitialState, {
2931
todoAdded1f: (state: TodoSliceState, action: PayloadAction<string>) => {
3032
//stuff
3133
},
32-
[todoAdded1g]: someFunc,
33-
todoAdded1h: todoAdapter.removeOne,
34+
[todoAdded1g]: addOne,
35+
todoAdded1h: todoAdapter.addOne,
3436
})
3537

3638
createReducer(todoInitialState, {

packages/rtk-codemods/transforms/createReducerBuilder/__testfixtures__/basic-ts.output.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const todoInitialState = todoAdapter.getInitialState()
1212

1313
export type TodoSliceState = typeof todoInitialState
1414

15+
const { addOne } = todoAdapter
16+
1517
createReducer(todoInitialState, (builder) => {
1618
builder.addCase(todoAdded1a, (state: TodoSliceState, action: PayloadAction<string>) => {
1719
// stuff
@@ -41,8 +43,8 @@ createReducer(todoInitialState, (builder) => {
4143
//stuff
4244
});
4345

44-
builder.addCase(todoAdded1g, someFunc);
45-
builder.addCase(todoAdded1h, todoAdapter.removeOne);
46+
builder.addCase(todoAdded1g, addOne);
47+
builder.addCase(todoAdded1h, todoAdapter.addOne);
4648
})
4749

4850
createReducer(todoInitialState, (builder) => {

packages/rtk-codemods/transforms/createReducerBuilder/__testfixtures__/basic.input.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export const todoAdapter = createEntityAdapter();
44

55
const todoInitialState = todoAdapter.getInitialState();
66

7+
const { addOne } = todoAdapter;
8+
79
createReducer(todoInitialState, {
810
[todoAdded1a]: (state, action) => {
911
// stuff
@@ -21,8 +23,8 @@ createReducer(todoInitialState, {
2123
todoAdded1f: (state, action) => {
2224
//stuff
2325
},
24-
[todoAdded1g]: someFunc,
25-
todoAdded1h: todoAdapter.removeOne,
26+
[todoAdded1g]: addOne,
27+
todoAdded1h: todoAdapter.addOne,
2628
});
2729

2830
createReducer(todoInitialState, {

packages/rtk-codemods/transforms/createReducerBuilder/__testfixtures__/basic.output.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export const todoAdapter = createEntityAdapter();
44

55
const todoInitialState = todoAdapter.getInitialState();
66

7+
const { addOne } = todoAdapter;
8+
79
createReducer(todoInitialState, (builder) => {
810
builder.addCase(todoAdded1a, (state, action) => {
911
// stuff
@@ -27,8 +29,8 @@ createReducer(todoInitialState, (builder) => {
2729
//stuff
2830
});
2931

30-
builder.addCase(todoAdded1g, someFunc);
31-
builder.addCase(todoAdded1h, todoAdapter.removeOne);
32+
builder.addCase(todoAdded1g, addOne);
33+
builder.addCase(todoAdded1h, todoAdapter.addOne);
3234
});
3335

3436
createReducer(todoInitialState, (builder) => {

packages/rtk-codemods/transforms/createSliceBuilder/__testfixtures__/basic-ts.input.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { PayloadAction } from '@reduxjs/toolkit'
1+
import type { PayloadAction } from '@reduxjs/toolkit';
22
import {
33
createAsyncThunk,
44
createEntityAdapter,
55
createSlice
6-
} from '@reduxjs/toolkit'
6+
} from '@reduxjs/toolkit';
77

88
export interface Todo {
99
id: string
@@ -30,6 +30,8 @@ export const incrementAsync = createAsyncThunk(
3030
}
3131
)
3232

33+
const { addOne } = todoAdapter
34+
3335
const todoSlice = createSlice({
3436
name: 'todo',
3537
initialState: todoInitialState,
@@ -44,7 +46,31 @@ const todoSlice = createSlice({
4446
// stuff
4547
},
4648
[incrementAsync.rejected]: todoAdapter.removeAll,
47-
todoAdded: todoAdapter.addOne
49+
[incrementAsync.fulfilled](
50+
state: TodoSliceState,
51+
action: PayloadAction<string>) {
52+
// stuff
53+
},
54+
todoAdded: todoAdapter.addOne,
55+
56+
[todoAdded1a]: (state: TodoSliceState, action: PayloadAction<string>) => {
57+
// stuff
58+
},
59+
[todoAdded1b]: (state: TodoSliceState, action: PayloadAction<string>) => action.payload,
60+
[todoAdded1c + 'test']: (state:TodoSliceState, action: PayloadAction<string>) => {
61+
// stuff
62+
},
63+
[todoAdded1d](state: TodoSliceState, action: PayloadAction<string>) {
64+
// stuff
65+
},
66+
[todoAdded1e]: function(state: TodoSliceState, action: PayloadAction<string>) {
67+
// stuff
68+
},
69+
todoAdded1f: (state: TodoSliceState, action: PayloadAction<string>) => {
70+
//stuff
71+
},
72+
[todoAdded1g]: addOne,
73+
todoAdded1h: todoAdapter.addOne,
4874
}
4975
})
5076

packages/rtk-codemods/transforms/createSliceBuilder/__testfixtures__/basic-ts.output.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { PayloadAction } from '@reduxjs/toolkit'
1+
import type { PayloadAction } from '@reduxjs/toolkit';
22
import {
33
createAsyncThunk,
44
createEntityAdapter,
55
createSlice
6-
} from '@reduxjs/toolkit'
6+
} from '@reduxjs/toolkit';
77

88
export interface Todo {
99
id: string
@@ -30,6 +30,8 @@ export const incrementAsync = createAsyncThunk(
3030
}
3131
)
3232

33+
const { addOne } = todoAdapter
34+
3335
const todoSlice = createSlice({
3436
name: 'todo',
3537
initialState: todoInitialState,
@@ -47,7 +49,46 @@ const todoSlice = createSlice({
4749
);
4850

4951
builder.addCase(incrementAsync.rejected, todoAdapter.removeAll);
52+
53+
builder.addCase(
54+
incrementAsync.fulfilled,
55+
(state: TodoSliceState, action: PayloadAction<string>) => {
56+
// stuff
57+
}
58+
);
59+
5060
builder.addCase(todoAdded, todoAdapter.addOne);
61+
62+
builder.addCase(todoAdded1a, (state: TodoSliceState, action: PayloadAction<string>) => {
63+
// stuff
64+
});
65+
66+
builder.addCase(
67+
todoAdded1b,
68+
(state: TodoSliceState, action: PayloadAction<string>) => action.payload
69+
);
70+
71+
builder.addCase(
72+
todoAdded1c + 'test',
73+
(state:TodoSliceState, action: PayloadAction<string>) => {
74+
// stuff
75+
}
76+
);
77+
78+
builder.addCase(todoAdded1d, (state: TodoSliceState, action: PayloadAction<string>) => {
79+
// stuff
80+
});
81+
82+
builder.addCase(todoAdded1e, (state: TodoSliceState, action: PayloadAction<string>) => {
83+
// stuff
84+
});
85+
86+
builder.addCase(todoAdded1f, (state: TodoSliceState, action: PayloadAction<string>) => {
87+
//stuff
88+
});
89+
90+
builder.addCase(todoAdded1g, addOne);
91+
builder.addCase(todoAdded1h, todoAdapter.addOne);
5192
}
5293
})
5394

packages/rtk-codemods/transforms/createSliceBuilder/__testfixtures__/basic.input.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export const incrementAsync = createAsyncThunk('counter/fetchCount', async (amou
1313
return response.data;
1414
});
1515

16+
const { addOne } = todoAdapter;
17+
1618
const todoSlice = createSlice({
1719
name: 'todo',
1820
initialState: todoInitialState,
@@ -24,7 +26,29 @@ const todoSlice = createSlice({
2426
// stuff
2527
},
2628
[incrementAsync.rejected]: todoAdapter.removeAll,
27-
todoAdded: todoAdapter.addOne
29+
[incrementAsync.fulfilled](state, action) {
30+
// stuff
31+
},
32+
todoAdded: todoAdapter.addOne,
33+
34+
[todoAdded1a]: (state, action) => {
35+
// stuff
36+
},
37+
[todoAdded1b]: (state, action) => action.payload,
38+
[todoAdded1c + 'test']: (state, action) => {
39+
// stuff
40+
},
41+
[todoAdded1d](state, action) {
42+
// stuff
43+
},
44+
[todoAdded1e]: function (state, action) {
45+
// stuff
46+
},
47+
todoAdded1f: (state, action) => {
48+
//stuff
49+
},
50+
[todoAdded1g]: addOne,
51+
todoAdded1h: todoAdapter.addOne,
2852
}
2953
});
3054

packages/rtk-codemods/transforms/createSliceBuilder/__testfixtures__/basic.output.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export const incrementAsync = createAsyncThunk('counter/fetchCount', async (amou
1313
return response.data;
1414
});
1515

16+
const { addOne } = todoAdapter;
17+
1618
const todoSlice = createSlice({
1719
name: 'todo',
1820
initialState: todoInitialState,
@@ -27,7 +29,37 @@ const todoSlice = createSlice({
2729
});
2830

2931
builder.addCase(incrementAsync.rejected, todoAdapter.removeAll);
32+
33+
builder.addCase(incrementAsync.fulfilled, (state, action) => {
34+
// stuff
35+
});
36+
3037
builder.addCase(todoAdded, todoAdapter.addOne);
38+
39+
builder.addCase(todoAdded1a, (state, action) => {
40+
// stuff
41+
});
42+
43+
builder.addCase(todoAdded1b, (state, action) => action.payload);
44+
45+
builder.addCase(todoAdded1c + 'test', (state, action) => {
46+
// stuff
47+
});
48+
49+
builder.addCase(todoAdded1d, (state, action) => {
50+
// stuff
51+
});
52+
53+
builder.addCase(todoAdded1e, (state, action) => {
54+
// stuff
55+
});
56+
57+
builder.addCase(todoAdded1f, (state, action) => {
58+
//stuff
59+
});
60+
61+
builder.addCase(todoAdded1g, addOne);
62+
builder.addCase(todoAdded1h, todoAdapter.addOne);
3163
}
3264
});
3365

0 commit comments

Comments
 (0)