Skip to content

Instantly share code, notes, and snippets.

View PCreations's full-sized avatar

Pierre Criulanscy PCreations

View GitHub Profile
@PCreations
PCreations / 96e53377eb76.diff
Created July 17, 2024 08:23
96e53377eb76.diff
-14,12 +15,14 @@ test.describe('Feature: Adding a book', () => {
page,
}) => {
await page.goto('http://localhost:3000');
- await page.getByLabel(/title/i).fill('Clean Code');
+ await page.getByLabel(/title/i).fill('The Pragmatic Programmer');
await page.getByText(/add book/i).click();
- await page.getByLabel(/title/i).fill('Clean Code');
+ await page.getByLabel(/title/i).fill('The Pragmatic Programmer');
@PCreations
PCreations / 84d82ebad90f.diff
Created July 17, 2024 08:23
84d82ebad90f.diff
-3,8 +3,9 @@ import { test, expect } from '@playwright/test';
test.describe('Feature: Adding a book', () => {
test('Example: User can add a book', async ({ page }) => {
await page.goto('http://localhost:3000');
+ const rand = Math.floor(Math.random() * 1000000);
- await page.getByLabel(/title/i).fill('Clean Code');
+ await page.getByLabel(/title/i).fill(`Clean Code ${rand}`);
await page.getByText(/add book/i).click();
@PCreations
PCreations / 388f49f42450.diff
Created July 17, 2024 08:23
388f49f42450.diff
-10,7 +10,7 @@ import { BookRepository } from './book-repository.port';
providers: [
{
provide: BookRepository,
- useValue: InMemoryBookRepository,
+ useClass: InMemoryBookRepository,
},
AddBookUseCase,
],
@PCreations
PCreations / 2f68f6c62a11.diff
Created July 17, 2024 08:23
2f68f6c62a11.diff
-35,4 +34,56 @@ export class AppController {
</body>
</html>`;
}
+
+ @Post()
+ async addBook(@Body() body: { title: string }) {
+ try {
+ await this.addBookUseCase.execute({ title: body.title });
+ return `
@PCreations
PCreations / 731b56e1bcb6.diff
Created July 17, 2024 08:23
731b56e1bcb6.diff
-15,12 +15,11 @@ export class AppController {
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
+ <script src="https://unpkg.com/htmx.org@2.0.1" integrity="sha384-QWGpdj554B4ETpJJC9z+ZHJcA/i59TyjxEPXiiUgN2WmTyV5OEZWCD6gQhgkdpB/" crossorigin="anonymous"></script>
</head>
<body>
<main class="container mx-auto px-4 py-8">
- ${query?.title ? '<p>Book added</p>' : ''}
- <form class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
@PCreations
PCreations / 86029df06966.diff
Created July 17, 2024 08:23
86029df06966.diff
-6,7 +6,7 @@ export class AppController {
constructor(private readonly addBookUseCase: AddBookUseCase) {}
@Get()
- getHello(@Query() query?: { title: string }): string {
+ index(): string { // plus besoin de récupérer la querystring, cette route devient simplement notre page de base
return `
<!DOCTYPE html>
<html>
@PCreations
PCreations / e82a8741f87c.diff
Created July 17, 2024 08:22
e82a8741f87c.diff
-22,6 +22,8 @@ describe('Feature: Adding a book', () => {
const addingBook = addBook.execute({ title: 'Clean Code' });
- await expect(addingBook).rejects.toThrow(new BookAlreadyExistsError('Foo'));
+ await expect(addingBook).rejects.toThrow(
+ new BookAlreadyExistsError('Clean Code'),
+ );
});
});
@PCreations
PCreations / 3374f79ea11b.diff
Created July 17, 2024 08:22
3374f79ea11b.diff
-1,11 +1,15 @@
import { Injectable } from '@nestjs/common';
import { BookRepository } from './book-repository.port';
+import { BookAlreadyExistsError } from './book-already-exists.error';
@Injectable()
export class AddBookUseCase {
constructor(private readonly bookRepository: BookRepository) {}
- execute(book: { title: string }) {
@PCreations
PCreations / e80a4d7414b8.diff
Created July 17, 2024 08:22
e80a4d7414b8.diff
-21,8 +22,6 @@ describe('Feature: Adding a book', () => {
const addingBook = addBook.execute({ title: 'Clean Code' });
- await expect(addingBook).rejects.toThrow(
- new BookAlreadyExistsError('The book Clean Code already exists'),
- );
+ await expect(addingBook).rejects.toThrow(new BookAlreadyExistsError('Foo'));
});
});
@PCreations
PCreations / 7617b3482e57.diff
Created July 17, 2024 08:22
7617b3482e57.diff
-9,4 +9,8 @@ export class InMemoryBookRepository implements BookRepository {
this.lastSavedBook = book;
this.booksByTitle.set(book.title, book);
}
+
+ async doesBookExist(title: string): Promise<boolean> {
+ return this.booksByTitle.has(title);
+ }
}