@dasch-swiss/dsp-js
Version:
JavaScript library that handles API requests to Knora
82 lines • 3.88 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { JsonConvert, JsonObject, JsonProperty, OperationMode, ValueCheckingMode } from "json2typescript";
import { ApiResponseData } from "./api-response-data";
describe("Test class ApiResponseData", function () {
// Setup json convert instance
var jsonConvert = new JsonConvert(OperationMode.ENABLE, ValueCheckingMode.DISALLOW_NULL, false);
describe("Test method fromAjaxResponse()", function () {
// Setup a test class for responses
var Person = /** @class */ (function () {
function Person() {
this.id = 0;
this.name = "";
}
__decorate([
JsonProperty("id", Number),
__metadata("design:type", Number)
], Person.prototype, "id", void 0);
__decorate([
JsonProperty("name", String),
__metadata("design:type", String)
], Person.prototype, "name", void 0);
Person = __decorate([
JsonObject("Person")
], Person);
return Person;
}());
// Generate data
var ajaxResponses = [
{
request: { method: "GET", url: "http://localhost" },
xhr: { status: 200 },
response: { id: 123, name: "Test" }
},
{
request: { method: "GET", url: "http://knora.org" },
xhr: { status: 201 },
response: { id: 1337, name: "Gaga" }
},
{
request: { method: "PUT", url: "htt://0.0.0.0" },
xhr: { status: 200 },
response: { id: 1893, name: "Hello" }
},
{
request: { method: "GET", url: "https://dasch.swiss" },
xhr: { status: 200 },
response: { id: 0, name: "World" }
},
{
request: { method: "POST", url: "https://salsah.org" },
xhr: { status: 500 },
response: { id: 1, name: "Earth" }
}
];
it("should verify parameters", function () {
ajaxResponses.forEach(function (ajaxResponse) {
var apiResponseData = ApiResponseData.fromAjaxResponse(ajaxResponse, Person, jsonConvert);
// should match request method and url
expect(apiResponseData.url).toBe(ajaxResponse.request.url);
expect(apiResponseData.method).toBe(ajaxResponse.request.method);
// should match response status
expect(apiResponseData.status).toBe(ajaxResponse.xhr.status);
// should match response object
expect(apiResponseData.response).toEqual(ajaxResponse);
// should match response body
var person = new Person();
person.id = ajaxResponse.response.id;
person.name = ajaxResponse.response.name;
expect(apiResponseData.body).toEqual(person);
});
});
});
});
//# sourceMappingURL=api-response-data.spec.js.map