Skip to content

Commit

Permalink
Testing setup with jest and RTL
Browse files Browse the repository at this point in the history
  • Loading branch information
PS1242 committed Jun 22, 2023
1 parent efc7f68 commit 152855e
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 16 deletions.
20 changes: 10 additions & 10 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module.exports = {
env: { browser: true, es2020: true },
env: { browser: true, es2020: true, node: true, jest: true },
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:react/jsx-runtime',
'plugin:react-hooks/recommended',
"eslint:recommended",
"plugin:react/recommended",
"plugin:react/jsx-runtime",
"plugin:react-hooks/recommended",
],
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
settings: { react: { version: '18.2' } },
plugins: ['react-refresh'],
parserOptions: { ecmaVersion: "latest", sourceType: "module" },
settings: { react: { version: "18.2" } },
plugins: ["react-refresh"],
rules: {
'react-refresh/only-export-components': 'warn',
"react-refresh/only-export-components": "warn",
},
}
};
14 changes: 14 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "current",
},
},
],
"@babel/preset-react",
"@babel/preset-typescript",
],
};
1 change: 1 addition & 0 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/jest-dom";
19 changes: 17 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,36 @@
"dev": "vite",
"build": "vite build",
"lint": "eslint src --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
"preview": "vite preview",
"test": "jest"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"@babel/preset-env": "^7.22.5",
"@babel/preset-react": "^7.22.5",
"@babel/preset-typescript": "^7.22.5",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^4.0.0",
"eslint": "^8.38.0",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.5.0",
"vite": "^4.3.2"
},
"jest": {
"testEnvironment": "jsdom",
"setupFilesAfterEnv": [
"<rootDir>/jest-setup.js"
]
}
}
}
1 change: 0 additions & 1 deletion public/vite.svg

This file was deleted.

4 changes: 2 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createContext, useEffect, useState } from "react";
import ProductsPage from "./ProductsPage/ProductsPage";
import Cart from "./Cart/Cart";
import { getProducts } from "./api/api";
import "./App.css";

export const CartContext = createContext();
Expand All @@ -10,8 +11,7 @@ function App() {
const [cartItems, setCartItems] = useState([]);

const fetchProducts = async () => {
const data = await fetch("https://dummyjson.com/products");
const resp = await data.json();
const resp = await getProducts("https://dummyjson.com/products");
setProducts(resp.products.map((item) => ({ ...item, addedToCart: false })));
};

Expand Down
3 changes: 3 additions & 0 deletions src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test("that jest is working", () => {
expect(true).toBe(true);
});
5 changes: 5 additions & 0 deletions src/api/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export async function getProducts(url) {
const res = await fetch(url);
const data = await res.json();
return data;
}
1 change: 0 additions & 1 deletion src/assets/react.svg

This file was deleted.

0 comments on commit 152855e

Please sign in to comment.