@@ -43,69 +43,104 @@ describe('listChanges', () => {
43
43
44
44
describe ( 'events' , ( ) => {
45
45
46
- it ( 'should stream child_added events ' , ( done ) => {
46
+ it ( 'should stream value at first ' , ( done ) => {
47
47
const someRef = ref ( rando ( ) ) ;
48
- someRef . set ( batch ) ;
49
48
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 ( ) ) ;
52
51
expect ( data ) . toEqual ( items ) ;
53
- done ( ) ;
54
- } ) ;
52
+ } ) . add ( done ) ;
53
+ someRef . set ( batch ) ;
55
54
} ) ;
56
55
57
- it ( 'should process a new child_added event' , ( done ) => {
56
+ it ( 'should process a new child_added event' , done => {
58
57
const aref = ref ( rando ( ) ) ;
59
- aref . set ( batch ) ;
60
58
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 ( ) ) ;
63
61
expect ( data [ 3 ] ) . toEqual ( { name : 'anotha one' } ) ;
64
- done ( ) ;
65
- } ) ;
62
+ } ) . add ( done ) ;
63
+ aref . set ( batch ) ;
66
64
aref . push ( { name : 'anotha one' } ) ;
67
65
} ) ;
68
66
69
- it ( 'should process a new child_removed event ' , ( done ) => {
67
+ it ( 'should stream in order events ' , ( done ) => {
70
68
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 ) ;
71
76
aref . set ( batch ) ;
72
- const obs = listChanges ( aref , [ 'child_added' , 'child_removed' ] )
77
+ } ) ;
73
78
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 ( ) ) ;
76
110
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 ( ) ;
78
115
} ) ;
79
- const childR = aref . child ( items [ 0 ] . key ) ;
80
- childR . remove ( ) . then ( console . log ) ;
81
116
} ) ;
82
117
83
118
it ( 'should process a new child_changed event' , ( done ) => {
84
119
const aref = ref ( rando ( ) ) ;
85
- aref . set ( batch ) ;
86
120
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' } ) ;
91
128
} ) ;
92
- const childR = aref . child ( items [ 0 ] . key ) ;
93
- childR . update ( { name : 'lol' } ) ;
94
129
} ) ;
95
130
96
131
it ( 'should process a new child_moved event' , ( done ) => {
97
132
const aref = ref ( rando ( ) ) ;
98
- aref . set ( batch ) ;
99
133
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 ( ) ) ;
102
136
// We moved the first item to the last item, so we check that
103
137
// the new result is now the last result
104
138
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' , ( ) => { } ) ;
106
143
} ) ;
107
- const childR = aref . child ( items [ 0 ] . key ) ;
108
- childR . setPriority ( 'a' , ( ) => { } ) ;
109
144
} ) ;
110
145
111
146
} ) ;
0 commit comments