1
1
import React from 'react' ;
2
2
import configureStore from 'redux-mock-store' ;
3
3
import thunk from 'redux-thunk' ;
4
+ import axios from 'axios' ;
4
5
import { unmountComponentAtNode } from 'react-dom' ;
5
6
import { act } from 'react-dom/test-utils' ;
6
- import moxios from 'moxios' ;
7
- import * as ProjectActions from '../actions/projects' ;
8
- import * as ActionTypes from '../../../constants' ;
9
7
import SketchList from './SketchList' ;
10
8
import { reduxRender , fireEvent , screen } from '../../../test-utils' ;
9
+ import {
10
+ initialTestState ,
11
+ mockProjects
12
+ } from '../../../redux_test_stores/test_store' ;
11
13
12
- const mockStore = configureStore ( [ thunk ] ) ;
14
+ jest . mock ( '../../../i18n' ) ;
13
15
14
- const mockProjects = [
15
- {
16
- name : 'testsketch1' ,
17
- _id : 'testid1' ,
18
- updatedAt : '2021-02-26T04:58:29.390Z' ,
19
- files : [ ] ,
20
- createdAt : '2021-02-26T04:58:14.136Z' ,
21
- id : 'testid1'
22
- } ,
23
- {
24
- name : 'testsketch2' ,
25
- _id : 'testid2' ,
26
- updatedAt : '2021-02-23T17:40:43.789Z' ,
27
- files : [ ] ,
28
- createdAt : '2021-02-23T17:40:43.789Z' ,
29
- id : 'testid2'
30
- }
31
- ] ;
32
-
33
- const initialTestState = {
34
- ide : null ,
35
- files : [ ] ,
36
- preferences : { } ,
37
- user : {
38
-
39
- username : 'happydog' ,
40
- preferences : { } ,
41
- apiKeys : [ ] ,
42
- verified : 'sent' ,
43
- id : '123456789' ,
44
- totalSize : 0 ,
45
- authenticated : true
46
- } ,
47
- project : null ,
48
- sketches : mockProjects ,
49
- search : {
50
- collectionSearchTerm : '' ,
51
- sketchSearchTerm : ''
52
- } ,
53
- sorting : {
54
- field : 'createdAt' ,
55
- direction : 'DESCENDING'
56
- } ,
57
- editorAccessibility : { } ,
58
- toast : { } ,
59
- console : [ ] ,
60
- assets : { } ,
61
- loading : false ,
62
- collections : [ ]
63
- } ;
16
+ /*
17
+ * there seem to be polarizing opinions about whether or not
18
+ * we should test the unconnected component or the
19
+ * connected one. For the sake of not editing the original SketchList file
20
+ * with an unneccessary export statement, I'm testing
21
+ * the connected component with redux-mock-store.
22
+ * this approach is outlined here -
23
+ * https://www.robinwieruch.de/react-connected-component-test
24
+ */
64
25
65
26
describe ( '<Sketchlist />' , ( ) => {
66
- let container = null ;
67
27
let store ;
28
+ let container ;
29
+ const mockStore = configureStore ( [ thunk ] ) ;
30
+
68
31
beforeEach ( ( ) => {
69
32
// setup a DOM element as a render target
70
33
container = document . createElement ( 'div' ) ;
71
34
document . body . appendChild ( container ) ;
35
+ axios . get . mockImplementationOnce ( ( x ) => Promise . resolve ( { data : 'foo' } ) ) ;
36
+ store = mockStore ( initialTestState ) ;
72
37
} ) ;
73
38
74
39
afterEach ( ( ) => {
@@ -79,71 +44,88 @@ describe('<Sketchlist />', () => {
79
44
store . clearActions ( ) ;
80
45
} ) ;
81
46
82
- // it('creates GET_PROJECTS after successfuly fetching projects', async () => {
83
- // moxios.install();
84
- // function resolveAfter2Seconds() {
85
- // console.log("called resolve");
86
- // return new Promise(resolve => {
87
- // setTimeout(() => {
88
- // resolve('resolved');
89
- // }, 5000);
90
- // });
91
- // }
92
- // console.log("moxios", moxios);
93
- // moxios.wait(() => {
94
-
95
- // const request = moxios.requests.mostRecent();
96
- // console.log(moxios.requests, request)
97
- // console.log("recieved request for get projects")
98
- // request.respondWith({
99
- // status: 200,
100
- // response: mockProjects,
101
- // }).then(() => done());
102
- // });
103
-
104
- // const expectedActions = [
105
- // {type: ActionTypes.START_LOADING},
106
- // { type: ActionTypes.SET_PROJECTS,
107
- // projects: mockProjects }
108
- // ];
109
-
110
- // store = mockStore(initialTestState);
111
- // console.log("dispatching action");
112
- // //store.dispatch(ProjectActions.getProjects("happydog"))
113
- // act(() => {
114
- // reduxRender(<SketchList />, { store, container });
115
- // });
116
-
117
- // const hasSetProjectKey = (currActions) => {
118
- // return currActions.filter(ac => ac.type === ActionTypes.SET_PROJECTS).length > 0;
119
- // }
120
-
121
- // await waitFor(() => expect(hasSetProjectKey(store.getActions())).toEqual(true));
122
- // moxios.uninstall();
123
- // //expect(store.getActions()).toEqual(expectedActions);
124
- // //return resolveAfter2Seconds().then(res => console.log("resolved"))
125
- // });
126
-
127
- it ( 'has both of the sample projects' , ( ) => {
128
- store = mockStore ( initialTestState ) ;
47
+ it ( 'has sample projects' , ( ) => {
129
48
let component ;
130
49
act ( ( ) => {
131
- component = reduxRender ( < SketchList /> , { store, container } ) ;
50
+ component = reduxRender ( < SketchList username = "happydog1" /> , {
51
+ store,
52
+ container
53
+ } ) ;
132
54
} ) ;
133
55
expect ( screen . getByText ( 'testsketch1' ) ) . toBeInTheDocument ( ) ;
134
56
expect ( screen . getByText ( 'testsketch2' ) ) . toBeInTheDocument ( ) ;
135
57
} ) ;
136
58
137
59
it ( 'clicking on date created row header dispatches a reordering action' , ( ) => {
138
- store = mockStore ( initialTestState ) ;
139
60
let component ;
140
61
act ( ( ) => {
141
- component = reduxRender ( < SketchList /> , { store, container } ) ;
62
+ component = reduxRender ( < SketchList username = "happydog2" /> , {
63
+ store,
64
+ container
65
+ } ) ;
142
66
} ) ;
143
67
act ( ( ) => {
144
68
fireEvent . click ( screen . getByTestId ( 'toggle-direction-createdAt' ) ) ;
145
69
} ) ;
146
70
const expectedAction = [ { type : 'TOGGLE_DIRECTION' , field : 'createdAt' } ] ;
147
71
expect ( store . getActions ( ) ) . toEqual ( expect . arrayContaining ( expectedAction ) ) ;
148
72
} ) ;
73
+
74
+ it ( 'clicking on dropdown arrow opens sketch options' , ( ) => {
75
+ let component ;
76
+ act ( ( ) => {
77
+ component = reduxRender ( < SketchList username = "happydog2" /> , {
78
+ store,
79
+ container
80
+ } ) ;
81
+ } ) ;
82
+ const dropdown = screen . queryAllByTestId (
83
+ 'sketch-list-toggle-options-arrow'
84
+ ) ;
85
+
86
+ if ( dropdown . length > 0 ) {
87
+ act ( ( ) => {
88
+ fireEvent . click ( dropdown [ 0 ] ) ;
89
+ } ) ;
90
+
91
+ expect ( screen . queryByText ( 'Rename' ) ) . not . toBeInTheDocument ( ) ;
92
+ expect ( screen . queryByText ( 'Duplicate' ) ) . toBeInTheDocument ( ) ;
93
+ expect ( screen . queryByText ( 'Download' ) ) . toBeInTheDocument ( ) ;
94
+ expect ( screen . queryByText ( 'Add to collection' ) ) . toBeInTheDocument ( ) ;
95
+ expect ( screen . queryByText ( 'Delete' ) ) . not . toBeInTheDocument ( ) ;
96
+ }
97
+ } ) ;
98
+
99
+ it ( 'clicking on dropdown arrow opens sketch options - sketches belong to user' , ( ) => {
100
+ let component ;
101
+ act ( ( ) => {
102
+ component = reduxRender ( < SketchList username = "happydog" /> , {
103
+ store,
104
+ container
105
+ } ) ;
106
+ } ) ;
107
+ const dropdown = screen . queryAllByTestId (
108
+ 'sketch-list-toggle-options-arrow'
109
+ ) ;
110
+
111
+ if ( dropdown . length > 0 ) {
112
+ act ( ( ) => {
113
+ fireEvent . click ( dropdown [ 0 ] ) ;
114
+ } ) ;
115
+
116
+ expect ( screen . queryByText ( 'Rename' ) ) . toBeInTheDocument ( ) ;
117
+ expect ( screen . queryByText ( 'Duplicate' ) ) . toBeInTheDocument ( ) ;
118
+ expect ( screen . queryByText ( 'Download' ) ) . toBeInTheDocument ( ) ;
119
+ expect ( screen . queryByText ( 'Add to collection' ) ) . toBeInTheDocument ( ) ;
120
+ expect ( screen . queryByText ( 'Delete' ) ) . toBeInTheDocument ( ) ;
121
+ }
122
+ } ) ;
123
+
124
+ it ( 'snapshot testing' , ( ) => {
125
+ const { asFragment } = reduxRender ( < SketchList username = "happydog2" /> , {
126
+ store,
127
+ container
128
+ } ) ;
129
+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
130
+ } ) ;
149
131
} ) ;
0 commit comments