Home Reference Source

application/__tests__/components/content/content-item-test.js

jest.unmock('./../../../services/base-service');
jest.unmock('./../../../services/error-service');
jest.unmock('./../../../services/content-service');
jest.unmock('./../../../metaproperties/meta-properties');
jest.unmock('./../../../metaproperties/text-meta-property');

jest.unmock('./../../../components/content/content-item');
jest.unmock('./../../../components/content/content-meta-properties');
jest.unmock('./../../../util/strings');
jest.unmock('./../../../alt');

jest.unmock('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 TextField from 'material-ui/TextField';

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

import {ContentService, ContentStore} from './../../../services/content-service';
import ContentItem from './../../../components/content/content-item';
import MetaProperties from './../../../metaproperties/meta-properties';

describe('ContentItemComponent', () => {

    beforeEach(() => {
        jasmine.clock().uninstall();
        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(ContentItem.prototype, 'getRouter').returns({
            setRouteLeaveHook: sinon.stub()
        });
    });

    afterEach(() => {
        window.fetch.restore();

        ContentItem.prototype.getRouter.restore();
    });

    it('renders base form', (done) => {

        const el = TestUtils.renderIntoDocument(<ContentItem uuid='5432543-54325432' />);

        setTimeout(() => {
            expect(el.state.content.get('uuid')).toEqual('5432543-54325432');
            expect(el.isUserEditable()).toEqual(false);

            const inputs = TestUtils.scryRenderedDOMComponentsWithTag(el, 'input');
            expect(inputs.length).toBeGreaterThan(0);

            done();
        }, 100);
    });

    it('handles checkout', (done) => {
        const el = TestUtils.renderIntoDocument(<ContentItem uuid='5432543-54325432' />);

        setTimeout(() => {
            window.fetch.restore();
            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,
                    lock: {
                        user_id: 1
                    }
                }
            ]));

            const checkoutSpy = sinon.spy(ContentService, 'checkoutAndFetch');

            expect(el.state.content.get('uuid')).toEqual('5432543-54325432');
            expect(el.isUserEditable()).toEqual(false);

            // const btn = TestUtils.findRenderedDOMComponentWithClass(el, 'action-checkout');
            // TestUtils.Simulate.click(btn, {});
            el.handleCheckout();

            setTimeout(() => {

                expect(checkoutSpy.called).toEqual(true);
                ContentService.checkoutAndFetch.restore();

                expect(el.isUserEditable()).toEqual(true);

                done();
            }, 100);
        }, 100);

    });

    it('handles checkin', (done) => {
        window.fetch.restore();
        sinon.stub(window, 'fetch').returns(fetch([
            {
                id: 1,
                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,
                lock: {
                    user_id: 1
                }
            }
        ]));

        const el = TestUtils.renderIntoDocument(<ContentItem uuid='5432543-54325432' />);

        setTimeout(() => {

            const checkinSpy = sinon.spy(ContentService, 'checkinAndFetch');

            expect(el.state.content.get('uuid')).toEqual('5432543-54325432');
            expect(el.isUserEditable()).toEqual(true);

            window.fetch.restore();
            sinon.stub(window, 'fetch').returns(fetch([
                {
                    id: 1,
                    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
                }
            ]));

            // const btn = TestUtils.findRenderedDOMComponentWithClass(el, 'action-checkin');
            // expect(btn);
            // TestUtils.Simulate.click(btn, {});
            el.handleCheckin();

            setTimeout(() => {

                expect(checkinSpy.called).toEqual(true);
                ContentService.checkinAndFetch.restore();

                expect(el.isUserEditable()).toEqual(false);

                done();
            }, 100);
        }, 100);

    });

    it('handles checkout when locked', (done) => {
        window.fetch.restore();
        sinon.stub(window, 'fetch').returns(fetch([
            {
                id: 1,
                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,
                lock: {
                    // different user than the ID in the mock user
                    user_id: 2
                }
            }
        ]));

        const el = TestUtils.renderIntoDocument(<ContentItem uuid='5432543-54325432' />);

        setTimeout(() => {

            const checkinSpy = sinon.spy(ContentService, 'checkoutAndFetch');

            expect(el.state.content.get('uuid')).toEqual('5432543-54325432');
            expect(el.isUserEditable()).toEqual(false);

            // const btn = TestUtils.findRenderedDOMComponentWithClass(el, 'action-checkout');
            // TestUtils.Simulate.click(btn, {});
            el.handleCheckout();

            setTimeout(() => {

                expect(checkinSpy.called).toEqual(true);
                ContentService.checkoutAndFetch.restore();

                expect(el.isUserEditable()).toEqual(false);

                done();
            }, 100);
        }, 100);
    });

    it('opens preview', (done) => {
        const el = TestUtils.renderIntoDocument(<ContentItem uuid='5432543-54325432' />);

        setTimeout(() => {
            expect(el.state.content.get('uuid')).toEqual('5432543-54325432');
            expect(el.isUserEditable()).toEqual(false);

            const openSpy = sinon.stub(window, 'open').returnsArg(1);

            // const btn = TestUtils.findRenderedDOMComponentWithClass(el, 'action-preview');
            // TestUtils.Simulate.click(btn, {});
            el.handlePreview();

            expect(openSpy.called).toEqual(true);
            window.open.restore();
            done();
        }, 100);

    });

    it('handles save', (done) => {
        window.fetch.restore();
        sinon.stub(window, 'fetch').returns(fetch([
            {
                id: 1,
                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,
                lock: {
                    user_id: 1
                }
            }
        ]));

        const el = TestUtils.renderIntoDocument(<ContentItem uuid='5432543-54325432' />);

        setTimeout(() => {
            expect(el.state.content.get('uuid')).toEqual('5432543-54325432');
            expect(el.isUserEditable()).toEqual(true);

            const saveSpy = sinon.spy(ContentService, 'update');

            // const btn = TestUtils.findRenderedDOMComponentWithClass(el, 'action-save');
            // TestUtils.Simulate.click(btn, {});
            el.handleSave();

            setTimeout(() => {
                expect(saveSpy.called).toEqual(true);
                ContentService.update.restore();
                done();
            }, 100);
        }, 100);

    });

    it('handles save locked', (done) => {
        const el = TestUtils.renderIntoDocument(<ContentItem uuid='5432543-54325432' />);
            const saveSpy = sinon.spy(ContentService, 'update');

        setTimeout(() => {
            expect(el.state.content.get('uuid')).toEqual('5432543-54325432');
            expect(el.isUserEditable()).toEqual(false);

            // const btn = TestUtils.findRenderedDOMComponentWithClass(el, 'action-save');
            // TestUtils.Simulate.click(btn, {});
            el.handleSave();

            setTimeout(() => {
                expect(saveSpy.called).toEqual(false);
                ContentService.update.restore();
                done();
            }, 100);
        }, 100);

    });

    it('displays meta property', (done) => {
        window.fetch.restore();
        sinon.stub(window, 'fetch').returns(fetch([
            {
                id: 1,
                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,
                lock: {
                    user_id: 1
                }
            }
        ]));

        const property = {
            type: 'TextMetaProperty',
            field: 'subhead',
            label: 'Subhead'
        };
        sinon.stub(MetaProperties, 'propertiesForGroup').returns([property]);

        const el = TestUtils.renderIntoDocument(<ContentItem uuid='5432543-54325432' />);

        setTimeout(() => {
            expect(el.isUserEditable()).toEqual(true);

            const inputs = TestUtils.scryRenderedComponentsWithType(el, TextField);
            expect(inputs.length).toBeGreaterThan(0);

            let _found = 0;
            inputs.map((input, i) => {
                if (input.props.floatingLabelText == 'Subhead') {
                    _found = 1;
                }
            });

            expect(_found).toEqual(1);

            MetaProperties.propertiesForGroup.restore();
            done();
        }, 100);
    });

    it('displays new content', (done) => {
        const el = TestUtils.renderIntoDocument(<ContentItem />);

        setTimeout(() => {
            expect(el.isUserEditable()).toEqual(true);
            expect(el.isNew()).toEqual(true);

            const inputs = TestUtils.scryRenderedDOMComponentsWithTag(el, 'input');
            expect(inputs.length).toBeGreaterThan(0);

            done();
        }, 100);

    });

});