@@ -17,21 +17,39 @@ describe('Chat App', function () {
1717 // Reference to messages count
1818 var messagesCount = element ( by . id ( 'messagesCount' ) ) ;
1919
20- beforeEach ( function ( done ) {
21- // Navigate to the chat app
22- browser . get ( 'chat/chat.html' ) ;
20+ var flow = protractor . promise . controlFlow ( ) ;
21+
22+ function waitOne ( ) {
23+ return protractor . promise . delayed ( 500 ) ;
24+ }
2325
26+ function sleep ( ) {
27+ flow . execute ( waitOne ) ;
28+ }
29+
30+ beforeEach ( function ( ) {
2431 // Clear the Firebase before the first test and sleep until it's finished
2532 if ( ! firebaseCleared ) {
26- firebaseRef . remove ( function ( ) {
27- firebaseCleared = true ;
28- done ( ) ;
33+ flow . execute ( function ( ) {
34+ var def = protractor . promise . defer ( ) ;
35+ firebaseRef . remove ( function ( err ) {
36+ if ( err ) {
37+ def . reject ( err ) ;
38+ }
39+ else {
40+ firebaseCleared = true ;
41+ def . fulfill ( ) ;
42+ }
43+ } ) ;
44+ return def . promise ;
2945 } ) ;
3046 }
31- else {
32- ptor . sleep ( 500 ) ;
33- done ( ) ;
34- }
47+
48+ // Navigate to the chat app
49+ browser . get ( 'chat/chat.html' ) ;
50+
51+ // wait for page to load
52+ sleep ( ) ;
3553 } ) ;
3654
3755 it ( 'loads' , function ( ) {
@@ -53,72 +71,80 @@ describe('Chat App', function () {
5371 newMessageInput . sendKeys ( 'Oh, hi. How are you?\n' ) ;
5472 newMessageInput . sendKeys ( 'Pretty fantastic!\n' ) ;
5573
74+ sleep ( ) ;
75+
5676 // We should only have two messages in the repeater since we did a limit query
5777 expect ( messages . count ( ) ) . toBe ( 2 ) ;
5878
5979 // Messages count should include all messages, not just the ones displayed
6080 expect ( messagesCount . getText ( ) ) . toEqual ( '3' ) ;
6181 } ) ;
6282
63- it ( 'updates upon new remote messages' , function ( done ) {
64- // Simulate a message being added remotely
65- firebaseRef . child ( "messages" ) . push ( {
66- from : 'Guest 2000' ,
67- content : 'Remote message detected'
68- } , function ( ) {
69- // Update the message count as well
70- firebaseRef . child ( "numMessages" ) . transaction ( function ( currentCount ) {
71- if ( ! currentCount ) {
72- return 1 ;
73- } else {
74- return currentCount + 1 ;
75- }
76- } , function ( ) {
77- // We should only have two messages in the repeater since we did a limit query
78- expect ( messages . count ( ) ) . toBe ( 2 ) ;
79-
80- // Messages count should include all messages, not just the ones displayed
81- expect ( messagesCount . getText ( ) ) . toEqual ( '4' ) ;
82-
83- // We need to sleep long enough for the promises above to resolve
84- ptor . sleep ( 500 ) . then ( function ( ) {
85- done ( ) ;
86- } ) ;
87- } ) ;
88- } ) ;
89- } ) ;
90-
91- it ( 'updates upon removed remote messages' , function ( done ) {
92- // Simulate a message being deleted remotely
93- var onCallback = firebaseRef . child ( "messages" ) . limit ( 1 ) . on ( "child_added" , function ( childSnapshot ) {
94- firebaseRef . child ( "messages" ) . off ( "child_added" , onCallback ) ;
95- childSnapshot . ref ( ) . remove ( function ( ) {
83+ it ( 'updates upon new remote messages' , function ( ) {
84+ flow . execute ( function ( ) {
85+ var def = protractor . promise . defer ( ) ;
86+ // Simulate a message being added remotely
87+ firebaseRef . child ( "messages" ) . push ( {
88+ from : 'Guest 2000' ,
89+ content : 'Remote message detected'
90+ } , function ( ) {
91+ // Update the message count as well
9692 firebaseRef . child ( "numMessages" ) . transaction ( function ( currentCount ) {
9793 if ( ! currentCount ) {
9894 return 1 ;
9995 } else {
100- return currentCount - 1 ;
96+ return currentCount + 1 ;
10197 }
102- } , function ( ) {
103- // We should only have two messages in the repeater since we did a limit query
104- expect ( messages . count ( ) ) . toBe ( 2 ) ;
98+ } , function ( e , c , s ) {
99+ if ( e ) { def . reject ( e ) ; }
100+ else { def . fulfill ( ) ; }
101+ } ) ;
102+ } ) ;
103+ return def . promise ;
104+ } ) ;
105105
106- // Messages count should include all messages, not just the ones displayed
107- expect ( messagesCount . getText ( ) ) . toEqual ( '3' ) ;
106+ // We should only have two messages in the repeater since we did a limit query
107+ expect ( messages . count ( ) ) . toBe ( 2 ) ;
108+
109+ // Messages count should include all messages, not just the ones displayed
110+ expect ( messagesCount . getText ( ) ) . toEqual ( '4' ) ;
111+ } ) ;
108112
109- // We need to sleep long enough for the promises above to resolve
110- ptor . sleep ( 500 ) . then ( function ( ) {
111- done ( ) ;
113+ it ( 'updates upon removed remote messages' , function ( ) {
114+ flow . execute ( function ( ) {
115+ var def = protractor . promise . defer ( ) ;
116+ // Simulate a message being deleted remotely
117+ var onCallback = firebaseRef . child ( "messages" ) . limitToLast ( 1 ) . on ( "child_added" , function ( childSnapshot ) {
118+ firebaseRef . child ( "messages" ) . off ( "child_added" , onCallback ) ;
119+ childSnapshot . ref ( ) . remove ( function ( ) {
120+ firebaseRef . child ( "numMessages" ) . transaction ( function ( currentCount ) {
121+ if ( ! currentCount ) {
122+ return 1 ;
123+ } else {
124+ return currentCount - 1 ;
125+ }
126+ } , function ( err ) {
127+ if ( err ) { def . reject ( err ) ; }
128+ else { def . fulfill ( ) ; }
112129 } ) ;
113130 } ) ;
114131 } ) ;
132+ return def . promise ;
115133 } ) ;
134+
135+ // We should only have two messages in the repeater since we did a limit query
136+ expect ( messages . count ( ) ) . toBe ( 2 ) ;
137+
138+ // Messages count should include all messages, not just the ones displayed
139+ expect ( messagesCount . getText ( ) ) . toEqual ( '3' ) ;
116140 } ) ;
117141
118142 it ( 'stops updating once the AngularFire bindings are destroyed' , function ( ) {
119143 // Destroy the AngularFire bindings
120144 $ ( '#destroyButton' ) . click ( ) ;
121145
146+ sleep ( ) ;
147+
122148 expect ( messages . count ( ) ) . toBe ( 0 ) ;
123149 expect ( messagesCount . getText ( ) ) . toEqual ( '0' ) ;
124150 } ) ;
0 commit comments