Explorar el Código

Test mini project: redux without hooks

serg1557733 hace 1 año
commit
ef8fa18db1
Se han modificado 13 ficheros con 28936 adiciones y 0 borrados
  1. 28738 0
      package-lock.json
  2. 40 0
      package.json
  3. 34 0
      public/index.html
  4. 8 0
      src/App.test.js
  5. 5 0
      src/actions.js
  6. 6 0
      src/components/App.js
  7. 22 0
      src/components/Counter.js
  8. 13 0
      src/index.css
  9. 22 0
      src/index.js
  10. 27 0
      src/reducer.js
  11. 3 0
      src/redux.js
  12. 13 0
      src/reportWebVitals.js
  13. 5 0
      src/setupTests.js

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 28738 - 0
package-lock.json


+ 40 - 0
package.json

@@ -0,0 +1,40 @@
+{
+  "name": "testredux",
+  "version": "0.1.0",
+  "private": true,
+  "dependencies": {
+    "@testing-library/jest-dom": "^5.16.4",
+    "@testing-library/react": "^13.3.0",
+    "@testing-library/user-event": "^13.5.0",
+    "react": "^18.2.0",
+    "react-dom": "^18.2.0",
+    "react-redux": "^8.0.2",
+    "react-scripts": "5.0.1",
+    "redux": "^4.2.0",
+    "web-vitals": "^2.1.4"
+  },
+  "scripts": {
+    "start": "react-scripts start",
+    "build": "react-scripts build",
+    "test": "react-scripts test",
+    "eject": "react-scripts eject"
+  },
+  "eslintConfig": {
+    "extends": [
+      "react-app",
+      "react-app/jest"
+    ]
+  },
+  "browserslist": {
+    "production": [
+      ">0.2%",
+      "not dead",
+      "not op_mini all"
+    ],
+    "development": [
+      "last 1 chrome version",
+      "last 1 firefox version",
+      "last 1 safari version"
+    ]
+  }
+}

+ 34 - 0
public/index.html

@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8" />
+    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
+    <meta name="viewport" content="width=device-width, initial-scale=1" />
+    <meta name="theme-color" content="#000000" />
+    <meta
+      name="description"
+      content="Web site created using create-react-app"
+    />
+    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
+    <!--
+      manifest.json provides metadata used when your web app is installed on a
+      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
+    -->
+    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
+    <!--
+      Notice the use of %PUBLIC_URL% in the tags above.
+      It will be replaced with the URL of the `public` folder during the build.
+      Only files inside the `public` folder can be referenced from the HTML.
+
+      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
+      work correctly both with client-side routing and a non-root public URL.
+      Learn how to configure a non-root public URL by running `npm run build`.
+    -->
+    <title>React App</title>
+  </head>
+  <body>
+    <noscript>You need to enable JavaScript to run this app.</noscript>
+    <div id="root"></div
+
+  </body>
+</html>

+ 8 - 0
src/App.test.js

@@ -0,0 +1,8 @@
+import { render, screen } from '@testing-library/react';
+import App from './App';
+
+test('renders learn react link', () => {
+  render(<App />);
+  const linkElement = screen.getByText(/learn react/i);
+  expect(linkElement).toBeInTheDocument();
+});

+ 5 - 0
src/actions.js

@@ -0,0 +1,5 @@
+
+export const dec = () => ({type: 'DEC'});
+export const inc = () => ({type: 'INC'});
+export const rnd = () => ({type: 'RND', payload: Math.floor(Math.random() * 10)});
+

+ 6 - 0
src/components/App.js

@@ -0,0 +1,6 @@
+import Counter from './Counter';
+
+const App = () =>  <Counter/>
+
+export default App;
+

+ 22 - 0
src/components/Counter.js

@@ -0,0 +1,22 @@
+ import { connect } from "react-redux";
+ import * as action from '../actions'; 
+ 
+ const Counter = ({ counter, inc, dec, rnd}) => {
+    return (
+            <>
+                <div > {counter} </div>
+                <button onClick={dec} > - </button>
+                <button onClick={inc} > + </button>
+                <button onClick={rnd} > rnd </button>
+            </> 
+        )
+    }
+
+const mapStateToProps = (state) => {
+    return (
+        {
+            counter: state.value,
+        }
+    )
+}
+export default connect(mapStateToProps, action )(Counter);

+ 13 - 0
src/index.css

@@ -0,0 +1,13 @@
+body {
+  margin: 0;
+  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
+    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
+    sans-serif;
+  -webkit-font-smoothing: antialiased;
+  -moz-osx-font-smoothing: grayscale;
+}
+
+code {
+  font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
+    monospace;
+}

+ 22 - 0
src/index.js

@@ -0,0 +1,22 @@
+import React from 'react';
+import ReactDOM from 'react-dom/client';
+import App from './components/App';
+import { Provider } from 'react-redux';
+import {createStore} from 'redux';
+import reducer from './reducer';
+
+
+export const store = createStore(reducer);
+
+
+const root = ReactDOM.createRoot(document.getElementById('root'))
+root.render(
+        <Provider store = {store} >
+            <App />
+        </Provider>
+    )
+ 
+
+
+
+

+ 27 - 0
src/reducer.js

@@ -0,0 +1,27 @@
+
+const initialState = {
+    value: 5,    
+  }
+  
+const reducer = (state = initialState, action) => {
+    switch (action.type){
+    case 'INC':  
+      return {...state, 
+        value:
+          state.value + 1
+        };
+    case 'DEC':
+     return {...state, 
+      value:
+        state.value - 1
+      };
+    case 'RND':
+      return {...state, 
+        value: state.value * action.payload
+      };
+    default:
+      return state
+    }
+  };
+
+ export default reducer;

+ 3 - 0
src/redux.js

@@ -0,0 +1,3 @@
+
+
+

+ 13 - 0
src/reportWebVitals.js

@@ -0,0 +1,13 @@
+const reportWebVitals = onPerfEntry => {
+  if (onPerfEntry && onPerfEntry instanceof Function) {
+    import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
+      getCLS(onPerfEntry);
+      getFID(onPerfEntry);
+      getFCP(onPerfEntry);
+      getLCP(onPerfEntry);
+      getTTFB(onPerfEntry);
+    });
+  }
+};
+
+export default reportWebVitals;

+ 5 - 0
src/setupTests.js

@@ -0,0 +1,5 @@
+// jest-dom adds custom jest matchers for asserting on DOM nodes.
+// allows you to do things like:
+// expect(element).toHaveTextContent(/react/i)
+// learn more: https://github.com/testing-library/jest-dom
+import '@testing-library/jest-dom';