Home Reference Source

application/__tests__/components/content-test.js

jest.unmock('./../../components/content');
jest.unmock('./../../components/common/paginator');
jest.unmock('./../../services/content-service');
jest.unmock('./../../services/base-service');
jest.unmock('./../../services/error-service');
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 Content from './../../components/content';
import ContentTable from './../../components/content/content-table';
import LoadingIndicator from './../../components/common/loading-indicator';
import Paginator from './../../components/common/paginator';
import {ContentService, ContentStore} from './../../services/content-service';

import fetch from './../../util/mock-fetch';

const altMockFetch = (url, opts) => {
    let data = {
        '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
    };

    if (url && url.indexOf('page=2') !== -1) {
        data.items = [
            {
                id: 1,
                uuid: '123-456',
                attachment: {}
            }
        ];
    }

    return new Promise((resolve, reject) => {
        process.nextTick(() => {
            return resolve(
                {
                    status: 200,
                    json: () => {
                        return data;
                    }
                }
            );
        });
    });
}

describe('ContentComponent', () => {

    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('shows loading indicator', () => {
        const content = TestUtils.renderIntoDocument(
            <Content />
        );
        const node = ReactDOM.findDOMNode(content);

        const loader = TestUtils.scryRenderedComponentsWithType(content, LoadingIndicator);
        expect(loader.length).toEqual(1);
    });

    it('calls content service fetch', () => {
        const spy = sinon.spy(ContentService, 'fetch');

        const content = TestUtils.renderIntoDocument(
            <Content />
        );

        content.componentWillMount();

        expect(spy.called).toEqual(true);

        ContentService.fetch.restore();
    });

    it('loads content from service', (done) => {
        const content = TestUtils.renderIntoDocument(
            <Content />
        );
        sinon.stub(content, 'componentWillUpdate').returns(true);
        content.componentWillMount();

        jasmine.clock().uninstall();
        setTimeout(() => {
            const table = TestUtils.findRenderedComponentWithType(
                content, ContentTable
            );

            expect(table).not.toBeUndefined();
            content.componentWillUpdate.restore();
            done();
        }, 100);
    });

    it('paginates content', (done) => {
        const oldFetch = window.fetch;
        window.fetch = altMockFetch;

        const content = TestUtils.renderIntoDocument(
            <Content />
        );
        sinon.stub(content, 'componentWillUpdate').returns(true);
        content.componentWillMount();

        jasmine.clock().uninstall();
        setTimeout(() => {
            expect(content.state.contents.size).toEqual(2);

            ContentService.fetch({page:2});

            setTimeout(() => {
                content.componentWillUpdate.restore();
                expect(content.state.contents.size).toEqual(1);

                window.fetch = oldFetch;
                done();
            }, 100);

        }, 100);
    })

});