diff --git a/.env.example b/.env.example index 5ea9f001..d8516b00 100644 --- a/.env.example +++ b/.env.example @@ -1 +1 @@ -VITE_API_URL="http://localhost:8083" \ No newline at end of file +VITE_API_URL="http://localhost:8080/v1/" \ No newline at end of file diff --git a/src/components/Dashboard/MyCollections/MyCollectionsContent/index.test.tsx b/src/components/Dashboard/MyCollections/MyCollectionsContent/index.test.tsx index cc9e0997..1a19ffc4 100644 --- a/src/components/Dashboard/MyCollections/MyCollectionsContent/index.test.tsx +++ b/src/components/Dashboard/MyCollections/MyCollectionsContent/index.test.tsx @@ -3,7 +3,7 @@ import { MantineProvider } from '@mantine/core'; import MyCollectionsContent from './index'; import Collection from '../../../../models/user/collection'; import { mockCollection } from '../../../../test-utils/mocks/models/collection'; -import { mockPagination } from '../../../../test-utils/mocks'; +import { mockPagination } from '../../../../test-utils/mocks/pagination'; // Mock the CollectionCard component jest.mock('./CollectionCard', () => ({ diff --git a/src/components/Dashboard/MyCollections/index.test.tsx b/src/components/Dashboard/MyCollections/index.test.tsx index d51fdd89..fe30c765 100644 --- a/src/components/Dashboard/MyCollections/index.test.tsx +++ b/src/components/Dashboard/MyCollections/index.test.tsx @@ -4,7 +4,7 @@ import MyCollections from './index'; import User from '../../../models/user/user'; import { UserCollectionsContext, UserCollectionsContextState } from '../../../contexts/UserCollectionsContext'; import CollectionManagementRequests from '../../../services/requests/CollectionManagementRequests'; -import { mockPagination } from '../../../test-utils/mocks'; +import { mockPagination } from '../../../test-utils/mocks/pagination'; import { renderWithProviders } from '../../../test-utils'; import Collection from '../../../models/user/collection'; import CollectionItem from '../../../models/user/collection-items'; diff --git a/src/test-utils/mocks/contexts.tsx b/src/test-utils/mocks/contexts.tsx deleted file mode 100644 index c9ad7347..00000000 --- a/src/test-utils/mocks/contexts.tsx +++ /dev/null @@ -1,138 +0,0 @@ -import React, { PropsWithChildren } from 'react'; -import { MeContextStateConsumer, MeContext } from '../../contexts/MeContext'; -import { placeholderUser } from '../../models/user/user'; -import User from '../../models/user/user'; -import { CategoriesContextState } from '../../contexts/CategoriesContext'; -import Category from '../../models/category'; -import { Provider } from 'react-redux'; -import configureStore from 'redux-mock-store'; -import { mockCategory } from './models/category'; -import { mockPagination } from './pagination'; - -// Mock appState -// eslint-disable-next-line @typescript-eslint/no-explicit-any -(global as any).appState = { - state: { - persistent: { - tokenData: null - }, - session: { - loadingCount: 0 - } - }, - dispatch: jest.fn() -}; - -// Mock functions -export const mockSetFilter = jest.fn(); - -// Base mock context state creator -export const createBaseMockContextState = (data: T[]) => mockPagination({ - loadedData: data, - initialLoadComplete: true, - refreshing: false, - hasAnotherPage: false, - initiated: true, - noResults: false, - expands: [], - order: {}, - filter: {}, - search: {}, - limit: 20, - loadAll: false, - setFilter: jest.fn(), - setSearch: jest.fn(), - setOrder: jest.fn(), - // eslint-disable-next-line @typescript-eslint/no-unused-vars - addModel: jest.fn((_model: T) => {}), - // eslint-disable-next-line @typescript-eslint/no-unused-vars - removeModel: jest.fn((_model: T) => {}), - getModel: jest.fn((id: number) => data.find(item => item.id === id) || null), - params: {}, - loadNext: jest.fn(), - refreshData: jest.fn() -}); - -// Mock CategoriesContext -export const mockCategoriesContextValue = { - ...createBaseMockContextState([ - mockCategory({ id: 1, name: 'Test Category', can_be_primary: true }) - ]) -}; - -export const mockCategoriesContextValueLoading = { - ...createBaseMockContextState([]), - initialLoadComplete: false, - isLoading: true -}; - -export const mockCategoriesContextValueEmpty = { - ...createBaseMockContextState([]), - noResults: true -}; - -// Mock CategoriesContext -jest.mock('../../contexts/CategoriesContext', () => ({ - __esModule: true, - CategoriesContext: React.createContext(mockCategoriesContextValue) -})); - -// Create mock store -const mockStore = configureStore([]); - -type MeContextProviderProps = { - initialState?: { - me: { - user: User; - networkError: boolean; - isLoggedIn: boolean; - isLoading: boolean; - }; - }; - hideLoadingSpace?: boolean; -} - -// MeContext Provider -export const MeContextProvider: React.FC> = ({ children, initialState, hideLoadingSpace }) => { - const store = mockStore(initialState || { - me: { - user: placeholderUser(), - networkError: false, - isLoggedIn: false, - isLoading: false - } - }); - - const [meContext, setMeContext] = React.useState({ - me: initialState?.me?.user || placeholderUser(), - networkError: initialState?.me?.networkError || false, - isLoggedIn: initialState?.me?.isLoggedIn || false, - isLoading: initialState?.me?.isLoading || false, - }); - - const fullContext = { - ...meContext, - setMe: (user: User) => { - setMeContext(prev => ({ - ...prev, - me: user, - isLoggedIn: !!user.id, - isLoading: false - })); - store.dispatch({ type: 'SET_USER', payload: user }); - }, - } as MeContextStateConsumer; - - return ( - - - {(!meContext.isLoading || hideLoadingSpace) ? children : - (meContext.networkError ? -
Network Error
: -
Loading...
- ) - } -
-
- ); -}; \ No newline at end of file diff --git a/src/test-utils/mocks/contexts/base.ts b/src/test-utils/mocks/contexts/base.ts new file mode 100644 index 00000000..364209d9 --- /dev/null +++ b/src/test-utils/mocks/contexts/base.ts @@ -0,0 +1,46 @@ +import Category from '../../../models/category'; +import { mockPagination } from '../pagination'; + +// Mock appState +// eslint-disable-next-line @typescript-eslint/no-explicit-any +(global as any).appState = { + state: { + persistent: { + tokenData: null + }, + session: { + loadingCount: 0 + } + }, + dispatch: jest.fn() +}; + +// Mock functions +export const mockSetFilter = jest.fn(); + +// Base mock context state creator +export const createBaseMockContextState = (data: T[]) => mockPagination({ + loadedData: data, + initialLoadComplete: true, + refreshing: false, + hasAnotherPage: false, + initiated: true, + noResults: false, + expands: [], + order: {}, + filter: {}, + search: {}, + limit: 20, + loadAll: false, + setFilter: jest.fn(), + setSearch: jest.fn(), + setOrder: jest.fn(), + // eslint-disable-next-line @typescript-eslint/no-unused-vars + addModel: jest.fn((_model: T) => {}), + // eslint-disable-next-line @typescript-eslint/no-unused-vars + removeModel: jest.fn((_model: T) => {}), + getModel: jest.fn((id: number) => data.find(item => item.id === id) || null), + params: {}, + loadNext: jest.fn(), + refreshData: jest.fn() +}); \ No newline at end of file diff --git a/src/test-utils/mocks/contexts/categories.ts b/src/test-utils/mocks/contexts/categories.ts new file mode 100644 index 00000000..461d3ab0 --- /dev/null +++ b/src/test-utils/mocks/contexts/categories.ts @@ -0,0 +1,29 @@ +import React from 'react'; +import { CategoriesContextState } from '../../../contexts/CategoriesContext'; +import Category from '../../../models/category'; +import { mockCategory } from '../models/category'; +import { createBaseMockContextState } from './base'; + +// Mock CategoriesContext +export const mockCategoriesContextValue = { + ...createBaseMockContextState([ + mockCategory({ id: 1, name: 'Test Category', can_be_primary: true }) + ]) +}; + +export const mockCategoriesContextValueLoading = { + ...createBaseMockContextState([]), + initialLoadComplete: false, + isLoading: true +}; + +export const mockCategoriesContextValueEmpty = { + ...createBaseMockContextState([]), + noResults: true +}; + +// Mock CategoriesContext +jest.mock('../../../contexts/CategoriesContext', () => ({ + __esModule: true, + CategoriesContext: React.createContext(mockCategoriesContextValue) +})); \ No newline at end of file diff --git a/src/test-utils/mocks/contexts/index.ts b/src/test-utils/mocks/contexts/index.ts new file mode 100644 index 00000000..5f741350 --- /dev/null +++ b/src/test-utils/mocks/contexts/index.ts @@ -0,0 +1,12 @@ +// Base context utilities +export { createBaseMockContextState, mockSetFilter } from './base'; + +// Categories context mocks +export { + mockCategoriesContextValue, + mockCategoriesContextValueLoading, + mockCategoriesContextValueEmpty +} from './categories'; + +// MeContext provider and types +export { MeContextProvider, type MeContextProviderProps } from './me'; \ No newline at end of file diff --git a/src/test-utils/mocks/contexts/me.tsx b/src/test-utils/mocks/contexts/me.tsx new file mode 100644 index 00000000..e6cdfc8b --- /dev/null +++ b/src/test-utils/mocks/contexts/me.tsx @@ -0,0 +1,66 @@ +import React, { PropsWithChildren } from 'react'; +import { MeContextStateConsumer, MeContext } from '../../../contexts/MeContext'; +import { placeholderUser } from '../../../models/user/user'; +import User from '../../../models/user/user'; +import { Provider } from 'react-redux'; +import configureStore from 'redux-mock-store'; + +// Create mock store +const mockStore = configureStore([]); + +export type MeContextProviderProps = { + initialState?: { + me: { + user: User; + networkError: boolean; + isLoggedIn: boolean; + isLoading: boolean; + }; + }; + hideLoadingSpace?: boolean; +} + +// MeContext Provider +export const MeContextProvider: React.FC> = ({ children, initialState, hideLoadingSpace }) => { + const store = mockStore(initialState || { + me: { + user: placeholderUser(), + networkError: false, + isLoggedIn: false, + isLoading: false + } + }); + + const [meContext, setMeContext] = React.useState({ + me: initialState?.me?.user || placeholderUser(), + networkError: initialState?.me?.networkError || false, + isLoggedIn: initialState?.me?.isLoggedIn || false, + isLoading: initialState?.me?.isLoading || false, + }); + + const fullContext = { + ...meContext, + setMe: (user: User) => { + setMeContext(prev => ({ + ...prev, + me: user, + isLoggedIn: !!user.id, + isLoading: false + })); + store.dispatch({ type: 'SET_USER', payload: user }); + }, + } as MeContextStateConsumer; + + return ( + + + {(!meContext.isLoading || hideLoadingSpace) ? children : + (meContext.networkError ? +
Network Error
: +
Loading...
+ ) + } +
+
+ ); +}; \ No newline at end of file diff --git a/src/test-utils/mocks/index.ts b/src/test-utils/mocks/index.ts deleted file mode 100644 index 77f4f57e..00000000 --- a/src/test-utils/mocks/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './pagination'; \ No newline at end of file diff --git a/src/testing/wrappers.tsx b/src/testing/wrappers.tsx deleted file mode 100644 index e41519fa..00000000 --- a/src/testing/wrappers.tsx +++ /dev/null @@ -1,5 +0,0 @@ -import { configure } from 'enzyme'; -import Adapter from '@cfaester/enzyme-adapter-react-18'; - -configure({adapter: new Adapter()}); -