Skip to content

Commit

Permalink
chore(tests): add test for issue 639
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed May 28, 2019
1 parent f756461 commit 24394dd
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 23 deletions.
4 changes: 4 additions & 0 deletions test/integration/issue-639/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<h1>${message}</h1>
<router-view></router-view>
</template>
26 changes: 26 additions & 0 deletions test/integration/issue-639/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Router } from '../../../src/router';
import { RouterConfiguration, NavigationInstruction } from '../../../src/aurelia-router';
import { PLATFORM } from 'aurelia-pal';

export class App {
message = 'Hello World!';
router: Router;

configureRouter(config: RouterConfiguration, router: Router) {
config
.map([
{ route: ['', 'home'], name: 'home', moduleId: PLATFORM.moduleName('./landing') },
{ route: 'routePage', name: 'route.page', moduleId: PLATFORM.moduleName('./search') }
])
.mapUnknownRoutes(instruction => {
return { redirect: 'routePage' };
});

this.router = router;
}

activate() {
console.log('App activated');
}

}
24 changes: 24 additions & 0 deletions test/integration/issue-639/issue-639.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import '../../setup';
import { PLATFORM } from 'aurelia-pal';
import { bootstrapAurelia, wait } from '../utilities';
import { RoutableComponentDetermineActivationStrategy } from '../../../src/interfaces';
import { App } from './app';
import { Landing } from './landing';

fdescribe('issue-639.spec.ts', () => {

beforeEach(() => {
// location.replace('#/');
});

it('should works', async () => {
location.hash = 'none?queryTest=123';
const { aurelia, appRouter, dispose } = await bootstrapAurelia(
PLATFORM.moduleName('test/integration/issue-639/app')
);

expect(document.URL.endsWith('routePage?queryTest=123')).toBe(true, 'ends with routepage?keyword for url: ' + document.URL);

dispose();
});
});
5 changes: 5 additions & 0 deletions test/integration/issue-639/landing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div class="page-landing">Landing!</div>
<br>
<button class="page-landing-navigate-btn" click.trigger="goto()">Show dynamic page with random key</button><br>
</template>
21 changes: 21 additions & 0 deletions test/integration/issue-639/landing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { inject } from 'aurelia-framework';
import { Router, activationStrategy, Redirect } from '../../../src/aurelia-router';

@inject(Router)
export class Landing {

router: Router;
params: Record<string, any>;

constructor(router: Router) {
this.router = router;
}

activate(params: Record<string, any>) {
this.params = params;
}

goto() {
this.router.navigateToRoute('home', { key: Math.random(), compareQueryParams: true });
}
}
6 changes: 6 additions & 0 deletions test/integration/issue-639/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template>
<div class="page-search"></div>
Search!<br>
Key: ${params.key}<br>
<button click.trigger="home()">Go home</button>
</template>
15 changes: 15 additions & 0 deletions test/integration/issue-639/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Router } from '../../../src/aurelia-router';

export class Search {
router: Router;
params: Record<string, any> = {};

activate(params: Record<string, any>) {
this.params = params;
console.log('Search activate:', params);
}

home() {
this.router.navigateToRoute('home');
}
}
63 changes: 40 additions & 23 deletions test/integration/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,62 @@
import { Aurelia, PLATFORM, Controller } from 'aurelia-framework';
import { Aurelia, PLATFORM, Controller, LogManager } from 'aurelia-framework';
import { ConsoleAppender } from 'aurelia-logging-console';
import { AppRouter } from '../../src/app-router';

/**
* A bootstrapper utility to bootstrap an aurelia application for integration testing
*
* Handles all preparation and disposing steps for tests
*/
export const bootstrapAurelia = async (root: string | Function) => {
export const bootstrapAurelia = async <T extends object = object>(root: string | Function) => {
const baseMeta = document.createElement('base');
baseMeta.href = location.pathname;
document.head.prepend(baseMeta);
const host = document.body.appendChild(document.createElement('div'));
const aurelia = new Aurelia();
// avoid excessive logging behavior everytime an aurelia instance starts
const methodNames = ['log', 'warn', 'info', 'debug'];
const fns = methodNames.map(methodName => {
const fn = (console as Record<string, any>)[methodName];
(console as Record<string, any>)[methodName] = PLATFORM.noop;
return fn;
});
aurelia.use.standardConfiguration().developmentLogging();
await aurelia.start();
const appRouter = aurelia.container.get(AppRouter) as AppRouter;
await aurelia.setRoot(
root,
host
);
// restore logging behavior back to normal
methodNames.forEach((methodName, idx) => (console as Record<string, any>)[methodName] = fns[idx]);
return {
host,
aurelia,
appRouter,
dispose: () => {
const loggerImpl = new ConsoleAppender();
aurelia
.use
.standardConfiguration();
// avoid excessive logging behavior everytime an aurelia instance starts
// .developmentLogging();
let appRouter: AppRouter;
const disposeFn = () => {
try {
const root = (aurelia as any).root as Controller;
LogManager.removeAppender(loggerImpl);
baseMeta.remove();
root.unbind();
root.detached();
host.remove();
appRouter.reset();
appRouter.deactivate();
} catch {
// empty
}
};

try {
await aurelia.start();
appRouter = aurelia.container.get(AppRouter) as AppRouter;
await aurelia.setRoot(
root,
host
);
} catch (ex) {
console.log('Test bootstrapping error');
console.error(ex);
disposeFn();
throw ex;
}
LogManager.addAppender(loggerImpl);

return {
host,
aurelia,
appRouter,
viewModel: ((aurelia as any).root as Controller).viewModel as T,
dispose: disposeFn
};
};

export const setDocumentBaseUrl = (url: string) => {
Expand All @@ -50,3 +65,5 @@ export const setDocumentBaseUrl = (url: string) => {
// add new base tag
document.head.appendChild(document.createElement('base')).href = url;
};

export const wait = (time: number) => new Promise(r => setTimeout(r, time));

0 comments on commit 24394dd

Please sign in to comment.