Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configure jest as testing framework, and add some test code #36

Merged
merged 2 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
['@babel/preset-react', {runtime: 'automatic'}],
'@babel/preset-typescript',
],
};
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test": "jest",
"eject": "react-scripts eject"
},
"eslintConfig": {
Expand All @@ -56,6 +56,12 @@
]
},
"devDependencies": {
"babel-plugin-macros": "^3.1.0"
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.18.6",
"babel-jest": "^29.4.2",
"babel-plugin-macros": "^3.1.0",
"jest": "^29.4.2",
"react-test-renderer": "^18.2.0"
}
}
8 changes: 0 additions & 8 deletions src/App.test.js

This file was deleted.

7 changes: 4 additions & 3 deletions src/App.js → src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useDispatch } from 'react-redux';
import { setUser } from './store/user/user.slice';
import Spinner from './components/spinner/spinner.component';
import GlobalStyle from './global.styles';
import { User } from "firebase/auth";

const Home = lazy(()=>import('./routes/home/home.component'));
const Navigation = lazy(()=>import('./routes/navigation/navigation.component'));
Expand All @@ -17,10 +18,10 @@ const App = ()=> {

useEffect(() => {
const unsubscribe = onAuthStateChangeListener((user)=>{
const createUser = async (user) => {
const createUser = async (user: User|null) => {
if (!user) {return;}
// console.log(user); // here can't get displayName from userAuth
const userSnapshot = await createUserDocumentFromAuth(user);
const userSnapshot = await createUserDocumentFromAuth(user, {});
const userDoc = userSnapshot.data()
dispatch(setUser(userDoc));
};
Expand All @@ -34,7 +35,7 @@ const App = ()=> {
return (
<Fragment>
<GlobalStyle />
<Suspense callback={<Spinner />}>
<Suspense fallback={<Spinner />}>
<Routes>
<Route path="/" element={<Navigation />}>
<Route index element={<Home/>} />
Expand Down
9 changes: 9 additions & 0 deletions src/components/button/__snapshots__/button.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`changes the class when hovered 1`] = `
<button
className="sc-dkrFOg sc-eDvSVe bAenDU iJPJvM"
>
Test
</button>
`;
11 changes: 11 additions & 0 deletions src/components/button/button.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import renderer from 'react-test-renderer';
import Button, { BUTTON_TYPES } from './button.component';

it('changes the class when hovered', () => {
const component = renderer.create(
<Button buttonType={BUTTON_TYPES.inverted}>Test</Button>,
);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
}
);
1 change: 0 additions & 1 deletion src/components/cart-icon/cart-icon.styles.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import styled from 'styled-components';
import { ReactComponent as Svg } from "../../assets/shopping-bag.svg";
// TODO: load svg, migrate to typesript

export const CartIconContainer = styled.div`
width: 45px;
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { store } from './store/store';
import { store, persistor } from './store/store';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import { persistor } from './store/store';
import { Elements } from '@stripe/react-stripe-js';
import { stripePromise } from './utils/stripe/stripe.utils';
import * as serviceWorkerRegistration from './serviceWorkerRegistration';
Expand Down
34 changes: 34 additions & 0 deletions src/jest.resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// jest.resolver.js
module.exports = (path, options) => {
// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path, {
...options,
// Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
packageFilter: (pkg) => {
const pkgNamesToTarget = new Set([
'rxjs',
'@firebase/auth',
'@firebase/storage',
'@firebase/functions',
'@firebase/database',
'@firebase/auth-compat',
'@firebase/database-compat',
'@firebase/app-compat',
'@firebase/firestore',
'@firebase/firestore-compat',
'@firebase/messaging',
'@firebase/util',
'firebase',
'@stripe/stripe-js',
]);

if (pkgNamesToTarget.has(pkg.name)) {
// console.log('>>>', pkg.name)
delete pkg['exports'];
delete pkg['module'];
}

return pkg;
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { selectCategoriesMap } from '../../store/categories/categories.slice';
const CategoriesPreview = ()=> {
const isLoading = useSelector((state: RootState) => state.categories.isLoading);
const categoriesMap = useSelector(selectCategoriesMap);
console.log("categories:", categoriesMap);
// console.log("categories:", categoriesMap);
return (
<Fragment>
{isLoading ? <Spinner /> :
Expand Down
2 changes: 1 addition & 1 deletion src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

// then run the saga
sagaMiddleware.run(rootSaga)
sagaMiddleware.run(rootSaga);

export const persistor = persistStore(store);

Expand Down
2 changes: 1 addition & 1 deletion src/utils/firebase/firebase.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ provider.setCustomParameters({

export const signInWithGooglePopup = ()=> signInWithPopup(auth, provider);

export const db = getFirestore();
const db = getFirestore();

export type ObjectToAdd = {
title: string;
Expand Down
23 changes: 23 additions & 0 deletions src/utils/test/test.utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, {ReactElement} from 'react';
import { BrowserRouter } from 'react-router-dom';
import {render, RenderOptions} from '@testing-library/react';
import { store } from '../../store/store';
import { Provider } from 'react-redux';

const AllTheProviders = ({children}: {children: React.ReactNode}) => {
return (
<BrowserRouter>
<Provider store={store}>
{children}
</Provider>
</BrowserRouter>
)
};

const customRender = (
ui: ReactElement,
options?: Omit<RenderOptions, 'wrapper'>,
) => render(ui, {wrapper: AllTheProviders, ...options});

export * from '@testing-library/react';
export {customRender as render};
Loading