import AnalysisView from 'components/Analysis/AnalysisView'
import { mount } from 'enzyme'
import React from 'react'
import { Provider } from 'react-redux'
import * as globalActions from 'store/global/global.slice'
import * as storeHooks from 'store/hooks'
import * as profileActions from 'store/profile/profile.slice'
import {
  createMockEcolyoStore,
  mockAnalysisState,
  mockChartState,
  mockProfileState,
} from 'tests/__mocks__/store'

jest.mock('components/Header/CozyBar', () => 'mock-cozybar')
jest.mock('components/Header/Header', () => 'mock-header')
jest.mock('components/Content/Content', () => 'mock-content')
jest.mock('components/DateNavigator/DateNavigator', () => 'mock-datenavigator')
jest.mock('components/Analysis/MonthlyAnalysis', () => 'mock-monthlyanalysis')

jest.mock('react-router-dom', () => ({
  ...jest.requireActual('react-router-dom'),
  useLocation: () => ({
    pathname: 'ecolyo.cozy.localhost:8080/#/analysis',
  }),
}))

const mockAppDispatch = jest.spyOn(storeHooks, 'useAppDispatch')
const toggleAnalysisNotificationSpy = jest.spyOn(
  globalActions,
  'toggleAnalysisNotification'
)
const updateProfileSpy = jest.spyOn(profileActions, 'updateProfile')

describe('AnalysisView component', () => {
  const store = createMockEcolyoStore()
  beforeEach(() => {
    jest.clearAllMocks()
  })

  it('should be rendered correctly', () => {
    const wrapper = mount(
      <Provider store={store}>
        <AnalysisView />
      </Provider>
    )
    expect(wrapper.find('mock-cozybar').exists()).toBeTruthy()
    expect(wrapper.find('mock-header').exists()).toBeTruthy()
    expect(wrapper.find('mock-datenavigator').exists()).toBeTruthy()
    expect(wrapper.find('mock-monthlyanalysis').exists()).toBeTruthy()
  })

  it('should update profile and toggle analysis notification to false if notification is true', () => {
    const store = createMockEcolyoStore({
      analysis: mockAnalysisState,
      chart: mockChartState,
      global: { analysisNotification: true },
      profile: { ...mockProfileState, haveSeenLastAnalysis: true },
    })
    mockAppDispatch.mockReturnValue(jest.fn())
    const wrapper = mount(
      <Provider store={store}>
        <AnalysisView />
      </Provider>
    )
    expect(wrapper.find('mock-cozybar').exists()).toBeTruthy()
    expect(wrapper.find('mock-header').exists()).toBeTruthy()
    expect(wrapper.find('mock-datenavigator').exists()).toBeTruthy()
    expect(wrapper.find('mock-monthlyanalysis').exists()).toBeTruthy()
    expect(updateProfileSpy).toHaveBeenCalledTimes(1)
    expect(updateProfileSpy).toHaveBeenCalledWith({
      haveSeenLastAnalysis: true,
    })
    expect(toggleAnalysisNotificationSpy).toHaveBeenCalledTimes(1)
    expect(toggleAnalysisNotificationSpy).toHaveBeenCalledWith(false)
  })
})