From 562cdc563f831a3beea7b7d4d28642aedaed8c8e Mon Sep 17 00:00:00 2001 From: "gitstart-app[bot]" <57568882+gitstart-app[bot]@users.noreply.github.com> Date: Tue, 23 Jan 2024 14:15:41 +0100 Subject: [PATCH] TWNTY-3482 - Add tests for `modules/ui/utilities/recoil-scope/scopes-internal/hooks` (#3582) Add tests for `modules/ui/utilities/recoil-scope/scopes-internal/hooks` Co-authored-by: gitstart-twenty Co-authored-by: KlingerMatheus --- .../__tests__/useAvailableScopeId.test.tsx | 64 +++++++++++++++++++ .../useScopeInternalContext.test.tsx | 32 ++++++++++ 2 files changed, 96 insertions(+) create mode 100644 packages/twenty-front/src/modules/ui/utilities/recoil-scope/scopes-internal/hooks/__tests__/useAvailableScopeId.test.tsx create mode 100644 packages/twenty-front/src/modules/ui/utilities/recoil-scope/scopes-internal/hooks/__tests__/useScopeInternalContext.test.tsx diff --git a/packages/twenty-front/src/modules/ui/utilities/recoil-scope/scopes-internal/hooks/__tests__/useAvailableScopeId.test.tsx b/packages/twenty-front/src/modules/ui/utilities/recoil-scope/scopes-internal/hooks/__tests__/useAvailableScopeId.test.tsx new file mode 100644 index 0000000000..4187805be0 --- /dev/null +++ b/packages/twenty-front/src/modules/ui/utilities/recoil-scope/scopes-internal/hooks/__tests__/useAvailableScopeId.test.tsx @@ -0,0 +1,64 @@ +import React from 'react'; +import { renderHook } from '@testing-library/react'; + +import { useAvailableScopeIdOrThrow } from '@/ui/utilities/recoil-scope/scopes-internal/hooks/useAvailableScopeId'; +import { createScopeInternalContext } from '@/ui/utilities/recoil-scope/scopes-internal/utils/createScopeInternalContext'; + +const mockScopeIdFrom = { + Props: 'scopeIdFromProps', + Context: 'scopeIdFromContext', +}; +const MockedContext = createScopeInternalContext(); +const errorMessage = 'Scope id is not provided and cannot be found in context.'; + +const Wrapper = ({ children }: { children: React.ReactNode }) => ( + + {children} + +); + +describe('useAvailableScopeIdOrThrow', () => { + it('should return scopeIdFromProps if provided', () => { + const { + result: { + current: { availableScopeId }, + }, + } = renderHook( + () => ({ + availableScopeId: useAvailableScopeIdOrThrow( + MockedContext, + mockScopeIdFrom.Props, + ), + }), + { wrapper: Wrapper }, + ); + + expect(availableScopeId).toBe(mockScopeIdFrom.Props); + }); + + it('should return scopeIdFromContext if no scopeIdFromProps is present', () => { + const { + result: { + current: { availableScopeId }, + }, + } = renderHook( + () => ({ + availableScopeId: useAvailableScopeIdOrThrow(MockedContext), + }), + { wrapper: Wrapper }, + ); + + expect(availableScopeId).toBe(mockScopeIdFrom.Context); + }); + + it('should throw an error if no scopeId is provided and scopeId is undefined in the context', () => { + console.error = jest.fn(); + + const renderFunction = () => + renderHook(() => ({ + availableScopeId: useAvailableScopeIdOrThrow(MockedContext), + })); + + expect(renderFunction).toThrow(errorMessage); + }); +}); diff --git a/packages/twenty-front/src/modules/ui/utilities/recoil-scope/scopes-internal/hooks/__tests__/useScopeInternalContext.test.tsx b/packages/twenty-front/src/modules/ui/utilities/recoil-scope/scopes-internal/hooks/__tests__/useScopeInternalContext.test.tsx new file mode 100644 index 0000000000..dcd7159d56 --- /dev/null +++ b/packages/twenty-front/src/modules/ui/utilities/recoil-scope/scopes-internal/hooks/__tests__/useScopeInternalContext.test.tsx @@ -0,0 +1,32 @@ +import { renderHook } from '@testing-library/react'; + +import { useScopeInternalContext } from '@/ui/utilities/recoil-scope/scopes-internal/hooks/useScopeInternalContext'; +import { createScopeInternalContext } from '@/ui/utilities/recoil-scope/scopes-internal/utils/createScopeInternalContext'; + +const scopeId = 'scopeId'; +const MockedContext = createScopeInternalContext(); + +const Wrapper = ({ children }: { children: React.ReactNode }) => ( + + {children} + +); + +describe('useScopeInternalContext', () => { + it('should work as expected', () => { + const { + result: { + current: { scopeInternalContext }, + }, + } = renderHook( + () => ({ + scopeInternalContext: useScopeInternalContext(MockedContext), + }), + { + wrapper: Wrapper, + }, + ); + + expect(scopeInternalContext!.scopeId).toBe(scopeId); + }); +});