feat(doc): move table

This commit is contained in:
thomas
2023-10-17 23:13:44 +02:00
parent 16809d66b5
commit 05951bd4df
25 changed files with 42 additions and 43 deletions

View File

@@ -0,0 +1,10 @@
import { Component } from '@angular/core';
import { TableComponent } from './table.component';
@Component({
standalone: true,
imports: [TableComponent],
selector: 'app-root',
template: ` <app-table /> `,
})
export class AppComponent {}

View File

@@ -0,0 +1,98 @@
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { MatTableHarness } from '@angular/material/table/testing';
import { render } from '@testing-library/angular';
import userEvent from '@testing-library/user-event';
import { TableComponent } from './table.component';
describe('AppComponent', () => {
// let loader: HarnessLoader;
// let fixture: ComponentFixture<TableComponent>;
// let loader: HarnessLoader;
// beforeEach(async () => {
// const view = await render(TableComponent);
// fixture = view.fixture;
// loader = TestbedHarnessEnvironment.loader(fixture);
// });
it('error modal is displayed if you click on "Confirm" without inputing a name', async () => {
userEvent.setup();
const { fixture } = await render(TableComponent);
const loader = TestbedHarnessEnvironment.loader(fixture);
const tables = await loader.getAllHarnesses(MatTableHarness);
expect(tables.length).toBe(1);
// const confirmButton = await screen.findByRole('button', {
// name: /confirm/i,
// });
// await userEvent.click(confirmButton);
// const dialogControl = await screen.findByRole('dialog');
// expect(dialogControl).toBeInTheDocument();
// const errorTitle = await screen.findByRole('heading', {
// name: /error/i,
// });
// expect(errorTitle).toBeInTheDocument();
// const okButton = await screen.findByRole('button', {
// name: /ok/i,
// });
// await userEvent.click(okButton);
});
// test('error message is shown if you click "Cancel" in the confirmation modal after submitting a name', async () => {
// userEvent.setup();
// await render(AppComponent);
// const inputControl = await screen.getByRole('textbox');
// await userEvent.type(inputControl, 'toto');
// const confirmButton = await screen.findByRole('button', {
// name: /confirm/i,
// });
// await userEvent.click(confirmButton);
// const dialogControl = await screen.findByRole('dialog');
// expect(dialogControl).toBeInTheDocument();
// const profilTitle = await screen.findByRole('heading', {
// name: /profil/i,
// });
// expect(profilTitle).toBeInTheDocument();
// const cancelButton = await screen.findByRole('button', {
// name: /cancel/i,
// });
// await userEvent.click(cancelButton);
// const errorText = await screen.getByText('Name is invalid !!');
// expect(errorText).toBeInTheDocument();
// });
// test('confirm message is shown if you click "Confirm" in the confirmation modal after submitting a name', async () => {
// userEvent.setup();
// await render(AppComponent);
// const inputControl = await screen.getByRole('textbox');
// await userEvent.type(inputControl, 'toto');
// const confirmButton = await screen.findByRole('button', {
// name: /confirm/i,
// });
// await userEvent.click(confirmButton);
// const dialogControl = await screen.findByRole('dialog');
// expect(dialogControl).toBeInTheDocument();
// const profilTitle = await screen.findByRole('heading', {
// name: /profil/i,
// });
// expect(profilTitle).toBeInTheDocument();
// const confirmDialogButton = await screen.findByRole('button', {
// name: /confirm/i,
// });
// await userEvent.click(confirmDialogButton);
// const confirmText = await screen.getByText('Name has been submitted');
// expect(confirmText).toBeInTheDocument();
// });
});

View File

@@ -0,0 +1,162 @@
import { FakeBackendService } from '@angular-challenges/testing-table/backend';
import { AsyncPipe, DatePipe, NgIf } from '@angular/common';
import { AfterViewInit, Component, ViewChild, inject } from '@angular/core';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatPaginator, MatPaginatorModule } from '@angular/material/paginator';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatSort, MatSortModule, SortDirection } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import { User } from '@ngneat/falso';
import { LetDirective } from '@ngrx/component';
import { ComponentStore, tapResponse } from '@ngrx/component-store';
import { map, pipe, startWith, switchMap, tap } from 'rxjs';
interface TableState {
loading: boolean;
error?: string;
users: User[];
}
@Component({
selector: 'app-table',
standalone: true,
imports: [
MatFormFieldModule,
MatInputModule,
MatTableModule,
MatSortModule,
MatPaginatorModule,
MatProgressSpinnerModule,
LetDirective,
NgIf,
AsyncPipe,
DatePipe,
],
template: `
<div class="example-container mat-elevation-z8">
<div class="example-loading-shade" *ngIf="loading$ | async">
<mat-spinner></mat-spinner>
<!-- <div class="example-rate-limit-reached" *ngIf="isRateLimitReached">
GitHub's API rate limit has been reached. It will be reset in one
minute.
</div> -->
</div>
<div class="example-table-container">
<table
mat-table
[dataSource]="(issues$ | async) ?? []"
class="example-table"
matSort>
<!-- Number Column -->
<ng-container matColumnDef="firstName">
<th mat-header-cell *matHeaderCellDef mat-sort-header>FirstName</th>
<td mat-cell *matCellDef="let row">{{ row.firstName }}</td>
</ng-container>
<!-- Title Column -->
<ng-container matColumnDef="lastName">
<th mat-header-cell *matHeaderCellDef mat-sort-header>LastName</th>
<td mat-cell *matCellDef="let row">{{ row.lastName }}</td>
</ng-container>
<!-- State Column -->
<ng-container matColumnDef="email">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Email</th>
<td mat-cell *matCellDef="let row">{{ row.email }}</td>
</ng-container>
<!-- Created Column -->
<!-- <ng-container matColumnDef="created">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Created</th>
<td mat-cell *matCellDef="let row">{{ row.created_at | date }}</td>
</ng-container> -->
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
</div>
<mat-paginator
[length]="resultsLength$ | async"
[pageSize]="30"
aria-label="Select page of GitHub search results"></mat-paginator>
</div>
`,
})
export class TableComponent
extends ComponentStore<TableState>
implements AfterViewInit
{
readonly displayedColumns = ['firstName', 'lastName', 'email'];
private api = inject(FakeBackendService);
readonly issues$ = this.select((s) => s.users).pipe(
tap((t) => console.log('UserNEw ', t))
);
readonly loading$ = this.select((s) => s.loading);
readonly error$ = this.select((s) => s.error);
readonly resultsLength$ = this.select((s) => 100);
// resultsLength = 0;
// isLoadingResults = true;
// isRateLimitReached = false;
@ViewChild(MatPaginator) paginator!: MatPaginator;
@ViewChild(MatSort) sort!: MatSort;
constructor() {
super({ loading: false, users: [] });
}
readonly loadData = this.effect<{
sortActive: keyof User;
sortDir: SortDirection;
pageIndex: number;
}>(
pipe(
tap((t) => console.log('cocou', t)),
tap(() => this.patchState({ loading: true, users: [] })),
switchMap(({ sortActive, sortDir, pageIndex }) =>
this.api.getUsers(sortActive, sortDir, pageIndex).pipe(
tap((t) => console.log('user', t)),
tapResponse(
(data) => this.patchState({ users: data, loading: false }),
(err) => this.patchState({ error: err as string, loading: false })
)
)
)
)
);
ngAfterViewInit(): void {
// due to ExpressionChangedAfterItHasBeenCheckedError
console.log('cocuo', this.sort, this.paginator);
this.loadData(
this.select({
sortActive: this.sort.sortChange.pipe(
map((s) => s.active as keyof User)
),
sortDir: this.sort.sortChange.pipe(map((s) => s.direction)),
pageIndex: this.paginator.page.pipe(map((p) => p.pageIndex)),
}).pipe(
startWith({
sortActive: this.sort.active as keyof User,
sortDir: this.sort.direction,
pageIndex: 1,
})
)
);
}
// applyFilter(event: Event) {
// const filterValue = (event.target as HTMLInputElement).value;
// this.dataSource.filter = filterValue.trim().toLowerCase();
// if (this.dataSource.paginator) {
// this.dataSource.paginator.firstPage();
// }
// }
}

View File

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>TestingTable</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 { provideHttpClient } from '@angular/common/http';
import { bootstrapApplication } from '@angular/platform-browser';
import { provideAnimations } from '@angular/platform-browser/animations';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, {
providers: [provideAnimations(), provideHttpClient()],
}).catch((err) => console.error(err));

View File

@@ -0,0 +1,31 @@
@use '@angular/material' as mat;
@tailwind base;
@tailwind components;
@tailwind utilities;
/* You can add global styles to this file, and also import other style files */
@include mat.core();
$theme-primary: mat.define-palette(mat.$indigo-palette);
$theme-accent: mat.define-palette(mat.$pink-palette, A200, A100, A400);
$theme-warn: mat.define-palette(mat.$red-palette);
$theme: mat.define-light-theme(
(
color: (
primary: $theme-primary,
accent: $theme-accent,
warn: $theme-warn,
),
typography: mat.define-typography-config(),
)
);
@include mat.core-theme($theme);
@include mat.all-component-themes($theme);
// @include mat.form-field-theme($theme);
// @include mat.input-theme($theme);
// @include mat.paginator-theme($theme);

View File

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