Skip to content

Commit 5ee6fd1

Browse files
author
Romy Kusuma
committed
Add unit test
1. Create mock data 2. Add test cases to test infinite loop
1 parent dae715b commit 5ee6fd1

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

test/angular-spring-data-rest-provider.spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,51 @@ describe("the spring data rest adapter", function () {
107107
this.rootScope.$apply();
108108
});
109109

110+
it("must only fetch link once to avoid infinite loop", function () {
111+
var allLinks = this.config.fetchAllKey;
112+
var accidentHref = 'http://localhost:8080/api/reports/00001/accident';
113+
var reportHref = 'http://localhost:8080/api/accidents/00001/report';
114+
115+
this.httpBackend.whenGET(accidentHref).respond(200, mockDataAccident());
116+
this.httpBackend.expectGET(accidentHref);
117+
118+
this.httpBackend.whenGET(reportHref).respond(200, mockDataReport());
119+
this.httpBackend.expectGET(reportHref);
120+
121+
this.rawResponse = mockDataReport();
122+
SpringDataRestAdapter.process(this.rawResponse, allLinks, true).then(function (processedData) {
123+
// expect that accident will not fetched twice
124+
expect(processedData.accident).toBeDefined();
125+
expect(processedData.accident.report).toBeDefined();
126+
expect(processedData.accident.report.accident).not.toBeDefined();
127+
});
128+
129+
this.httpBackend.flush();
130+
this.rootScope.$apply();
131+
});
132+
133+
it("must only reinitialized the map when process called twice or more", function () {
134+
var allLinks = this.config.fetchAllKey;
135+
var accidentHref = 'http://localhost:8080/api/reports/00001/accident';
136+
var reportHref = 'http://localhost:8080/api/accidents/00001/report';
137+
138+
this.httpBackend.whenGET(accidentHref).respond(200, mockDataAccident());
139+
this.httpBackend.expectGET(accidentHref);
140+
141+
this.httpBackend.whenGET(reportHref).respond(200, mockDataReport());
142+
this.httpBackend.expectGET(reportHref);
143+
144+
this.rawResponse = mockDataReport();
145+
SpringDataRestAdapter.process(this.rawResponse, allLinks, true).then(function (processedData) {
146+
SpringDataRestAdapter.process(mockDataReport(), allLinks, true).then(function (processedData2) {
147+
// expect linkMap to be reinitialized after process method called twice
148+
expect(JSON.stringify(processedData)).toEqual(JSON.stringify(processedData2));
149+
});
150+
});
151+
152+
this.httpBackend.flush();
153+
this.rootScope.$apply();
154+
});
155+
110156
});
111157

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,30 @@ var mockWithRawEmbeddedValueTypes = function () {
366366
);
367367
};
368368

369+
var mockDataReport = function() {
370+
return angular.copy({
371+
"reportNumber": "00001",
372+
"_links": {
373+
"self": {
374+
"href": "http://localhost:8080/api/reports/00001"
375+
},
376+
"accident": {
377+
"href": "http://localhost:8080/api/reports/00001/accident"
378+
}
379+
}
380+
});
381+
};
382+
383+
var mockDataAccident = function() {
384+
return angular.copy({
385+
"accidentDate": "2015-07-05",
386+
"_links": {
387+
"self": {
388+
"href": "http://localhost:8080/api/accidents/00001"
389+
},
390+
"report": {
391+
"href": "http://localhost:8080/api/accidents/00001/report"
392+
}
393+
}
394+
});
395+
};

0 commit comments

Comments
 (0)