File size: 5,726 Bytes
5fae594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
describe('patternAdapter', function() {
  
   it('compiles the correct pattern when patterns are listened to', function(){
   
      bus('newListener').emit( 'node:test_pattern');
            
      expect( jsonPathCompiler ).toHaveBeenCalledWith('test_pattern');
   })

   it('listens for NODE_FOUND events when node:pattern is added', function(){
         
      bus('newListener').emit( 'node:test_pattern');
      
      expect(bus(NODE_FOUND).on)
         .toHaveBeenCalledWith(            
            jasmine.any(Function)
         ,  'node:test_pattern'
         )
   })
   
   it('does not listen for NODE_FOUND events second time same node:pattern is added', function(){
         
      bus('newListener').emit( 'node:test_pattern');
      bus('newListener').emit( 'node:test_pattern');
      
      expect(bus(NODE_FOUND).on.calls.length).toBe(1);
   })
           
   it('stops listening for node events when node:pattern is removed again', function(){
         
      bus('node:test_pattern').on( noop);
      bus('node:test_pattern').un( noop);
      
      expect(bus(NODE_FOUND).un)
         .toHaveBeenCalledWith(
            'node:test_pattern'
         )
   })
   
   it('starts listening again for node events when node:pattern is added, removed and added again', function(){
         
      bus('node:test_pattern').on( noop);
      
      expect(bus(NODE_FOUND).on.calls.length).toBe(1);
      
      bus('node:test_pattern').on( noop);
      
      expect(bus(NODE_FOUND).on.calls.length).toBe(1);                  
      
      bus('node:test_pattern').un( noop);
      
      expect(bus(NODE_FOUND).un.calls.length).toBe(0);
      
      bus('node:test_pattern').un( noop);
      
      expect(bus(NODE_FOUND).un.calls.length).toBe(1);                  
      
      bus('node:test_pattern').on( noop);      
      
      expect(bus(NODE_FOUND).on.calls.length).toBe(2);
   })   
   
   it('doesn\'t stop listening is there are still other node:pattern listeners', function(){
         
      bus('node:test_pattern').on( noop);
      bus('node:test_pattern').on( noop);
      bus('node:test_pattern').un( noop);
      
      expect(bus(NODE_FOUND).un)
         .not.toHaveBeenCalledWith(            
            'node:test_pattern'
         )
   })
   
   it('doesn\'t stop listening if a different pattern is unsubscribed from', function(){
         
      bus('node:test_pattern').on( noop);
      bus('node:other_test_pattern').on( noop);
      bus('node:other_test_pattern').un( noop);
      
      expect(bus(NODE_FOUND).un)
         .not.toHaveBeenCalledWith(           
            'node:test_pattern'
         )
   })
   
   it('doesn\'t stop listening if wrong event type is unsubscribed', function(){
         
      bus('node:test_pattern').on( noop);
      bus('path:test_pattern').on( noop);
      bus('path:test_pattern').un( noop);
      
      expect(bus(NODE_FOUND).un)
         .not.toHaveBeenCalledWith(            
            'node:test_pattern'
         )
   })
   
   it('only listens once to NODE_FOUND when same pattern is added twice', function(){
         
      bus('node:test_pattern').on( noop);
      bus('node:test_pattern').on( noop);
      
      expect( listAsArray( bus(NODE_FOUND).listeners ).length ).toBe(1);
   })
   
   it('listens to NODE_FOUND and PATH_FOUND when given node: and path: listeners', function(){
         
      bus('node:test_pattern').on( noop);
      bus('path:test_pattern').on( noop);
      
      expect(bus(NODE_FOUND).on)
         .toHaveBeenCalledWith(            
            jasmine.any(Function)
         ,  'node:test_pattern'
         )
         
      expect(bus(PATH_FOUND).on)
         .toHaveBeenCalledWith(            
            jasmine.any(Function)
         ,  'path:test_pattern'
         )         
   })                     

   it('fires node:pattern events when match is found', function(){
      
      var ascent = anAscentMatching('test_pattern');
   
      bus('node:test_pattern').on( noop);

      bus(NODE_FOUND).emit( ascent);
      
      expect( bus('node:test_pattern').emit ).toHaveBeenCalled();
   })
   
   it('fires gives node:pattern the node, path ' +
       'and ancestors from the given ascent', function(){
      
      var testJson = {animals:{mammals:{humans:'hi there!'}}},
          ascent = anAscentMatching('test_pattern', testJson);
         
      bus('node:test_pattern').on(noop);

      bus(NODE_FOUND).emit( ascent);
      
      expect( bus('node:test_pattern').emit )
         .toHaveBeenCalledWith( 
            'hi there!', 
            [  'animals', 'mammals', 'humans'],
            [  testJson, 
               testJson.animals, 
               testJson.animals.mammals, 
               testJson.animals.mammals.humans
            ] 
         );
   })   



// ----------- end of tests -----------

   var bus, matches, jsonPathCompiler;

   beforeEach(function(){
   
      matches = {};
   
      bus = spiedPubSub();
     
      jsonPathCompiler = jasmine.createSpy('jsonPathCompiler').andCallFake( 
         function (pattern){
   
            function compiled ( ascent ){         
               if( matches[pattern] === ascent ) {
                  return head(ascent);
               } else {
                  return false;
               }
            }
            
            return compiled;
         }
      );        
   
      patternAdapter(bus, jsonPathCompiler);      
   })   
   
   function anAscentMatching(pattern, json) {
      var ascent
   
      if( json ) {
         ascent = ascentFrom(json);               
      } else {
         ascent = list(namedNode('node', {}));
      }

      matches[pattern] = ascent;  

      return ascent;
   }

});