From 9b4cd2dec7a5ec9fd33d1d24e839748edf50fe53 Mon Sep 17 00:00:00 2001 From: Dallas <71103219+LeleDallas@users.noreply.github.com> Date: Tue, 2 May 2023 15:52:42 +0200 Subject: [PATCH] test: functions in file --- test/Components/SignInForm.test.tsx | 8 ++-- test/componentsFunctions.test.tsx | 69 +++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 test/componentsFunctions.test.tsx diff --git a/test/Components/SignInForm.test.tsx b/test/Components/SignInForm.test.tsx index f472f14..147ccb3 100644 --- a/test/Components/SignInForm.test.tsx +++ b/test/Components/SignInForm.test.tsx @@ -1,6 +1,6 @@ import SignIn from "../../src/Components/SignInForm" import React from 'react'; -import { expect, describe, it } from 'vitest' +import { expect, describe, it, vi } from 'vitest' import { fireEvent, render } from '@testing-library/react'; import '@testing-library/jest-dom'; import { BrowserRouter } from "react-router-dom"; @@ -40,11 +40,11 @@ describe('SignIn', () => { fireEvent.click(buttonSU) expect(getByText("Building Owner")).toBeInTheDocument() const buttonSubLog = queryAllByText("Sign In") - buttonSubLog.map(el => {fireEvent.click(el)}) + buttonSubLog.map(el => { fireEvent.click(el) }) expect(getByText("Building Owner")).toBeInTheDocument() const buttonSubSig = queryAllByText("Sign Up") - buttonSubSig.map(el => {fireEvent.click(el)}) + buttonSubSig.map(el => { fireEvent.click(el) }) expect(getByText("Building Owner")).toBeInTheDocument() }); -}); \ No newline at end of file +}); diff --git a/test/componentsFunctions.test.tsx b/test/componentsFunctions.test.tsx new file mode 100644 index 0000000..c8d3bf2 --- /dev/null +++ b/test/componentsFunctions.test.tsx @@ -0,0 +1,69 @@ +import { handleLoginSubmit, handleSignUpSubmit, signInButton, signUpButton } from "../src/componentsFunctions"; + +describe('Authentication functions', () => { + describe('signUpButton', () => { + it('should call the setError and setSwapPanel functions with the correct arguments', () => { + const setError = jest.fn(); + const setSwapPanel = jest.fn(); + signUpButton(setError, setSwapPanel); + expect(setError).toHaveBeenCalledWith([]); + expect(setSwapPanel).toHaveBeenCalledWith(true); + }); + }); + + describe('signInButton', () => { + it('should call the setError and setSwapPanel functions with the correct arguments', () => { + const setError = jest.fn(); + const setSwapPanel = jest.fn(); + signInButton(setError, setSwapPanel); + expect(setError).toHaveBeenCalledWith([]); + expect(setSwapPanel).toHaveBeenCalledWith(false); + }); + }); + + describe('handleLoginSubmit', () => { + it('should call setError with an error message if email or password is null', () => { + const setError = jest.fn(); + handleLoginSubmit("", 'password', setError); + expect(setError).toHaveBeenCalledWith(['Fill the form to continue']); + handleLoginSubmit('email', "", setError); + expect(setError).toHaveBeenCalledWith(['Fill the form to continue']); + }); + + it('should not call setError if email and password are not null', () => { + const setError = jest.fn(); + handleLoginSubmit('email', 'password', setError); + expect(setError).not.toHaveBeenCalled(); + }); + }); + + describe('handleSignUpSubmit', () => { + it('should call setError with an error message if any required fields are null', () => { + const setError = jest.fn(); + handleSignUpSubmit(null, 'name', 'surname', 'password', 'password', 'email', 'type', setError); + expect(setError).toHaveBeenCalledWith(['Fill the form to continue']); + handleSignUpSubmit('event', "", 'surname', 'password', 'password', 'email', 'type', setError); + expect(setError).toHaveBeenCalledWith(['Fill the form to continue']); + handleSignUpSubmit('event', 'name', "", 'password', 'password', 'email', 'type', setError); + expect(setError).toHaveBeenCalledWith(['Fill the form to continue']); + handleSignUpSubmit('event', 'name', 'surname', "", 'password', 'email', 'type', setError); + expect(setError).toHaveBeenCalledWith(['Fill the form to continue']); + handleSignUpSubmit('event', 'name', 'surname', 'password', "", 'email', 'type', setError); + expect(setError).toHaveBeenCalledWith(['Fill the form to continue']); + handleSignUpSubmit('event', 'name', 'surname', 'password', 'password', "", 'type', setError); + expect(setError).toHaveBeenCalledWith(['Fill the form to continue']); + }); + + it('should call setError with an error message if password and passwordConf do not match', () => { + const setError = jest.fn(); + handleSignUpSubmit('event', 'name', 'surname', 'password', 'different', 'email', 'type', setError); + expect(setError).toHaveBeenCalledWith(['Typed Password are different']); + }); + + it('should not call setError if all required fields are present and password and passwordConf match', () => { + const setError = jest.fn(); + handleSignUpSubmit('event', 'name', 'surname', 'password', 'password', 'email', 'type', setError); + expect(setError).not.toHaveBeenCalled(); + }); + }); +}); \ No newline at end of file