55 FetchableStatus ,
66} from './src/reducktion' ;
77
8- // TODO: fix tests related to auto-generated selectors!
9-
108describe ( 'fetchable' , ( ) => {
119 it ( 'should create fetchable value' , ( ) => {
1210 const f = fetchable . value ( [ ] ) ;
@@ -31,6 +29,82 @@ describe('fetchable', () => {
3129 const state = { test : 1 , test2 : 2 } ;
3230 expect ( reducer ( state ) ) . toEqual ( state ) ;
3331 } ) ;
32+
33+ it ( 'should track fetchable action if no state field is given' , ( ) => {
34+ const model = createModel ( {
35+ name : 'test' ,
36+ state : {
37+ something : 1 ,
38+ } ,
39+ actions : ( ) => ( {
40+ testAction : fetchable . action ( ) ,
41+ } ) ,
42+ } ) ;
43+
44+ const state = { test : { something : 1 } } ;
45+ const selector = model . selectors . getAction ( 'testAction' ) ;
46+
47+ expect ( selector ( state ) ) . toEqual ( {
48+ status : FetchableStatus . INITIAL ,
49+ error : null ,
50+ } ) ;
51+
52+ state . test . actions = {
53+ testAction : { status : FetchableStatus . LOADING , error : null } ,
54+ } ;
55+
56+ expect ( selector ( state ) ) . toEqual ( {
57+ status : FetchableStatus . LOADING ,
58+ error : null ,
59+ } ) ;
60+
61+ state . test . actions = {
62+ testAction : { status : FetchableStatus . FAILURE , error : 'err' } ,
63+ } ;
64+
65+ expect ( selector ( state ) ) . toEqual ( {
66+ status : FetchableStatus . FAILURE ,
67+ error : 'err' ,
68+ } ) ;
69+
70+ state . test . actions = { } ;
71+
72+ expect ( selector ( state ) ) . toEqual ( {
73+ status : FetchableStatus . INITIAL ,
74+ error : null ,
75+ } ) ;
76+ } ) ;
77+
78+ it ( 'should NOT track fetchable action when state field is given' , ( ) => {
79+ const model = createModel ( {
80+ name : 'test' ,
81+ state : {
82+ something : fetchable . value ( 1 ) ,
83+ } ,
84+ actions : ( ) => ( {
85+ testAction : fetchable . action ( 'something' ) ,
86+ } ) ,
87+ } ) ;
88+
89+ const state = {
90+ test : {
91+ something : { data : 1 , status : FetchableStatus . INITIAL , error : null } ,
92+ } ,
93+ } ;
94+
95+ // NOTE: `getAction` returns a simple fetchable value even if `testAction`
96+ // is not actually tracked
97+ expect ( model . selectors . getAction ( 'testAction' ) ( state ) ) . toEqual ( {
98+ status : FetchableStatus . INITIAL ,
99+ error : null ,
100+ } ) ;
101+
102+ expect ( model . selectors . get ( 'something' ) ( state ) ) . toEqual ( {
103+ data : 1 ,
104+ status : FetchableStatus . INITIAL ,
105+ error : null ,
106+ } ) ;
107+ } ) ;
34108} ) ;
35109
36110describe ( 'createModel' , ( ) => {
@@ -238,18 +312,6 @@ describe('createModel', () => {
238312 expect ( model . selectors . get ( 'orders' ) ( state ) ) . toEqual ( orders ) ;
239313 } ) ;
240314
241- it ( 'should throw if fetchable action has no success field' , ( ) => {
242- expect ( ( ) => {
243- createModel ( {
244- name : 'test' ,
245- state : {
246- orders : fetchable . value ( [ ] ) ,
247- } ,
248- actions : ( ) => ( { testAction : fetchable . action ( ) } ) ,
249- } ) ;
250- } ) . toThrowError ( / y o u m u s t p r o v i d e t h e n a m e o f t h e f i e l d / i) ;
251- } ) ;
252-
253315 it ( 'should not require initial data for fetchable' , ( ) => {
254316 const model = createModel ( {
255317 name : 'test' ,
@@ -320,7 +382,7 @@ describe('initModels', () => {
320382 'toggleNotifications' ,
321383 ] ) ;
322384 expect ( Object . keys ( settings . selectors ) . sort ( ) ) . toEqual (
323- [ 'get' , 'getCustomSelector' ] . sort ( )
385+ [ 'get' , 'getAction' , ' getCustomSelector'] . sort ( )
324386 ) ;
325387 expect ( settings . getSagas ( ) ) . toEqual ( [ ] ) ;
326388 } ) ;
0 commit comments