I have a two test suites. One actually makes API calls, and seems to run in about 20 seconds. Other one, is literally empty and takes over 248 seconds. Two of the tests are todo, and one had some implementation that I commented out. It's still taking forever. Why is this?
import { render, waitFor, screen } from "#testing-library/react";
import MyContent from "./MyContent.js";
describe('MyContent Component', () => {
it.todo('should call API when search button is clicked');
it('should render some text when api responds', () => {
/*
render(<MyContent />);
await waitFor(() => {
screen.getByText("some text");
});
*/
});
it.todo('should render error message when api fails');
});
Here is my jest config defined inside package.json
"jest": {
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
},
"testEnvironment": "jsdom"
},
I need to use jsdom because there is a window.location in existing code.
Update: Adding component code
import React, { useState } from "react";
import { Input } from "#progress/kendo-react-inputs";
import { Label } from "#progress/kendo-react-labels";
import * as Constants from "../common/Constants.js";
import SearchTextBox from "../../components/SearchTextbox/SearchTextbox.js";
import someService from "../../services/someService.js";
import { Update } from "#material-ui/icons";
function MyContent(props) {
const isThatPage = props.pageIdentifier == Constants.PageIdentifier.thatPage;
const [eDetailsOutput, seteDetailsOutput] = useState({
a: null,
b: null,
c: null,
d: null
});
const handleChange = (e) => {
let dataToPush = [];
dataToPush.push({ key: e.target.name, value: e.target.value });
if (props.handleChange) {
props.handleChange(dataToPush);
}
};
const thatInput = {
q: null,
w: null,
eId: null,
x: null,
z: props.z
};
const handleSearchClick = () => {
someService.getthatDetailsBya(thatInput)
.then((response) => {
let updatedValue = {};
updatedValue = {
a: response?.eDetails.a,
b: response?.eDetails.b,
c: response?.eDetails.c,
d: response?.eDetails.d
}
seteDetailsOutput(eDetailsOutput => ({
...eDetailsOutput,
...updatedValue
}));
})
.catch((error) => {
goToAPIErrorPage(error?.message);
});
};
return (
<div className="k-form form_margin">
<div className="k-geo-form">
<div className="card">
<div className="card-item">
<div className="row">
<div className="col-lg-4 col-md-4 col-sm-4 col-xs-4 form-group">
<Label>el Name</Label>
{isThatPage ? (
<SearchTextBox
id="z-textbox"
name="z"
value={props.z ?? ""}
handleChange={handleChange}
readOnly={props.isReadOnly}
handleSearchClick={handleSearchClick}
/>
) : (
<Input
id="z-textbox"
name="z"
value={props.z ?? ""}
onChange={handleChange}
className={props.isReadOnly ? "readOnlyInput" : ""}
readonly={props.isReadOnly}
/>
)}
</div>
<div className="col-lg-4 col-md-4 col-sm-4 col-xs-4 form-group">
<Label>a</Label>
{isThatPage ? (
<SearchTextBox
id="a-textbox"
name="a"
// value={props.a ?? ""}
value={eDetailsOutput.a ?? "aIII"}
handleChange={handleChange}
readOnly={props.isReadOnly}
handleSearchClick={handleSearchClick}
/>
) : (
<Input
id="a-textbox"
name="a"
value={props.a ?? ""}
onChange={handleChange}
className={props.isReadOnly ? "readOnlyInput" : ""}
readonly={props.isReadOnly}
/>
)}
</div>
<div className="col-lg-4 col-md-4 col-sm-4 col-xs-4 form-group">
<Label>Entity Category Type</Label>
<Input
id="b-textbox"
name="b"
// value={props.b ?? ""}
value={eDetailsOutput.b ?? "beee"}
onChange={handleChange}
className={props.isReadOnly ? "readOnlyInput" : ""}
readonly={props.isReadOnly}
/>
</div>
</div>
<div className="row">
<div className="col-lg-4 col-md-4 col-sm-4 col-xs-4 form-group">
<Label>Status</Label>
<Input
id="Status-textbox"
name="Status"
// value={props.Status ?? ""}
value={eDetailsOutput.c ?? "csssss"}
onChange={handleChange}
className={props.isReadOnly ? "readOnlyInput" : ""}
readonly={props.isReadOnly}
/>
</div>
<div className="col-lg-4 col-md-4 col-sm-4 col-xs-4 form-group">
<Label>Other Status</Label>
<Input
id="OtherStatus-textbox"
name="OtherStatus"
value={props.OtherStatus ?? ""}
onChange={handleChange}
className={props.isReadOnly ? "readOnlyInput" : ""}
readonly={props.isReadOnly}
/>
</div>
<div className="col-lg-4 col-md-4 col-sm-4 col-xs-4 form-group">
<Label>Some Role</Label>
<Input
id="Role-textbox"
name="Role"
value={props.Role ?? ""}
onChange={handleChange}
className={props.isReadOnly ? "readOnlyInput" : ""}
readonly={props.isReadOnly}
/>
</div>
</div>
<div className="row">
<div className="col-lg-4 col-md-4 col-sm-4 col-xs-4 form-group">
<Label>Some Type</Label>
<Input
id="Type-textbox"
name="Type"
value={eDetailsOutput.d ?? "deeeeee"}
onChange={handleChange}
className={props.isReadOnly ? "readOnlyInput" : ""}
readonly={props.isReadOnly}
/>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
export default MyContent;
Related
Uncaught TypeError: Cannot read properties of null (reading 'imageLink')
The above error occurred in the <ProductMoreDetails> component:
This Products page Display all the products in the database. When I click on any product card it will redirects to the more details page of that product. First time it loads without any issue and when I refresh the page or backward the page it will disappear all the content in the page.
ProductMoreDetails.js
import Navbar from "../components/Navbar";
import { useProductsContext } from "../hooks/useProductsContext";
import { useParams } from "react-router-dom";
import { useEffect } from "react";
//setproducts
const ProductMoreDetails = () => {
const {products,dispatch} = useProductsContext();
const {id} = useParams();
useEffect(() => {
const fetchProducts = async () => {
const response = await fetch(`/api/products/${id}`)
const json = await response.json()
if (response.ok) {
dispatch({type: 'SET_PRODUCTS', payload: json})
}
}
fetchProducts()
},[dispatch,id])
return (
<div className="moredetails">
<Navbar/>
<br /><br />
<div className="details">
<img className="productImage" src={products.imageLink} alt="productImage" />
<div className="details-section"></div>
<div className="row section1">
<div className="col-lg-8">
</div>
<div className="col-lg-2">
<button className="btn btn-primary addnew">Add to Cart</button>
</div>
</div>
<div className="row section1">
<div className="col-lg-8">
<h1>{products.productName}</h1>
</div>
<div className="col-lg-2">
<h1>Rs. {products.price}</h1>
</div>
</div>
<div className="section1">
<hr />
<h1>Product Details</h1>
<br />
<div className="row">
<div className="col-lg-6">
<div className="row">
<div className="col-lg-6">
<h6>Category</h6>
</div>
<div className="col-lg-6">
<p>{products.category}</p>
</div>
</div>
</div>
<div className="col-lg-6">
<div className="row">
<div className="col-lg-6">
<h6>Brand</h6>
</div>
<div className="col-lg-6">
<p>{products.brand}</p>
</div>
</div>
</div>
</div>
<br />
<div className="row">
<div className="col-lg-6">
<div className="row">
<div className="col-lg-6">
<h6>Model</h6>
</div>
<div className="col-lg-6">
<p>{products.model}</p>
</div>
</div>
</div>
<div className="col-lg-6">
<div className="row">
<div className="col-lg-6">
<h6>Year</h6>
</div>
<div className="col-lg-6">
<p>{products.year}</p>
</div>
</div>
</div>
</div>
<br />
<div className="row">
<div className="col-lg-6">
<div className="row">
<div className="col-lg-6">
<h6>Features</h6>
</div>
<div className="col-lg-6">
<p>{products.features}</p>
</div>
</div>
</div>
</div>
<hr />
<div className="section2">
<h1>Product Description</h1>
</div>
<p>
{products.description}
</p>
</div>
</div>
</div>
)
}
export default ProductMoreDetails;
ProductContext.js
import { createContext } from "react";
import { useReducer } from "react";
export const ProductsContext = createContext();
export const productsReducer = (state, action) => {
switch (action.type) {
// set products
case "SET_PRODUCTS":
return {
...state,
products: action.payload,
}
//set a single product
case "SET_PRODUCT":
return {
...state,
products: action.payload,
}
case 'CREATE_PRODUCT':
return {
products: [action.payload, ...state.products]
}
case 'DELETE_PRODUCT':
return {
products: state.products.filter((product) => product._id !== action.payload._id)
}
case 'UPDATE_PRODUCT':
return {
products: state.products.map((product) => product._id === action.payload._id ? action.payload : product)
}
default:
return state;
}
}
export const ProductsContextProvider = ({ children }) => {
const [state, dispatch] = useReducer(productsReducer, {
products: null
})
return (
<ProductsContext.Provider value={{...state, dispatch}}>
{ children }
</ProductsContext.Provider>
)
}
I am trying to push info from my sign up page . I can display it and even get an error 500 but i see a path validation even though i can directly post from postman to my db. Here is my signUp page . My model and route is written all the same so i dont understand
import React from "react";
import {
MDBBtn,
MDBContainer,
MDBRow,
MDBCol,
MDBCard,
MDBCardBody,
MDBCardImage,
MDBInput,
MDBIcon,
MDBCheckbox,
} from "mdb-react-ui-kit";
import Navbar from "./Navbar";
import Footer from "./Footer";
import {useState} from "react";
// const initFormData = Object({
// //declaration de l'objet initiale pour recevoir les credentials
// username: "",
// email: "",
// password: "",
// // passwordCheck: "",ls
// });
const SignUpForm = () => {
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const collectData = async () => {
console.warn("warn" , username,email,password)
let result = await fetch("http://localhost:8000/api/auth/register",{
method: "post",
body: JSON.stringify({username,email,password}),
headers:{
'Content-type':'shop/JSON'
}
});
result = await result.json();
console.warn(result);
}
return (
<>
<Navbar />
<MDBContainer fluid>
<MDBCard className="text-black m-5" style={{borderRadius: "25px"}}>
<MDBCardBody>
<MDBRow>
<MDBCol
md="10"
lg="6"
className="order-2 order-lg-1 d-flex flex-column align-items-center"
>
<p className="text-center h1 fw-bold mb-5 mx-1 mx-md-4 mt-4">
Sign up
</p>
<div className="d-flex flex-row align-items-center mb-4 ">
<MDBIcon fas icon="user me-3" size="lg" />
<MDBInput
label="Votre Nom"
id="form1"
type="text"
className="w-100"
value={username}
onChange={(e) => setUsername("username", e.target.value)}
/>
</div>
<div className="d-flex flex-row align-items-center mb-4 ">
<MDBIcon fas icon="user me-3" size="lg" />
<MDBInput
label="Email"
id="email"
type="email"
className="w-100"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</div>
<div className="d-flex flex-row align-items-center mb-4">
<MDBIcon fas icon="lock me-3" size="lg" />
<MDBInput
label="Mot de passe"
id="form3"
type="password"
name="password"
value={password}
onChange={(e) => setPassword("password" , e.target.value)}
/>
</div>
<div className="mb-4">
<MDBCheckbox
name="flexCheck"
value=""
id="flexCheckDefault"
label="Subscribe to our newsletter"
/>
</div>
<MDBBtn className="mb-4" size="lg" onClick={collectData()}>
Register
</MDBBtn>
</MDBCol>
<MDBCol
md="10"
lg="6"
className="order-1 order-lg-2 d-flex align-items-center"
>
<MDBCardImage
src="https://mdbcdn.b-cdn.net/img/Photos/new-templates/bootstrap-registration/draw1.webp"
fluid
/>
</MDBCol>
</MDBRow>
</MDBCardBody>
</MDBCard>
</MDBContainer>
<Footer />
</>
);
};
export default SignUpForm;
i expected to be able to push but i get
Hello i want asking about pushing array from form
This is sample my code post form to id
how to turn bellow form from post to id, to push to array.
function handleName(e) {
setInputForm({ ...inputForm, nama: e.target.value });
}
function handleJam(e) {
setInputForm({ ...inputForm, jam: e.target.value });
}
{formMode !== "" && (
<div id="form" className="card py-2 my-3 bg-secondary">
<div className="card-body">
<h4>Add </h4>
<form className="row" onSubmit={submitForm}>
<div className="col-5">
<input
type="text"
name="nama"
className="form-control mx-2"
placeholder="nama..."
value={inputForm.nama || ""}
onChange={handleName}
/>
</div>
<div className="col-2">
<input
type="text"
name="jam"
className="form-control mx-2"
placeholder="Jam ..."
value={inputForm.jam || ""}
onChange={handleJam}
/>
</div>
<div className="col-2">
<input
type="submit"
className="btn btn-success"
value="Send"
/>
</div>
</form>
</div>
</div>
)}
i can use push on postman with this way
put -> localhost:4000/kelas/update/:id
{
"$push":{
"jadwal":
[
{
"nama":"Susanti",
"jam":"12"
}
]
}
}
Trying to display IMGs with React/JSX with Buffered/Binary data saved to my MongoDB/Mongoose database.
i iterate over the data array with the IMG element looking like this:
<img src={`data:${item.img.contentType};base64,${item.img.data.data.toString("base64")}`} alt="" />
import React, { useState, useEffect } from "react";
import axios from "axios";
const FilesUpload = () => {
const [allPics, setAllPics] = useState([]);
useEffect(() => {
const getPics = async () => {
let res = await axios.get("http://localhost:8080/");
setAllPics(res.data);
};
// query the server for all of the picture objects
getPics();
}, []);
const [state, setState] = useState({});
const handleChange = (e) => {
e.preventDefault();
setState(e.target.value);
console.log(state);
};
return (
<>
<h1>upload an image</h1>
<hr />
<div>
<form
action="http://localhost:8080/"
method="POST"
encType="multipart/form-data"
>
<div>
<label>Image Title</label>
<input type="text" id="name" placeholder="Name" name="name" onChange={handleChange} value={state.name}/>
</div>
<div>
<label htmlFor="desc">Image Description</label>
<textarea id="desc" name="desc" rows="2" placeholder="Description"
onChange={handleChange} value={state.desc}/>
</div>
<div>
<label htmlFor="image">Upload Image</label>
<input type="file" id="image" name="image" required />
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
</div>
{allPics.length > 0 ? (
<div>
{allPics.map((item, index) => (
<div key={index}>
<div>
<img src={`data:${item.img.contentType};base64,${item.img.data.data.toString("base64")}`} alt="" />
</div>
</div>
))}
</div>
) : (
<>
<hr />
<br />
<h1>uploaded files</h1>
<h5>Loading..</h5>
</>
)}
</>
);
};
export default FilesUpload;
but I always get ERR_INVALID_URL:
from similar threads on the net I've read that I need to take those comma-delimitated values and remove the comma which will give me the proper data. having a hard time figuring that out. any help would be great. thanks
I was facing the same problem after saving image like this in my mongoose model and after many research I resolved this, hope this works for you also
const base64String = btoa(String.fromCharCode(...new Uint8Array(item.img.data.data)));
and in img tag put this -:
src={`data:image/${item?.img?.contentType};base64,${base64String}`}
I am currently finishing up with a MERN system and noticed this very weird bug , when i click submit in the react page the user is saved to Mongo db , a little bit later another user is saved with the same data.
Here is my code (React frontend)
import React from "react";
import withStyles from "#material-ui/core/styles/withStyles";
import InputLabel from "#material-ui/core/InputLabel";
// core components
import GridItem from "components/Grid/GridItem.jsx";
import GridContainer from "components/Grid/GridContainer.jsx";
import CustomInput from "components/CustomInput/CustomInput.jsx";
import Button from "components/CustomButtons/Button.jsx";
import Card from "components/Card/Card.jsx";
import CardHeader from "components/Card/CardHeader.jsx";
import CardAvatar from "components/Card/CardAvatar.jsx";
import CardBody from "components/Card/CardBody.jsx";
import CardFooter from "components/Card/CardFooter.jsx";
import axios from 'axios';
import avatar from "assets/img/faces/marc.jpg";
import Styles from './Styles'
import { Form, Field } from 'react-final-form'
const styles = {
cardCategoryWhite: {
color: "rgba(255,255,255,.62)",
margin: "0",
fontSize: "14px",
marginTop: "0",
marginBottom: "0"
},
cardTitleWhite: {
color: "#FFFFFF",
marginTop: "0px",
minHeight: "auto",
fontWeight: "300",
fontFamily: "'Roboto', 'Helvetica', 'Arial', sans-serif",
marginBottom: "3px",
textDecoration: "none"
}
};
class UserProfile extends React.Component {
onChange = (e) => {
/*
Because we named the inputs to match their
corresponding values in state, it's
super easy to update the state
*/
this.setState({ [e.target.name]: e.target.value });
console.log('data', this.state.name)
}
onSubmit = async (values , e) => {
alert('User Created ')
let data = values;
axios.post('/api/users/newuser', {data})
.then(result => console.log(result))
.catch(err => console.log(err))
}
state = {
id: '',
name: "",
address:"",
phonenumber: "",
isp: "",
account: "",
accounttype: "",
paid: '',
staticip:'',
staticipdate: Date,
bill: '',
balance: '',
username: '',
pass: '',
apip: ''
render(){
return <div>
<h1>Create New User</h1>
<h3>Unlimitik POS system V1.0</h3>
<Form
onSubmit={this.onSubmit}
initialValues={{ }}
render={({ handleSubmit, form, submitting, pristine, values }) => (
<form onSubmit={handleSubmit}>
<div>
<label>Full Name</label>
<Field
name="name"
component="input"
type="text"
placeholder="Full Name"
/>
</div>
<div>
<label>Address</label>
<Field
name="address"
component="input"
type="text"
placeholder="address"
/>
</div>
<div>
<label>Phone Number</label>
<Field
name="phonenumber"
component="input"
type="text"
placeholder="phonenumber"
/>
</div>
<div>
<label>ISP</label>
<Field
name="isp"
component="input"
type="text"
placeholder="isp"
/>
</div>
<div>
<label>Account</label>
<Field
name="account"
component="input"
type="text"
placeholder="account"
/>
</div>
<div>
<label>Account Type</label>
<Field
name="accounttype"
component="input"
type="text"
placeholder="accounttype"
/>
</div>
<div>
<label>Paid</label>
<Field
name="paid"
component="input"
type="text"
placeholder="paid"
/>
</div>
<div>
<label>Static IP</label>
<Field
name="staticip"
component="input"
type="text"
placeholder="staticip"
/>
</div>
<div>
<label>Static IP Date</label>
<Field
name="staticipdate"
component="input"
type="date"
placeholder="staticipdate"
/>
</div>
<div>
<label>Bill</label>
<Field
name="bill"
component="input"
type="text"
placeholder="bill"
/>
</div>
<div>
<label>Balance</label>
<Field
name="balance"
component="input"
type="text"
placeholder="balance"
/>
</div>
<div>
<label>username</label>
<Field
name="username"
component="input"
type="text"
placeholder="username"
/>
</div>
<div>
<label>pass</label>
<Field
name="pass"
component="input"
type="text"
placeholder="pass"
/>
</div>
<div>
<label>AP / IP</label>
<Field
name="apip"
component="input"
type="text"
placeholder="AP / IP"
/>
</div>
<div>
<label>Notes</label>
<Field name="notes" component="textarea" placeholder="Notes" />
</div>
<div className="buttons">
<button type="submit" disabled={submitting || pristine} >
Submit
</button>
<button
type="button"
onClick={form.reset}
>
Reset
</button>
</div>
<pre>{JSON.stringify(values, 0, 2)}</pre>
</form>
)}
/>
export default UserProfile;
What is happening is the request is saved in mongodb after 2 or 3 seconds ( no exact timing ) another request is send and a document is saved 2 times.
here is the route express.js
router.post('/newuser', async (req, res) => {
var x = Math.floor(Math.random() * Date.now() * 0.000000002);
const data = new User ({
id: x,
name: req.body.data.name,
address: req.body.data.address,
phonenumber: req.body.data.phonenumber,
isp: req.body.data.isp,
account: req.body.data.account,
accounttype: req.body.data.accounttype,
paid: req.body.data.paid,
staticip: req.body.data.staticip,
staticipdate: req.body.data.staticipdate,
bill: req.body.data.bill,
balance: req.body.data.balance,
username: req.body.data.username,
pass: req.body.data.pass,
apip: req.body.data.apip
} )
data.save()
.then(r => console.log(r))
.catch(err => console.log(err))
})
the log after (the ids are different since it is generated on backend not by form)
You seem to have 2 onSubmit handlers.
I would suggest removing one of them:
<Form
// onSubmit={this.onSubmit} <--- remove this line
initialValues={{ }}
render={({ handleSubmit, form, submitting, pristine, values }) => (
<form onSubmit={handleSubmit}>