1
+ import React from 'react' ;
2
+ import { shallow } from 'enzyme' ;
3
+ import { UserListItem } from 'components' ;
4
+
5
+ const store = {
6
+ subscribe : jest . fn ( ) ,
7
+ getState : jest . fn ( ( ) => ( { auth : { user : { login : 'gitpoint' } } } ) ) ,
8
+ dispatch : jest . fn ( )
9
+ } ;
10
+
11
+ const defaultProps = {
12
+ user : {
13
+ type : 'User' ,
14
+ login : 'gitpoint' ,
15
+ avatar_url : 'gitpoint-avatar.png' ,
16
+ } ,
17
+ navigation : {
18
+ navigate : jest . fn ( ) ,
19
+ }
20
+ } ;
21
+
22
+ describe ( '<UserListItem />' , ( ) => {
23
+ beforeEach ( ( ) => {
24
+ jest . clearAllMocks ( ) ;
25
+ } ) ;
26
+
27
+ it ( 'should navigate to AuthProfile screen when item is the logged in user' , ( ) => {
28
+ const wrapper = shallow ( < UserListItem { ...defaultProps } /> , { context : { store } } ) . dive ( ) ;
29
+
30
+ wrapper . find ( 'Styled(TouchableHighlight)' ) . simulate ( 'press' ) ;
31
+
32
+ expect ( defaultProps . navigation . navigate ) . toHaveBeenCalledTimes ( 1 ) ;
33
+ expect ( defaultProps . navigation . navigate ) . toHaveBeenCalledWith ( 'AuthProfile' , expect . anything ( ) ) ;
34
+ } ) ;
35
+
36
+ it ( 'should navigate to Profile screen when item is not the logged in user' , ( ) => {
37
+ const wrapper = shallow (
38
+ < UserListItem { ...defaultProps } user = { { type : 'User' , login : 'someone' } } /> ,
39
+ { context : { store } }
40
+ ) . dive ( ) ;
41
+
42
+ wrapper . find ( 'Styled(TouchableHighlight)' ) . simulate ( 'press' ) ;
43
+
44
+ expect ( defaultProps . navigation . navigate ) . toHaveBeenCalledTimes ( 1 ) ;
45
+ expect ( defaultProps . navigation . navigate ) . toHaveBeenCalledWith ( 'Profile' , expect . anything ( ) ) ;
46
+ } ) ;
47
+
48
+ it ( 'should navigate to Organization screen when item is not user type' , ( ) => {
49
+ const wrapper = shallow (
50
+ < UserListItem { ...defaultProps } user = { { type : '' , login : 'org' } } /> ,
51
+ { context : { store } }
52
+ ) . dive ( ) ;
53
+
54
+ wrapper . find ( 'Styled(TouchableHighlight)' ) . simulate ( 'press' ) ;
55
+
56
+ expect ( defaultProps . navigation . navigate ) . toHaveBeenCalledTimes ( 1 ) ;
57
+ expect ( defaultProps . navigation . navigate ) . toHaveBeenCalledWith ( 'Organization' , expect . anything ( ) ) ;
58
+ } ) ;
59
+
60
+ it ( 'should navigate to user screen when user component is pressed' , ( ) => {
61
+ const wrapper = shallow ( < UserListItem { ...defaultProps } /> , { context : { store } } ) . dive ( ) ;
62
+
63
+ wrapper . find ( '[data-testid="userListItem-user"]' ) . simulate ( 'press' ) ;
64
+
65
+ expect ( defaultProps . navigation . navigate ) . toHaveBeenCalledTimes ( 1 ) ;
66
+ expect ( defaultProps . navigation . navigate ) . toHaveBeenCalledWith ( expect . any ( String ) , { user : defaultProps . user } ) ;
67
+ } ) ;
68
+
69
+ it ( 'should navigate to user screen when user image container is pressed' , ( ) => {
70
+ const wrapper = shallow ( < UserListItem { ...defaultProps } /> , { context : { store } } ) . dive ( ) ;
71
+
72
+ wrapper . find ( '[data-testid="userListItem-imageContainer"]' ) . simulate ( 'press' ) ;
73
+
74
+ expect ( defaultProps . navigation . navigate ) . toHaveBeenCalledTimes ( 1 ) ;
75
+ expect ( defaultProps . navigation . navigate ) . toHaveBeenCalledWith ( expect . any ( String ) , { user : defaultProps . user } ) ;
76
+ } ) ;
77
+ it ( 'should render user image' , ( ) => {
78
+ const wrapper = shallow ( < UserListItem { ...defaultProps } /> , { context : { store } } ) . dive ( ) ;
79
+ const image = wrapper . find ( '[data-testid="userListItem-image"]' ) ;
80
+
81
+ expect ( image . prop ( 'source' ) . uri ) . toBe ( defaultProps . user . avatar_url ) ;
82
+ } ) ;
83
+
84
+ it ( 'should render user login as title' , ( ) => {
85
+ const wrapper = shallow ( < UserListItem { ...defaultProps } /> , { context : { store } } ) . dive ( ) ;
86
+ const title = wrapper . find ( 'Styled(Text)' ) ;
87
+
88
+ expect ( title . contains ( defaultProps . user . login ) ) . toBe ( true ) ;
89
+ } ) ;
90
+
91
+ it ( 'should render title and subtitle' , ( ) => {
92
+ const wrapper = shallow (
93
+ < UserListItem { ...defaultProps } title = "git" subtitle = "point" /> ,
94
+ { context : { store } }
95
+ ) . dive ( ) ;
96
+ const hasTitle = wrapper . find ( 'Styled(Text)' ) . contains ( 'git' ) ;
97
+ const hasSubtitle = wrapper . find ( 'Styled(Text)' ) . contains ( 'point' ) ;
98
+
99
+ expect ( hasTitle ) . toBe ( true ) ;
100
+ expect ( hasSubtitle ) . toBe ( true ) ;
101
+ } ) ;
102
+
103
+ it ( 'should call iconAction' , ( ) => {
104
+ const iconAction = jest . fn ( ) ;
105
+ const wrapper = shallow (
106
+ < UserListItem { ...defaultProps } iconAction = { iconAction } /> ,
107
+ { context : { store } }
108
+ ) . dive ( ) ;
109
+
110
+ wrapper . find ( '[data-testid="userListItem-icon"]' ) . simulate ( 'press' ) ;
111
+
112
+ expect ( iconAction ) . toHaveBeenCalledTimes ( 1 ) ;
113
+ expect ( iconAction ) . toHaveBeenCalledWith ( defaultProps . user . login ) ;
114
+ } ) ;
115
+ } ) ;
0 commit comments