Skip to content

Commit

Permalink
Fixes specs
Browse files Browse the repository at this point in the history
  • Loading branch information
xcambar committed Mar 9, 2013
1 parent 78253a0 commit b8f8613
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
14 changes: 10 additions & 4 deletions src/resource.js
@@ -1,17 +1,23 @@
var parser = require('./parser'),
ResourceLinks = require('./links');
ResourceLinks = require('./links'),
utils = require('./utils');

function parseEmbedded(embedded) {
"use strict";
var parsed = {};
Object.keys(embedded).forEach(function (key) {
var typedResources = embedded[key];
parsed[key] = typedResources.map(parser);
parsed[key] = typedResources.map ? typedResources.map(parser) : typedResources;
});
return parsed;
}

function Resource(json, links, embedded) {
"use strict";
if (!utils.isObjectLiteral(json)) {
throw new Error('No object provided');
}

var embeddedResources = parseEmbedded(embedded || {});
var resourceLinks = new ResourceLinks(links);

Expand All @@ -23,8 +29,8 @@ function Resource(json, links, embedded) {
return key ? embeddedResources[key] : embeddedResources;
};

this.link = function (key) {
return key ? resourceLinks[key] : resourceLinks[key];
this.links = function (key) {
return key ? resourceLinks.get(key) : resourceLinks;
};
}

Expand Down
11 changes: 1 addition & 10 deletions test/specs/parser_spec.js
Expand Up @@ -8,15 +8,6 @@ describe("HAL-Parser", function () {
parser.should.be.a('function');
});

it("should throw an exception if no `_links` attribute is available", function () {
parser.bind(undefined, {}).should.throw(TypeError);
parser.bind(undefined, {}).should.throw(/no _links/i);
});

it("should return an instance of Resource", function () {
parser({_links: { self: "..." }}).should.be.instanceOf(Resource);
});

it("should be non-destructive to original json", function () {
var json = {
_links: {
Expand All @@ -29,7 +20,7 @@ describe("HAL-Parser", function () {
};

parser(json);
json.should.contain.keys(['_links', 'embedded', 'prop'])
json.should.contain.keys(['_links', 'embedded', 'prop']);
});
});

43 changes: 29 additions & 14 deletions test/specs/resource_spec.js
@@ -1,28 +1,43 @@
/*global it:true, describe:true */
var Resource = require('src/resource');
var LinkCollection = require('src/links');
var Link = require('src/link');
var parser = require('src/parser');

describe("Resources", function () {
"use strict";
it("should be a function", function () {
Resource.should.be.a('function');
});

it("can not be instantiated without links", function () {
var errorFn = function () {
new Resource({}, null, {});
};
errorFn.should.throw(Error);
it("MUST have a resource as its root object", function () {
parser({}).should.be.instanceOf(Resource);
parser.bind(undefined, []).should.throw(Error);
parser.bind(undefined).should.throw(Error);
parser.bind(undefined, "invalid").should.throw(Error);
});

it("OPTIONALLY has a _embedded property", function () {
var r1 = parser({});
r1.embedded().should.be.empty;

var _withEmbedded = {_embedded: {emb: {}}};
var r2 = parser(_withEmbedded);
r2.embedded().should.not.be.empty;
});

it("can be instantiated only with _self link", function () {
var errorFn = function () {
new Resource({}, {}, null);
};
errorFn.should.throw(Error);
it("OPTIONALLY has a _links property", function () {
var r1 = parser({});
r1.links().should.be.empty;

var goodFn = function () {
new Resource({}, {self: 'dummy'});
};
goodFn.should.not.throw(Error);
var r2 = parser({_links: {self: '..'}});
r2.links().should.be.instanceOf(LinkCollection);
});

it("should have Link instances as link properties", function () {
var r2 = parser({_links: {self: '..'}});
r2.links('self').should.be.instanceOf(Link);
});

});

0 comments on commit b8f8613

Please sign in to comment.