Skip to content

Commit

Permalink
Refactoring, post integration, testing
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
matteofigus committed Feb 9, 2016
1 parent e2bd5d9 commit ca9e7ba
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 288 deletions.
2 changes: 1 addition & 1 deletion client/src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = function(options, callback){
callbackDone = true;
return callback('timeout');
}
}, 1000 * options.timeout);
}, 1000 * timeout);

var req = require(httpProtocol).request(requestData).on('response', function(response) {

Expand Down
288 changes: 1 addition & 287 deletions test/unit/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,7 @@ describe('client', function(){
validatorStub = sinon.stub();
Client = injectr('../../client/src/index.js', {
'./validator': { validateConfiguration: validatorStub }
}, { __dirname: '/something/' });
};

var executeWithServerEndpointOnly = function(fn){
return execute({ serverRendering: 'http://components.company.com' }, fn);
};

var executeWithClientEndpointOnly = function(fn){
return execute({ clientRendering: 'https://components.com/' }, fn);
};

var execute = function(endpoints, fn){
if(typeof(endpoints) === 'function'){
fn = endpoints;
endpoints = {
clientRendering: 'https://components.com/',
serverRendering: 'http://components.company.com'
};
}

client = new Client({
registries: endpoints,
components: {
hello: '1.2.3'
}
});
fn();
}, { __dirname: '/something/', console: console });
};

describe('when not correctly initialised', function(){
Expand All @@ -56,264 +30,4 @@ describe('client', function(){
expect(init).to.throw('argh!');
});
});

describe('when initialised with client-side rendering endpoint only', function(){
describe('when rendering the component on the client-side', function(){
describe('when client-side failover enabled', function(){
before(function(done){
initialise();

validatorStub.returns({ isValid: true });
requestStub.onCall(0).yields('error');
readFileStub.yields(null, 'document.write("hi");');

executeWithClientEndpointOnly(function(){
client.renderComponent('hello', {
headers: { 'accept-language': 'es-MX' },
params: { name: 'matt'},
render: 'client'
}, function(err, res){
error = err;
response = res;
done();
});
});
});

it('should include client-side tag using clientRendering baseUrl', function(){
var $ocComponent = cheerio.load(response)('oc-component');
expect($ocComponent.attr('href')).to.equal('https://components.com/hello/1.2.3/?name=matt');
});

it('should not include oc-client javascript library', function(){
var $script = cheerio.load(response)('script');
expect($script.length).to.be.empty;
});

it('should not respond with error', function(){
expect(error).to.be.null;
});
});

describe('when client-side failover disabled', function(){
before(function(done){
initialise();

validatorStub.returns({ isValid: true });
requestStub.onCall(0).yields('error');
readFileStub.yields(null, 'document.write("hi");');

executeWithClientEndpointOnly(function(){
client.renderComponent('hello', {
headers: { 'accept-language': 'es-MX' },
params: { name: 'matt'},
disableFailoverRendering: true,
render: 'client'
}, function(err, res){
error = err;
response = res;
done();
});
});
});

it('should include client-side tag using clientRendering baseUrl', function(){
var $ocComponent = cheerio.load(response)('oc-component');
expect($ocComponent.attr('href')).to.equal('https://components.com/hello/1.2.3/?name=matt');
});

it('should not include oc-client javascript library', function(){
var $script = cheerio.load(response)('script');
expect($script.length).to.be.empty;
});

it('should not respond with error', function(){
expect(error).to.be.null;
});
});
});

describe('when rendering the component on the server-side', function(){
describe('when client-side failover enabled', function(){
before(function(done){
initialise();

validatorStub.returns({ isValid: true });
requestStub.onCall(0).yields('error');
readFileStub.yields(null, 'document.write("hi");');

executeWithClientEndpointOnly(function(){
client.renderComponent('hello', {
headers: { 'accept-language': 'es-MX' },
params: { name: 'matt'}
}, function(err, res){
error = err;
response = res;
done();
});
});
});

it('should include client-side failover tag using clientRendering baseUrl', function(){
var $ocComponent = cheerio.load(response)('oc-component');
expect($ocComponent.attr('href')).to.equal('https://components.com/hello/1.2.3/?name=matt');
});

it('should include oc-client javascript library', function(){
var $script = cheerio.load(response)('script');
expect($script.text()).to.equal('document.write("hi");');
});

it('should respond with error', function(){
expect(error).to.equal('Server-side rendering failed');
});
});

describe('when client-side failover disabled', function(){
before(function(done){
initialise();

validatorStub.returns({ isValid: true });
requestStub.onCall(0).yields('error');
readFileStub.yields(null, 'document.write("hi");');

executeWithClientEndpointOnly(function(){
client.renderComponent('hello', {
headers: { 'accept-language': 'es-MX' },
params: { name: 'matt'},
disableFailoverRendering: true
}, function(err, res){
error = err;
response = res;
done();
});
});
});

it('should respond with error', function(){
expect(error).to.equal('Server-side rendering failed');
});

it('should respond with blank html content', function(){
expect(response).to.be.empty;
});
});
});
});

describe('when initialised with server-side rendering endpoint only', function(){
describe('when rendering the component on the server fails', function(){
before(function(done){
initialise();

validatorStub.returns({ isValid: true });
requestStub.onCall(0).yields('error');
readFileStub.yields(null, 'document.write("hi");');

executeWithServerEndpointOnly(function(){
client.renderComponent('hello', {
headers: { 'accept-language': 'es-MX' },
params: { name: 'matt'}
}, function(err, res){
error = err;
response = res;
done();
});
});
});

it('should respond with error', function(){
expect(error).to.equal('Server-side rendering failed');
});

it('should respond with blank html content', function(){
expect(response).to.be.empty;
});
});
});

describe('when correctly initialised', function(){
before(function(){
initialise();
validatorStub.returns({ isValid: true });
});

describe('when rendering component on the server-side', function(){

describe('when server-side rendering fails', function(){

describe('when client-side failover is enabled', function(){
before(function(done){
initialise();

validatorStub.returns({ isValid: true });
requestStub.onCall(0).yields(null, JSON.stringify({
data: { name: 'john'},
name: 'hello',
template: {
key: 'hash',
src: 'https://cdn.com/template.js',
type: 'jade'
}
}));
requestStub.onCall(1).yields('error');
readFileStub.yields(null, 'document.write("hi");');

execute(function(){
client.renderComponent('hello', {
headers: { 'accept-language': 'es-MX' },
params: { name: 'matt'}
}, function(err, res){
response = res;
done();
});
});
});

it('should make server-side request using serverRendering baseUrl', function(){
expect(requestStub.args[0][0].url).to.equal('http://components.company.com/hello/1.2.3/?name=matt');
});

it('should include client-side failover tag using clientRendering baseUrl', function(){
var $ocComponent = cheerio.load(response)('oc-component');
expect($ocComponent.attr('href')).to.equal('https://components.com/hello/1.2.3/?name=matt');
});

it('should include oc-client javascript library', function(){
var $script = cheerio.load(response)('script');
expect($script.text()).to.equal('document.write("hi");');
});
});

describe('when client-side failover is disabled', function(){
before(function(done){
initialise();

validatorStub.returns({ isValid: true });
requestStub.onCall(0).yields('error');
readFileStub.yields(null, 'document.write("hi");');

execute(function(){
client.renderComponent('hello', {
headers: { 'accept-language': 'es-MX' },
params: { name: 'matt'},
disableFailoverRendering: true
}, function(err, res){
response = res;
done();
});
});
});

it('should make server-side request using serverRendering baseUrl', function(){
expect(requestStub.args[0][0].url).to.equal('http://components.company.com/hello/1.2.3/?name=matt');
});

it('shouldn\'t make client-side failover request', function(){
expect(requestStub.calledOnce).to.be.true;
});
});
});
});
});
});

0 comments on commit ca9e7ba

Please sign in to comment.