Home Reference Source

application/__tests__/components/common/delete-button-test.js

jest.unmock('./../../../components/common/delete-button');

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 DeleteButton from './../../../components/common/delete-button';

describe('DeleteButtonComponent', () => {
    let spy;

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

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

    it('loads default state', () => {
        const btn = TestUtils.renderIntoDocument(
            <DeleteButton />
        );
        const node = ReactDOM.findDOMNode(btn);

        expect(btn.props.disabled).not.toEqual(true);
    });

    it('loads disabled', () => {
        const btn = TestUtils.renderIntoDocument(
            <DeleteButton disabled={true} />
        );
        const node = ReactDOM.findDOMNode(btn);

        expect(btn.props.disabled).toEqual(true);
    });

    it('calls delete handler', () => {
        const handler = sinon.stub();

        const btn = TestUtils.renderIntoDocument(
            <DeleteButton onDelete={handler} />
        );

        const node = ReactDOM.findDOMNode(btn);
        TestUtils.Simulate.mouseDown(
            TestUtils.findRenderedDOMComponentWithTag(btn, 'button'),
            {}
        );
        jasmine.clock().tick(10000);

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

    it('cancels on mouse up', () => {
        const handler = sinon.stub();

        const btn = TestUtils.renderIntoDocument(
            <DeleteButton onDelete={handler} />
        );

        const node = ReactDOM.findDOMNode(btn);
        TestUtils.Simulate.mouseDown(
            TestUtils.findRenderedDOMComponentWithTag(btn, 'button'),
            {}
        );
        jasmine.clock().tick(2000);

        TestUtils.Simulate.mouseUp(
            TestUtils.findRenderedDOMComponentWithTag(btn, 'button'),
            {}
        );
        jasmine.clock().tick(10000);

        expect(handler.called).toEqual(false);
    });

    it('cancels on mouse pit', () => {
        const handler = sinon.stub();

        const btn = TestUtils.renderIntoDocument(
            <DeleteButton onDelete={handler} />
        );

        const node = ReactDOM.findDOMNode(btn);
        TestUtils.Simulate.mouseDown(
            TestUtils.findRenderedDOMComponentWithTag(btn, 'button'),
            {}
        );
        jasmine.clock().tick(2000);

        TestUtils.Simulate.mouseOut(
            TestUtils.findRenderedDOMComponentWithTag(btn, 'button'),
            {}
        );
        jasmine.clock().tick(10000);

        expect(handler.called).toEqual(false);
    });

});