import React from 'react' import { act, renderHook } from '@testing-library/react' import { useCounterLogic } from './useCounterLogic' import { IntlProvider } from 'react-intl' describe('useCounterLogic', () => { describe('basic behavior', () => { it('should return defaults when nothing is passed in', () => { const { result } = renderHook(() => useCounterLogic({ length: 10, }) ) expect(result.current).toEqual({ maxLength: undefined, fieldError: false, count: undefined, handleBeforeInput: expect.any(Function), handlePaste: expect.any(Function), handleBlur: expect.any(Function), handleFocus: expect.any(Function), }) }) it('should return handlers and details', () => { const { result } = renderHook(() => useCounterLogic({ length: 10, limit: 10, }) ) expect(result.current).toEqual({ maxLength: 10, fieldError: false, count: { text: '10/10', status: 'default', }, handleBeforeInput: expect.any(Function), handlePaste: expect.any(Function), handleBlur: expect.any(Function), handleFocus: expect.any(Function), }) }) describe('without react-intl', () => { it('should return counters for large numbers without formatting', () => { const { result } = renderHook(() => useCounterLogic({ length: 4900, limit: 5000, }) ) expect(result.current.count).toEqual({ text: '4900/5000', status: 'default', }) }) }) describe('with react-intl', () => { it('should return counters for large numbers with formatting', () => { const { result } = renderHook( () => useCounterLogic({ length: 4900, limit: 5000, }), { wrapper: ({ children }) => ( {children} ), } ) expect(result.current.count).toEqual({ text: '4,900/5,000', status: 'default', }) }) }) }) describe('warning when typing in truncated field', () => { it('should call passed onBeforeInput', async () => { const onBeforeInput = jest.fn() const { result } = renderHook(() => useCounterLogic({ length: 10, limit: 10, onBeforeInput, }) ) const target = document.createElement('input') const evt = { target, data: 'type', } as unknown as React.InputEvent act(() => { result.current.handleBeforeInput(evt) }) expect(onBeforeInput).toHaveBeenCalledWith(evt) }) it('should flash the error when exceeding limit', async () => { const { result } = renderHook(() => useCounterLogic({ length: 10, limit: 10, }) ) const target = document.createElement('input') target.value = '1234567890' const evt = { target, data: 'type', } as unknown as React.InputEvent act(() => { result.current.handleBeforeInput(evt) }) expect(result.current.fieldError).toBe(true) await act(async () => { await new Promise((res) => setTimeout(res, 500)) }) expect(result.current.fieldError).toBe(false) }) it('should not flash the error when not exceeding limit', async () => { const { result } = renderHook(() => useCounterLogic({ length: 8, limit: 10, }) ) const target = document.createElement('input') target.value = '12345678' const evt = { target, data: 'to', } as unknown as React.InputEvent act(() => { result.current.handleBeforeInput(evt) }) expect(result.current.fieldError).toBe(false) await act(async () => { await new Promise((res) => setTimeout(res, 500)) }) expect(result.current.fieldError).toBe(false) }) }) describe('warning when pasting in a truncated field', () => { it('should call passed onPaste', async () => { const onPaste = jest.fn() const { result } = renderHook(() => useCounterLogic({ length: 10, limit: 10, onPaste, }) ) const getClipboardData = jest.fn().mockReturnValue('type') const target = document.createElement('input') const evt = { target, clipboardData: { getData: getClipboardData, }, } as unknown as React.ClipboardEvent act(() => { result.current.handlePaste(evt) }) expect(onPaste).toHaveBeenCalledWith(evt) }) it('should trigger onPasteTruncated when exceeding limit', async () => { const onPasteTruncated = jest.fn() const { result } = renderHook(() => useCounterLogic({ length: 10, limit: { value: 10, onPasteTruncated }, }) ) const getClipboardData = jest.fn().mockReturnValue('type') const target = document.createElement('input') target.value = '1234567890' const evt = { target, clipboardData: { getData: getClipboardData, }, } as unknown as React.ClipboardEvent result.current.handlePaste(evt) expect(onPasteTruncated).toHaveBeenCalledTimes(1) }) it('should not trigger onPasteTruncated when not exceeding limit', async () => { const onPasteTruncated = jest.fn() const { result } = renderHook(() => useCounterLogic({ length: 8, limit: { value: 10, onPasteTruncated }, }) ) const getClipboardData = jest.fn().mockReturnValue('to') const target = document.createElement('input') target.value = '12345678' const evt = { target, clipboardData: { getData: getClipboardData, }, } as unknown as React.ClipboardEvent result.current.handlePaste(evt) expect(onPasteTruncated).not.toHaveBeenCalled() }) }) })