Home Reference Source

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

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

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 {ContentService, ContentStore} from './../../services/content-service';
import fetch from './../../util/mock-fetch';

describe('ContentService create', () => {
    let spyPost;
    let spyUpdate;

    beforeEach((done) => {

        sinon.stub(window, 'fetch').returns(fetch({
            'items': [
                {
                    id: 1,
                    uuid: '123-456',
                    attachment: {}
                },
                {
                    id: 2,
                    uuid: '987-654',
                    attachment: {}
                }
            ],
            'first': 1,
            'before': 1,
            'current': 1,
            'last': 1,
            'next': 1,
            'total_pages': 1,
            'total_items': 1,
            'limit': 1
        }));

        spyPost = sinon.spy(ContentService, 'post');
        spyUpdate = sinon.spy(ContentService, 'updateContent');
        ContentService.create({'foo': 'bar'}).then(() => done());

    });

    afterEach(() => {
        ContentService.post.restore();
        ContentService.updateContent.restore();
        window.fetch.restore();
    });

    it('expects create to call post', () => {
        expect(spyPost.called).toEqual(true);
    });

    it('expects create to call updateContent', () => {
        expect(spyUpdate.called).toEqual(true);
        expect(ContentStore.getState().content.size).toBeGreaterThan(0);
    });

});

describe('ContentService update', () => {
    let spyPut;
    let spyUpdate;

    beforeEach((done) => {
        sinon.stub(window, 'fetch').returns(fetch({
            'items': [
                {
                    id: 1,
                    uuid: '123-456',
                    attachment: {}
                },
                {
                    id: 2,
                    uuid: '987-654',
                    attachment: {}
                }
            ],
            'first': 1,
            'before': 1,
            'current': 1,
            'last': 1,
            'next': 1,
            'total_pages': 1,
            'total_items': 1,
            'limit': 1
        }));

        spyPut = sinon.spy(ContentService, 'put');
        spyUpdate = sinon.spy(ContentService, 'updateContent');

        ContentService.update('123', {'foo': 'bar'}).then(() => done());

    });

    afterEach(() => {
        ContentService.put.restore();
        ContentService.updateContent.restore();
        window.fetch.restore();
    });

    it('expects update to call put', () => {
        expect(spyPut.called).toEqual(true);
    });

    it('expects update to call updateContent', () => {
        expect(spyUpdate.called).toEqual(true);
        expect(ContentStore.getState().content.size).toBeGreaterThan(0);
    });

});

describe('ContentService fetch', () => {
    let spyGet;
    let spyUpdate;
    let spyUpdatePagination;

    beforeEach((done) => {
        sinon.stub(window, 'fetch').returns(fetch({
            'items': [
                {
                    id: 1,
                    uuid: '123-456',
                    attachment: {}
                },
                {
                    id: 2,
                    uuid: '987-654',
                    attachment: {}
                }
            ],
            'first': 1,
            'before': 1,
            'current': 1,
            'last': 1,
            'next': 1,
            'total_pages': 1,
            'total_items': 1,
            'limit': 1
        }));

        spyGet = sinon.spy(ContentService, 'get');
        spyUpdate = sinon.spy(ContentService, 'updateContents');
        spyUpdatePagination = sinon.spy(ContentService, 'updatePagination');

        ContentService.fetch().then(() => done());

    });

    afterEach(() => {
        ContentService.get.restore();
        ContentService.updateContents.restore();
        ContentService.updatePagination.restore();
        window.fetch.restore();
    });

    it('expects fetch to call get', () => {
        expect(spyGet.called).toEqual(true);
    });

    it('expects fetch to call updateContents', () => {
        expect(spyUpdate.called).toEqual(true);
    });

    it('expects fetch to call updatePagination', () => {
        expect(spyUpdatePagination.called).toEqual(true);
    });

});

describe('ContentService fetchOne', () => {
    let spyGet;
    let spyUpdate;

    beforeEach(() => {
        sinon.stub(window, 'fetch').returns(fetch({
            'items': [
                {
                    id: 1,
                    uuid: '123-456',
                    attachment: {}
                },
                {
                    id: 2,
                    uuid: '987-654',
                    attachment: {}
                }
            ],
            'first': 1,
            'before': 1,
            'current': 1,
            'last': 1,
            'next': 1,
            'total_pages': 1,
            'total_items': 1,
            'limit': 1
        }));

        spyGet = sinon.spy(ContentService, 'get');
        spyUpdate = sinon.spy(ContentService, 'updateContent');
    });

    afterEach(() => {
        ContentService.get.restore();
        ContentService.updateContent.restore();
        window.fetch.restore();
    });

    it('expects fetch to call get', (done) => {
        return ContentService.fetchOne('123').then(() => {
            expect(spyGet.calledOnce).toEqual(true);
            return done();
        });

    });

    it('expects fetch to call updateContent', (done) => {
        return ContentService.fetchOne('123').then(() => {
            expect(spyUpdate.calledTwice).toEqual(true);
            return done();
        });

    });

    it('expects fetch force to call updateContent once', (done) => {
        return ContentService.fetchOne('123', null, false).then(() => {
            expect(spyUpdate.calledOnce).toEqual(true);
            return done();
        });
    });
});

describe('ContentService remove', () => {
    let spyDelete;

    beforeEach(() => {
        sinon.stub(window, 'fetch').returns(fetch({
            'items': [
                {
                    id: 1,
                    uuid: '123-456',
                    attachment: {}
                },
                {
                    id: 2,
                    uuid: '987-654',
                    attachment: {}
                }
            ],
            'first': 1,
            'before': 1,
            'current': 1,
            'last': 1,
            'next': 1,
            'total_pages': 1,
            'total_items': 1,
            'limit': 1
        }));
        spyDelete = sinon.spy(ContentService, 'delete');
    });

    afterEach(() => {
        ContentService.delete.restore();
        window.fetch.restore();
    });

    it('expects delete to be called once', (done) => {
        return ContentService.remove('123').then(() => {
            expect(spyDelete.calledOnce).toEqual(true);
            return done();
        });
    });
});

describe('ContentService search', () => {
    let spyGet;
    let spyUpdate;
    let spyUpdatePagination;

    beforeEach(() => {
        sinon.stub(window, 'fetch').returns(fetch({
            'items': [
                {
                    id: 1,
                    uuid: '123-456',
                    attachment: {}
                },
                {
                    id: 2,
                    uuid: '987-654',
                    attachment: {}
                }
            ],
            'first': 1,
            'before': 1,
            'current': 1,
            'last': 1,
            'next': 1,
            'total_pages': 1,
            'total_items': 1,
            'limit': 1
        }));
        spyGet = sinon.spy(ContentService, 'get');
        spyUpdate = sinon.spy(ContentService, 'updateContents');
        spyUpdatePagination = sinon.spy(ContentService, 'updatePagination');
    });

    afterEach(() => {
        ContentService.get.restore();
        ContentService.updateContents.restore();
        ContentService.updatePagination.restore();
        window.fetch.restore();
    });

    it('expects search to call get', (done) => {
        return ContentService.search(null).then(() => {
            expect(spyGet.calledOnce).toEqual(true);
            return done();
        });
    });

    it('expects search to call updateContents', (done) => {
        return ContentService.search(null).then(() => {
            expect(spyUpdate.calledOnce).toEqual(true);
            return done();
        });
    });

    it('expects search to call updatePagination', (done) => {
        return ContentService.search(null).then(() => {
            expect(spyUpdatePagination.calledOnce).toEqual(true);
            return done();
        });
    });
});

describe('ContentStore', () => {
    beforeEach(() => {
        sinon.stub(window, 'fetch').returns(fetch({
            'items': [
                {
                    id: 1,
                    uuid: '123-456',
                    attachment: {}
                },
                {
                    id: 2,
                    uuid: '987-654',
                    attachment: {}
                }
            ],
            'first': 1,
            'before': 1,
            'current': 1,
            'last': 1,
            'next': 1,
            'total_pages': 1,
            'total_items': 1,
            'limit': 1
        }));

    });


    afterEach(() => {
        window.fetch.restore();
    });

    it('expects state is updated on fetchOne', (done) => {
        return ContentService.fetchOne('123').then(() => {
            expect(ContentStore.getState().content.size).toBeGreaterThan(0);
            return done();
        });
    });

    it('expects state is updated on fetch', (done) => {
        return ContentService.fetch().then(() => {
            expect(ContentStore.getState().contents.size).toBeGreaterThan(0);
            expect(ContentStore.getState().pagination.size).toBeGreaterThan(0);
            return done();
        });
    });
});