@@ -2,35 +2,51 @@ var protractor = require('protractor');
22var Firebase = require ( 'firebase' ) ;
33
44describe ( 'Priority App' , function ( ) {
5- // Protractor instance
6- var ptor = protractor . getInstance ( ) ;
5+ // Reference to the message repeater
6+ var messages = element . all ( by . repeater ( 'message in messages' ) ) ;
77
88 // Reference to the Firebase which stores the data for this demo
99 var firebaseRef = new Firebase ( 'https://angularFireTests.firebaseio-demo.com/priority' ) ;
1010
1111 // Boolean used to clear the Firebase on the first test only
1212 var firebaseCleared = false ;
1313
14- // Reference to the messages repeater
15- var messages = element . all ( by . repeater ( 'message in messages' ) ) ;
14+ beforeEach ( function ( ) {
15+ var flow = protractor . promise . controlFlow ( ) ;
16+
17+ if ( ! firebaseCleared ) {
18+ firebaseCleared = true ;
19+ flow . execute ( purge ) ;
20+ }
1621
17- beforeEach ( function ( done ) {
1822 // Navigate to the priority app
1923 browser . get ( 'priority/priority.html' ) ;
2024
21- // Clear the Firebase before the first test and sleep until it's finished
22- if ( ! firebaseCleared ) {
23- firebaseRef . remove ( function ( ) {
24- firebaseCleared = true ;
25- done ( ) ;
25+ // Wait for all data to load into the client
26+ flow . execute ( waitForData ) ;
27+
28+ function purge ( ) {
29+ var def = protractor . promise . defer ( ) ;
30+ firebaseRef . remove ( function ( err ) {
31+ if ( err ) { def . reject ( err ) ; }
32+ else { def . fulfill ( true ) ; }
2633 } ) ;
34+ return def . promise ;
2735 }
28- else {
29- ptor . sleep ( 500 ) ;
30- done ( ) ;
36+
37+ function waitForData ( ) {
38+ var def = protractor . promise . defer ( ) ;
39+ firebaseRef . once ( 'value' , function ( ) {
40+ def . fulfill ( true ) ;
41+ } ) ;
42+ return def . promise ;
3143 }
3244 } ) ;
3345
46+ afterEach ( function ( ) {
47+ firebaseRef . off ( ) ;
48+ } ) ;
49+
3450 it ( 'loads' , function ( ) {
3551 } ) ;
3652
@@ -64,33 +80,57 @@ describe('Priority App', function () {
6480 expect ( $ ( '.message:nth-of-type(3) .content' ) . getText ( ) ) . toEqual ( 'Pretty fantastic!' ) ;
6581 } ) ;
6682
67- xit ( 'responds to external priority updates' , function ( done ) {
68- // Update the priority of the first message
69- firebaseRef . startAt ( ) . limit ( 1 ) . once ( "child_added" , function ( dataSnapshot1 ) {
70- dataSnapshot1 . ref ( ) . setPriority ( 4 , function ( ) {
71- // Update the priority of the third message
72- firebaseRef . startAt ( 2 ) . limit ( 1 ) . once ( "child_added" , function ( dataSnapshot2 ) {
73- dataSnapshot2 . ref ( ) . setPriority ( 0 , function ( ) {
74- // Make sure the page has three messages
75- expect ( messages . count ( ) ) . toBe ( 3 ) ;
76-
77- // Make sure the priority of each message is correct
78- expect ( $ ( '.message:nth-of-type(1) .priority' ) . getText ( ) ) . toEqual ( '0' ) ;
79- expect ( $ ( '.message:nth-of-type(2) .priority' ) . getText ( ) ) . toEqual ( '1' ) ;
80- expect ( $ ( '.message:nth-of-type(3) .priority' ) . getText ( ) ) . toEqual ( '4' ) ;
81-
82- // Make sure the content of each message is correct
83- expect ( $ ( '.message:nth-of-type(1) .content' ) . getText ( ) ) . toEqual ( 'Pretty fantastic!' ) ;
84- expect ( $ ( '.message:nth-of-type(2) .content' ) . getText ( ) ) . toEqual ( 'Oh, hi. How are you?' ) ;
85- expect ( $ ( '.message:nth-of-type(3) .content' ) . getText ( ) ) . toEqual ( 'Hey there!' ) ;
86-
87- // We need to sleep long enough for the promises above to resolve
88- ptor . sleep ( 500 ) . then ( function ( ) {
89- done ( ) ;
90- } ) ;
91- } ) ;
92- } ) ;
93- } ) ;
94- } ) ;
83+ it ( 'responds to external priority updates' , function ( ) {
84+ var movesDone = waitForMoveEvents ( ) ;
85+ var flow = protractor . promise . controlFlow ( ) ;
86+ flow . execute ( moveRecords ) ;
87+
88+ expect ( movesDone ) . toBe ( true ) ;
89+ expect ( messages . count ( ) ) . toBe ( 3 ) ;
90+ expect ( $ ( '.message:nth-of-type(1) .priority' ) . getText ( ) ) . toEqual ( '0' ) ;
91+ expect ( $ ( '.message:nth-of-type(2) .priority' ) . getText ( ) ) . toEqual ( '1' ) ;
92+ expect ( $ ( '.message:nth-of-type(3) .priority' ) . getText ( ) ) . toEqual ( '4' ) ;
93+
94+ // Make sure the content of each message is correct
95+ expect ( $ ( '.message:nth-of-type(1) .content' ) . getText ( ) ) . toEqual ( 'Pretty fantastic!' ) ;
96+ expect ( $ ( '.message:nth-of-type(2) .content' ) . getText ( ) ) . toEqual ( 'Oh, hi. How are you?' ) ;
97+ expect ( $ ( '.message:nth-of-type(3) .content' ) . getText ( ) ) . toEqual ( 'Hey there!' ) ;
98+
99+ function moveRecords ( ) {
100+ return setPriority ( null , 4 )
101+ . then ( setPriority . bind ( null , 2 , 0 ) ) ;
102+ }
103+
104+ function waitForMoveEvents ( ) {
105+ var def = protractor . promise . defer ( ) ;
106+ var count = 0 ;
107+ firebaseRef . on ( 'child_moved' , updateCount , def . reject ) ;
108+
109+ function updateCount ( ) {
110+ if ( ++ count === 2 ) {
111+ setTimeout ( function ( ) {
112+ def . fulfill ( true ) ;
113+ } , 10 ) ;
114+ firebaseRef . off ( 'child_moved' , updateCount ) ;
115+ }
116+ }
117+ return def . promise ;
118+ }
119+
120+ function setPriority ( start , pri ) {
121+ var def = protractor . promise . defer ( ) ;
122+ firebaseRef . startAt ( start ) . limit ( 1 ) . once ( 'child_added' , function ( snap ) {
123+ var data = snap . val ( ) ;
124+ //todo https://github.com/firebase/angularFire/issues/333
125+ //todo makeItChange just forces Angular to update the dom since it won't change
126+ //todo when a $ variable updates
127+ data . makeItChange = true ;
128+ snap . ref ( ) . setWithPriority ( data , pri , function ( err ) {
129+ if ( err ) { def . reject ( err ) ; }
130+ else { def . fulfill ( snap . name ( ) ) ; }
131+ } )
132+ } , def . reject ) ;
133+ return def . promise ;
134+ }
95135 } ) ;
96136} ) ;
0 commit comments