Skip to content
Snippets Groups Projects
StepAddress.spec.tsx 1.94 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { act, render, screen } from '@testing-library/react'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { userEvent } from '@testing-library/user-event'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import React from 'react'
    
    import { mockSgeState } from 'tests/__mocks__/forms.mock'
    
    import StepAddress from './StepAddress'
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const mockHandleChange = jest.fn()
    
    
    describe('StepAddress component', () => {
      it('should be rendered correctly', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const { container } = render(
    
          <StepAddress sgeState={mockSgeState} onChange={mockHandleChange} />
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        expect(container).toMatchSnapshot()
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    
      describe('should change inputs', () => {
        beforeEach(() => {
          render(
    
            <StepAddress sgeState={mockSgeState} onChange={mockHandleChange} />
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          )
        })
        it('should change address value', async () => {
          const input = screen.getByRole('textbox', {
            name: 'auth.enedissgegrandlyon.address',
          })
    
          await act(async () => {
            await userEvent.type(input, 't')
          })
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          expect(mockHandleChange).toHaveBeenCalledWith('address', 't')
        })
        it('should change zipCode value', async () => {
          const input = screen.getByRole('spinbutton', {
            name: 'auth.enedissgegrandlyon.zipCode',
          })
    
          await act(async () => {
    
            await userEvent.type(input, '1')
    
          expect(mockHandleChange).toHaveBeenCalledWith('zipCode', '1', 5)
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        })
    
        it('should change city value', async () => {
          const input = screen.getByRole('textbox', {
            name: 'auth.enedissgegrandlyon.city',
          })
    
          await act(async () => {
            await userEvent.type(input, 'c')
          })
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          expect(mockHandleChange).toHaveBeenCalledWith('city', 'c')
        })
    
      })
      it('should have an existing zipCode value', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        render(
          <StepAddress
    
            sgeState={{ ...mockSgeState, zipCode: 69200 }}
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            onChange={mockHandleChange}
          />
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const input = screen.getByRole('spinbutton', {
          name: 'auth.enedissgegrandlyon.zipCode',
        })
        expect(input).toHaveValue(69200)