Home Reference Source

application/__tests__/components/layout/root-test.js

jest.unmock('./../../../util/events');
jest.unmock('./../../../components/layout/root');

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 uuid from 'uuid';

import RootExtension from './../../../components/layout/root';
import Events from './../../../util/events';

describe('RootExtension', () => {

    beforeEach(() => {
        // it's like the clock is shadow installed or something...
        jasmine.clock().uninstall();
    });

    it('loads child on add', (done) => {
        const id = uuid.v4();
        const span = <span>Hi there</span>;
        const root = TestUtils.renderIntoDocument(
            <RootExtension />
        );
        const spy = sinon.spy(root, 'onAdd');
        root.componentWillMount();

        Events.emit('addRootComponent', id, span);

        setTimeout(() => {
            expect(spy.called).toEqual(true);
            root.onAdd.restore();
            done();
        }, 2);
    });

    it('removes child on remove', (done) => {
        const root = TestUtils.renderIntoDocument(
            <RootExtension />
        );
        const spy = sinon.spy(root, 'onAdd');
        const spyremove = sinon.spy(root, 'onRemove');
        root.componentWillMount();

        const id = uuid.v4();
        const span = <span>Hi there</span>;
        Events.emit('addRootComponent', id, span);

        const id2 = uuid.v4();
        const span2 = <span>Hi there</span>;
        Events.emit('addRootComponent', id2, span2);

        setTimeout(() => {
            expect(root.state.components.length).toEqual(2);
            Events.emit('removeRootComponent', id2);

            expect(spy.calledTwice).toEqual(true);
            expect(spyremove.calledOnce).toEqual(true);

            root.onAdd.restore();
            root.onRemove.restore();

            expect(root.state.components.length).toEqual(1);
            done();
        }, 2);
    });

});