import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import injectTapEventPlugin from 'react-tap-event-plugin';
import React from 'react';
import PropTypes from 'prop-types';
import expect from 'expect';
import { mount } from 'enzyme';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import jsdom from 'jsdom';
import sinon from 'sinon';

import RootConnected, { Root } from './index';
import * as network from '../../actions/networking';

const doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.document = doc;
global.window = doc.defaultView;

const middleware = [thunk];
const initialState = { language: { selected: 'en' } };
const mockStore = configureMockStore(middleware);
const store = mockStore(initialState);

injectTapEventPlugin();

describe('<Root/>', () => {
  const login = sinon.stub();
  login.resolves({});
  const logout = sinon.spy();
  const props = {
    languageSelected: 'en',
    sideBar: {
      open: false,
    },
    authenticationEffects: {
      login,
      logout,
    },
  };

  const authenticationActions = {
    doLogin() {
      return Promise.resolve('mockAuthenticationActions');
    },
    doLogout() {
      return Promise.resolve('mockAuthenticationActions');
    },
  };

  const sideBarActions = {
    open() {
      return true;
    },
    close() {
      return false;
    },
  };


  it('should call action doLogin on mounting', () => {
    const request = sinon.spy(network, 'request');
    mount(<MuiThemeProvider><RootConnected store={store} /></MuiThemeProvider>);

    expect(login.calledOnce);
    expect(request.calledOnce);
  });

  it('should display <div></div> with no authentication', () => {
    const wrapper = mount(<Root {...props} authenticationActions={authenticationActions} />);
    expect(wrapper.html()).toEqual('<div></div>');
  });

  it('should display AppBar when connected', () => {
    const muiTheme = getMuiTheme();
    const wrapper = mount(
      <Root {...props} authenticationActions={authenticationActions} sideBarActions={sideBarActions} />,
      {
        context: { muiTheme },
        childContextTypes: { muiTheme: PropTypes.object },
      },
    );
    wrapper.setProps({
      authentication: { user: {
        firstName: 'Test',
      } },
    });

    expect(wrapper.find('AppBar').length).toEqual(2);
  });
});

@@ -540,16 +540,69 @@
 orking';
+%0Aimport HeaderBar from '../../components/header-bar';
 %0A%0Aconst 
@@ -772,16 +772,18 @@
 tate = %7B
+%0A 
  languag
@@ -786,16 +786,20 @@
 guage: %7B
+%0A   
  selecte
@@ -805,19 +805,23 @@
 ed: 'en'
- %7D 
+,%0A  %7D,%0A
 %7D;%0Aconst
@@ -1090,49 +1090,8 @@
 n',%0A
-    sideBar: %7B%0A      open: false,%0A    %7D,%0A
     
@@ -1473,17 +1473,16 @@
 ,%0A  %7D;%0A%0A
-%0A
   it('sh
@@ -1589,16 +1589,23 @@
   mount(
+%0A      
 %3CMuiThem
@@ -1614,16 +1614,25 @@
 rovider%3E
+%0A        
 %3CRootCon
@@ -1654,16 +1654,23 @@
 tore%7D /%3E
+%0A      
 %3C/MuiThe
@@ -1680,16 +1680,22 @@
 rovider%3E
+,%0A    
 );%0A%0A    
@@ -1851,16 +1851,23 @@
 = mount(
+%0A      
 %3CRoot %7B.
@@ -1923,16 +1923,22 @@
 ions%7D /%3E
+,%0A    
 );%0A    e
@@ -2011,19 +2011,22 @@
 display 
-App
+Header
 Bar when
@@ -2112,32 +2112,40 @@
 unt(%0A      %3CRoot
+%0A       
  %7B...props%7D auth
@@ -2131,32 +2131,40 @@
       %7B...props%7D
+%0A       
  authenticationA
@@ -2193,16 +2193,24 @@
 Actions%7D
+%0A       
  sideBar
@@ -2225,32 +2225,136 @@
 %7BsideBarActions%7D
+%0A        sideBar=%7B%7B open: true %7D%7D%0A        authentication=%7B%7B authentication: %7B test: 'noEmpty' %7D %7D%7D%0A     
  /%3E,%0A      %7B%0A   
@@ -2377,16 +2377,23 @@
 muiTheme
+, store
  %7D,%0A    
@@ -2416,16 +2416,26 @@
 Types: %7B
+%0A         
  muiThem
@@ -2453,16 +2453,60 @@
 s.object
+,%0A          store: PropTypes.object,%0A       
  %7D,%0A    
@@ -2563,16 +2563,24 @@
 ation: %7B
+%0A       
  user: %7B
@@ -2588,16 +2588,18 @@
         
+ 
+ 
 firstNam
@@ -2615,17 +2615,64 @@
 ,%0A      
-%7D
+    roles: %5B%7B name: 'ADMIN' %7D%5D,%0A        %7D,%0A     
  %7D,%0A    
@@ -2679,42 +2679,89 @@
 %7D);%0A
-%0A
     
-expect(wrapper.find('AppBar')
+const headerBarWrapper = wrapper.find(HeaderBar);%0A    expect(headerBarWrapper
 .len
@@ -2777,9 +2777,9 @@
 ual(
-2
+1
 );%0A 

Fix tests