1+ describe ( 'directiveAccountsSelect' . bold . underline . blue , function ( ) {
2+ var element ;
3+ var $scope , $elScope ;
4+ var $rootScope ;
5+ var ctx ;
6+ var apiMocks = require ( '../apiMocks/index' ) ;
7+
8+ function makeDefaultScope ( ) {
9+ ctx . fakeuser = {
10+ attrs : angular . copy ( apiMocks . user ) ,
11+ oauthName : function ( ) {
12+ return 'user' ;
13+ } ,
14+ gravitar : function ( ) {
15+ return true ;
16+ }
17+ } ;
18+ ctx . fakeOrg1 = {
19+ attrs : angular . copy ( apiMocks . user ) ,
20+ oauthName : function ( ) {
21+ return 'org1' ;
22+ } ,
23+ gravitar : function ( ) {
24+ return true ;
25+ }
26+ } ;
27+ ctx . fakeOrg2 = {
28+ attrs : angular . copy ( apiMocks . user ) ,
29+ oauthName : function ( ) {
30+ return 'org2' ;
31+ } ,
32+ gravitar : function ( ) {
33+ return true ;
34+ }
35+ } ;
36+ return {
37+ data : {
38+ activeAccount : ctx . fakeuser ,
39+ orgs : { models : [ ctx . fakeOrg1 , ctx . fakeOrg2 ] } ,
40+ user : ctx . fakeuser
41+ }
42+ } ;
43+ }
44+ function initState ( addToScope ) {
45+ ctx = { } ;
46+ if ( ! addToScope ) {
47+ addToScope = makeDefaultScope ( ) ;
48+ }
49+ angular . mock . module ( 'app' ) ;
50+ ctx . stateMock = {
51+ '$current' : {
52+ name : 'instance.instanceEdit'
53+ } ,
54+ go : function ( ) { }
55+ } ;
56+ angular . mock . module ( 'app' , function ( $provide ) {
57+ $provide . value ( '$state' , ctx . stateMock ) ;
58+ $provide . value ( '$stateParams' , {
59+ userName : 'username' ,
60+ instanceName : 'instanceName'
61+ } ) ;
62+ } ) ;
63+ angular . mock . inject ( function ( $compile , _$rootScope_ , $timeout ) {
64+ $rootScope = _$rootScope_ ;
65+ $scope = $rootScope . $new ( ) ;
66+
67+ $rootScope . safeApply = function ( cb ) {
68+ $timeout ( function ( ) {
69+ $scope . $digest ( ) ;
70+ } ) ;
71+ } ;
72+
73+ var tpl = directiveTemplate ( 'accounts-select' , {
74+ 'data' : 'data'
75+ } ) ;
76+
77+ Object . keys ( addToScope ) . forEach ( function ( key ) {
78+ $scope [ key ] = addToScope [ key ] ;
79+ } ) ;
80+
81+ ctx . element = $compile ( tpl ) ( $scope ) ;
82+ $scope . $digest ( ) ;
83+ } ) ;
84+ $elScope = ctx . element . isolateScope ( ) ;
85+ }
86+
87+ describe ( 'directive logic' . bold . blue , function ( ) {
88+ it ( 'should set isChangeAccount to false' , function ( ) {
89+ initState ( ) ;
90+ $scope . $digest ( ) ;
91+ expect ( $elScope . isChangeAccount ) . to . be . false ;
92+ $elScope . isChangeAccount = true ;
93+ $scope . $apply ( ) ;
94+ $rootScope . $broadcast ( 'app-document-click' ) ;
95+ expect ( $elScope . isChangeAccount ) . to . be . false ;
96+ } ) ;
97+ it ( 'should not emit signal and change state on when isChangeAccount to false' , function ( ) {
98+ initState ( ) ;
99+ var instanceFetchSpy = sinon . spy ( ) ;
100+ $rootScope . $on ( 'INSTANCE_LIST_FETCH' , instanceFetchSpy ) ;
101+ $scope . $digest ( ) ;
102+ expect ( $elScope . isChangeAccount ) . to . be . false ;
103+ $elScope . selectActiveAccount ( ctx . fakeOrg1 ) ;
104+ $scope . $apply ( ) ;
105+ sinon . assert . notCalled ( instanceFetchSpy ) ;
106+ } ) ;
107+ it ( 'should emit signal and change state on account change' , function ( done ) {
108+ initState ( ) ;
109+ ctx . stateMock . go = sinon . spy ( function ( location , state ) {
110+ expect ( state ) . to . deep . equal ( {
111+ userName : ctx . fakeOrg1 . oauthName ( ) ,
112+ instanceName : ''
113+ } ) ;
114+ done ( ) ;
115+ } ) ;
116+ var instanceFetchSpy = sinon . spy ( function ( event , username ) {
117+ expect ( username ) . to . equal ( ctx . fakeOrg1 . oauthName ( ) ) ;
118+ } ) ;
119+ $rootScope . $on ( 'INSTANCE_LIST_FETCH' , instanceFetchSpy ) ;
120+ $elScope . isChangeAccount = true ;
121+ $scope . $digest ( ) ;
122+ $elScope . selectActiveAccount ( ctx . fakeOrg1 ) ;
123+ $scope . $apply ( ) ;
124+ expect ( $scope . data . activeAccount ) . to . equal ( ctx . fakeOrg1 ) ;
125+ } ) ;
126+ } ) ;
127+
128+ describe ( 'directive logic' . bold . blue , function ( ) {
129+ afterEach ( function ( ) {
130+ $rootScope . $destroy ( ) ;
131+ } ) ;
132+ it ( 'should display with an active account' , function ( ) {
133+ initState ( ) ;
134+ $scope . $digest ( ) ;
135+ expect ( ctx . element [ 0 ] . classList . contains ( 'ng-hide' ) ) . to . not . be . ok ;
136+ expect ( getAccountsGroupItemsList ( ) . length ) . to . equal ( 3 ) ;
137+ expect ( getAccountSelectorElement ( ) . classList . contains ( 'in' ) ) . to . not . be . ok ;
138+ } ) ;
139+ it ( 'shouldn\'t display without an active account' , function ( ) {
140+ var scope = makeDefaultScope ( ) ;
141+ delete scope . data . activeAccount ;
142+ initState ( scope ) ;
143+ $scope . $digest ( ) ;
144+ expect ( ctx . element [ 0 ] . classList . contains ( 'ng-hide' ) ) . to . be . ok ;
145+ } ) ;
146+ it ( 'should display selector after click' , function ( ) {
147+ initState ( ) ;
148+ $rootScope . $digest ( ) ;
149+ click ( getAccountSelectorElement ( ) ) ;
150+ $rootScope . $digest ( ) ;
151+ expect ( getAccountSelectorElement ( ) . classList . contains ( 'in' ) ) . to . be . ok ;
152+ expect ( getAccountsGroupItemsList ( ) . length ) . to . equal ( 3 ) ;
153+ click ( getAccountSelectorElement ( ) ) ;
154+ $scope . $digest ( ) ;
155+ expect ( getAccountSelectorElement ( ) . classList . contains ( 'in' ) ) . to . not . be . ok ;
156+ } ) ;
157+ it ( 'should be able to select one of the selectors to change the account' , function ( ) {
158+ initState ( ) ;
159+ $elScope . selectActiveAccount = sinon . spy ( ) ;
160+ $rootScope . $digest ( ) ;
161+ click ( getAccountSelectorElement ( ) ) ;
162+ $rootScope . $digest ( ) ;
163+ expect ( getAccountSelectorElement ( ) . classList . contains ( 'in' ) ) . to . be . ok ;
164+ expect ( getAccountsGroupItemsList ( ) . length ) . to . equal ( 3 ) ;
165+ click ( getAccountsGroupItemsList ( ) [ 1 ] ) ;
166+ $scope . $digest ( ) ;
167+ sinon . assert . calledWith ( $elScope . selectActiveAccount , ctx . fakeOrg1 ) ;
168+ } ) ;
169+ } ) ;
170+
171+ function getAccountSelectorElement ( ) {
172+ return ctx . element [ 0 ]
173+ . querySelector ( 'ol.accounts-group' ) ;
174+ }
175+ function getAccountsGroupItemsList ( ) {
176+ return ctx . element [ 0 ]
177+ . querySelectorAll ( 'li.accounts-group-item' ) ;
178+ }
179+ } ) ;
180+
181+ function click ( el ) {
182+ var ev = document . createEvent ( 'MouseEvent' ) ;
183+ ev . initMouseEvent (
184+ 'click' ,
185+ true /* bubble */ , true /* cancelable */ ,
186+ window , null ,
187+ 0 , 0 , 0 , 0 , /* coordinates */
188+ false , false , false , false , /* modifier keys */
189+ 0 /*left*/ , null
190+ ) ;
191+ el . dispatchEvent ( ev ) ;
192+ }
0 commit comments