Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
252 additions
and
35 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
blocks/i-api-request/__debounce/i-api-request__debounce.common.tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
3 changes: 3 additions & 0 deletions
3
tests.blocks/i-api-request/__debounce/i-api-request__debounce.deps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
({ | ||
shouldDeps: {block: 'i-test-api-debounced'} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
({ | ||
shouldDeps: [ | ||
{block: 'tests'}, | ||
{block: 'i-test-api'}, | ||
{block: 'fake-api-server'} | ||
] | ||
}) |
3 changes: 3 additions & 0 deletions
3
tests.blocks/i-test-api-debounced/i-test-api-debounced.common.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
BEM.decl({block: 'i-test-api-debounced', baseBlock: 'i-api-request'}, null, { | ||
_debounceParams: ['id'] | ||
}); |
6 changes: 6 additions & 0 deletions
6
tests.blocks/i-test-api-debounced/i-test-api-debounced.deps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
({ | ||
mustDeps: [ | ||
{block: 'i-api-request', elems: 'debounce'}, | ||
{block: 'i-ajax-proxy'} | ||
] | ||
}) |
4 changes: 4 additions & 0 deletions
4
tests.blocks/i-test-api-debounced/i-test-api-debounced.priv.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters