File size: 4,843 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 |
describe('instance api and pattern adaptor composed',function(){
"use strict";
var bus, api, matches;
beforeEach(function(){
bus = spiedPubSub();
matches = {};
function jsonPathCompiler(pattern){
function compiled ( ascent ){
if( matches[pattern] === ascent ) {
return head(ascent);
} else {
return false;
}
}
return compiled;
}
api = instanceApi(bus);
// For now, tie the patternAdapter into the bus. These tests are
// for the composition between patternAdaptor and instanceApi
patternAdapter(bus, jsonPathCompiler);
});
function anAscentMatching(pattern) {
var ascent = list(namedNode('node', {}));
matches[pattern] = ascent;
return ascent;
}
it('has chainable methods that don\'t explode', function() {
// test that nothing forgot to return 'this':
expect(function(){
function fn(){}
api
.path('*', fn)
.node('*', fn)
.fail(fn).path('*', fn)
.path({'*':fn})
.node({'*': fn})
.done(fn)
.path({'*':fn})
.start(fn)
.on('path','*', fn)
.on('node','*', fn)
.fail(fn)
.on('path','*', fn)
.on('path',{'*':fn})
.on('node',{'*': fn})
.on('path',{'*':fn})
.on('done',fn)
.on('start',fn);
}).not.toThrow();
})
describe('header method', function(){
it('returns undefined if not available', function() {
expect( api.header() ).toBeUndefined();
});
it('can provide object once available', function() {
var headers = {"x-remainingRequests": 100};
bus(HTTP_START).emit( 200, headers );
expect( api.header() ).toEqual(headers);
});
it('can provide single header once available', function() {
var headers = {"x-remainingRequests": 100};
bus(HTTP_START).emit( 200, headers );
expect( api.header('x-remainingRequests') ).toEqual(100);
});
it('gives undefined for non-existent single header', function() {
var headers = {"x-remainingRequests": 100};
bus(HTTP_START).emit( 200, headers );
expect( api.header('x-remainingBathtubs') ).toBeUndefined();
});
});
describe('root method', function(){
it('returns undefined if not available', function() {
expect( api.root() ).toBeUndefined();
});
it('can provide object once available', function() {
var root = {I:'am', the:'root'};
bus(ROOT_FOUND).emit( root);
expect( api.root() ).toEqual(root);
});
});
describe('node and path callbacks', function(){
it('calls node callback on matching node', function() {
var callback = jasmine.createSpy('node callback'),
ascent = anAscentMatching('a_pattern');
api.on('node', 'a_pattern', callback);
expect(callback).not.toHaveBeenCalled()
bus(NODE_FOUND).emit( ascent)
expect(callback).toHaveBeenCalled()
});
it('calls path callback on matching path', function() {
var callback = jasmine.createSpy(),
ascent = anAscentMatching('a_pattern');
api.on('path', 'a_pattern', callback);
expect(callback).not.toHaveBeenCalled()
bus(PATH_FOUND).emit( ascent)
expect(callback).toHaveBeenCalled()
});
it('does not call node callback on non-matching node', function() {
var callback = jasmine.createSpy(),
ascent = anAscentMatching('a_pattern');
api.on('node', 'a_different_pattern', callback);
bus(NODE_FOUND).emit( ascent)
expect(callback).not.toHaveBeenCalled()
});
it('calls node callback again on second match', function() {
var callback = jasmine.createSpy(),
ascent = anAscentMatching('a_pattern');
api.on('node', 'a_pattern', callback);
bus(NODE_FOUND).emit( ascent)
expect(callback.call.length).toBe(1)
bus(NODE_FOUND).emit( ascent)
expect(callback.calls.length).toBe(2)
});
});
});
|