Skip to content

Commit

Permalink
added tests for i-api-request
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfil committed Mar 24, 2014
1 parent 1094444 commit 20a0aee
Show file tree
Hide file tree
Showing 21 changed files with 252 additions and 35 deletions.
@@ -0,0 +1,40 @@
/**
* Api format
*
* i-test-api-debounced support debounce only with 'id' param
*
* /debounced?id=1 -> {result: {1: 'result_1'}}
* /debounced?id=2 -> {result: {1: 'result_2'}}
* /debounced?id=1,2 -> {result: {1: 'result_1', 2: 'result_2'}}
*/
describe('i-api-request__debounce.common.js', function () {

var api = BEM.blocks['i-test-api-debounced'];

it('debounce with correct param', function () {
return env(function () {
var p1 = api.get('debounced', {params: {id: 1}}),
p2 = api.get('debounced', {params: {id: 2}});

return Vow.all([p1, p2]).spread(function (r1, r2) {
return Vow.all([
expect(r1.responseId).equal(r2.responseId, 'queryes was not debounced'),
expect(r1.result[1]).equal('result_1'),
expect(r1.result[2]).equal('result_2')
]);
});
});
});

it('no debounce with incorrent param', function () {
return env(function () {
var p1 = api.get('debounced', {params: {foo: 1}}),
p2 = api.get('debounced', {params: {foo: 2}});

return Vow.all([p1, p2]).spread(function (r1, r2) {
return expect(r1.responseId).not.equal(r2.responseId, 'queryes was debounced');
});
});
});

});
12 changes: 12 additions & 0 deletions blocks/i-api-request/i-api-request.common.js
Expand Up @@ -35,6 +35,18 @@ BEM.decl('i-api-request', null, {
return String(resource).replace(/^\/|\/$/g, '');
},

/**
* Checking if cache should be droped before request
*
* @param {String} method
* @param {String} [resource]
* @param {Object} [data]
* @return {Boolean}
*/
_checkDropCache: function (method) {
return method !== 'get';
},

/**
* Get json string of body
*/
Expand Down
93 changes: 85 additions & 8 deletions blocks/i-api-request/i-api-request.common.tests.js
@@ -1,17 +1,94 @@
describe('i-api-request', function () {
describe('i-api-request.common.js', function () {

var api = BEM.blocks['i-test-api'];

it('resourse only', function () {
return expect(env(function () {
return api.get('index.json');
})).eventually.have.property('source');
return env(function () {
return api.get('source');
}).then(function (response) {
return expect(response.handle).equal('source');
});
});

it('params', function () {
return expect(env(function () {
return api.get('index.json', {params: {foo: '%bar'}});
})).eventually.have.property('source');
it('with params', function () {
return env(function () {
return api.get('source', {params: {foo: 'bar'}});
}).then(function (response) {
return expect(response.params.foo).equal('bar');
});
});

it('with params with %', function () {
return env(function () {
return api.get('source', {params: {foo: '%bar'}});
}).then(function (response) {
return expect(response.params.foo).equal('%bar');
});
});

it('cache in state', function () {
return env(function () {
return Vow.all([
api.get('handle1', {params: {foo: 'bar'}}),
api.get('handle1', {params: {foo: 'bar'}})
]);
}).spread(function (r1, r2) {
return expect(r1.responseId).equal(r2.responseId, 'cache is not working');
});
});

it('drop cache after post', function () {
return env(function () {
var responseId;
return api.get('data', {params: {p: 1}})
.then(function (response) {
responseId = response.responseId;
return api.post('data', {params: {p: 2}});
})
.then(function () {
return api.get('data', {params: {p: 1}});
})
.then(function (response) {
return expect(responseId).not.equal(response.responseId);
})
});
});

it('drop cache', function () {
return env(function () {
var responseId;
return api.get('data', {params: {p: 1}})
.then(function (response) {
responseId = response.responseId;
api.dropCache();
return api.get('data', {params: {p: 1}});
})
.then(function (response) {
return expect(responseId).not.equal(response.responseId);
})
});
});

describe('errors', function () {

it('timeout', function () {
return expect(env(function () {
return api.get('timeout');
})).to.be.rejectedWith(api._HttpError);
});

it('bad resourse', function () {
return expect(env(function () {
return api.get('secret?uid=123')
})).to.be.rejectedWith(api._HttpError);
});

it('bad status', function () {
return expect(env(function () {
return api.get('error')
})).to.be.rejectedWith(api._HttpError);
});

});

});
5 changes: 0 additions & 5 deletions blocks/i-api-request/i-api-request.deps.js
Expand Up @@ -3,10 +3,5 @@
{block: 'i-ajax-proxy'},
{block: 'i-promise'},
{block: 'i-state'},
],
shouldDeps: [
// tests
{block: 'tests'},
{block: 'i-test-api'}
]
})
12 changes: 0 additions & 12 deletions blocks/i-api-request/i-api-request.js
Expand Up @@ -117,18 +117,6 @@ BEM.decl('i-api-request', null, {
return '/ajax/' + this._name + '/' + method;
},

/**
* Checking if cache should be droped before request
*
* @param {String} method
* @param {String} [resource]
* @param {Object} [data]
* @return {Boolean}
*/
_checkDropCache: function (method) {
return method !== 'get';
},

/**
* Calls when http status is not equal to 200
*
Expand Down
6 changes: 5 additions & 1 deletion blocks/i-api-request/i-api-request.priv.js
Expand Up @@ -115,9 +115,13 @@
var requestUrl,
parsedUrl;

if (this._checkDropCache(method, resource, data)) {
this.dropCache();
}

if (resource.indexOf('http') !== 0) {
if (!this._checkResource(resource)) {
return Vow.reject(new this._HttpError(400));
return Vow.reject(new this._HttpError(400, 'resource "' + resource + '" denier'));
}
if (!this._apiHost) {
return Vow.reject(new Error('_apiHost is not specified; Define ._apiHost on your level first'));
Expand Down
17 changes: 17 additions & 0 deletions blocks/i-api-request/i-api-request.priv.tests.js
@@ -0,0 +1,17 @@
describe('i-api-request.priv.js', function () {

var api = BEM.blocks['i-test-api'];

it('cache not in state', function () {
return Vow.all([
env(function () {
return api.get('handle2', {params: {foo: 'bar'}});
}),
env(function () {
return api.get('handle2', {params: {foo: 'bar'}});
})
]).spread(function (r1, r2) {
return expect(r1.responseId).not.equal(r2.responseId, 'cache should works only within state');
});
});
});
3 changes: 1 addition & 2 deletions blocks/i-bem/__data-bind/i-bem__data-bind.common.tests.js
Expand Up @@ -180,8 +180,7 @@ describe('BEM data bindings', function () {
});

it('to context property by path', function () {
var value = randomValue(),
block2 = staticBlock({
var block2 = staticBlock({
dataBindings: {'param-1-1': { initAccessor: 'path.to.param' }}
});
expect(block2.path.to.param).toBe(BEM.dataBindVal('param-1-1'));
Expand Down
2 changes: 1 addition & 1 deletion blocks/i-www-server/i-www-server.server.js
Expand Up @@ -50,7 +50,7 @@ BEM.decl({name: 'i-www-server', baseBlock: 'i-server'}, null, {

//handling uncaught exception
process.on('uncaughtException', function (err) {
console.error('UNCAUGHT EXCEPTION:', err);
console.error('UNCAUGHT EXCEPTION:', err.stack);
//gracefull exit
httpServer.close(function () {
process.exit(1);
Expand Down
1 change: 1 addition & 0 deletions tests.blocks/env/env.priv.js
Expand Up @@ -57,6 +57,7 @@ global.env = env = function (path, fn) {
process.domain.state = env.states[meta.stateKey];
delete meta.stateKey;
}

var result = fn(meta);
if (Vow.isPromise(result)) {
result.then(
Expand Down
42 changes: 42 additions & 0 deletions tests.blocks/fake-api-server/fake-api-server.priv.js
@@ -0,0 +1,42 @@
var http = require('http'),
parse = require('url').parse;


function createResponse(path, params) {
var response = {
handle: path,
params: params,
responseId: Math.random() + Date.now(),
result: {}
};


if (path === 'debounced' && params.id) {
response.result = params.id.split(',').reduce(function (o, id) {
o[id] = 'result_' + id;
return o;
}, {});
}

return response;
}


function onRequest(req, res) {
var p = parse(req.url, true),
path = p.pathname.replace(/^\//, ''),
params = p.query,
response = JSON.stringify(createResponse(path, params));

if (path === 'timeout') {
return setTimeout(res.end.bind(res, response), 600);
}
if (path === 'error') {
res.writeHead(500, {'Content-Type': 'application/json'});
} else {
res.writeHead(200, {'Content-Type': 'application/json'});
}
res.end(response);
}

http.createServer(onRequest).listen(3001, '127.0.0.1');
@@ -0,0 +1,3 @@
({
shouldDeps: {block: 'i-test-api-debounced'}
})
7 changes: 7 additions & 0 deletions tests.blocks/i-api-request/i-api-request.deps.js
@@ -0,0 +1,7 @@
({
shouldDeps: [
{block: 'tests'},
{block: 'i-test-api'},
{block: 'fake-api-server'}
]
})
@@ -0,0 +1,3 @@
BEM.decl({block: 'i-test-api-debounced', baseBlock: 'i-api-request'}, null, {
_debounceParams: ['id']
});
@@ -0,0 +1,6 @@
({
mustDeps: [
{block: 'i-api-request', elems: 'debounce'},
{block: 'i-ajax-proxy'}
]
})
@@ -0,0 +1,4 @@
BEM.blocks['i-ajax-proxy'].allowBlock('i-test-api-debounced');
BEM.decl('i-test-api-debounced', null, {
_apiHost: 'http://localhost:3001/'
});
8 changes: 7 additions & 1 deletion tests.blocks/i-test-api/i-test-api.common.js
@@ -1 +1,7 @@
BEM.decl({block: 'i-test-api', baseBlock: 'i-api-request'});
BEM.decl({block: 'i-test-api', baseBlock: 'i-api-request'}, null, {

post: function (r, p) {
return this._request('post', r, p);
}

});
3 changes: 2 additions & 1 deletion tests.blocks/i-test-api/i-test-api.priv.js
@@ -1,4 +1,5 @@
BEM.blocks['i-ajax-proxy'].allowBlock('i-test-api');
BEM.decl('i-test-api', null, {
_apiHost: 'http://nodejs.org/api/'
_apiHost: 'http://localhost:3001/',
TIMEOUT: 500
});
11 changes: 8 additions & 3 deletions tests.sh
Expand Up @@ -4,20 +4,25 @@ PHANTOM=./node_modules/.bin/mocha-phantomjs
ENB=./node_modules/.bin/enb
JSHINT=./node_modules/.bin/jshint

function kn {
killall node 2>/dev/null
}
function client {
echo Client tests
for D in `find tests/*/*server.js -type f`; do
echo " Testing $D"
killall node
kn
node $D & $PHANTOM http://127.0.0.1:3000/
kn
done
}
function server {
echo Server tests
for D in `find tests/*/*server.tests.js -type f`; do
echo " Testing $D"
killall node
kn
$MOCHA -R spec $D
kn
done
}

Expand All @@ -30,7 +35,7 @@ function coverage {
}

function lint {
find blocks tests.blocks -type f | grep -vEe 'deps.js' | xargs $JSHINT
find blocks tests.blocks -type f | grep -vEe 'deps.js' | xargs $JSHINT 1>&2
}

if [ $1 ]; then
Expand Down
7 changes: 7 additions & 0 deletions tests/debounce/debounce.bemdecl.js
@@ -0,0 +1,7 @@
exports.blocks = [
{block: 'i-router'},
{block: 'i-content', mods: {type: 'bemhtml'}},
{block: 'i-api-request', elems: ['debounce']},
{block: 'i-page'},
{block: 'i-ycssjs'}
]
2 changes: 1 addition & 1 deletion tests/simple/simple.bemdecl.js
@@ -1,5 +1,5 @@
exports.blocks = [
{block: 'i-router', mods: {init: 'auto'}},
{block: 'i-router'},
{block: 'i-content', mods: {type: 'bemhtml'}},
{block: 'i-api-request'},
{block: 'i-page'},
Expand Down

0 comments on commit 20a0aee

Please sign in to comment.