Skip to content

Commit cae1e21

Browse files
committed
test: add inject params transform & default value tests
1 parent 8de5654 commit cae1e21

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

libs/ngxtension/inject-params/src/inject-params.spec.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { Component, numberAttribute } from '@angular/core';
22
import { TestBed } from '@angular/core/testing';
33

44
import { provideRouter } from '@angular/router';
@@ -30,6 +30,34 @@ describe(injectParams.name, () => {
3030
expect(instance.userId()).toEqual('test');
3131
expect(instance.paramKeysList()).toEqual(['id']);
3232
});
33+
34+
it('returns a signal everytime the route params change based on the param id and transform option', async () => {
35+
TestBed.configureTestingModule({
36+
providers: [
37+
provideRouter([
38+
{ path: 'post/:id', component: PostComponent },
39+
{ path: 'post', component: PostComponent },
40+
]),
41+
],
42+
});
43+
44+
const harness = await RouterTestingHarness.create();
45+
46+
const instanceNull = await harness.navigateByUrl('/post', PostComponent);
47+
48+
expect(instanceNull.postId()).toEqual(null);
49+
expect(instanceNull.postIdDefault()).toEqual(69);
50+
51+
const instance = await harness.navigateByUrl('/post/420', PostComponent);
52+
53+
expect(instance.postId()).toEqual(420);
54+
expect(instance.postIdDefault()).toEqual(420);
55+
56+
await harness.navigateByUrl('/post/test', PostComponent);
57+
58+
expect(instance.postId()).toEqual(NaN);
59+
expect(instance.postIdDefault()).toEqual(NaN);
60+
});
3361
});
3462

3563
@Component({
@@ -41,3 +69,15 @@ export class UserProfileComponent {
4169
userId = injectParams('id');
4270
paramKeysList = injectParams((params) => Object.keys(params));
4371
}
72+
73+
@Component({
74+
standalone: true,
75+
template: ``,
76+
})
77+
export class PostComponent {
78+
postId = injectParams('id', { transform: numberAttribute });
79+
postIdDefault = injectParams('id', {
80+
transform: numberAttribute,
81+
defaultValue: 69,
82+
});
83+
}

0 commit comments

Comments
 (0)