Skip to content

Commit

Permalink
refactor(kit): add lazy loading logic to tuiGenerateDialogableRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Puris committed Feb 17, 2023
1 parent 2651f81 commit 84df153
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {TuiPage1ExampleComponent} from './page-1.component';
component: TuiPage1ExampleComponent,
children: [
// step 3: use tuiGenerateDialogableRoute inside children property
tuiGenerateDialogableRoute(`path/to/dialog`, DialogContentComponent),
tuiGenerateDialogableRoute(DialogContentComponent, {
path: `path/to/dialog`,
}),
],
},
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {TuiRoutableDialogModule} from '@taiga-ui/kit';
{
path: ``,
component: MyPageComponent,
children: [tuiGenerateDialogableRoute(`path/to/dialog`, MyDialogComponent)],
children: [tuiGenerateDialogableRoute(MyDialogComponent, {path: `path/to/dialog`})],
},
]),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export class RoutableDialogComponent {

readonly addRouterOutlet = import('./examples/setup/add-router-outlet.md?raw');
readonly importModule = import('./examples/setup/import-module.md?raw');
readonly useWrapper = import('./examples/setup/use-helper.md?raw');
readonly useRouteGenerator = import('./examples/setup/use-route-generator.md?raw');
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</p>
<tui-doc-code
filename="my-page.module.ts"
[code]="useWrapper"
[code]="useRouteGenerator"
></tui-doc-code>
</li>
</ol>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
import {tuiGenerateLazyDialogableRoute} from '@taiga-ui/kit';
import {tuiGenerateDialogableRoute} from '@taiga-ui/kit';

import {DialogContentComponent} from './dialog-content.component';

@NgModule({
// step 4: use tuiGenerateLazyDialogableRoute
// step 4: use tuiGenerateDialogableRoute
imports: [
RouterModule.forChild([tuiGenerateLazyDialogableRoute(DialogContentComponent)]),
RouterModule.forChild([tuiGenerateDialogableRoute(DialogContentComponent)]),
],
declarations: [DialogContentComponent],
exports: [DialogContentComponent],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
```ts
import {tuiGenerateLazyDialogableRoute} from '@taiga-ui/kit';
import {tuiGenerateDialogableRoute} from '@taiga-ui/kit';

// ...

@NgModule({
imports: [
// ...
RouterModule.forChild([tuiGenerateLazyDialogableRoute(MyDialogComponent)]),
RouterModule.forChild([tuiGenerateDialogableRoute(MyDialogComponent)]),
],
declarations: [MyDialogComponent],
exports: [MyDialogComponent],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class LazyRoutableDialogComponent {

readonly addRouterOutlet = import('./examples/setup/add-router-outlet.md?raw');
readonly importModule = import('./examples/setup/import-module.md?raw');
readonly useWrapper = import('./examples/setup/use-helper.md?raw');
readonly useRouteGenerator = import('./examples/setup/use-route-generator.md?raw');
readonly addLazyLoadedModule = import(
'./examples/setup/add-lazy-loaded-module.md?raw'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@
<li>
<p i18n>
Use
<code>tuiGenerateLazyDialogableRoute</code>
<code>tuiGenerateDialogableRoute</code>
in the
<code>MyDialogModule</code>
router config
</p>
<tui-doc-code
filename="my-dialog.module.ts"
[code]="useWrapper"
[code]="useRouteGenerator"
></tui-doc-code>
</li>
</ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {TuiDialogOptions} from '@taiga-ui/core';
import {TuiRoutableDialogComponent} from './routable-dialog.component';

export function tuiGenerateDialogableRoute<I>(
path: string,
component: Type<any>,
options: Partial<TuiDialogOptions<I>> = {},
{path, ...dialogOptions}: Partial<TuiDialogOptions<I>> & {path: string} = {path: ``},
): Route {
return {
path,
Expand All @@ -18,7 +17,8 @@ export function tuiGenerateDialogableRoute<I>(
.split(`/`)
.map(() => `..`)
.join(`/`),
options,
isLazy: path === ``,
dialogOptions,
},
};
}

This file was deleted.

1 change: 0 additions & 1 deletion projects/kit/components/routable-dialog/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './generate-dialogable-route';
export * from './generate-lazy-dialogable-route';
export * from './routable-dialog.component';
export * from './routable-dialog.module';
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class TuiRoutableDialogComponent {
this.route.snapshot.data['dialog'],
this.injector,
),
this.route.snapshot.data['options'],
this.route.snapshot.data['dialogOptions'],
)
.pipe(takeUntil(this.destroy$))
.subscribe({
Expand All @@ -34,8 +34,11 @@ export class TuiRoutableDialogComponent {
}

private navigateToParent(): void {
const backUrl =
this.route.snapshot.data['backUrl'] ?? this.getLazyLoadedBackUrl();
const isLazy = this.route.snapshot.data['isLazy'];

const backUrl = isLazy
? this.getLazyLoadedBackUrl()
: this.route.snapshot.data['backUrl'];

void this.router.navigate([backUrl], {
relativeTo: this.route,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,77 @@ import {TuiRoutableDialogComponent} from '../routable-dialog.component';
class DialogComponent {}

describe(`tuiGenerateDialogableRoute`, () => {
it(`route are configured correctly`, () => {
const result = tuiGenerateDialogableRoute(`path/to/dialog`, DialogComponent);
it(`generated route should have component: TuiRoutableDialogComponent`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent);

const expected = {
expect(result.component).toBe(TuiRoutableDialogComponent);
});

it(`path passed correctly`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent, {
path: `path/to/dialog`,
component: TuiRoutableDialogComponent,
data: {
dialog: DialogComponent,
backUrl: `../../..`,
options: {},
},
};
});

expect(result).toEqual(expected);
expect(result.path).toEqual(`path/to/dialog`);
});

it(`options are passed correctly`, () => {
const options = {
it(`dialog options are passed correctly`, () => {
const dialogOptions = {
dismissible: true,
closeable: true,
};

const result = tuiGenerateDialogableRoute(``, DialogComponent, options);
const result = tuiGenerateDialogableRoute(DialogComponent, {
path: ``,
...dialogOptions,
});

expect(result?.data?.dialogOptions).toEqual(dialogOptions);
});

it(`if path is empty string then isLazy: true`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent);

expect(result?.data?.isLazy).toBe(true);
});

it(`if path is not empty string then isLazy: false`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent, {path: `path`});

expect(result?.data?.isLazy).toBe(false);
});

describe(`checking back url calculation`, () => {
it(`back url calculated correctly for empty path`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent, {
path: ``,
});

expect(result?.data?.backUrl).toBe(`..`);
});

it(`back url calculated correctly for single segment`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent, {
path: `path`,
});

expect(result?.data?.backUrl).toBe(`..`);
});

it(`back url calculated correctly for double segments`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent, {
path: `path/to`,
});

expect(result?.data?.backUrl).toBe(`../..`);
});

it(`back url calculated correctly for triple segments`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent, {
path: `path/to/dialog`,
});

expect(result?.data?.options).toEqual(options);
expect(result?.data?.backUrl).toBe(`../../..`);
});
});
});

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe(`TuiRoutableDialog`, () => {
snapshot: {
data: {
dialog: DialogComponent,
options: dialogOptions,
dialogOptions,
},
} as any,
});
Expand All @@ -86,7 +86,12 @@ describe(`TuiRoutableDialog`, () => {

it(`Closing the dialog navigates back to the parent route for lazy loaded case`, fakeAsync(() => {
createComponent({
...DEFAULT_ACTIVATED_ROUTE_MOCK,
snapshot: {
data: {
dialog: DialogComponent,
isLazy: true,
},
},
parent: {
snapshot: {
url: `path/to/lazy/dialog`,
Expand Down

0 comments on commit 84df153

Please sign in to comment.