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: call navigate correctly without queryparams #154

Merged
merged 1 commit into from
Oct 30, 2020
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
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',
},
});
});