11import React from 'react' ;
22import configureStore from 'redux-mock-store' ;
3+
4+ import { mountWithIntl } from '../../helpers/intl-helpers.jsx' ;
5+
36import ProjectFetcherHOC from '../../../src/lib/project-fetcher-hoc.jsx' ;
47import storage from '../../../src/lib/storage' ;
5- import { mountWithIntl } from '../../helpers/intl-helpers.jsx ' ;
8+ import { LoadingState } from '../../../src/reducers/project-state ' ;
69
710jest . mock ( 'react-ga' ) ;
811
@@ -11,36 +14,41 @@ describe('ProjectFetcherHOC', () => {
1114 let store ;
1215
1316 beforeEach ( ( ) => {
14- store = mockStore ( { scratchGui : { } } ) ;
17+ store = mockStore ( {
18+ scratchGui : {
19+ projectState : { }
20+ }
21+ } ) ;
1522 } ) ;
1623
17- test ( 'when there is an id, it tries to load that project ' , ( ) => {
18- const Component = ( { projectData } ) => < div > { projectData } </ div > ;
24+ test ( 'when there is an id, it tries to update the store with that id ' , ( ) => {
25+ const Component = ( { projectId } ) => < div > { projectId } </ div > ;
1926 const WrappedComponent = ProjectFetcherHOC ( Component ) ;
20- const originalLoad = storage . load ;
21- storage . load = jest . fn ( ( type , id ) => Promise . resolve ( { data : id } ) ) ;
22- const mounted = mountWithIntl (
27+ const mockSetProjectIdFunc = jest . fn ( ) ;
28+ mountWithIntl (
2329 < WrappedComponent
2430 projectId = "100"
31+ setProjectId = { mockSetProjectIdFunc }
2532 store = { store }
2633 />
2734 ) ;
28- expect ( mounted . props ( ) . projectId ) . toEqual ( '100' ) ;
29- expect ( storage . load ) . toHaveBeenLastCalledWith (
30- storage . AssetType . Project , '100' , storage . DataFormat . JSON
31- ) ;
32- storage . load = originalLoad ;
35+ expect ( mockSetProjectIdFunc . mock . calls [ 0 ] [ 0 ] ) . toBe ( '100' ) ;
3336 } ) ;
34-
35- test ( 'when there is no project data, it renders null' , ( ) => {
36- const Component = ( { projectData} ) => < div > { projectData } </ div > ;
37- const WrappedComponent = ProjectFetcherHOC ( Component ) ;
37+ test ( 'when there is a reduxProjectId and isFetchingWithProjectId is true, it loads the project' , ( ) => {
3838 const originalLoad = storage . load ;
39- storage . load = jest . fn ( ( ) => Promise . resolve ( null ) ) ;
39+ storage . load = jest . fn ( ( type , id ) => Promise . resolve ( { data : id } ) ) ;
40+ const Component = ( { projectId} ) => < div > { projectId } </ div > ;
41+ const WrappedComponent = ProjectFetcherHOC ( Component ) ;
4042 const mounted = mountWithIntl ( < WrappedComponent store = { store } /> ) ;
43+ mounted . setProps ( {
44+ reduxProjectId : '100' ,
45+ isFetchingWithId : true ,
46+ loadingState : LoadingState . FETCHING_WITH_ID
47+ } ) ;
48+ mounted . update ( ) ;
49+ expect ( storage . load ) . toHaveBeenLastCalledWith (
50+ storage . AssetType . Project , '100' , storage . DataFormat . JSON
51+ ) ;
4152 storage . load = originalLoad ;
42- const mountedDiv = mounted . find ( 'div' ) ;
43- expect ( mountedDiv . exists ( ) ) . toEqual ( false ) ;
4453 } ) ;
45-
4654} ) ;
0 commit comments