Home Reference Source

application/__tests__/components/content/content-meta-properties-test.js

jest.unmock('./../../../components/content/content-meta-properties');
jest.unmock('./../../../metaproperties/meta-properties');
jest.unmock('./../../../metaproperties/text-meta-property');

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 MetaProperties from './../../../metaproperties/meta-properties';
import ContentMetaProperties from './../../../components/content/content-meta-properties';

describe('AppComponent', () => {

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

    it('displays meta property', (done) => {
        const content = Immutable.fromJS({
            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(<ContentMetaProperties content={content} />);

        setTimeout(() => {
            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('pings callback', (done) => {
        const content = Immutable.fromJS({
            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 mockChange = sinon.mock();

        const el = TestUtils.renderIntoDocument(<ContentMetaProperties content={content} onChange={mockChange} />);

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

            inputs.map((input, i) => {
                if (input.props.floatingLabelText == 'Subhead') {
                    el.onChange(property,  {
                        target: {
                            getAttribute: () => {
                                return 'subhead';
                            },
                            value: 'This is a test subhead'
                        }
                    });
                }
            });

            expect(mockChange.called).toEqual(true);
            expect(mockChange.getCalls()[0].args[0].value).toEqual('This is a test subhead');

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

});