1+ var $state , user , fetchInstances , ctx ;
2+ describe ( 'serviceFetchUser' . bold . underline . blue , function ( ) {
3+ var apiMocks = require ( '../apiMocks/index' ) ;
4+ beforeEach ( function ( ) {
5+ user = { } ;
6+ ctx = { } ;
7+ angular . mock . module ( 'app' ) ;
8+ angular . mock . module ( function ( $provide ) {
9+ $provide . value ( 'user' , user ) ;
10+ $provide . value ( 'fetchUser' , function ( cb ) {
11+ cb ( null , user ) ;
12+ } ) ;
13+ } ) ;
14+ angular . mock . inject ( function ( _fetchInstances_ , _$state_ ) {
15+ $state = _$state_ ;
16+ fetchInstances = _fetchInstances_ ;
17+ } ) ;
18+ ctx . fakeuser = {
19+ attrs : angular . copy ( apiMocks . user ) ,
20+ oauthName : function ( ) {
21+ return 'user' ;
22+ }
23+ } ;
24+ ctx . fakeOrg1 = {
25+ attrs : angular . copy ( apiMocks . user ) ,
26+ oauthName : function ( ) {
27+ return 'org1' ;
28+ }
29+ } ;
30+ ctx . fakeOrg2 = {
31+ attrs : angular . copy ( apiMocks . user ) ,
32+ oauthName : function ( ) {
33+ return 'org2' ;
34+ }
35+ } ;
36+
37+ ctx . userList = {
38+ user : ctx . fakeuser ,
39+ org1 : ctx . fakeOrg1 ,
40+ org2 : ctx . fakeOrg2
41+ } ;
42+
43+ ctx . instanceLists = {
44+ user : {
45+ models : [ {
46+ attrs : angular . copy ( apiMocks . instances . running )
47+ } , {
48+ attrs : angular . copy ( apiMocks . instances . stopped )
49+ } ]
50+ } ,
51+ org1 : {
52+ models : [ {
53+ attrs : angular . copy ( apiMocks . instances . building )
54+ } ]
55+ } ,
56+ org2 : {
57+ models : [ ]
58+ }
59+ } ;
60+ } ) ;
61+
62+ /**
63+ * Things to check
64+ *
65+ * Make sure forceQuery works
66+ * Make sure it always performs the query when changing active accounts
67+ *
68+ */
69+
70+ it ( 'should fetch the instances the first time' , function ( done ) {
71+ user . fetchInstances = sinon . spy ( function ( opts , innerCb ) {
72+ expect ( opts ) . to . deep . equal ( {
73+ githubUsername : 'user'
74+ } ) ;
75+ setTimeout ( innerCb , 10 ) ;
76+ return ctx . instanceLists [ 'user' ] ;
77+ } ) ;
78+ var cb = sinon . spy ( function ( err , instances , username ) {
79+ sinon . assert . called ( user . fetchInstances ) ;
80+ expect ( instances ) . to . equal ( ctx . instanceLists [ 'user' ] ) ;
81+ expect ( username ) . to . equal ( 'user' ) ;
82+ done ( ) ;
83+ } ) ;
84+ fetchInstances ( 'user' , false , cb ) ;
85+ } ) ;
86+ it ( 'should use its own cache when refetching' , function ( done ) {
87+ user . fetchInstances = sinon . spy ( function ( opts , innerCb ) {
88+ expect ( opts ) . to . deep . equal ( {
89+ githubUsername : 'user'
90+ } ) ;
91+ setTimeout ( innerCb , 10 ) ;
92+ return ctx . instanceLists [ 'user' ] ;
93+ } ) ;
94+ var cb = sinon . spy ( function ( err , instances , username ) {
95+ sinon . assert . calledOnce ( user . fetchInstances ) ;
96+
97+ expect ( instances ) . to . equal ( ctx . instanceLists [ 'user' ] ) ;
98+ expect ( username ) . to . equal ( 'user' ) ;
99+ part2 ( ) ;
100+ } ) ;
101+ fetchInstances ( 'user' , false , cb ) ;
102+ function part2 ( ) {
103+ user . fetchInstances = sinon . spy ( function ( opts , innerCb ) {
104+ innerCb ( null , ctx . instanceLists [ 'user' ] , 'user' ) ;
105+ } ) ;
106+ cb = sinon . spy ( function ( err , instances , username ) {
107+ sinon . assert . notCalled ( user . fetchInstances ) ;
108+ expect ( instances ) . to . equal ( ctx . instanceLists [ 'user' ] ) ;
109+ expect ( username ) . to . equal ( 'user' ) ;
110+ done ( ) ;
111+ } ) ;
112+ fetchInstances ( 'user' , false , cb ) ;
113+ }
114+ } ) ;
115+ it ( 'should skip its own cache when forced' , function ( done ) {
116+ user . fetchInstances = sinon . spy ( function ( opts , innerCb ) {
117+ expect ( opts ) . to . deep . equal ( {
118+ githubUsername : 'user'
119+ } ) ;
120+ setTimeout ( innerCb , 10 ) ;
121+ return ctx . instanceLists [ 'user' ] ;
122+ } ) ;
123+ var cb = sinon . spy ( function ( err , instances , username ) {
124+ sinon . assert . calledOnce ( user . fetchInstances ) ;
125+
126+ expect ( instances ) . to . equal ( ctx . instanceLists [ 'user' ] ) ;
127+ expect ( username ) . to . equal ( 'user' ) ;
128+ part2 ( ) ;
129+ } ) ;
130+ fetchInstances ( 'user' , false , cb ) ;
131+ function part2 ( ) {
132+ user . fetchInstances = sinon . spy ( function ( opts , innerCb ) {
133+ innerCb ( null , ctx . instanceLists [ 'user' ] , 'user' ) ;
134+ } ) ;
135+ cb = sinon . spy ( function ( err , instances , username ) {
136+ sinon . assert . calledOnce ( user . fetchInstances ) ;
137+ expect ( instances ) . to . equal ( ctx . instanceLists [ 'user' ] ) ;
138+ expect ( username ) . to . equal ( 'user' ) ;
139+ done ( ) ;
140+ } ) ;
141+ fetchInstances ( 'user' , true , cb ) ;
142+ }
143+ } ) ;
144+ it ( 'should return the correct user with the correct instances when they come out of order' , function ( done ) {
145+ var cb1 , cb2 ;
146+ var isUser = true ;
147+ user . fetchInstances = sinon . spy ( function ( opts , innerCb ) {
148+ if ( opts . githubUsername === 'user' ) {
149+ expect ( isUser ) . to . be . true ;
150+ isUser = false ;
151+ cb1 = innerCb ;
152+ } else {
153+ expect ( isUser ) . to . be . false ;
154+ cb2 = innerCb ;
155+ }
156+ return ctx . instanceLists [ opts . githubUsername ] ;
157+ } ) ;
158+ var doneCb = sinon . spy ( function ( err , instances , username ) {
159+ } ) ;
160+ var doneCb2 = sinon . spy ( function ( err , instances , username ) {
161+ sinon . assert . notCalled ( doneCb ) ;
162+ expect ( 'org1' ) . to . deep . equal ( username ) ;
163+ expect ( instances ) . to . not . deep . equal ( ctx . instanceLists [ 'user' ] ) ;
164+ expect ( instances ) . to . deep . equal ( ctx . instanceLists [ username ] ) ;
165+ done ( ) ;
166+ } ) ;
167+ fetchInstances ( 'user' , false , doneCb ) ;
168+ fetchInstances ( 'org1' , false , doneCb2 ) ;
169+ cb2 ( ) ;
170+ cb1 ( ) ;
171+ } ) ;
172+ } ) ;
0 commit comments