test(router): fix tests

This commit is contained in:
Manu Mtz.-Almeida
2018-05-11 19:45:44 +02:00
parent 3da0c989b2
commit d0bd3ba89e
7 changed files with 11048 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
import { RouteChain, RouterDirection } from '../utils/interface';
import { RouteChain, RouterIntent } from '../utils/interface';
import { chainToPath, generatePath, parsePath, readPath, writePath } from '../utils/path';
describe('parseURL', () => {
@@ -179,62 +179,62 @@ describe('readPath', () => {
describe('writePath', () => {
it('should write root path (no hash)', () => {
const history = mockHistory();
writePath(history, '', false, [''], RouterDirection.Forward, 123);
writePath(history, '', false, [''], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '/');
writePath(history, '', false, ['schedule'], RouterDirection.Forward, 123);
writePath(history, '', false, ['schedule'], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '/schedule');
writePath(history, '/', false, [''], RouterDirection.Forward, 123);
writePath(history, '/', false, [''], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '/');
writePath(history, '/', false, ['to', 'schedule'], RouterDirection.Forward, 123);
writePath(history, '/', false, ['to', 'schedule'], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '/to/schedule');
});
it('should write non root path (no hash)', () => {
const history = mockHistory();
writePath(history, '/path', false, [''], RouterDirection.Forward, 2);
writePath(history, '/path', false, [''], RouterIntent.Forward, 2);
expect(history.pushState).toHaveBeenCalledWith(2, '', '/path');
writePath(history, '/path', false, ['to', 'page'], RouterDirection.Forward, 2);
writePath(history, '/path', false, ['to', 'page'], RouterIntent.Forward, 2);
expect(history.pushState).toHaveBeenCalledWith(2, '', '/path/to/page');
writePath(history, 'path/to', false, ['second', 'page'], RouterDirection.Forward, 2);
writePath(history, 'path/to', false, ['second', 'page'], RouterIntent.Forward, 2);
expect(history.pushState).toHaveBeenCalledWith(2, '', '/path/to/second/page');
writePath(history, '/path/to/', false, ['second', 'page'], RouterDirection.Forward, 2);
writePath(history, '/path/to/', false, ['second', 'page'], RouterIntent.Forward, 2);
expect(history.pushState).toHaveBeenCalledWith(2, '', '/path/to/second/page');
});
it('should write root path (no hash)', () => {
const history = mockHistory();
writePath(history, '', true, [''], RouterDirection.Forward, 123);
writePath(history, '', true, [''], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/');
writePath(history, '', true, ['schedule'], RouterDirection.Forward, 123);
writePath(history, '', true, ['schedule'], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/schedule');
writePath(history, '/', true, [''], RouterDirection.Forward, 123);
writePath(history, '/', true, [''], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/');
writePath(history, '/', true, ['to', 'schedule'], RouterDirection.Forward, 123);
writePath(history, '/', true, ['to', 'schedule'], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/to/schedule');
});
it('should write non root path (no hash)', () => {
const history = mockHistory();
writePath(history, '/path', true, [''], RouterDirection.Forward, 123);
writePath(history, '/path', true, [''], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/path');
writePath(history, '/path', true, ['to', 'page'], RouterDirection.Forward, 123);
writePath(history, '/path', true, ['to', 'page'], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/path/to/page');
writePath(history, 'path/to', true, ['second', 'page'], RouterDirection.Forward, 123);
writePath(history, 'path/to', true, ['second', 'page'], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/path/to/second/page');
writePath(history, '/path/to/', true, ['second', 'page'], RouterDirection.Forward, 123);
writePath(history, '/path/to/', true, ['second', 'page'], RouterIntent.Forward, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/path/to/second/page');
});
});

View File

@@ -1,4 +1,5 @@
import { NavOutletElement, RouteChain, RouteID, RouterIntent } from '../../../interface';
import { NavOutletElement, RouteChain, RouteID } from '../../../interface';
import { RouterIntent } from './interface';
export async function writeNavState(root: HTMLElement | undefined, chain: RouteChain, intent: RouterIntent, index: number, changed = false): Promise<boolean> {
try {

View File

@@ -1,4 +1,5 @@
import { RouteChain, RouterIntent } from '../../../interface';
import { RouteChain } from '../../../interface';
import { RouterIntent } from './interface';
export function generatePath(segments: string[]): string {
const path = segments