Something went wrong on our end
Select Git revision
restapi.mjs
-
Volker Schukai authoredVolker Schukai authored
restapi.mjs 2.85 KiB
"use strict";
import {expect} from "chai"
import {RestAPI} from "../../../../../../application/source/data/datasource/server/restapi.mjs";
import {validateObject} from "../../../../../../application/source/types/validate.mjs";
describe('RestAPI', function () {
let fetchReference;
let returnStatus;
afterEach(() => {
globalThis['fetch'] = fetchReference;
});
beforeEach(() => {
returnStatus = 200;
fetchReference = globalThis['fetch'];
globalThis['fetch'] = function (options) {
return new Promise((resolve, reject) => {
resolve({
text: function () {
return new Promise((resolve, reject) => {
resolve(JSON.stringify({
a: "test"
}));
});
},
status: returnStatus
});
})
};
})
it('should instance of RestAPI ', function () {
expect(new RestAPI('https://monsterjs.org/assets/world.json')).to.be.instanceof(RestAPI)
});
describe('rw', function () {
it('read should return object', function (done) {
const ds = new RestAPI({url: 'https://monsterjs.org/assets/world.json'})
ds.read().then(data => {
validateObject(data);
done();
}).catch(e => done(e));
});
it('write should ', function (done) {
const ds = new RestAPI({
read: {
url: 'https://monsterjs.org/assets/world.json'
},
write: {
url: 'https://monsterjs.org/assets/world.json',
acceptedStatus: [99]
}
}
)
ds.write().then(data => {
done("should not be here");
}).catch(e => done());
});
});
describe('rw with errors', function () {
it('read should throw exception', function (done) {
returnStatus = 400; // fetch response with 400
const ds = new RestAPI({url: 'https://monsterjs.org/assets/world.json'})
ds.read().then(data => {
done("should not run.");
}).catch(e => done()); // should throw exception because returnStatus=400
});
it('write should ', function (done) {
returnStatus = 400; // fetch response with 400
const ds = new RestAPI({url: 'https://monsterjs.org/assets/world.json'}, {url: 'https://monsterjs.org/assets/world.json'})
ds.write().then(data => {
validateObject(data);
done("error");
}).catch(e => done()); // should throw exception because returnStatus=400
});
})
})