Skip to content

Instantly share code, notes, and snippets.

@ovpv
Last active December 15, 2020 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ovpv/a9b827be8f2b17693180e626983f7c4b to your computer and use it in GitHub Desktop.
Save ovpv/a9b827be8f2b17693180e626983f7c4b to your computer and use it in GitHub Desktop.
Jest Mock: App file where moment js is used
import React, { useState } from "react";
import "./styles.css";
import moment from "moment";
export default function App() {
const [date, setDate] = useState("");
const [age, setAge] = useState(0);
const calculateAge = () => {
console.log('diff called');
const calcAge = moment().diff(date, "years");
setAge(calcAge);
};
return (
<div className="App">
<h2>How old are you?</h2>
<label>Birthday: </label>
<input
type="date"
onChange={(e) => setDate(e.target.value)}
value={date}
id="birthday"
name="birthday"
/>
<button disabled={!date.length} onClick={calculateAge}>
Calculate Age
</button>
<div>Your age is: {age}</div>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment