1
+ import React from "react" ;
2
+
1
3
import TaskList from "./TaskList" ;
2
4
import * as TaskStories from "./Task.stories" ;
3
5
4
- export default {
5
- component : TaskList ,
6
- name : `TaskList` ,
7
- decorators : [ ( story ) => < div style = { { padding : "3rem" } } > { story ( ) } </ div > ] ,
8
- } ;
6
+ import { Provider } from "react-redux" ;
9
7
10
- const Template = ( args ) => < TaskList { ... args } /> ;
8
+ import { configureStore , createSlice } from "@reduxjs/toolkit" ;
11
9
12
- const Default = Template . bind ( { } ) ;
13
- Default . args = {
14
- // Shaping the stories through args composition.
15
- // The data was inherited from the Default story in Task.stories.js.
10
+ // A super-simple mock of the state of the store
11
+ export const MockedState = {
16
12
tasks : [
17
13
{ ...TaskStories . Default . args . task , id : "1" , title : "Task 1" } ,
18
14
{ ...TaskStories . Default . args . task , id : "2" , title : "Task 2" } ,
@@ -21,30 +17,96 @@ Default.args = {
21
17
{ ...TaskStories . Default . args . task , id : "5" , title : "Task 5" } ,
22
18
{ ...TaskStories . Default . args . task , id : "6" , title : "Task 6" } ,
23
19
] ,
20
+ status : "idle" ,
21
+ error : null ,
24
22
} ;
25
23
26
- const WithPinnedTasks = Template . bind ( { } ) ;
27
- WithPinnedTasks . args = {
28
- // Shaping the stories through args composition.
29
- // Inherited data coming from the Default story.
30
- tasks : [
31
- ...Default . args . tasks . slice ( 0 , 5 ) ,
32
- { id : "6" , title : "Task 6 (pinned)" , state : "TASK_PINNED" } ,
33
- ] ,
34
- } ;
24
+ // A super-simple mock of a redux store
25
+ const Mockstore = ( { taskboxState, children } ) => (
26
+ < Provider
27
+ store = { configureStore ( {
28
+ reducer : {
29
+ taskbox : createSlice ( {
30
+ name : "taskbox" ,
31
+ initialState : taskboxState ,
32
+ reducers : {
33
+ updateTaskState : ( state , action ) => {
34
+ const { id, newTaskState } = action . payload ;
35
+ const task = state . tasks . findIndex (
36
+ ( task ) => task . id === id
37
+ ) ;
38
+ if ( task >= 0 ) {
39
+ state . tasks [ task ] . state = newTaskState ;
40
+ }
41
+ } ,
42
+ } ,
43
+ } ) . reducer ,
44
+ } ,
45
+ } ) }
46
+ >
47
+ { children }
48
+ </ Provider >
49
+ ) ;
35
50
36
- const Loading = Template . bind ( { } ) ;
37
- Loading . args = {
38
- tasks : [ ] ,
39
- loading : true ,
51
+ export default {
52
+ component : TaskList ,
53
+ title : "TaskList" ,
54
+ decorators : [ ( story ) => < div style = { { padding : "3rem" } } > { story ( ) } </ div > ] ,
55
+ excludeStories : / .* M o c k e d S t a t e $ / ,
40
56
} ;
41
57
42
- const Empty = Template . bind ( { } ) ;
43
- Empty . args = {
44
- // Shaping the stories through args composition.
45
- // Inherited data coming from the Loading story.
46
- ...Loading . args ,
47
- loading : false ,
48
- } ;
58
+ const Template = ( ) => < TaskList /> ;
59
+
60
+ export const Default = Template . bind ( { } ) ;
61
+ Default . decorators = [
62
+ ( story ) => < Mockstore taskboxState = { MockedState } > { story ( ) } </ Mockstore > ,
63
+ ] ;
64
+
65
+ export const WithPinnedTasks = Template . bind ( { } ) ;
66
+ WithPinnedTasks . decorators = [
67
+ ( story ) => {
68
+ const pinnedtasks = [
69
+ ...MockedState . tasks . slice ( 0 , 5 ) ,
70
+ { id : "6" , title : "Task 6 (pinned)" , state : "TASK_PINNED" } ,
71
+ ] ;
72
+
73
+ return (
74
+ < Mockstore
75
+ taskboxState = { {
76
+ ...MockedState ,
77
+ tasks : pinnedtasks ,
78
+ } }
79
+ >
80
+ { story ( ) }
81
+ </ Mockstore >
82
+ ) ;
83
+ } ,
84
+ ] ;
85
+
86
+ export const Loading = Template . bind ( { } ) ;
87
+ Loading . decorators = [
88
+ ( story ) => (
89
+ < Mockstore
90
+ taskboxState = { {
91
+ ...MockedState ,
92
+ status : "loading" ,
93
+ } }
94
+ >
95
+ { story ( ) }
96
+ </ Mockstore >
97
+ ) ,
98
+ ] ;
49
99
50
- export { Default , Loading , Empty , WithPinnedTasks } ;
100
+ export const Empty = Template . bind ( { } ) ;
101
+ Empty . decorators = [
102
+ ( story ) => (
103
+ < Mockstore
104
+ taskboxState = { {
105
+ ...MockedState ,
106
+ tasks : [ ] ,
107
+ } }
108
+ >
109
+ { story ( ) }
110
+ </ Mockstore >
111
+ ) ,
112
+ ] ;
0 commit comments