-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from arnaugomez/feat/utils-unit-tests
Feat: unit tests of utils functions
- Loading branch information
Showing
34 changed files
with
558 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
src/auth/data/repositories/email-verification-codes-repository-impl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { AuthTypeModel } from "./auth-type-model"; | ||
import type { UserModelData } from "./user-model"; | ||
import { UserModel } from "./user-model"; | ||
|
||
describe("UserModel", () => { | ||
const mockData: UserModelData = { | ||
id: "user-123", | ||
email: "test@example.com", | ||
authTypes: [AuthTypeModel.email], | ||
isEmailVerified: true, | ||
}; | ||
|
||
it("should instantiate correctly with provided data", () => { | ||
const user = new UserModel(mockData); | ||
expect(user).toBeInstanceOf(UserModel); | ||
}); | ||
|
||
it("should have getters id, email and authTypes and isEmailVerified that match the values of the constructor argument", () => { | ||
const user = new UserModel(mockData); | ||
expect(user.id).toBe(mockData.id); | ||
expect(user.email).toBe(mockData.email); | ||
expect(user.authTypes).toEqual(mockData.authTypes); | ||
expect(user.isEmailVerified).toBe(mockData.isEmailVerified); | ||
}); | ||
|
||
it("isEmailVerified should be false if the data value is undefined", () => { | ||
const userDataWithoutEmailVerification = { | ||
...mockData, | ||
isEmailVerified: undefined, | ||
}; | ||
const user = new UserModel(userDataWithoutEmailVerification); | ||
expect(user.isEmailVerified).toBe(false); | ||
}); | ||
|
||
it("isEmailVerified should be false if the data value is false", () => { | ||
const userDataWithEmailVerificationFalse = { | ||
...mockData, | ||
isEmailVerified: false, | ||
}; | ||
const user = new UserModel(userDataWithEmailVerificationFalse); | ||
expect(user.isEmailVerified).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { collection } from "./mongo"; | ||
|
||
interface MockDoc { | ||
foo: string; | ||
bar: number; | ||
} | ||
|
||
describe("collection", () => { | ||
it("should return an object with the correct name property", () => { | ||
const collectionName = "testCollection"; | ||
const result = collection<MockDoc>(collectionName); | ||
expect(result.name).toBe(collectionName); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { singleton } from "./locator-utils"; | ||
|
||
describe("singleton", () => { | ||
it("should always return the same result", () => { | ||
const memoizedFn = singleton(() => "test1"); | ||
const result1 = memoizedFn(); | ||
const result2 = memoizedFn(); | ||
const result3 = memoizedFn(); | ||
|
||
expect(result1).toBe("test1"); | ||
expect(result1).toBe(result2); | ||
expect(result2).toBe(result3); | ||
|
||
const memoizedFn2 = singleton(() => Symbol("test1")); | ||
const result4 = memoizedFn2(); | ||
const result5 = memoizedFn2(); | ||
expect(result4).toBe(result5); | ||
}); | ||
|
||
it("should always return the same result when the function is asynchronous", async () => { | ||
const memoizedFn = singleton(async () => Symbol("test2")); | ||
const result1 = await memoizedFn(); | ||
const result2 = await memoizedFn(); | ||
const result3 = await memoizedFn(); | ||
|
||
expect(result1).toBe(result2); | ||
expect(result2).toBe(result3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
import memoizeOne from "memoize-one"; | ||
|
||
/** | ||
* Memoizes the result of a function. | ||
* @param resultFn The function to memoize. | ||
* @param fn The function to memoize. | ||
* @returns The memoized function. | ||
*/ | ||
export const singleton = memoizeOne; | ||
export function singleton<T>(fn: () => T) { | ||
let isCached = false; | ||
let cachedResult: T; | ||
return () => { | ||
if (!isCached) { | ||
isCached = true; | ||
cachedResult = fn(); | ||
} | ||
return cachedResult; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { shuffle } from "./array"; | ||
|
||
const createArray = (length: number) => Array.from({ length }, (_, i) => i); | ||
|
||
describe("shuffle", () => { | ||
it("should return an array of the same length", () => { | ||
const array = createArray(10); | ||
const shuffledArray = shuffle(array); | ||
expect(shuffledArray).toHaveLength(array.length); | ||
}); | ||
|
||
it("should contain the same elements", () => { | ||
const array = createArray(42); | ||
const shuffledArray = shuffle(array); | ||
expect(shuffledArray.sort()).toEqual(array.sort()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { waitMilliseconds } from "./promise"; | ||
|
||
describe("waitMilliseconds", () => { | ||
it("should call setTimeout with the correct number of milliseconds", async () => { | ||
const mock = vi.spyOn(global, "setTimeout"); | ||
const ms = 100; | ||
await waitMilliseconds(ms); | ||
expect(mock).toHaveBeenCalledWith(expect.anything(), ms); | ||
mock.mockRestore(); | ||
}); | ||
|
||
it("should wait for at least the specified number of milliseconds", async () => { | ||
const start = performance.now(); | ||
const ms = 80; | ||
await waitMilliseconds(ms); | ||
const end = performance.now(); | ||
expect(end - start).toBeGreaterThanOrEqual(ms); | ||
}); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { mockConsoleError } from "@/test/utils/mock-console"; | ||
import "@testing-library/jest-dom"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { describe, expect, it } from "vitest"; | ||
import { ReactContextNotFoundError } from "../models/context-errors"; | ||
import { createContextHook, createNullContext } from "./context"; // Adjust the import path as necessary | ||
|
||
describe("createContextHook", () => { | ||
it("throws CourseDoesNotExistError when context value is null", () => { | ||
const TestContext = createNullContext<string>(); | ||
const useTestContext = createContextHook(TestContext); | ||
const TestComponent = () => { | ||
const value = useTestContext(); | ||
return <div>{value}</div>; | ||
}; | ||
|
||
const mock = mockConsoleError(); | ||
expect(() => render(<TestComponent />)).toThrow(ReactContextNotFoundError); | ||
mock.mockRestore(); | ||
}); | ||
|
||
it("returns the context value when it is not null", () => { | ||
const TestContext = createNullContext<string>(); | ||
const useTestContext = createContextHook(TestContext); | ||
const TestComponent = () => { | ||
const value = useTestContext(); | ||
return <div>{value}</div>; | ||
}; | ||
|
||
render( | ||
<TestContext.Provider value="Test Value"> | ||
<TestComponent /> | ||
</TestContext.Provider>, | ||
); | ||
|
||
expect(screen.getByText("Test Value")).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { CourseDoesNotExistError } from "@/src/courses/domain/models/course-errors"; | ||
import type { Context } from "react"; | ||
import { createContext, useContext } from "react"; | ||
import { ReactContextNotFoundError } from "../models/context-errors"; | ||
|
||
export const createNullContext = <T>() => createContext<T | null>(null); | ||
|
||
export function createContextHook<T>(context: Context<T | null>) { | ||
return () => { | ||
const value = useContext(context); | ||
if (!value) throw new CourseDoesNotExistError(); | ||
if (!value) throw new ReactContextNotFoundError(); | ||
return value; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { cn } from "./shadcn"; | ||
|
||
describe("cn", () => { | ||
test("does nothing when receives a single class", () => { | ||
expect(cn("flex-col")).toBe("flex-col"); | ||
}); | ||
|
||
test("merges two classes and adds a space in between", () => { | ||
expect(cn("btn", "btn-primary")).toBe("btn btn-primary"); | ||
}); | ||
|
||
test("handles conditional class inputs", () => { | ||
let isActive = true; | ||
expect(cn("btn", isActive && "active")).toBe("btn active"); | ||
isActive = false; | ||
expect(cn("btn", isActive && "active")).toBe("btn"); | ||
}); | ||
|
||
test("removes duplicate classes", () => { | ||
expect(cn("text-red", "text-red")).toBe("text-red"); | ||
}); | ||
|
||
test("merges TailwindCSS utility classes correctly", () => { | ||
expect(cn("text-center", "text-center md:text-left")).toBe( | ||
"text-center md:text-left", | ||
); | ||
}); | ||
|
||
test("handles array inputs", () => { | ||
expect(cn(["btn", "btn-primary"])).toBe("btn btn-primary"); | ||
}); | ||
|
||
test("handles object inputs", () => { | ||
expect(cn({ flex: true, "btn-primary": false, active: true })).toBe( | ||
"flex active", | ||
); | ||
}); | ||
}); |
Oops, something went wrong.