Home Reference Source

application/__tests__/components/common/rich-editor/link-test.js

jest.unmock('./../../../../components/common/rich-editor/link');
jest.mock('draft-js');

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 Link from './../../../../components/common/rich-editor/link';
import {Entity} from 'draft-js';

describe('LinkEditorComponent', () => {
    let windowStub;

    beforeEach(() => {
        jasmine.clock().uninstall();
        jasmine.clock().install();

    });

    afterEach(() => {
        jasmine.clock().uninstall();
    })

    it('loads default state', () => {
        const el = TestUtils.renderIntoDocument(<Link />);
        el.componentWillMount();

        expect(el.state.editMode).toEqual(true);
    });

    it('loads demo url', () => {
        const el = TestUtils.renderIntoDocument(<Link />);
        el.componentWillMount();
        el.refs.content.value = 'http://statenews.com';

        windowStub = sinon.stub(window, 'open');

        const link = TestUtils.findRenderedDOMComponentWithClass(
            el, 'fa-link'
        );

        TestUtils.Simulate.click(link, {});

        expect(windowStub.called).toEqual(true);
    });

    it('handles enter key', () => {
        const el = TestUtils.renderIntoDocument(<Link />);
        el.componentWillMount();
        el.refs.content.value = 'http://statenews.com';

        const input = TestUtils.findRenderedDOMComponentWithTag(
            el, 'input'
        );

        const inputSpy = sinon.spy(el, 'confirm');

        TestUtils.Simulate.keyDown(input, {which: 13});

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

        el.confirm.restore();
    });

    it('handles escape key', () => {
        const el = TestUtils.renderIntoDocument(<Link />);
        el.componentWillMount();
        el.refs.content.value = 'http://statenews.com';

        const input = TestUtils.findRenderedDOMComponentWithTag(
            el, 'input'
        );

        const inputSpy = sinon.spy(el, 'cancel');

        TestUtils.Simulate.keyDown(input, {which: 27});

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

        el.cancel.restore();
    });

    it('sets fetches url from data', () => {
        sinon.stub(Entity.props, 'getData').returns({url: 'http://statenews.com'});

        const el = TestUtils.renderIntoDocument(<Link entityKey={true} />);
        el.componentWillMount();

        expect(el.state.editMode).toEqual(false);
        Entity.props.getData.restore();
    });

    it('renders correct URL in edit mode', () => {
        sinon.stub(Entity.props, 'getData').returns({url: 'http://statenews.com'});

        const el = TestUtils.renderIntoDocument(<Link entityKey={true} />);
        el.componentWillMount();

        el.setState({'editMode': true});

        const input = TestUtils.findRenderedDOMComponentWithTag(el, 'input');
        expect(input);

        const node = ReactDOM.findDOMNode(input);
        expect(node.value).toEqual('http://statenews.com');

        Entity.props.getData.restore();
    });

});