Skip to content
Snippets Groups Projects
StepAddress.spec.tsx 2.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { mount } from 'enzyme'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import toJson from 'enzyme-to-json'
    import React from 'react'
    
    import { Provider } from 'react-redux'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import configureStore from 'redux-mock-store'
    
    import { globalStateData } from '../../../../tests/__mocks__/globalStateData.mock'
    import StepAddress from './StepAddress'
    
    jest.mock('cozy-ui/transpiled/react/I18n', () => {
      return {
        useI18n: jest.fn(() => {
          return {
            t: (str: string) => str,
          }
        }),
      }
    })
    const mockStore = configureStore([])
    
    describe('StepAddress component', () => {
      it('should be rendered correctly', () => {
        const mockHandleChange = jest.fn()
        const store = mockStore({
          ecolyo: {
            global: globalStateData,
          },
        })
        const wrapper = mount(
          <Provider store={store}>
            <StepAddress
              sgeState={globalStateData.sgeConnect}
              onChange={mockHandleChange}
            />
          </Provider>
        )
        expect(toJson(wrapper)).toMatchSnapshot()
      })
      it('should change address value', () => {
        const mockHandleChange = jest.fn()
        const store = mockStore({
          ecolyo: {
            global: globalStateData,
          },
        })
        const wrapper = mount(
          <Provider store={store}>
            <StepAddress
              sgeState={globalStateData.sgeConnect}
              onChange={mockHandleChange}
            />
          </Provider>
        )
        wrapper.find('#address').first().simulate('change')
        expect(mockHandleChange).toHaveBeenCalledWith('address', '')
      })
      it('should change zipCode value', () => {
        const mockHandleChange = jest.fn()
        const store = mockStore({
          ecolyo: {
            global: globalStateData,
          },
        })
        const wrapper = mount(
          <Provider store={store}>
            <StepAddress
              sgeState={globalStateData.sgeConnect}
              onChange={mockHandleChange}
            />
          </Provider>
        )
        wrapper.find('#zipCode').first().simulate('change')
        expect(mockHandleChange).toHaveBeenCalledWith('zipCode', '', 5)
      })
      it('should have an existing zipCode value', () => {
        const mockHandleChange = jest.fn()
        const store = mockStore({
          ecolyo: {
            global: globalStateData,
          },
        })
        const wrapper = mount(
          <Provider store={store}>
            <StepAddress
              sgeState={{ ...globalStateData.sgeConnect, zipCode: 69200 }}
              onChange={mockHandleChange}
            />
          </Provider>
        )
        expect(wrapper.find('#zipCode').first().props().value).toBe(69200)
      })
      it('should change city value', () => {
        const mockHandleChange = jest.fn()
        const store = mockStore({
          ecolyo: {
            global: globalStateData,
          },
        })
        const wrapper = mount(
          <Provider store={store}>
            <StepAddress
              sgeState={globalStateData.sgeConnect}
              onChange={mockHandleChange}
            />
          </Provider>
        )
        wrapper.find('#city').first().simulate('change')
        expect(mockHandleChange).toHaveBeenCalledWith('city', '')
      })
    })