import React from 'react';
import sinon from 'sinon';
import {expect} from 'chai';
import {mount, shallow} from "enzyme";
import CreateThat from "../../../../src/component/dialog/create-that/create-that";
import ResolutionDropDown from "../../../../src/component/resolution-drop-down/resolution-drop-down";
describe('CreateThat Spec', () => {
let wrapper;
let spyOnClose;
let spyReplaceContentNameValidity;
let spyFetchExistContentNameAsync;
beforeEach(() => {
spyOnClose = sinon.spy();
spyReplaceContentNameValidity = sinon.spy();
spyFetchExistContentNameAsync = sinon.spy();
wrapper = shallow(<CreateThat.WrappedDialogComponent whatThatIs="Scene" onClose={spyOnClose}
visibility={true}
breadcrumb={[{name: 'Contents', id: 'rootId'}]}
replaceContentNameValidity={spyReplaceContentNameValidity}
contentNameExisted="NONE"
fetchExistContentNameAsync={spyFetchExistContentNameAsync}
/>);
});
it('shows that + Name as a title', () => {
expect(wrapper.find('.create-that-body__title').text()).to.equal("Scene Name");
});
describe('title input focus and blur test', () => {
beforeEach(() => {
wrapper = mount(<CreateThat.WrappedDialogComponent whatThatIs="Scene" onClose={spyOnClose} visibility={true}
breadcrumb={[{name: 'Contents', id: 'rootId'}]}
replaceContentNameValidity={spyReplaceContentNameValidity}
contentNameExisted="NONE"
fetchExistContentNameAsync={spyFetchExistContentNameAsync}
/>);
});
it('focuses on title Input', () => {
const focusedElement = document.activeElement;
expect(wrapper.find('.create-that-body__input').matchesElement(focusedElement)).to.be.true;
});
it('call fetchExistContentNameAsync when after input name and focus out', () => {
wrapper.instance().titleInput.value = 'some scene name';
wrapper.find('.create-that-body__input').simulate('blur');
expect(spyFetchExistContentNameAsync.calledWith('rootId', 'Scene', 'some scene name')).to.true;
});
it('show error message when name is invalid', () => {
wrapper.instance().titleInput.value = 'some name';
wrapper.setProps({contentNameExisted: 'EXIST'});
expect(wrapper.find('.create-that-body__error-message').at(0).text()).to.not.empty;
});
});
describe('ResolutionDropDown', () => {
it('passes changeResolution as onChange prop', () => {
expect(wrapper.find(ResolutionDropDown).prop('onChange')).to.equal(wrapper.instance().changeResolution);
});
});
it('passes changeTitle as onChange of input', () => {
expect(wrapper.find('.create-that-body__input').prop('onChange')).to.equal(wrapper.instance().changeTitle);
});
it('changeTitle changes title state', () => {
wrapper.instance().changeTitle({target: {value: 'new title'}});
expect(wrapper.state('title')).to.equal('new title');
});
it('changeResolution changes resolution state', () => {
wrapper.instance().changeResolution({value: 'value', text: 'text'});
expect(wrapper.state('resolution')).to.deep.equal({value: 'value', text: 'text'});
});
it('activates when title, resolution has value', () => {
wrapper.setState({title: 'some title'});
expect(wrapper.find('.create-that-footer__create--inactive').exists()).to.be.true;
wrapper.setState({resolution: {value: 'value', text: 'text'}});
expect(wrapper.find('.create-that-footer__create--inactive').exists()).to.be.true;
wrapper.setProps({contentNameExisted: 'NOT_EXIST'});
expect(wrapper.find('.create-that-footer__create').exists()).to.be.true;
});
it('click on CREATE triggers onCreated prop with title, resolution', () => {
const spyOnCreated = sinon.spy();
wrapper.setProps({onCreated: spyOnCreated});
wrapper.setState({title: 'some title', resolution: 'some resolution'});
wrapper.setProps({contentNameExisted: 'NOT_EXIST'});
wrapper.find('.create-that-footer__create').simulate('click');
expect(spyOnCreated.calledWith('some title', 'some resolution')).to.be.true;
});
it('click on CANCEL triggers onCancel prop', () => {
const spyOnCancel = sinon.spy();
wrapper.setProps({onCancel: spyOnCancel});
wrapper.find('.create-that-footer__cancel').simulate('click');
expect(spyOnCancel.called).to.be.true;
});
}); |