Skip to content

Commit

Permalink
feat(kit): make path param optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Puris committed Feb 20, 2023
1 parent 52c0798 commit 74c1382
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {tuiGenerateDialogableRoute} from '@taiga-ui/kit';
import {DialogContentComponent} from './dialog-content.component';

@NgModule({
// step 4: use tuiGenerateDialogableRoute
// step 4: use tuiGenerateDialogableRoute without path param(or with path: '')
imports: [
RouterModule.forChild([tuiGenerateDialogableRoute(DialogContentComponent)]),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import {TuiRoutableDialogComponent} from './routable-dialog.component';

export function tuiGenerateDialogableRoute<I>(
component: Type<any>,
{path, ...dialogOptions}: Partial<TuiDialogOptions<I>> & {path: string} = {path: ``},
{path, ...dialogOptions}: Partial<TuiDialogOptions<I>> & {path?: string} = {},
): Route {
const processedPath = path ?? ``;

return {
path,
path: processedPath,
component: TuiRoutableDialogComponent,
data: {
dialog: component,
backUrl: path
backUrl: processedPath
.split(`/`)
.map(() => `..`)
.join(`/`),
isLazy: path === ``,
isLazy: processedPath === ``,
dialogOptions,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ describe(`tuiGenerateDialogableRoute`, () => {
expect(result.component).toBe(TuiRoutableDialogComponent);
});

it(`if passed path is undefined then route path is empty string`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent);

expect(result.path).toEqual(``);
});

it(`path passed correctly`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent, {
path: `path/to/dialog`,
Expand All @@ -32,19 +38,31 @@ describe(`tuiGenerateDialogableRoute`, () => {
expect(result?.data?.dialogOptions).toEqual(dialogOptions);
});

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

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

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

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 undefined path`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent);

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

it(`back url calculated correctly for empty path`, () => {
const result = tuiGenerateDialogableRoute(DialogComponent, {
path: ``,
Expand Down

0 comments on commit 74c1382

Please sign in to comment.