Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow passing any values to the Route#data #504

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ interface SpectatorRouting<C> extends Spectator<C> {
/**
* Updates the route data and triggers a route navigation.
*/
setRouteData(name: string, value: string): void;
setRouteData(name: string, value: any): void;

/**
* Updates the route fragment and triggers a route navigation.
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/testing-with-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface SpectatorRouting<C> extends Spectator<C> {
/**
* Updates the route data and triggers a route navigation.
*/
setRouteData(name: string, value: string): void;
setRouteData(name: string, value: any): void;

/**
* Updates the route fragment and triggers a route navigation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class ActivatedRouteStub extends ActivatedRoute {
this.snapshot = this.buildSnapshot();
}

public setData(name: string, value: string): void {
public setData(name: string, value: any): void {
this.testData = { ...this.testData, [name]: value };
this.snapshot = this.buildSnapshot();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ export class SpectatorRouting<C> extends Spectator<C> {

/**
* Updates the route data and triggers a route navigation.
* The `value` is typed as `any` since the `Route#data` is a record with `any` values.
* There's no sense to make it generic until `Route#data` starts supporting generic types.
*/
public setRouteData(name: string, value: string): void {
public setRouteData(name: string, value: any): void {
if (this.checkStubPresent()) {
this.activatedRouteStub.setData(name, value);
this.triggerNavigationAndUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,27 @@ describe('ActivatedRouteStub', () => {
});
});

it('should update data in snapshot with any type of value', () => {
const activatedRouteStub = new ActivatedRouteStub();

activatedRouteStub.setData('movieName', 'Avengers')
activatedRouteStub.setData('countries', ['USA', 'Canada']);
activatedRouteStub.setData('animal', { type: 'dog' });

expect(activatedRouteStub.snapshot).not.toBeNull();

const { data } = activatedRouteStub.snapshot;

expect(data).toEqual({
movieName: 'Avengers',
countries: ['USA', 'Canada'],
animal: {
type: 'dog'
}
});
console.error(data);
})

it('should update fragment in snapshot when set', () => {
const activatedRouteStub = new ActivatedRouteStub();

Expand Down