import React from 'react';
import {expect} from 'chai';
import sinon from "sinon";
import {shallow} from "enzyme";
import configureStore from 'redux-mock-store';
import {Router} from "react-router";
import thunk from 'redux-thunk'
import {DiscardConfirm} from 'nexshop-web-alert';
import {contentsActions} from 'nexshop-web-store';
import RootComponent from '../../src/component/root-component';
import NotificationPopup from '../../src/component/popup/notification-popup';
describe('Root Component Spec', () => {
let wrapper;
let mockStore;
let mockHistory = {};
const createRootComponent = (store) => {
return shallow(<RootComponent store={store} history={mockHistory}/>);
};
beforeEach(() => {
sinon.stub(contentsActions, 'fetchAllUnderRootFolderAsync').returns({
type: 'SOME_ACTION'
});
sinon.stub(contentsActions, 'fetchRootFolderAsync').returns({
type: 'ANOTHER_ACTION'
});
});
afterEach(() => {
contentsActions.fetchAllUnderRootFolderAsync.restore();
contentsActions.fetchRootFolderAsync.restore();
});
describe('without notification popup', () => {
beforeEach(() => {
mockStore = configureStore([thunk])({popup: {}, nexFetch: {isLoading: false}});
wrapper = createRootComponent(mockStore).dive();
});
describe('rendering', () => {
it('Router has history created in constructor', () => {
expect(wrapper.find(Router).prop('history')).to.equal(mockHistory);
});
it('DiscardConfirm has history created in constructor', () => {
expect(wrapper.find(DiscardConfirm).prop('history')).to.equal(mockHistory);
});
it('does not render NotificationPopup component', () => {
expect(wrapper.find(NotificationPopup).length).to.equal(0);
});
});
describe('componentWillMount', () => {
it('calls fetchAllUnderRootFolderAsync action creator', () => {
expect(contentsActions.fetchAllUnderRootFolderAsync.called).to.be.true;
});
it('calls store.dispatch with fetchAllUnderRootFolderAsync', () => {
expect(mockStore.getActions()[0]).to.deep.equal({type: 'SOME_ACTION'});
});
it('calls fetchRootFolderAsync action creator', () => {
expect(contentsActions.fetchRootFolderAsync.called).to.be.true;
});
it('calls store.dispatch with fetchRootFolderAsync', () => {
expect(mockStore.getActions()[1]).to.deep.equal({type: 'ANOTHER_ACTION'});
});
});
it('calls store.dispatch with closePopup Actions', () => {
mockStore.clearActions();
wrapper.instance().closeNotificationPopup();
expect(mockStore.getActions()[0]).to.deep.equal({type: 'SCHEDULE_CLOSE_POPUP', name: 'notification'});
});
});
describe('with notification popup', () => {
beforeEach(() => {
mockStore = configureStore([thunk])({
popup: {notification: {visibility: true, message: 'some text'}},
nexFetch: {isLoading: false}
});
wrapper = createRootComponent(mockStore).dive();
mockStore.dispatch({type: 'DUMMY'});
});
describe('rendering', () => {
it('shows NotificationPopup component', () => {
const notificationPopup = wrapper.find(NotificationPopup);
expect(notificationPopup.length).to.equal(1);
expect(notificationPopup.props().text).to.equal('some text');
expect(notificationPopup.props().onClose).to.equal(wrapper.instance().closeNotificationPopup);
});
});
describe('componentWillMount', () => {
it('sets notificationPopupVisibility to true', () => {
expect(wrapper.state('notificationPopupVisibility')).to.be.true;
});
});
});
describe('show spinner spec', () => {
it('if isLoading is false, disappear CircularProgress component.', () => {
mockStore = configureStore([thunk])({
popup: {notification: {visibility: true, message: 'some text'}},
nexFetch: {isLoading: false}
});
wrapper = createRootComponent(mockStore).dive();
expect(wrapper.find('.spinner-wrapper')).to.length(0);
});
it('if isLoading is true, show CircularProgress component.', () => {
mockStore = configureStore([thunk])({
popup: {notification: {visibility: false, message: undefined}},
nexFetch: {isLoading: true}
});
wrapper = createRootComponent(mockStore).dive();
expect(wrapper.find('.spinner-wrapper')).to.length(1);
});
});
}); |