Skip to content
Snippets Groups Projects
Select Git revision
  • bdf7a2908fcfa531cda4685eb3c20e8d819305d5
  • master default protected
  • 1.31
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
  • 4.31.0
  • 4.30.1
  • 4.30.0
  • 4.29.1
  • 4.29.0
  • 4.28.0
  • 4.27.0
  • 4.26.0
  • 4.25.5
  • 4.25.4
  • 4.25.3
  • 4.25.2
  • 4.25.1
23 results

restapi.mjs

Blame
  • 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
            });
    
    
        })
    
    
    })