Add jest tests for twenty-front (#2983)

* Add jest tests for twenty-front

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Fix tests

---------

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
gitstart-twenty
2023-12-15 17:53:20 +08:00
committed by GitHub
parent af9d3fb217
commit 5f7442cf23
27 changed files with 693 additions and 2 deletions

View File

@ -0,0 +1,23 @@
import React from 'react';
import { renderHook } from '@testing-library/react';
import { useCombinedRefs } from '~/hooks/useCombinedRefs';
describe('useCombinedRefs', () => {
it('should combine refs', () => {
const { result } = renderHook(() => {
const ref1 = React.useRef<string>(null);
const ref2 = React.useRef<string>(null);
const refCallback = useCombinedRefs(ref1, ref2);
React.useEffect(() => {
refCallback('test');
}, [refCallback]);
return [ref1, ref2];
});
expect(result.current[0].current).toBe('test');
expect(result.current[1].current).toBe('test');
});
});

View File

@ -0,0 +1,23 @@
import { renderHook } from '@testing-library/react';
import { useFirstMountState } from '~/hooks/useFirstMountState';
describe('useFirstMountState', () => {
it('should return true on first mount', () => {
const { result } = renderHook(() => {
return useFirstMountState();
});
expect(result.current).toBe(true);
});
it('should return false on second mount', () => {
const { result, rerender } = renderHook(() => {
return useFirstMountState();
});
rerender();
expect(result.current).toBe(false);
});
});

View File

@ -0,0 +1,39 @@
import { MemoryRouter } from 'react-router-dom';
import { renderHook } from '@testing-library/react';
import { useIsMatchingLocation } from '~/hooks/useIsMatchingLocation';
const Wrapper = ({ children }: { children: React.ReactNode }) => (
<MemoryRouter
initialEntries={['/one', '/two', { pathname: '/three' }]}
initialIndex={1}
>
{children}
</MemoryRouter>
);
describe('useIsMatchingLocation', () => {
it('should return true for a matching location', () => {
const { result } = renderHook(
() => {
const checkMatchingLocation = useIsMatchingLocation();
return checkMatchingLocation('/two');
},
{ wrapper: Wrapper },
);
expect(result.current).toBe(true);
});
it('should return false for a non-matching location', () => {
const { result } = renderHook(
() => {
const checkMatchingLocation = useIsMatchingLocation();
return checkMatchingLocation('/four');
},
{ wrapper: Wrapper },
);
expect(result.current).toBe(false);
});
});

View File

@ -0,0 +1,18 @@
import { renderHook } from '@testing-library/react';
import { useUpdateEffect } from '~/hooks/useUpdateEffect';
describe('useUpdateEffect', () => {
it('should call the effect callback on update', () => {
const effect = jest.fn();
const { rerender } = renderHook(() => {
useUpdateEffect(effect);
});
expect(effect).not.toHaveBeenCalled();
rerender();
expect(effect).toHaveBeenCalledTimes(1);
});
});