Home Reference Source

application/__tests__/components/common/rich-editor/media-search-test.js

jest.unmock('./../../../../components/common/rich-editor/media-search');
jest.unmock('./../../../../components/common/rich-editor/media-results');
jest.unmock('./../../../../components/common/rich-editor/media-result');
jest.unmock('./../../../../components/common/loading-indicator');
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 alt from './../../../../alt';
import {ContentFlux} from './../../../../services/content-service';

import MediaSearch from './../../../../components/common/rich-editor/media-search';
import MediaResults from './../../../../components/common/rich-editor/media-results';
import MediaResult from './../../../../components/common/rich-editor/media-result';
import LoadingIndicator from './../../../../components/common/loading-indicator';
import Paginator from './../../../../components/common/paginator';

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

describe('MediaSearchEditorComponent', () => {

    beforeEach(() => {
        jasmine.clock().uninstall();
        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('loads empty results', () => {
        const el = TestUtils.renderIntoDocument(<MediaSearch />);

        const results = TestUtils.findRenderedComponentWithType(el, MediaResults);
        expect(results).not.toBeUndefined();
    });

    it('fires search', (done) => {
        const el = TestUtils.renderIntoDocument(<MediaSearch />);

        const items = TestUtils.scryRenderedComponentsWithType(el, MediaResult);
        expect(items.length).toEqual(0);

        el.refs.keywords.value = 'franch';
        el.refs.type.value = 'image';

        const btn = TestUtils.findRenderedDOMComponentWithTag(el, 'button');
        TestUtils.Simulate.click(btn, {});

        setTimeout(() => {
            const items = TestUtils.scryRenderedComponentsWithType(el, MediaResult);
            expect(items.length).toBeGreaterThan(0);
            done();
        }, 500);
    });

    it('fires paginator', () => {
        const el = TestUtils.renderIntoDocument(<MediaSearch />);

        const items = TestUtils.scryRenderedComponentsWithType(el, MediaResult);
        expect(items.length).toEqual(0);

        setTimeout(() => {
            const pag = TestUtils.findRenderedComponentWithType(el, Paginator);
            const pageLinks = TestUtils.scryRenderedDOMComponentsWithTag(
                pag, 'a'
            );

            const paginateSpy = sinon.spy(el, 'handlePagination');

            TestUtils.Simulate.click(pageLinks[1], {});
            expect(paginateSpy.called).toEqual(true);
        });
    });
});