Home Reference Source

application/__tests__/hocs/draftable-test.js

jest.unmock('./../../hocs/draftable');
jest.unmock('./../../services/base-service');
jest.unmock('./../../services/error-service');
jest.unmock('./../../services/draft-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 {Input} from 'react-materialize';

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

import {DraftService, DraftsStore} from './../../services/draft-service';
import {withDraftable} from './../../hocs/draftable';

class MockChild extends React.Component {
    constructor(props) {
        super(props);
    }

    draftCb() {
        return;
    }

    updateCb() {
        return;
    }

    componentWillMount() {
        this.props.draftable.doesHaveDraft({'srn': 'tsn:123'}, this.draftCb);
        this.props.draftable.setDraftCreatedHook(this.updateCb);
    }

    componentWillUnmount() {

    }

    render() {
        return (
            <div className='internalObject'></div>
        );
    }
}
const MockWrapped = withDraftable(MockChild);

describe('DraftableComponent', () => {

    beforeEach(() => {
        jasmine.clock().uninstall();
        jasmine.clock().install();
        sinon.stub(window, 'fetch').returns(fetch([
            {
                id: 1,
                srn: 'srn:tsn:ceo-core:content/5432543-54325432',
                uuid: '5432543-54325432',
                slug: 'imma-test-slug',
                created_at: '2016-05-09 15:23:01',
                modified_at: '2016-05-09 15:34:16',
                published_at: null,
                version: 5
            }
        ]));

        sinon.stub(MockChild.prototype, 'componentWillMount');
    });

    afterEach(() => {
        window.fetch.restore();
        jasmine.clock().uninstall();
        MockChild.prototype.componentWillMount.restore();
    });

    it('wraps component', () => {
        const wrapSpy = sinon.spy(MockWrapped.prototype, 'componentWillMount');

        const el = TestUtils.renderIntoDocument(<MockWrapped />);
        expect(wrapSpy.called).toEqual(true);
        expect(el.interval).not.toEqual(false);
        MockWrapped.prototype.componentWillMount.restore();
    });

    it('calls interval', () => {
        const wrapSpy = sinon.spy(MockWrapped.prototype, 'createDraft');

        const el = TestUtils.renderIntoDocument(<MockWrapped />);

        expect(wrapSpy.called).toEqual(false);
        expect(el.isDirty).toEqual(false);

        jasmine.clock().tick(5001);

        expect(wrapSpy.called).toEqual(true);
        expect(el.isDirty).toEqual(false);

        MockWrapped.prototype.createDraft.restore();
    });

    it('is dirty', (done) => {
        const wrapSpy = sinon.spy(MockWrapped.prototype, 'createDraft');

        const el = TestUtils.renderIntoDocument(<MockWrapped />);

        el.setState({'raw': {}});

        expect(wrapSpy.called).toEqual(false);
        expect(el.isDirty).toEqual(false);
        expect(JSON.stringify(el.state.raw)).toEqual('{}');

        el.onChange({'raw': {'foo': 'bar'}});
        expect(JSON.stringify(el.state.raw)).toEqual('{"foo":"bar"}');
        expect(el.isDirty).toEqual(true);

        jasmine.clock().tick(5001);
        expect(wrapSpy.called).toEqual(true);

        jasmine.clock().tick(101);
        expect(window.fetch.called).toEqual(true);

        // actually wait for the return
        jasmine.clock().uninstall();
        setTimeout(() => {
            expect(el.isDirty).toEqual(false);

            MockWrapped.prototype.createDraft.restore();
            done()
        }, 100);
    });

    it('is not dirty', () => {
        const wrapSpy = sinon.spy(MockWrapped.prototype, 'createDraft');

        const el = TestUtils.renderIntoDocument(<MockWrapped />);

        el.setState({'raw': {'foo': 'bar'}});

        expect(wrapSpy.called).toEqual(false);
        expect(el.isDirty).toEqual(false);

        el.onChange({'raw': {'foo': 'bar'}});
        expect(el.isDirty).toEqual(false);

        jasmine.clock().tick(5001);
        expect(wrapSpy.called).toEqual(true);

        jasmine.clock().tick(101);
        expect(el.isDirty).toEqual(false);
        expect(window.fetch.called).toEqual(false);

        MockWrapped.prototype.createDraft.restore();
    });

    it('returns valid draft', (done) => {
        window.fetch.restore();
        MockChild.prototype.componentWillMount.restore();

        sinon.stub(window, 'fetch').returns(fetch([
            {
                id: 1,
                content_raw: '{"foo":"bar"}'
            }
        ]));

        const mockSpy = sinon.spy(MockChild.prototype, 'draftCb');
        const checkSpy = sinon.spy(MockWrapped.prototype, 'checkDraft');

        jasmine.clock().uninstall();
        const el = TestUtils.renderIntoDocument(<MockWrapped />);

        setTimeout(() => {
            expect(checkSpy.called).toEqual(true);
            expect(window.fetch.called).toEqual(true);
            expect(mockSpy.called).toEqual(true);

            const ret = mockSpy.getCalls()[0].args[0];
            expect(ret.get('content_raw')).toEqual('{"foo":"bar"}');

            MockChild.prototype.draftCb.restore();
            MockWrapped.prototype.checkDraft.restore();

            sinon.stub(MockChild.prototype, 'componentWillMount');
            done();
        }, 100);
    });

    it('returns empty draft', (done) => {
        window.fetch.restore();
        MockChild.prototype.componentWillMount.restore();

        sinon.stub(window, 'fetch').returns(fetch([]));

        const mockSpy = sinon.spy(MockChild.prototype, 'draftCb');
        const checkSpy = sinon.spy(MockWrapped.prototype, 'checkDraft');

        jasmine.clock().uninstall();
        const el = TestUtils.renderIntoDocument(<MockWrapped />);

        setTimeout(() => {
            expect(checkSpy.called).toEqual(true);
            expect(window.fetch.called).toEqual(true);
            expect(mockSpy.called).toEqual(true);

            const ret = mockSpy.getCalls()[0].args[0];
            expect(ret).toEqual(false);

            MockChild.prototype.draftCb.restore();
            MockWrapped.prototype.checkDraft.restore();

            sinon.stub(MockChild.prototype, 'componentWillMount');
            done();
        }, 100);
    });

    it('returns calls update callback', (done) => {
        window.fetch.restore();
        MockChild.prototype.componentWillMount.restore();

        sinon.stub(window, 'fetch').returns(fetch([
            {
                id: 1,
                content_raw: '{"foo":"bar"}'
            }
        ]));

        const mockSpy = sinon.spy(MockChild.prototype, 'updateCb');

        const el = TestUtils.renderIntoDocument(<MockWrapped />);
        expect(mockSpy.called).toEqual(false);

        el.onChange({'raw': {'foo': 'bar'}});

        jasmine.clock().tick(5101);

        // actually wait for the return
        jasmine.clock().uninstall();
        setTimeout(() => {
            expect(mockSpy.called).toEqual(true);

            MockChild.prototype.updateCb.restore();

            sinon.stub(MockChild.prototype, 'componentWillMount');
            done();
        }, 100);
    });

});