Home Reference Source

application/__tests__/components/common/dropzone-test.js

jest.unmock('./../../../components/common/dropzone');
jest.unmock('./../../../services/content-service');
jest.unmock('./../../../services/base-service');
jest.unmock('./../../../services/error-service');

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 Dropzone from 'react-dropzone';

import DropzoneRoot from './../../../components/common/dropzone';
import {ErrorService} from './../../../services/error-service';
import {ContentService} from './../../../services/content-service';

describe('DropzoneComponent', () => {
    let errorSpy;
    let contentSpy;

    beforeEach(() => {
        errorSpy = sinon.spy(ErrorService, 'add');
        contentSpy = sinon.spy(ContentService, 'create');
        jasmine.clock().uninstall();
    });

    afterEach(() => {
        ErrorService.add.restore();
        ContentService.create.restore();
    });

    it('loads default state', () => {
        const dz = TestUtils.renderIntoDocument(
            <DropzoneRoot />
        );

        expect(dz.state.isUploading).toEqual(false);
    });

    it('inserts file', () => {
        const files = [
            {
                name: 'a-test-file.jpg',
                size: 1024,
                type: 'image/jpg'
            }
        ];

        const dz = TestUtils.renderIntoDocument(
            <DropzoneRoot />
        );
        dz.onDrop(files);
        expect(dz.state.files.length).toEqual(1);

        const node = ReactDOM.findDOMNode(dz);

        const file = TestUtils.scryRenderedDOMComponentsWithClass(dz, 'dropzone-file');
        expect(file.length).toBeGreaterThan(0);
    });

    it('removes file', () => {
        const evt = {'stopPropagation': () => {}, 'preventDefault': () => {}};

        const dropFiles = [
            {
                name: 'a-test-file.jpg',
                size: 1024,
                type: 'image/jpg'
            },
            {
                name: 'a-second-file.jpg',
                size: 1024,
                type: 'image/jpg'
            }
        ];

        const dz = TestUtils.renderIntoDocument(
            <DropzoneRoot />
        );
        dz.onDrop(dropFiles);

        expect(dz.state.files.length).toEqual(2);
        dz.onRemoveFile(dropFiles[0], evt);
        expect(dz.state.files.length).toEqual(1);
    });

    it('uploads', (done) => {
        const evt = {'stopPropagation': () => {}, 'preventDefault': () => {}};
        const files = [
            {
                name: 'a-test-file.jpg',
                size: 1024,
                type: 'image/jpg'
            }
        ];

        const dz = TestUtils.renderIntoDocument(
            <DropzoneRoot />
        );
        const spy = sinon.spy(dz, 'uploadReady');

        dz.onDrop(files);
        expect(dz.state.files.length).toEqual(1);

        const node = ReactDOM.findDOMNode(dz);
        dz.onUpload(evt);

        setTimeout(() => {
            expect(spy.called).toEqual(true);
            expect(contentSpy.called).toEqual(true);
            dz.uploadReady.restore();
            done();
        });
    });
});