Skip to content

Commit

Permalink
[test(global)][index/app]: setup initial testing environment
Browse files Browse the repository at this point in the history
DESCRIPTION: This commit sets up the initial testing environment for the application.

BREAKING_CHANGE: Error caused by react/swiper import not being found causes breaking change for tests.
  • Loading branch information
reedoooo committed May 7, 2024
1 parent ccea1ef commit d077c4e
Show file tree
Hide file tree
Showing 50 changed files with 475 additions and 714 deletions.
7 changes: 1 addition & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
"ecmaVersion": 2021,
"sourceType": "module"
},
"plugins": [
"react",
"react-hooks",
"@typescript-eslint",
"prettier"
],
"plugins": ["react", "react-hooks", "@typescript-eslint", "prettier"],
"rules": {
"no-console": "off",
"@typescript-eslint/indent": ["error", 2],
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: CI/CD

on:
push:
Expand Down Expand Up @@ -31,7 +31,7 @@ jobs:
run: npm install netlify-cli -g

- name: Deploy to Netlify
run: npx netlify deploy --dir=build --prod
run: npx netlify deploy --dir=src --prod
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
1 change: 1 addition & 0 deletions __mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'test-file-stub';
9 changes: 9 additions & 0 deletions __mocks__/swiper/react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// <rootDir>/__mocks__/swiper/react.js
import React from 'react';

export const Swiper = ({ children }) => (
<div className="mock-swiper">{children}</div>
);
export const SwiperSlide = ({ children }) => (
<div className="mock-swiper-slide">{children}</div>
);
104 changes: 61 additions & 43 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,56 +26,86 @@
"url": "https://github.com/reedvogt/enhanced-card-store/issues"
},
"dependencies": {
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@floating-ui/react": "^0.26.9",
"@fortawesome/fontawesome-free": "^6.4.0",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@floating-ui/react": "^0.26.13",
"@fortawesome/fontawesome-free": "^6.5.2",
"@hookform/resolvers": "^3.3.4",
"@mui/icons-material": "^5.14.5",
"@mui/joy": "^5.0.0-beta.16",
"@mui/lab": "^5.0.0-alpha.162",
"@mui/material": "^5.15.6",
"@mui/styled-engine-sc": "^6.0.0-alpha.12",
"@mui/system": "^5.15.5",
"@mui/x-charts": "^6.18.4",
"@mui/x-data-grid": "^6.16.2",
"@mui/x-date-pickers": "^6.10.1",
"@nivo/line": "^0.83.0",
"@nivo/tooltip": "^0.86.0",
"@stripe/react-stripe-js": "^2.1.1",
"@stripe/stripe-js": "^1.54.2",
"@mui/icons-material": "^5.15.16",
"@mui/joy": "^5.0.0-beta.36",
"@mui/lab": "^5.0.0-alpha.170",
"@mui/material": "^5.15.16",
"@mui/styled-engine-sc": "^6.0.0-alpha.18",
"@mui/system": "^5.15.15",
"@mui/x-charts": "^7.3.2",
"@mui/x-data-grid": "^7.3.2",
"@mui/x-date-pickers": "^7.3.2",
"@nivo/line": "^0.86.0",
"@stripe/react-stripe-js": "^2.7.1",
"@stripe/stripe-js": "^3.4.0",
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^15.0.6",
"axios": "^1.4.0",
"@testing-library/user-event": "^14.5.2",
"axios": "^1.6.8",
"chroma-js": "^2.4.2",
"dotenv": "^16.4.5",
"eslint-config-react-app": "^7.0.1",
"jwt-decode": "^3.1.2",
"jwt-decode": "^4.0.0",
"lodash": "^4.17.21",
"mathjs": "^12.4.1",
"mathjs": "^12.4.2",
"nanoid": "^5.0.7",
"notistack": "^3.0.1",
"react": "^18.3.1",
"react-cookie": "^4.1.1",
"react-cookie": "^7.1.4",
"react-device-detect": "^2.2.3",
"react-dom": "^18.3.1",
"react-error-boundary": "^4.0.13",
"react-helmet-async": "^2.0.4",
"react-hook-form": "^7.50.0",
"react-icons": "^4.10.1",
"react-hook-form": "^7.51.4",
"react-icons": "^5.2.1",
"react-perfect-scrollbar": "^1.5.8",
"react-router-dom": "^6.23.0",
"react-scroll-parallax": "^3.4.5",
"react-spring": "^9.7.3",
"react-stripe-checkout": "^2.6.3",
"react-table": "^7.8.0",
"react-transition-group": "^4.4.5",
"recharts": "^2.12.3",
"sass": "^1.69.7",
"stripe": "^12.14.0",
"styled-components": "^6.1.8",
"swiper": "^11.0.6",
"three": "^0.156.1",
"web-vitals": "^2.1.4",
"zod": "^3.22.4"
"recharts": "^2.12.6",
"sass": "^1.77.0",
"stripe": "^15.5.0",
"styled-components": "^6.1.9",
"swiper": "^11.1.1",
"three": "^0.164.1",
"web-vitals": "^3.5.2",
"zod": "^3.23.6"
},
"devDependencies": {
"@babel/core": "^7.24.5",
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/preset-env": "^7.24.5",
"@babel/preset-react": "^7.24.1",
"babel-jest": "^29.7.0",
"depcheck": "^1.4.7",
"eslint": "^8.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-react-hooks": "^4.6.2",
"http-proxy-middleware": "^3.0.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"react-scripts": "5.0.1"
},
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-private-property-in-object"
]
},
"eslintConfig": {
"extends": [
Expand Down Expand Up @@ -104,17 +134,5 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"depcheck": "^1.4.7",
"eslint": "^8.46.0",
"eslint-config-prettier": "^8.9.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-react-hooks": "^4.6.2",
"http-proxy-middleware": "^2.0.6",
"prettier": "^3.0.0",
"react-scripts": "5.0.1"
}
}
2 changes: 2 additions & 0 deletions setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import '@testing-library/jest-dom/extend-expect';
// Directly in your test file or in src/setupTests.js
3 changes: 1 addition & 2 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// App.js
import React, { Suspense, lazy, useEffect } from 'react';
import React, { Suspense, useEffect } from 'react';
import { Route, Routes, useNavigate } from 'react-router-dom';
import { CSSTransition, TransitionGroup } from 'react-transition-group';
import LoginDialog from 'layout/dialogs/LoginDialog';
Expand All @@ -11,7 +11,6 @@ import {
PageLayout,
} from 'layout/REUSABLE_COMPONENTS';
import {
useUserData,
useConfigurator,
useManageCookies,
useMode,
Expand Down
63 changes: 63 additions & 0 deletions src/__tests__/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// App.test.js
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { BrowserRouter as Router } from 'react-router-dom';
import App from 'App';

// Mocking modules and context
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => jest.fn(),
}));
jest.mock('context', () => ({
useManageCookies: () => ({
getCookie: jest.fn().mockReturnValue({ authUser: null, isLoggedIn: false }),
}),
}));
jest.mock('context', () => ({
useMode: () => ({ theme: {} }),
}));
jest.mock('context', () => ({
useAuthManager: () => ({
logout: jest.fn(),
}),
}));
jest.mock('context', () => ({
useConfigurator: () => ({
isConfiguratorOpen: false,
}),
}));
jest.mock('layout/dialogs/LoginDialog', () => () => <div>LoginDialog</div>);
jest.mock('layout/navigation', () => () => <div>Navigation</div>);
jest.mock('layout/REUSABLE_COMPONENTS/Configurator', () => () => (
<div>Configurator</div>
));
jest.mock('layout/REUSABLE_COMPONENTS', () => () => (
<div>LoadingOverlay</div>
));
jest.mock('layout/REUSABLE_COMPONENTS', () => () => (
<div>PageLayout</div>
));

// Helper to wrap component with router context
const renderWithRouter = (ui, { route = '/' } = {}) => {
window.history.pushState({}, 'Test page', route);
return render(ui, { wrapper: Router });
};

describe('App Component', () => {
it('should redirect to login if not authenticated', async () => {
renderWithRouter(<App />);
await waitFor(() =>
expect(screen.getByText('LoginDialog')).toBeInTheDocument()
);
});

it('should display the navigation and page layout', () => {
renderWithRouter(<App />);
expect(screen.getByText('Navigation')).toBeInTheDocument();
expect(screen.getByText('PageLayout')).toBeInTheDocument();
});

// Add more tests as needed for different scenarios
});
2 changes: 1 addition & 1 deletion src/context/ColorModeProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useMemo, createContext, useEffect } from 'react';
import { createTheme } from '@mui/material';
import { themeSettings } from 'assets/themeSettings';
import { useManageCookies } from './hooks';
import { useManageCookies } from 'context';

export const ColorModeContext = createContext({
mode: 'dark',
Expand Down
4 changes: 0 additions & 4 deletions src/context/hooks/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import useEventHandlers from './useEventHandlers';
import useFetchWrapper from './useFetchWrapper';
import useGridItems from './useGridItems';
import useLoading from './useLoading';
import useLogger from './useLogger';
import useManageCookies from './useManageCookies';
import usePagination from './usePagination';
import useSelectedContext from './useSelectedContext';
import usePopover from './usePopover';
import useSelectorActions from './useSelectorActions';
import useFormManagement from './useFormManagement';
Expand All @@ -25,10 +23,8 @@ export {
useFetchWrapper,
useGridItems,
useLoading,
useLogger,
useManageCookies,
usePagination,
useSelectedContext,
usePopover,
useSelectorActions,
useFormManagement,
Expand Down
1 change: 0 additions & 1 deletion src/context/hooks/useBreakPoint.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useTheme } from '@mui/material/styles';
import useMediaQuery from '@mui/material/useMediaQuery';
import { useMode } from 'context';

Expand Down
29 changes: 0 additions & 29 deletions src/context/hooks/useDebounce.jsx

This file was deleted.

1 change: 0 additions & 1 deletion src/context/hooks/useDialogState.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// hooks/useDialogState.js
import { useState, useCallback } from 'react';

const useDialogState = () => {
Expand Down
3 changes: 1 addition & 2 deletions src/context/hooks/useFetchWrapper.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState, useCallback, useEffect } from 'react';
import useLogger from './useLogger';
import { useState, useCallback } from 'react';
import useLocalStorage from './useLocalStorage';
import useLoading from './useLoading';
import { useSnackbar } from 'notistack';
Expand Down
1 change: 1 addition & 0 deletions src/context/hooks/useFormManagement.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState, useCallback } from 'react';

const useFormManagement = (initialFormKey, formSchemas) => {
const [activeForm, setActiveForm] = useState(initialFormKey);

Expand Down
7 changes: 5 additions & 2 deletions src/context/hooks/useGridItems.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React, { useMemo } from 'react';
import { Grid, Grow, IconButton, Tooltip } from '@mui/material';
import GenericCard from 'layout/cards/GenericCard';
import HighlightOffRoundedIcon from '@mui/icons-material/HighlightOffRounded';
import { useCardStore, useManager, useMode } from 'context';

import { MDBox, SkeletonCard } from 'layout/REUSABLE_COMPONENTS';
import GenericCard from 'layout/cards/GenericCard';

import { useCardStore, useManager, useMode } from 'context';

const useGridItems = ({
itemsPerPage,
cards,
Expand Down
4 changes: 0 additions & 4 deletions src/context/hooks/useLocalStorage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ function useLocalStorage(key, initialValue) {
url: window.location.href,
});
window.dispatchEvent(storageEvent);
// Optionally, for cross-tab communication, you might want to trigger a storage event manually
// window.dispatchEvent(
// new Event('storage', { key, newValue: JSON.stringify(valueToStore) })
// );
} catch (error) {
console.error(`Error setting localStorage key "${key}":`, error);
}
Expand Down
Loading

0 comments on commit d077c4e

Please sign in to comment.