Skip to content

Commit

Permalink
fix: call navigate correctly without queryparams
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Oct 30, 2020
1 parent 10d5079 commit 6130dad
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
15 changes: 9 additions & 6 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { Component, Type, NgZone, SimpleChange, OnChanges, SimpleChanges } from
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
import { Router } from '@angular/router';
import { NavigationExtras, Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import {
getQueriesForElement as dtlGetQueriesForElement,
prettyDOM as dtlPrettyDOM,
waitFor as dtlWaitFor,
waitForElementToBeRemoved as dtlWaitForElementToBeRemoved,
screen as dtlScreen,
queries as dtlQueries,
waitForOptions as dtlWaitForOptions,
configure as dtlConfigure,
} from '@testing-library/dom';
Expand Down Expand Up @@ -134,15 +133,19 @@ export async function render<SutType, WrapperType = SutType>(
const queryParams = params
? params.split('&').reduce((qp, q) => {
const [key, value] = q.split('=');
// TODO(Breaking): group same keys qp[key] ? [...qp[key], value] : value
qp[key] = value;
return qp;
}, {})
: undefined;

const doNavigate = () =>
router.navigate([path], {
queryParams,
});
const navigateOptions: NavigationExtras = queryParams
? {
queryParams,
}
: undefined;

const doNavigate = () => (navigateOptions ? router.navigate([path], navigateOptions) : router.navigate([path]));

let result;

Expand Down
2 changes: 1 addition & 1 deletion projects/testing-library/tests/find-by.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component } from '@angular/core';
import { timer } from 'rxjs';
import { render, screen } from '../src/public_api';
import { mapTo, timeout } from 'rxjs/operators';
import { mapTo } from 'rxjs/operators';

@Component({
selector: 'fixture',
Expand Down
41 changes: 41 additions & 0 deletions projects/testing-library/tests/navigate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
import { render } from '../src/public_api';

@Component({
selector: 'fixture',
template: ``,
})
class FixtureComponent {}

test('should navigate correctly', async () => {
const { navigate } = await render(FixtureComponent, {
routes: [{ path: 'details', component: FixtureComponent }],
});

const router = TestBed.inject(Router);
const navSpy = jest.spyOn(router, 'navigate');

navigate('details');

expect(navSpy).toBeCalledWith(['details']);
});

test('should pass queryParams if provided', async () => {
const { navigate } = await render(FixtureComponent, {
routes: [{ path: 'details', component: FixtureComponent }],
});

const router = TestBed.inject(Router);
const navSpy = jest.spyOn(router, 'navigate');

navigate('details?sortBy=name&sortOrder=asc');

expect(navSpy).toBeCalledWith(['details'], {
queryParams: {
sortBy: 'name',
sortOrder: 'asc',
},
});
});

0 comments on commit 6130dad

Please sign in to comment.