Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Quick Question: How would you do unit tests on this function? ```
- async function submit() {
- setIsSubmitting(true);
- if (!isConnected){
- setDisplayToast(true);
- setToastMessage("Please check your connection and try again.");
- setToastMsgLines(1);
- setToastColor(colors.danger);
- setTimeout(() => {
- setDisplayToast(false);
- setIsSubmitting(false);
- }, 3600);
- return ;
- }
- console.log("Signing in...");
- // Sign in with Supabase
- const { error } = await supabase.auth.signInWithPassword({
- email: form.email,
- password: form.password,
- })
- // Handle sign in failure
- if (error){
- console.log(error.status);
- console.log(error.message);
- if (error.name = "invalid_credentials"){
- setDisplayToast(true);
- setToastMessage("Invalid email or password. Please try again.");
- setToastMsgLines(1);
- setToastColor(colors.danger);
- setFormWarn(true);
- setTimeout(() => {
- setDisplayToast(false);
- }, 3600);
- setTimeout(() => {
- setFormWarn(false);
- }, 1000);
- } else if (error.name = "email_not_confirmed"){
- setDisplayToast(true);
- setToastMessage("Please confirm your email address first.");
- setToastMsgLines(1);
- setToastColor(colors.danger);
- setFormWarn(true);
- setTimeout(() => {
- setDisplayToast(false);
- }, 3600);
- setTimeout(() => {
- setFormWarn(false);
- }, 1000);
- } else if (error.status == 429){
- setDisplayToast(true);
- setToastMessage("There is a problem logging you in. Please come back in a few minutes.");
- setToastMsgLines(2);
- setToastColor(colors.danger);
- setFormWarn(true);
- setTimeout(() => {
- setDisplayToast(false);
- }, 3600);
- setTimeout(() => {
- setFormWarn(false);
- }, 1000);
- }
- // Re-activate submit button
- setTimeout(() => {
- setIsSubmitting(false);
- }, 3600);
- } else {
- setIsSubmitting(false);
- // Load database data and redirect to home page once logged in.
- setIsOpening(true);
- console.log("Getting data from database:");
- // Get database data
- const fiberBreakdown = await getFiberBreakdownByGrade();
- setFiberBreakdownDB(fiberBreakdown);
- const fibersScanned = await getFibersScannedDaily();
- setFibersScannedDB(fibersScanned);
- const systemStatus = await getSystemStatusDatabase();
- setSystemStatusDB(systemStatus);
- const fiberLogs = await getFiberLogs();
- setFibersScanLogsDB(fiberLogs);
- // Store last update time
- await storeLastDatabaseUpdateTime();
- setIsOpening(false);
- router.replace("(main_app)/home");
- }
- }```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement