Skip to content

Commit

Permalink
test lib folder (#165)
Browse files Browse the repository at this point in the history
* validate lib

* helper file

* hook test

* Balance organism
  • Loading branch information
abhiShandy authored Jan 12, 2024
1 parent e5918c2 commit abc1176
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 0 deletions.
71 changes: 71 additions & 0 deletions components/organisms/Wallet/Balance.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import '@testing-library/jest-dom';
import { render, screen } from "@testing-library/react";
import WalletBalance from "./Balance";
import { SessionProvider } from "next-auth/react";

describe("WalletBalance", () => {
test("renders loading message when data is loading", () => {
render(
<SessionProvider session={{ data: { userId: "12345678" } }}>
<WalletBalance data={{ isLoading: true }} />
</SessionProvider>);
const loadingMessage = screen.getByText("Loading...");
expect(loadingMessage).toBeInTheDocument();
});

test("renders wallet balance details when data is loaded", () => {
const sessionData = {
user: {
data: {
userId: "1234567890abcdef",
},
},
};
const data = {
isLoading: false,
data: [
{
type: "MOI",
amount: 100,
currency: "USD",
operations: [],
},
{
type: "Other",
amount: 200,
currency: "EUR",
operations: [],
},
],
};

render(
<SessionProvider session={sessionData}>
<WalletBalance data={data} />
</SessionProvider>
);

// Check if wallet balance details are rendered correctly
const walletId = screen.getByText("#12345678...0abcdef");
expect(walletId).toBeInTheDocument();

const walletType = screen.getByText("POUR MOI");
expect(walletType).toBeInTheDocument();

const walletAmount = screen.getByText("$100.00");
expect(walletAmount).toBeInTheDocument();

});

// test("renders 'No savings currently in progress' message when data is loaded but no savings are available", () => {
// const data = {
// isLoading: false,
// data: [],
// };

// render(<WalletBalance data={data} />);

// const noSavingsMessage = screen.getByText("Aucun épargne actuellement en cours...");
// expect(noSavingsMessage).toBeInTheDocument();
// });
});
33 changes: 33 additions & 0 deletions hooks/useLocalStorage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { renderHook, act } from "@testing-library/react";
import { useLocalStorage } from "./useLocalStorage";

describe("useLocalStorage", () => {
beforeEach(() => {
localStorage.clear();
});

it("should initialize with fallback value if no stored value exists", () => {
const { result } = renderHook(() => useLocalStorage("testKey", "fallback"));

expect(result.current[0]).toBe("fallback");
});

it("should initialize with stored value if it exists", () => {
localStorage.setItem("testKey", JSON.stringify("storedValue"));

const { result } = renderHook(() => useLocalStorage("testKey", "fallback"));

expect(result.current[0]).toBe("storedValue");
});

it("should update stored value when setValue is called", () => {
const { result } = renderHook(() => useLocalStorage("testKey", "fallback"));

act(() => {
result.current[1]("newValue");
});

expect(result.current[0]).toBe("newValue");
expect(JSON.parse(localStorage.getItem("testKey"))).toBe("newValue");
});
});
26 changes: 26 additions & 0 deletions lib/helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
sendMessage,
sendOtp,
sendSMSHash,
setKyc,
checkKyc,
} from './helper';

describe('Helper functions', () => {
Expand Down Expand Up @@ -133,6 +135,18 @@ describe('Helper functions', () => {

expect(response).toEqual({ hello: 'world' });
});

it('setKyc', async () => {
const response = await setKyc({ test: 'payload' });

expect(response).toEqual({ hello: 'world' });
});

it('checkKyc', async () => {
const response = await checkKyc({ test: 'payload' });

expect(response).toEqual({ hello: 'world' });
});
});

describe('Helper functions errors', () => {
Expand Down Expand Up @@ -240,4 +254,16 @@ describe('Helper functions errors', () => {

expect(response).toEqual(new Error('test'));
});

it('setKyc', async () => {
const response = await setKyc({ test: 'payload' });

expect(response).toEqual(new Error('test'));
});

it('checkKyc', async () => {
const response = await checkKyc({ test: 'payload' });

expect(response).toEqual(new Error('test'));
});
});
165 changes: 165 additions & 0 deletions lib/validate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { registerValidate, emailValidate, loginValidate } from "./validate";


describe("loginValidate", () => {
it("should return errors for missing email", async () => {
const values = {
password: "password123",
};
const errors = await loginValidate(values);
expect(errors.email).toEqual("Adresse email ou numéro de téléphone est requis ");
});

it("should return errors for missing password", async () => {
const values = {
email: "[email protected]"
};
const errors = await loginValidate(values);
expect(errors.password).toEqual("Mot de passe est requis");
});

it("should return errors for password with spaces", async () => {
const values = {
email: "[email protected]",
password: "password with spaces",
};
const errors = await loginValidate(values);
expect(errors.password).toEqual("Mot de passe invalide");
});
});


describe("registerValidate", () => {
it("should return errors for missing email", async () => {
const values = {
password: "password123",
cpassword: "password123",
username: "john",
};

const errors = await registerValidate(values);

expect(errors.email).toEqual("Adresse email est requis");
});

it("should return errors for invalid email", async () => {
const values = {
email: "invalidemail",
password: "password123",
cpassword: "password123",
username: "john",
};

const errors = await registerValidate(values);

expect(errors.email).toEqual("Adresse email invalide");
});

it("should return errors for missing password", async () => {
const values = {
email: "[email protected]",
cpassword: "password123",
username: "john",
};

const errors = await registerValidate(values);

expect(errors.password).toEqual("Mot de passe est requis");
});

it("should return errors for short password", async () => {
const values = {
email: "[email protected]",
password: "pass",
cpassword: "password123",
username: "john",
};

const errors = await registerValidate(values);

expect(errors.password).toEqual("Mot de passe trop court");
});

it("should return errors for password with spaces", async () => {
const values = {
email: "[email protected]",
password: "password with spaces",
cpassword: "password123",
username: "john",
};

const errors = await registerValidate(values);

expect(errors.password).toEqual("Mot de passe invalide");
});

it("should return errors for mismatched passwords", async () => {
const values = {
email: "[email protected]",
password: "password123",
cpassword: "differentpassword",
username: "john",
};

const errors = await registerValidate(values);

expect(errors.cpassword).toEqual("Mot de passe de confirmation différent");
});

it("should return errors for missing username", async () => {
const values = {
email: "[email protected]",
password: "password123",
cpassword: "password123",
};

const errors = await registerValidate(values);

expect(errors.username).toEqual("Nom d'utilisateur est requis");
});

it("should return errors for short username", async () => {
const values = {
email: "[email protected]",
password: "password123",
cpassword: "password123",
username: "jo",
};

const errors = await registerValidate(values);

expect(errors.username).toEqual(
"Le nom d'utilisateur doit avoir au délà de 3 caractères"
);
});
});

describe("emailValidate", () => {
it("should return errors for missing email", async () => {
const values = {};

const errors = await emailValidate(values);

expect(errors.email).toEqual("Adresse email est requis");
});

it("should return errors for invalid email", async () => {
const values = {
email: "invalidemail",
};

const errors = await emailValidate(values);

expect(errors.email).toEqual("Adresse email invalide");
});

it("should not return errors for valid email", async () => {
const values = {
email: "[email protected]",
};

const errors = await emailValidate(values);

expect(errors.email).toBeUndefined();
});
});

1 comment on commit abc1176

@vercel
Copy link

@vercel vercel bot commented on abc1176 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wii-qare-fe – ./

wii-qare-fe-wiiqare.vercel.app
wii-qare-fe.vercel.app
wii-qare-fe-git-main-wiiqare.vercel.app

Please sign in to comment.