Skip to content

Commit ad69392

Browse files
author
guylabs
committed
Improve resources method to allow pass parameters in the resource object. Add tests for the resources method and scope the use strict call.
1 parent 89d439e commit ad69392

6 files changed

+336
-204
lines changed

src/angular-spring-data-rest.js

Lines changed: 205 additions & 162 deletions
Large diffs are not rendered by default.

test/angular-spring-data-rest._test-helper.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,25 @@ var beforeEachFunction = function () {
3737
*
3838
* @param {object} data the data object in which the resource key exists
3939
* @param {string} resourcesKey the resource key
40-
* @param {string} inLinkName the link name for which the correct link href must be used
40+
* @param {string} expectedUrl the expected url
41+
* @param {object} httpBackend the angular http backend object
42+
* @param {string|object} inResourceName the resource name or the resource object
43+
* @param {object} parameters the parameter object
4144
*/
42-
var spyOnResourceExecution = function (data, resourcesKey, inLinkName) {
43-
spyOn(data, resourcesKey);
45+
var expectResourceExecution = function (data, resourcesKey, expectedUrl, httpBackend, inResourceName, parameters) {
46+
// create resource name and parameters
47+
var resourceName = (inResourceName == undefined) ? "self" : inResourceName;
4448

45-
// create parameters
46-
var linkName = (inLinkName == undefined) ? "linkName" : inLinkName;
47-
var paramDefaults = {userId: 123, cardId: '@id'};
48-
var actions = { charge: {method: 'POST', params: {charge: true}} };
49+
// remove template parameters from url
50+
expectedUrl = expectedUrl.replace(/{.*}/g, '');
4951

50-
// call the resource method
51-
data[resourcesKey](linkName, paramDefaults, actions);
52+
// expect the url
53+
httpBackend.whenGET(expectedUrl).respond(200);
54+
httpBackend.expectGET(expectedUrl);
5255

53-
// expect that the resource method has been called with the given parameters
54-
expect(data[resourcesKey]).toHaveBeenCalledWith(linkName, paramDefaults, actions);
56+
// call the resource method
57+
data[resourcesKey](resourceName, parameters).get();
58+
httpBackend.flush();
5559
};
5660

5761
var mockData = function () {

test/angular-spring-data-rest.embedded.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ describe("the response with the embedded values", function () {
2626
expect(this.response[this.config.embedded.value][key][this.config.resourcesKey]).toBeDefined();
2727

2828
// the resource value must be a valid function with the given parameters
29-
spyOnResourceExecution(this.response[this.config.embedded.value][key], this.config.resourcesKey);
29+
expectResourceExecution(this.response[this.config.embedded.value][key], this.config.resourcesKey,
30+
this.response[this.config.embedded.value][key][this.config.links.key]["self"].href, this.httpBackend, "self");
3031
}
3132
});
3233

test/angular-spring-data-rest.links.test.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/angular-spring-data-rest.resource.test.js

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,120 @@
1-
describe("the resource property", function () {
1+
describe("the resources property", function () {
22

33
beforeEach(beforeEachFunction);
44

5-
it("must return the resources of the object", function () {
6-
this.rawResponse = mockData();
7-
this.response = new SpringDataRestAdapter(this.rawResponse);
5+
it("must contain the 'links key' key", function () {
6+
expect(this.response[this.config.links.key]).toBeDefined();
7+
});
8+
9+
it("must call the correct href url if a resource name is passed to the $resource method", function () {
10+
11+
// define the resource name and the correct resource href url
12+
var resourceName = "self";
13+
var resourceHref = "http://localhost:8080/categories";
14+
15+
// check if the underlying $resource method is called with the correct href url
16+
var expectedResult = {categoryId: '123'};
17+
this.httpBackend.whenGET(resourceHref).respond(200, expectedResult);
18+
this.httpBackend.expectGET(resourceHref);
19+
var result = this.response[this.config.resourcesKey](resourceName).get(function () {
20+
expect(result.categoryId).toEqual(expectedResult.categoryId);
21+
});
22+
this.httpBackend.flush();
23+
});
24+
25+
it("must call the correct href url if a resource object is passed to the $resource method", function () {
26+
27+
// define the resource object and the correct link href url
28+
var resourceObject = {
29+
'name': 'self'
30+
};
31+
var resourceHref = "http://localhost:8080/categories";
32+
33+
// check if the underlying $resource method is called with the correct href url
34+
var expectedResult = {categoryId: '123'};
35+
this.httpBackend.whenGET(resourceHref).respond(200, expectedResult);
36+
this.httpBackend.expectGET(resourceHref);
37+
var result = this.response[this.config.resourcesKey](resourceObject).get(function () {
38+
expect(result.categoryId).toEqual(expectedResult.categoryId);
39+
});
40+
this.httpBackend.flush();
41+
});
42+
43+
it("must throw an exception if a wrong resource object is given", function () {
44+
// expect that the resources method is present
45+
expect(this.response[this.config.resourcesKey]).toBeDefined();
46+
47+
var thisResponse = this.response;
48+
var thisResourceKey = this.config.resourcesKey;
49+
var thisHttpBackend = this.httpBackend;
50+
51+
// it must throw an exception if a wrong resource object is given
52+
expect(function () {
53+
expectResourceExecution(thisResponse, thisResourceKey,
54+
"expectedUrl", thisHttpBackend, {'wrongPropertyName': 'value'});
55+
}).toThrow("The provided resource object must contain a name property.");
56+
});
57+
58+
it("must not use any parameters if the resource object is a string and no parameters are given", function () {
59+
// expect that the resources method is present
60+
expect(this.response[this.config.resourcesKey]).toBeDefined();
61+
62+
// the parameters must not be used in the resources method
63+
expectResourceExecution(this.response, this.config.resourcesKey,
64+
this.response[this.config.links.key]["self"].href, this.httpBackend, "self")
65+
});
66+
67+
it("must use the given parameters if the resource object is a string", function () {
68+
// expect that the resources method is present
69+
expect(this.response[this.config.resourcesKey]).toBeDefined();
70+
71+
// the given parameters must be used in the resources method
72+
expectResourceExecution(this.response, this.config.resourcesKey,
73+
this.response[this.config.links.key]["self"].href + "?parameterName=parameterValue", this.httpBackend, "self",
74+
{'parameterName': 'parameterValue'});
75+
});
76+
77+
it("must use the resource name if the correct resource object is given", function () {
78+
// expect that the resources method is present
79+
expect(this.response[this.config.resourcesKey]).toBeDefined();
80+
81+
// the given resource name must be used
82+
expectResourceExecution(this.response, this.config.resourcesKey,
83+
this.response[this.config.links.key]["self"].href, this.httpBackend, {'name': 'self'})
84+
});
85+
86+
it("must use the correct resource object name and the given parameters", function () {
87+
// expect that the resources method is present
88+
expect(this.response[this.config.resourcesKey]).toBeDefined();
89+
90+
// the given parameters must be used in the resources method
91+
expectResourceExecution(this.response, this.config.resourcesKey,
92+
this.response[this.config.links.key]["self"].href + "?parameterName=parameterValue",
93+
this.httpBackend, {'name': 'self'}, {'parameterName': 'parameterValue'})
94+
});
95+
96+
it("must use the correct resource object name and parameters", function () {
97+
// expect that the resources method is present
98+
expect(this.response[this.config.resourcesKey]).toBeDefined();
99+
100+
// the given parameters must be used in the resources method
101+
expectResourceExecution(this.response, this.config.resourcesKey,
102+
this.response[this.config.links.key]["self"].href + "?parameterName=parameterValue",
103+
this.httpBackend, {'name': 'self', 'parameters': {'parameterName': 'parameterValue'}})
104+
});
8105

106+
it("must use the correct resource object name and merge the resource object parameters with the given parameters", function () {
107+
// expect that the resources method is present
108+
expect(this.response[this.config.resourcesKey]).toBeDefined();
109+
110+
// the given parameters must be used in the resources method
111+
expectResourceExecution(this.response, this.config.resourcesKey,
112+
this.response[this.config.links.key]["self"].href + "?objectParameterName=objectParameterValue&parameterName=parameterValue",
113+
this.httpBackend, {'name': 'self', 'parameters': {'objectParameterName': 'objectParameterValue'}},
114+
{'parameterName': 'parameterValue'})
115+
});
116+
117+
it("must return the resources of the object", function () {
9118
// expect that the resources method is present
10119
expect(this.response[this.config.resourcesKey]).toBeDefined();
11120

test/angular-spring-data-rest.response.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("the response", function () {
1717
expect(this.response[this.config.resourcesKey]).toBeDefined();
1818

1919
// the resource value must be a valid function with the given parameters
20-
spyOnResourceExecution(this.response, this.config.resourcesKey);
20+
expectResourceExecution(this.response, this.config.resourcesKey, this.response[this.config.links.key]["self"].href, this.httpBackend, "self");
2121
});
2222

2323
it("must retain all original object properties", function () {

0 commit comments

Comments
 (0)