feat(test): rename project to feat upcoming challenges

This commit is contained in:
thomas
2023-04-08 13:36:57 +02:00
parent 3b45e10570
commit 95e0f6b5f8
31 changed files with 36 additions and 37 deletions

View File

@@ -0,0 +1,23 @@
import { AppComponent } from './app.component';
describe(AppComponent.name, () => {
it('shows error message and disabled button because no search criteria are typed', () => {
//todo
});
it('shows No book found because no book match the search', () => {
//todo
});
it('shows One book because the search matches one book', () => {
//todo
});
it('shows One book because the search matches one book even with different cases', () => {
//todo
});
it('shows a list of books because the search matches multiples books', () => {
//todo
});
});

View File

@@ -0,0 +1,21 @@
describe('AppComponent', () => {
it('shows error message and disabled button because no search criteria are typed', async () => {
//todo
});
it('shows No book found because no book match the search', async () => {
//todo
});
it('shows One book because the search matches one book', async () => {
//todo
});
it('shows One book because the search matches one book even with different cases', async () => {
//todo
});
it('shows a list of books because the search matches multiples books', async () => {
//todo
});
});

View File

@@ -0,0 +1,42 @@
import { Component } from '@angular/core';
import { RouterLink, RouterOutlet } from '@angular/router';
@Component({
standalone: true,
imports: [RouterOutlet, RouterLink],
selector: 'app-root',
styles: [
`
h1 {
margin-bottom: 0;
}
nav a {
padding: 1rem;
text-decoration: none;
margin-top: 10px;
display: inline-block;
background-color: #e8e8e8;
color: #3d3d3d;
border-radius: 4px;
margin-bottom: 10px;
}
nav a:hover {
color: white;
background-color: #42545c;
}
nav a.active {
background-color: black;
}
`,
],
template: `
<h1>Library</h1>
<nav>
<a routerLink="/search" routerLinkActive="active">Borrow a Book</a>
</nav>
<router-outlet />
`,
})
export class AppComponent {}

View File

@@ -0,0 +1,18 @@
import { ActivatedRouteSnapshot, Route } from '@angular/router';
import { bookGuard } from './book.guard';
export const appRoutes: Route[] = [
{
path: 'search',
loadComponent: () => import('./search.component'),
},
{
path: 'shelf',
canActivate: [(route: ActivatedRouteSnapshot) => bookGuard(route)],
loadComponent: () => import('./shelf.component'),
},
{
path: 'no-result',
loadComponent: () => import('./no-book-search.component'),
},
];

View File

@@ -0,0 +1,20 @@
import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { availableBooks } from './book.model';
export const bookGuard = (
route: ActivatedRouteSnapshot,
router = inject(Router)
) => {
const searchParam = route.queryParams?.['book'].toLowerCase();
const isBookAvailable =
!!searchParam &&
availableBooks.some(
(b) =>
b.author.toLowerCase().includes(searchParam) ||
b.name.toLowerCase().includes(searchParam)
);
return isBookAvailable || router.parseUrl('no-result');
};

View File

@@ -0,0 +1,17 @@
export interface Book {
name: string;
author: string;
}
export const availableBooks = [
{ name: 'To Kill a Mockingbird', author: 'Harper Lee' },
{ name: '1984', author: 'George Orwell' },
{ name: 'The Catcher in the Rye', author: 'J.D. Salinger' },
{ name: 'The Great Gats', author: 'F. Scott Fitzgerald' },
{ name: 'Pride and Prejudice', author: 'Jane Austen' },
{ name: 'The Hobbit', author: 'J.R.R. Tolkien' },
{ name: 'The Lord of the Rings', author: 'J.R.R. Tolkien' },
{ name: "Harry Potter and the Philosopher's Stone", author: 'J.K. Rowling' },
{ name: 'The Hunger Games', author: 'Suzanne Collins' },
{ name: 'Animal Farm', author: 'George Orwell' },
];

View File

@@ -0,0 +1,8 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
@Component({
standalone: true,
template: ` <div>No book found for this search</div> `,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export default class ShelfComponent {}

View File

@@ -0,0 +1,66 @@
import { NgFor, NgIf } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormControl, ReactiveFormsModule, Validators } from '@angular/forms';
import { RouterLink } from '@angular/router';
import { availableBooks } from './book.model';
@Component({
standalone: true,
imports: [ReactiveFormsModule, RouterLink, NgFor, NgIf],
styles: [
`
:host {
display: flex;
flex-direction: column;
gap: 10px;
}
.error {
color: red;
}
button {
width: 300px;
padding: 5px;
border-radius: 5px;
}
.search label {
margin-right: 15px;
}
`,
],
template: `
<div class="search">
<label for="bookName">Search Book by author or title</label>
<input
type="text"
id="bookName"
name="bookName"
[formControl]="searchBook"
required />
<div class="error" *ngIf="searchBook.errors">
Search criteria is required!
</div>
</div>
<button
data-cy="borrow-btn"
routerLink="/shelf"
[queryParams]="{ book: searchBook.value }"
[disabled]="searchBook.errors"
routerLinkActive="router-link-active">
Borrow
</button>
<div>
<h3>List of books available:</h3>
<ul>
<li *ngFor="let book of books">{{ book.name }} by {{ book.author }}</li>
</ul>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export default class SearchComponent {
searchBook = new FormControl('', Validators.required);
books = availableBooks;
}

View File

@@ -0,0 +1,32 @@
import { AsyncPipe, JsonPipe, NgFor } from '@angular/common';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { map } from 'rxjs';
import { availableBooks } from './book.model';
@Component({
selector: 'app-shelf',
standalone: true,
imports: [AsyncPipe, JsonPipe, NgFor],
template: `
<ul>
<li *ngFor="let book of books | async">
Borrowed Book: {{ book.name }} by {{ book.author }}
</li>
</ul>
`,
styles: [],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export default class ShelfComponent {
readonly books = inject(ActivatedRoute).queryParams.pipe(
map((params) => params?.['book'].toLowerCase()),
map((param) =>
availableBooks.filter(
(b) =>
b.name.toLowerCase().includes(param) ||
b.author.toLowerCase().includes(param)
)
)
);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>RouterTesting</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<app-root></app-root>
</body>
</html>

View File

@@ -0,0 +1,8 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter } from '@angular/router';
import { AppComponent } from './app/app.component';
import { appRoutes } from './app/app.routes';
bootstrapApplication(AppComponent, {
providers: [provideRouter(appRoutes)],
}).catch((err) => console.error(err));

View File

@@ -0,0 +1 @@
/* You can add global styles to this file, and also import other style files */

View File

@@ -0,0 +1,2 @@
import '@testing-library/jest-dom';
import 'jest-preset-angular/setup-jest';