Home Reference Source

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

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

jest.unmock('./../../../components/assignment/assignment-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 {Input} from 'react-materialize';

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

import {AssignmentService, AssignmentsStore} from './../../../services/assignment-service';
import AssignmentItem from './../../../components/assignment/assignment-item';
import MetaProperties from './../../../metaproperties/meta-properties';

describe('AssignmentItemComponent', () => {

    beforeEach(() => {
        jasmine.clock().uninstall();
        sinon.stub(window, 'fetch').returns(fetch([
            {
                id: 1,
                srn: 'srn:tsn:ceo-core:assignment/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
            }
        ]));
    });

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

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

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

        setTimeout(() => {
            expect(el.state.assignment.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(<AssignmentItem uuid='5432543-54325432' />);

        setTimeout(() => {
            window.fetch.restore();
            sinon.stub(window, 'fetch').returns(fetch([
                {
                    id: 1,
                    srn: 'srn:tsn:ceo-core:assignment/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,
                    lock: {
                        user_id: 1
                    }
                }
            ]));

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

            expect(el.state.assignment.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);
                AssignmentService.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,
                lock: {
                    user_id: 1
                }
            }
        ]));

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

        setTimeout(() => {

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

            expect(el.state.assignment.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
                }
            ]));

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

            setTimeout(() => {

                expect(checkinSpy.called).toEqual(true);
                AssignmentService.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,
                lock: {
                    // different user than the ID in the mock user
                    user_id: 2
                }
            }
        ]));

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

        setTimeout(() => {

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

            expect(el.state.assignment.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);
                AssignmentService.checkoutAndFetch.restore();

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

                done();
            }, 100);
        }, 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,
                lock: {
                    user_id: 1
                }
            }
        ]));

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

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

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

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

            el.handleSave();

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

    });

    it('handles save with users', (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,
                lock: {
                    user_id: 1
                }
            }
        ]));

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

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

            el.onSelectUser({}, Immutable.fromJS([
                {
                    uuid: 6,
                    name: 'foo bar'
                }
            ]));

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

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

            setTimeout(() => {
                expect(saveSpy.called).toEqual(true);
                AssignmentService.update.restore();

                const data = window.fetch.getCalls()[1].args[1];
                expect(data.body);
                expect(data.body).toContain('"users":[6]');

                // expect(window.fetch.getCalls()[1].arguments);
                done();
            }, 100);
        }, 100);

    });

    it('handles save with content', (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,
                lock: {
                    user_id: 1
                }
            }
        ]));

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

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

            el.onSelectContent({}, Immutable.fromJS([
                {
                    uuid: '12321-432432',
                    content: 'foo bar'
                }
            ]));

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

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

            setTimeout(() => {
                expect(saveSpy.called).toEqual(true);
                AssignmentService.update.restore();

                const data = window.fetch.getCalls()[1].args[1];
                expect(data.body);
                expect(data.body).toContain('"children":["12321-432432"]');

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

    });

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

        setTimeout(() => {
            expect(el.state.assignment.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);
                AssignmentService.update.restore();
                done();
            }, 100);
        }, 100);

    });

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

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

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

            done();
        }, 100);

    });


});