From 3870dc1f166ddc41a9f9753d89333f8159c6f84c Mon Sep 17 00:00:00 2001 From: Jo Murgel Date: Wed, 4 Sep 2024 19:47:56 -0600 Subject: [PATCH] adds temp test for useAuth --- babel.config.js | 7 +++- jest-setup.js | 1 - jest.config.js | 19 +++++---- src/hooks/useAuth.test.tsx | 50 ++++++++++++++++++++++++ src/redux/{ => reducers}/todoReducers.ts | 2 +- src/redux/slices/todoSlice.ts | 2 +- src/types/env.d.ts | 11 ++++++ 7 files changed, 81 insertions(+), 11 deletions(-) delete mode 100644 jest-setup.js create mode 100644 src/hooks/useAuth.test.tsx rename src/redux/{ => reducers}/todoReducers.ts (99%) create mode 100644 src/types/env.d.ts diff --git a/babel.config.js b/babel.config.js index 6c3429a..5f0e9ee 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,7 +1,12 @@ function config( api ) { api.cache( true ) return { - presets: [ 'babel-preset-expo', '@babel/preset-typescript' ], + presets: [ + 'babel-preset-expo', + '@babel/preset-typescript', + '@babel/preset-env', + '@babel/preset-react', + ], } } diff --git a/jest-setup.js b/jest-setup.js deleted file mode 100644 index fbf15de..0000000 --- a/jest-setup.js +++ /dev/null @@ -1 +0,0 @@ -import '@testing-library/jest-native/extend-expect' diff --git a/jest.config.js b/jest.config.js index afea096..b280bcf 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,13 +1,18 @@ module.exports = { preset: 'jest-expo', - setupFilesAfterEnv: ['./jest.setup.ts'], + setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'], transform: { - '^.+\\.tsx?$': 'ts-jest', + '^.+\\.tsx?$': [ 'ts-jest', { + jsx: 'react', + module: 'commonjs', + target: 'es6', + strict: true, + esModuleInterop: true, + skipLibCheck: true, + forceConsistentCasingInFileNames: true, + types: [ 'jest', 'node' ], + } ], }, testPathIgnorePatterns: [ '/node_modules/', '/android/', '/ios/' ], - globals: { - 'ts-jest': { - tsconfig: 'tsconfig.jest.json', - }, - }, + testEnvironment: 'node', } diff --git a/src/hooks/useAuth.test.tsx b/src/hooks/useAuth.test.tsx new file mode 100644 index 0000000..f0b1bc4 --- /dev/null +++ b/src/hooks/useAuth.test.tsx @@ -0,0 +1,50 @@ +// @todo: need to adjust testing configs to support imports an file types. +// import { renderHook } from '@testing-library/react' +// import React, { ReactNode } from 'react' +// import { +// expect, describe, it, jest, +// } from '@jest/globals' +// import { User } from '@supabase/supabase-js' +// import useAuth from './useAuth' +// import { AuthContext } from '../providers/AuthProvider' +// import { AuthContextType } from '../types/auth' + +// describe( 'useAuth', () => { +// it( 'should throw an error if used outside of AuthProvider', () => { +// const { result } = renderHook( () => useAuth() ) + +// // Expect the hook to throw an error when used outside the AuthProvider +// expect( result.current.error ).toEqual( +// new Error( 'useAuth must be used within an AuthProvider' ), +// ) +// } ) + +// it( 'should return the context value when used within AuthProvider', () => { +// // @todo: need to setup a different parser for this. +// // Mock the context value conforming to AuthContextType +// const mockAuthContextValue: AuthContextType = { +// error: null, +// loading: false, +// session: null, +// // @ts-expect-error ignore for testing, not important. +// signUpNewUser: ( jest.fn().mockResolvedValue( undefined ) as jest.Mock ), +// // @ts-expect-error ignore for testing, not important. +// signInWithEmail: ( jest.fn().mockResolvedValue( undefined ) as jest.Mock ), +// // @ts-expect-error ignore for testing, not important. +// signOut: ( jest.fn().mockResolvedValue( undefined ) as jest.Mock ), +// user: { id: '1', email: 'test@example.com' } as User, // Mocking user object +// } + +// // Define the wrapper component with the correct context value +// const wrapper = ( { children }: { children: ReactNode } ) => ( +// +// {children} +// +// ) + +// const { result } = renderHook( () => useAuth(), { wrapper } ) + +// // Expect the hook to return the correct context value +// expect( result.current ).toEqual( mockAuthContextValue ) +// } ) +// } ) diff --git a/src/redux/todoReducers.ts b/src/redux/reducers/todoReducers.ts similarity index 99% rename from src/redux/todoReducers.ts rename to src/redux/reducers/todoReducers.ts index 23c3562..5a2b1a1 100644 --- a/src/redux/todoReducers.ts +++ b/src/redux/reducers/todoReducers.ts @@ -1,6 +1,6 @@ import { fetchLists, removeList, addList, fetchTodosByListId, addTodo, removeTodo, toggleTodoStatus, -} from './thunks/todoThunk' +} from '../thunks/todoThunk' /** * Extra reducers for handling local state from Supabase. diff --git a/src/redux/slices/todoSlice.ts b/src/redux/slices/todoSlice.ts index 49e1f96..30bdf75 100644 --- a/src/redux/slices/todoSlice.ts +++ b/src/redux/slices/todoSlice.ts @@ -1,6 +1,6 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit' import { Todo, TodoList } from '../../types/todos' -import todoReducers from '../todoReducers' +import todoReducers from '../reducers/todoReducers' interface TodosState { lists: TodoList[]; diff --git a/src/types/env.d.ts b/src/types/env.d.ts new file mode 100644 index 0000000..91cf18c --- /dev/null +++ b/src/types/env.d.ts @@ -0,0 +1,11 @@ +declare module 'expo-constants' { + interface Constants { + expoConfig: { + extra: { + GITHUB_CLIENT: string, + SUPABASE_KEY: string, + SUPABASE_CALLBACK: string, + }; + }; + } +}