Home Reference Source

application/__tests__/services/base-service-test.js

jest.unmock('./../../services/base-service');
jest.unmock('./../../util/mock-fetch');
jest.unmock('./../../util/strings');

jest.unmock('./../../alt');

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import Immutable from 'immutable';
import sinon from 'sinon';
import AltTestingUtils from 'alt-utils/lib/AltTestingUtils';

import alt from './../../alt';
import BaseService from './../../services/base-service';
import fetch from './../../util/mock-fetch';

describe('BaseService', () => {

    it('calls get', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }]));

        const base = new BaseService;

        base.get('/foo/fraz', {'content': 'fraz'}).then(() => {
            expect(window.fetch.called).toEqual(true);
            expect(window.fetch.getCalls()[0].args[0]).toEqual('/foo/fraz?content=fraz');

            const params = window.fetch.getCalls()[0].args[1];
            expect(params.headers.Authorization).toEqual('Basic ' + btoa('1:'));

            window.fetch.restore();
            done();
        });
    });

    it('calls post', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }]));

        const base = new BaseService;

        base.post('/foo/fraz', {'content': 'fraz'}).then(() => {
            expect(window.fetch.called).toEqual(true);
            expect(window.fetch.getCalls()[0].args[0]).toEqual('/foo/fraz');

            const params = window.fetch.getCalls()[0].args[1];
            expect(params.headers.Authorization).toEqual('Basic ' + btoa('1:'));
            expect(params.method).toEqual('post');
            expect(params.body).toEqual('{"content":"fraz"}');

            window.fetch.restore();
            done();
        });
    });

    it('calls post with params', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }]));

        const base = new BaseService;

        base.post('/foo/fraz', {'content': 'fraz'}, {'getparam': 'foo'}).then(() => {
            expect(window.fetch.called).toEqual(true);
            expect(window.fetch.getCalls()[0].args[0]).toEqual('/foo/fraz?getparam=foo');

            const params = window.fetch.getCalls()[0].args[1];
            expect(params.headers.Authorization).toEqual('Basic ' + btoa('1:'));
            expect(params.method).toEqual('post');
            expect(params.body).toEqual('{"content":"fraz"}');

            window.fetch.restore();
            done();
        });
    });

    it('calls put', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }]));

        const base = new BaseService;

        base.put('/foo/fraz', {'content': 'fraz'}).then(() => {
            expect(window.fetch.called).toEqual(true);
            expect(window.fetch.getCalls()[0].args[0]).toEqual('/foo/fraz');

            const params = window.fetch.getCalls()[0].args[1];
            expect(params.headers.Authorization).toEqual('Basic ' + btoa('1:'));
            expect(params.method).toEqual('put');
            expect(params.body).toEqual('{"content":"fraz"}');

            window.fetch.restore();
            done();
        });
    });

    it('calls delete', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }]));

        const base = new BaseService;

        base.delete('/foo/fraz').then(() => {
            expect(window.fetch.called).toEqual(true);
            expect(window.fetch.getCalls()[0].args[0]).toEqual('/foo/fraz');

            const params = window.fetch.getCalls()[0].args[1];
            expect(params.headers.Authorization).toEqual('Basic ' + btoa('1:'));
            expect(params.method).toEqual('delete');

            window.fetch.restore();
            done();
        });
    });

    it('throws exception on get', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }], 400));

        const base = new BaseService;

        base.get('/foo/fraz').then(() => {
            expect(window.fetch.called).toEqual(true);
        }).catch((e) => {
            expect(e);
            window.fetch.restore();
            done();
        });
    });

    it('throws exception on post', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }], 400));

        const base = new BaseService;

        base.post('/foo/fraz').then(() => {
            expect(window.fetch.called).toEqual(true);
        }).catch((e) => {
            expect(e);
            window.fetch.restore();
            done();
        });
    });

    it('throws exception on put', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }], 400));

        const base = new BaseService;

        base.put('/foo/fraz').then(() => {
            expect(window.fetch.called).toEqual(true);
        }).catch((e) => {
            expect(e);
            window.fetch.restore();
            done();
        });
    });

    it('throws exception on delete', (done) => {
        sinon.stub(window, 'fetch').returns(fetch([{
            id: 1,
            user_id: 2,
            content_id: 3,
            content: 'this is a test'
        }], 400));

        const base = new BaseService;

        base.delete('/foo/fraz').then(() => {
            expect(window.fetch.called).toEqual(true);
        }).catch((e) => {
            expect(e);
            window.fetch.restore();
            done();
        });
    });
});