Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added oc.build functionality and tests #50

Merged
merged 1 commit into from
Apr 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions components/oc-client/src/oc-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var oc = oc || {};

(function(head, $document, $window, debug){
(function(head, $document, $window){

oc.conf = oc.conf || {};
oc.cmd = oc.cmd || [];
Expand All @@ -26,7 +26,8 @@ var oc = oc || {};
MESSAGES_RETRIEVING = 'Unrendered component found. Trying to retrieve it...';

// The code
var headScripts = [],
var debug = oc.conf.debug || false,
headScripts = [],
$,
noop = function(){},
nav = $window.navigator.userAgent,
Expand All @@ -40,9 +41,7 @@ var oc = oc || {};
return console.log(msg);
},
info: function(msg){
if(debug){
return console.log(msg);
}
return !!debug ? console.log(msg) : false;
}
};

Expand Down Expand Up @@ -119,6 +118,46 @@ var oc = oc || {};
callback();
};

oc.build = function(options){

if(!options.baseUrl){
throw 'baseUrl parameter is required';
}

if(!options.name){
throw 'name parameter is required';
}

var withFinalSlash = function(s){
s = s || '';

if(s.slice(-1) !== '/'){
s += '/';
}

return s;
};

var href = withFinalSlash(options.baseUrl) + withFinalSlash(options.name);

if(!!options.version){
href += withFinalSlash(options.version);
}

if(!!options.parameters){
href += '?';
for(var parameter in options.parameters){
if(options.parameters.hasOwnProperty(parameter)){
href += parameter + '=' + options.parameters[parameter] + '&';
}
}
href = href.slice(0, -1);
}

return '<' + OC_TAG + ' href="' + href + '"></' + OC_TAG + '>';

};

oc.ready = function(callback){

if(initialised){
Expand Down Expand Up @@ -294,4 +333,4 @@ var oc = oc || {};

oc.ready(oc.renderUnloadedComponents);

})(head, document, window, false); // jshint ignore:line
})(head, document, window); // jshint ignore:line
105 changes: 105 additions & 0 deletions test/front-end/build-href.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
'use strict';

var execute = function(parameters){
return oc.build(parameters);
};

describe('oc-client : build', function(){

describe('when custom tags supported', function(){

describe('when building a component without baseUrl', function(){

var throwingFunction = function(){
return execute({ name: 'someName' });
};

it('should throw an error', function(){
expect(throwingFunction).toThrow('baseUrl parameter is required');
});
});

describe('when building a component without name', function(){

var throwingFunction = function(){
return execute({ baseUrl: 'http://www.opencomponents.com' });
};

it('should throw an error', function(){
expect(throwingFunction).toThrow('name parameter is required');
});
});

describe('when building a component with baseUrl, name', function(){

var result = execute({
baseUrl: 'http://www.components.com/v2',
name: 'myComponent'
});

it('should build the correct Href', function(){
expect(result).toEqual('<oc-component href="http://www.components.com/v2/myComponent/"></oc-component>');
});
});

describe('when building a component with baseUrl/, name', function(){

var result = execute({
baseUrl: 'http://www.components.com/v2/',
name: 'myComponent'
});

it('should build the correct Href', function(){
expect(result).toEqual('<oc-component href="http://www.components.com/v2/myComponent/"></oc-component>');
});
});

describe('when building a component with baseUrl, name, params', function(){

var result = execute({
baseUrl: 'http://www.components.com/v2',
name: 'myComponent',
parameters: {
hello: 'world',
integer: 123,
boo: true
}
});

it('should build the correct Href', function(){
expect(result).toEqual('<oc-component href="http://www.components.com/v2/myComponent/?hello=world&integer=123&boo=true"></oc-component>');
});
});

describe('when building a component with baseUrl, name, version', function(){

var result = execute({
baseUrl: 'http://www.components.com/v2',
name: 'myComponent',
version: '1.0.X'
});

it('should build the correct Href', function(){
expect(result).toEqual('<oc-component href="http://www.components.com/v2/myComponent/1.0.X/"></oc-component>');
});
});
});

describe('when building a component with baseUrl, name, params, version', function(){

var result = execute({
baseUrl: 'http://www.components.com/v2',
name: 'myComponent',
parameters: {
hello: 'world',
integer: 123,
boo: true
},
version: '1.2.3'
});

it('should build the correct Href', function(){
expect(result).toEqual('<oc-component href="http://www.components.com/v2/myComponent/1.2.3/?hello=world&integer=123&boo=true"></oc-component>');
});
});
});