@@ -43,69 +43,104 @@ describe('listChanges', () => {
4343
4444 describe ( 'events' , ( ) => {
4545
46- it ( 'should stream child_added events ' , ( done ) => {
46+ it ( 'should stream value at first ' , ( done ) => {
4747 const someRef = ref ( rando ( ) ) ;
48- someRef . set ( batch ) ;
4948 const obs = listChanges ( someRef , [ 'child_added' ] ) ;
50- const sub = obs . skip ( 2 ) . subscribe ( changes => {
51- const data = changes . map ( change => change . payload ! . val ( ) ) ;
49+ const sub = obs . take ( 1 ) . subscribe ( changes => {
50+ const data = changes . map ( change => change . payload . val ( ) ) ;
5251 expect ( data ) . toEqual ( items ) ;
53- done ( ) ;
54- } ) ;
52+ } ) . add ( done ) ;
53+ someRef . set ( batch ) ;
5554 } ) ;
5655
57- it ( 'should process a new child_added event' , ( done ) => {
56+ it ( 'should process a new child_added event' , done => {
5857 const aref = ref ( rando ( ) ) ;
59- aref . set ( batch ) ;
6058 const obs = listChanges ( aref , [ 'child_added' ] ) ;
61- const sub = obs . skip ( 3 ) . subscribe ( changes => {
62- const data = changes . map ( change => change . payload ! . val ( ) ) ;
59+ const sub = obs . skip ( 1 ) . take ( 1 ) . subscribe ( changes => {
60+ const data = changes . map ( change => change . payload . val ( ) ) ;
6361 expect ( data [ 3 ] ) . toEqual ( { name : 'anotha one' } ) ;
64- done ( ) ;
65- } ) ;
62+ } ) . add ( done ) ;
63+ aref . set ( batch ) ;
6664 aref . push ( { name : 'anotha one' } ) ;
6765 } ) ;
6866
69- it ( 'should process a new child_removed event ' , ( done ) => {
67+ it ( 'should stream in order events ' , ( done ) => {
7068 const aref = ref ( rando ( ) ) ;
69+ const obs = listChanges ( aref . orderByChild ( 'name' ) , [ 'child_added' ] ) ;
70+ const sub = obs . take ( 1 ) . subscribe ( changes => {
71+ const names = changes . map ( change => change . payload . val ( ) . name ) ;
72+ expect ( names [ 0 ] ) . toEqual ( 'one' ) ;
73+ expect ( names [ 1 ] ) . toEqual ( 'two' ) ;
74+ expect ( names [ 2 ] ) . toEqual ( 'zero' ) ;
75+ } ) . add ( done ) ;
7176 aref . set ( batch ) ;
72- const obs = listChanges ( aref , [ 'child_added' , 'child_removed' ] )
77+ } ) ;
7378
74- const sub = obs . skip ( 3 ) . subscribe ( changes => {
75- const data = changes . map ( change => change . payload ! . val ( ) ) ;
79+ it ( 'should stream in order events w/child_added' , ( done ) => {
80+ const aref = ref ( rando ( ) ) ;
81+ const obs = listChanges ( aref . orderByChild ( 'name' ) , [ 'child_added' ] ) ;
82+ const sub = obs . skip ( 1 ) . take ( 1 ) . subscribe ( changes => {
83+ const names = changes . map ( change => change . payload . val ( ) . name ) ;
84+ expect ( names [ 0 ] ) . toEqual ( 'anotha one' ) ;
85+ expect ( names [ 1 ] ) . toEqual ( 'one' ) ;
86+ expect ( names [ 2 ] ) . toEqual ( 'two' ) ;
87+ expect ( names [ 3 ] ) . toEqual ( 'zero' ) ;
88+ } ) . add ( done ) ;
89+ aref . set ( batch ) ;
90+ aref . push ( { name : 'anotha one' } ) ;
91+ } ) ;
92+
93+ it ( 'should stream events filtering' , ( done ) => {
94+ const aref = ref ( rando ( ) ) ;
95+ const obs = listChanges ( aref . orderByChild ( 'name' ) . equalTo ( 'zero' ) , [ 'child_added' ] ) ;
96+ obs . skip ( 1 ) . take ( 1 ) . subscribe ( changes => {
97+ const names = changes . map ( change => change . payload . val ( ) . name ) ;
98+ expect ( names [ 0 ] ) . toEqual ( 'zero' ) ;
99+ expect ( names [ 1 ] ) . toEqual ( 'zero' ) ;
100+ } ) . add ( done ) ;
101+ aref . set ( batch ) ;
102+ aref . push ( { name : 'zero' } ) ;
103+ } ) ;
104+
105+ it ( 'should process a new child_removed event' , done => {
106+ const aref = ref ( rando ( ) ) ;
107+ const obs = listChanges ( aref , [ 'child_added' , 'child_removed' ] ) ;
108+ const sub = obs . skip ( 1 ) . take ( 1 ) . subscribe ( changes => {
109+ const data = changes . map ( change => change . payload . val ( ) ) ;
76110 expect ( data . length ) . toEqual ( items . length - 1 ) ;
77- done ( ) ;
111+ } ) . add ( done ) ;
112+ app . database ( ) . goOnline ( ) ;
113+ aref . set ( batch ) . then ( ( ) => {
114+ aref . child ( items [ 0 ] . key ) . remove ( ) ;
78115 } ) ;
79- const childR = aref . child ( items [ 0 ] . key ) ;
80- childR . remove ( ) . then ( console . log ) ;
81116 } ) ;
82117
83118 it ( 'should process a new child_changed event' , ( done ) => {
84119 const aref = ref ( rando ( ) ) ;
85- aref . set ( batch ) ;
86120 const obs = listChanges ( aref , [ 'child_added' , 'child_changed' ] )
87- const sub = obs . skip ( 3 ) . subscribe ( changes => {
88- const data = changes . map ( change => change . payload ! . val ( ) ) ;
89- expect ( data [ 0 ] . name ) . toEqual ( 'lol' ) ;
90- done ( ) ;
121+ const sub = obs . skip ( 1 ) . take ( 1 ) . subscribe ( changes => {
122+ const data = changes . map ( change => change . payload . val ( ) ) ;
123+ expect ( data [ 1 ] . name ) . toEqual ( 'lol' ) ;
124+ } ) . add ( done ) ;
125+ app . database ( ) . goOnline ( ) ;
126+ aref . set ( batch ) . then ( ( ) => {
127+ aref . child ( items [ 1 ] . key ) . update ( { name : 'lol' } ) ;
91128 } ) ;
92- const childR = aref . child ( items [ 0 ] . key ) ;
93- childR . update ( { name : 'lol' } ) ;
94129 } ) ;
95130
96131 it ( 'should process a new child_moved event' , ( done ) => {
97132 const aref = ref ( rando ( ) ) ;
98- aref . set ( batch ) ;
99133 const obs = listChanges ( aref , [ 'child_added' , 'child_moved' ] )
100- const sub = obs . skip ( 3 ) . subscribe ( changes => {
101- const data = changes . map ( change => change . payload ! . val ( ) ) ;
134+ const sub = obs . skip ( 1 ) . take ( 1 ) . subscribe ( changes => {
135+ const data = changes . map ( change => change . payload . val ( ) ) ;
102136 // We moved the first item to the last item, so we check that
103137 // the new result is now the last result
104138 expect ( data [ data . length - 1 ] ) . toEqual ( items [ 0 ] ) ;
105- done ( ) ;
139+ } ) . add ( done ) ;
140+ app . database ( ) . goOnline ( ) ;
141+ aref . set ( batch ) . then ( ( ) => {
142+ aref . child ( items [ 0 ] . key ) . setPriority ( 'a' , ( ) => { } ) ;
106143 } ) ;
107- const childR = aref . child ( items [ 0 ] . key ) ;
108- childR . setPriority ( 'a' , ( ) => { } ) ;
109144 } ) ;
110145
111146 } ) ;
0 commit comments