-
Notifications
You must be signed in to change notification settings - Fork 1
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 #45 from WildCodeSchool/refacto/WILD_017_Paginer-u…
…rls-tests WILD_017: Refacto - Ajout test pagination, fix test lié
- Loading branch information
Showing
13 changed files
with
541 additions
and
376 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.env | ||
.env.test | ||
data/* | ||
data/* | ||
docker-compose-local.test.yaml |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { Url } from "../entities/Url"; | ||
import PaginateUrls from "@/types/PaginatesUrls"; | ||
import UrlResolver from "../resolvers/UrlResolver"; | ||
import { MyContext, JwtPayload } from ".."; | ||
|
||
type PartialUrl = Partial<Url>; | ||
|
||
|
@@ -9,6 +11,23 @@ const mockUrl: PartialUrl = { | |
path: "https://example.com", | ||
}; | ||
const mockUrls: PartialUrl[] = [mockUrl]; | ||
const mockPaginateUrls: PaginateUrls = { | ||
urls: mockUrls as Url[], | ||
totalPages: 1, | ||
currentPage: 1, | ||
previousPage: 1, | ||
nextPage: 1, | ||
}; | ||
|
||
const mockContext: MyContext = { | ||
res: { | ||
setHeader: jest.fn(), | ||
}, | ||
payload: { | ||
id: "testId", | ||
email: "[email protected]", | ||
} as JwtPayload, | ||
}; | ||
|
||
describe("Unit Test Url Resolver", () => { | ||
let urlResolver: UrlResolver; | ||
|
@@ -21,19 +40,38 @@ describe("Unit Test Url Resolver", () => { | |
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("Query urls should return an array of Url", async () => { | ||
jest.spyOn(Url, "find").mockImplementation(() => | ||
Promise.resolve(mockUrls as Url[]), | ||
it("Query urls whith context should return a pagination of urls", async () => { | ||
jest.spyOn(Url, "getPaginateUrls").mockImplementation(() => | ||
Promise.resolve(mockPaginateUrls as PaginateUrls), | ||
); | ||
|
||
const result = await urlResolver.urls(mockContext, false, 1, "", ""); | ||
expect(result).toEqual(mockPaginateUrls); | ||
}); | ||
|
||
it("Query urls whith context should throw an error when fetching urls fails", async () => { | ||
jest.spyOn(Url, "getPaginateUrls").mockRejectedValue( | ||
new Error("Internal server error"), | ||
); | ||
await expect(urlResolver.urls(mockContext, false, 1, "", "")).rejects.toThrow( | ||
"Internal server error", | ||
); | ||
}); | ||
|
||
it("Query urls whithout context should return a pagination of urls", async () => { | ||
jest.spyOn(Url, "getPaginateUrls").mockImplementation(() => | ||
Promise.resolve(mockPaginateUrls as PaginateUrls), | ||
); | ||
const result = await urlResolver.urls(); | ||
expect(result).toEqual(mockUrls); | ||
|
||
const result = await urlResolver.urls({} as MyContext, false, 1, "", ""); | ||
expect(result).toEqual(mockPaginateUrls); | ||
}); | ||
|
||
it("Query urls should throw an error when fetching urls fails", async () => { | ||
jest.spyOn(Url, "find").mockRejectedValue( | ||
it("Query urls whithout context should throw an error when fetching urls fails", async () => { | ||
jest.spyOn(Url, "getPaginateUrls").mockRejectedValue( | ||
new Error("Internal server error"), | ||
); | ||
await expect(urlResolver.urls()).rejects.toThrow( | ||
await expect(urlResolver.urls({} as MyContext, false, 1, "", "")).rejects.toThrow( | ||
"Internal server error", | ||
); | ||
}); | ||
|
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 |
---|---|---|
|
@@ -12,3 +12,10 @@ export const historiesData = [ | |
response: "Facebook response", | ||
}, | ||
]; | ||
|
||
export const userData = { | ||
id: "e5fb990e-f9d9-4858-82d1-1fd1755485a5", | ||
username: "test", | ||
email: "[email protected]", | ||
hashedPassword: "hashedPasswordTest", | ||
}; |
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,4 +1,6 @@ | ||
import { Url } from "../../entities/Url"; | ||
import { User } from "../../entities/User"; | ||
import { UserUrl } from "../../entities/UserUrl"; | ||
import { History } from "../../entities/History"; | ||
import dataSource from "../dataSource"; | ||
import { urlsData, historiesData } from "./dataInput"; | ||
|
@@ -21,6 +23,24 @@ async function generateFixtures() { | |
return url; | ||
}), | ||
); | ||
await Url.create({ | ||
id: "737fbb42-1dd9-40c5-a29e-604407a7bc7b", | ||
name: "TestUrl for fetch private urls", | ||
path: "https://excalidraw.com" | ||
}); | ||
|
||
await User.save({ | ||
id: "741fbb42-1dd9-40c5-a29e-604407a7bc8c", | ||
username: "testuser", | ||
email: "[email protected]", | ||
hashedPassword: "hashedPasswordTest", | ||
}); | ||
|
||
await UserUrl.save({ | ||
userId: "741fbb42-1dd9-40c5-a29e-604407a7bc8c", | ||
urlId: "737fbb42-1dd9-40c5-a29e-604407a7bc7b", | ||
}); | ||
|
||
await History.save({ ...historiesData[0], url: savedUrls[0] }); | ||
await History.save({ ...historiesData[1], url: savedUrls[1] }); | ||
console.log("Fixtures generated successfully!"); | ||
|
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,54 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import CustomPagination from '@/components/custom/CustomPagination'; | ||
|
||
|
||
vi.mock('@/hooks/useScreenDimensions', () => ({ | ||
default: () => ({ width: 1024 }) | ||
})); | ||
|
||
describe('CustomPagination', () => { | ||
const defaultProps = { | ||
currentPage: 3, | ||
totalPages: 10, | ||
previousPage: 2, | ||
nextPage: 4, | ||
onPageChange: vi.fn(), | ||
}; | ||
|
||
it('renders correctly', () => { | ||
render(<CustomPagination {...defaultProps} />); | ||
expect(screen.getByText('3')).toHaveAttribute('aria-current', 'page'); | ||
expect(screen.getByText('Précédent')).toBeInTheDocument(); | ||
expect(screen.getByText('Suivant')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls onPageChange with correct page number when clicking on a page', () => { | ||
render(<CustomPagination {...defaultProps} />); | ||
fireEvent.click(screen.getByText('4')); | ||
expect(defaultProps.onPageChange).toHaveBeenCalledWith(4); | ||
}); | ||
|
||
it('calls onPageChange with Previous page when clicking Précédent', () => { | ||
render(<CustomPagination {...defaultProps} />); | ||
fireEvent.click(screen.getByText('Précédent')); | ||
expect(defaultProps.onPageChange).toHaveBeenCalledWith(2); | ||
}); | ||
|
||
it('calls onPageChange with next page when clicking Next', () => { | ||
render(<CustomPagination {...defaultProps} />); | ||
fireEvent.click(screen.getByText('Suivant')); | ||
expect(defaultProps.onPageChange).toHaveBeenCalledWith(4); | ||
}); | ||
|
||
it('renders ellipsis when there are many pages', () => { | ||
render(<CustomPagination {...defaultProps} />); | ||
expect(screen.getAllByText('More pages')).toHaveLength(1); | ||
}); | ||
|
||
it('renders first and last page numbers', () => { | ||
render(<CustomPagination {...defaultProps} />); | ||
expect(screen.getByText('1')).toBeInTheDocument(); | ||
expect(screen.getByText('10')).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.