UNPKG

shaman-website-compiler

Version:

Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.

125 lines 6.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("mocha"); var sinon = require("sinon"); var http = require("http"); var https = require("https"); var chai_1 = require("chai"); var http_adapter_1 = require("./http.adapter"); var query_model_1 = require("../models/query-model"); describe('HttpAdapter', function () { var sandbox = sinon.createSandbox(); afterEach(function () { sandbox.restore(); }); it('openConnection should return empty promise', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: '/' }); adapter.openConnection().then(done); }); it('run should return an error if the request returns an error', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: '/' }); var request = { on: function (_, cb) { return cb(new Error("testing")); } }; sandbox.stub(http, 'get').returns(request); adapter.run(new query_model_1.QueryModel()).catch(function (_) { return done(); }); }); it('run should return an error if response code < 200', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: '/' }); var response = { statusCode: 100 }; sandbox.stub(http, 'get').yields(response); adapter.run(new query_model_1.QueryModel()).catch(function (_) { return done(); }); }); it('run should return an error if response code > 299', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: '/' }); var response = { statusCode: 400 }; sandbox.stub(http, 'get').yields(response); adapter.run(new query_model_1.QueryModel()).catch(function (_) { return done(); }); }); it('run (http) should return an object', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: '/' }); var response = { statusCode: 200, on: sandbox.stub() }; response.on.withArgs('data').yields('[{}]'); response.on.withArgs('end').yields(null); sandbox.stub(http, 'get').yields(response); adapter.run(new query_model_1.QueryModel()).then(function (result) { (0, chai_1.expect)(result.length).to.equal(1); done(); }); }); it('run (https) should return an object', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: 'https://localhost/' }); var response = { statusCode: 200, on: sandbox.stub() }; response.on.withArgs('data').yields('[{}]'); response.on.withArgs('end').yields(null); sandbox.stub(https, 'get').yields(response); adapter.run(new query_model_1.QueryModel()).then(function (result) { (0, chai_1.expect)(result.length).to.equal(1); done(); }); }); it('run (https) should use query.args.name to return object', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: 'https://localhost/' }); var response = { statusCode: 200, on: sandbox.stub() }; response.on.withArgs('data').yields('{"products": [{}]}'); response.on.withArgs('end').yields(null); sandbox.stub(https, 'get').yields(response); var query = new query_model_1.QueryModel(); query.args = { name: 'products' }; adapter.run(query).then(function (result) { (0, chai_1.expect)(result.length).to.equal(1); done(); }); }); it('run should sort objects in ascending order', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: '/' }); var response = { statusCode: 200, on: sandbox.stub() }; response.on.withArgs('data').yields('[{"title": "a"},{"title": "b"}]'); response.on.withArgs('end').yields(null); sandbox.stub(http, 'get').yields(response); var query = new query_model_1.QueryModel(); query.sort = { key: 'title' }; adapter.run(query).then(function (result) { (0, chai_1.expect)(result[0].title).to.equal('a'); done(); }); }); it('run should sort objects in descending order', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: '/' }); var response = { statusCode: 200, on: sandbox.stub() }; response.on.withArgs('data').yields('[{"title": "a"},{"title": "b"}]'); response.on.withArgs('end').yields(null); sandbox.stub(http, 'get').yields(response); var query = new query_model_1.QueryModel(); query.sort = { key: 'title', descending: true }; adapter.run(query).then(function (result) { (0, chai_1.expect)(result[0].title).to.equal('b'); done(); }); }); it('run should limit result length', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: '/' }); var response = { statusCode: 200, on: sandbox.stub() }; response.on.withArgs('data').yields('[{"title": "a"},{"title": "b"}]'); response.on.withArgs('end').yields(null); sandbox.stub(http, 'get').yields(response); var query = new query_model_1.QueryModel(); query.limit = 1; adapter.run(query).then(function (result) { (0, chai_1.expect)(result.length).to.equal(1); done(); }); }); it('run should not limit results if limit is greater than length', function (done) { var adapter = new http_adapter_1.HttpAdapter({ apiBaseUri: '/' }); var response = { statusCode: 200, on: sandbox.stub() }; response.on.withArgs('data').yields('[{"title": "a"},{"title": "b"}]'); response.on.withArgs('end').yields(null); sandbox.stub(http, 'get').yields(response); var query = new query_model_1.QueryModel(); query.limit = 3; adapter.run(query).then(function (result) { (0, chai_1.expect)(result.length).to.equal(2); done(); }); }); }); //# sourceMappingURL=http.adapter.spec.js.map