I have a ViewModel, which is the modem version of the login view:
Src / pages / confirm.ts
import { autoinject } from 'aurelia-framework';
import { Router, NavigationInstruction } from 'aurelia-router';
import { ValidationControllerFactory, ValidationController, ValidationRules } from 'aurelia-validation';
import { LoginService } from '../services/login.service';
import { Settings } from '../config/settings';
import { State } from '../services/state';
import { Helpers } from '../services/helpers';
@autoinject
export class Confirm {
userName: string;
error: Error;
controller: ValidationController;
provider: string;
constructor(public service: LoginService,
private router: Router,
private state: State,
private helpers: Helpers,
controllerFactory: ValidationControllerFactory) {
this.controller = controllerFactory.createForCurrentScope();
this.provider = this.helpers.getUrlParameter('p');
this.userName = this.helpers.getUrlParameter('u');
window.history.replaceState(null, null, '/');
}
confirm() {
this.controller.validate()
.then(() => {
this.service.confirm(this.userName)
.then(() => {
this.router.navigateToRoute('home');
})
.catch((e: Error) => {
if (e.name === 'NullInfo') {
this.router.navigateToRoute('login');
} else {
this.error = e;
}
});
})
.catch(e => this.error = e);
}
}
ValidationRules
.ensure((c: Confirm) => c.userName)
.satisfies((value, obj) => obj.service.exists(value))
.withMessage('This user name already exists, please choose another one')
.on(Confirm);
I want to test it with unit test with aurelia-cli , I wrote these specifications:
test / pages / confirm.spec.ts
import { Router, NavigationInstruction } from 'aurelia-router';
import { Confirm } from '../../../src/pages/confirm';
import { LoginService } from '../../../src/services/login.service';
import { Settings } from '../../../src/config/settings';
import { State } from '../../../src/services/state';
import { Helpers } from '../../../src/services/helpers';
describe('confirm page spec', () => {
let service: LoginService;
let router: Router;
let state: State;
let helpers: Helpers;
let controllerFactory;
let userName;
let promise;
let resolveCallback;
let rejectCallback;
beforeEach(() => {
promise = {
then: r => {
resolveCallback = r;
return {
catch: e => {
rejectCallback = e;
}
}
}
};
service = {
confirm: u => {
userName = u;
return promise;
}
} as LoginService;
router = {
navigateToRoute: r => { }
} as Router;
state = new State();
helpers = new Helpers(state);
spyOn(helpers, 'getUrlParameter')
controllerFactory = {
createForCurrentScope: () => { }
};
spyOn(controllerFactory, 'createForCurrentScope')
.and.returnValue({
validate: () => {
return promise;
}
});
});
it('constructor should get url paratemeters', () => {
spyOn(helpers, 'getUrlParameter');
let page = new Confirm(service, router, state, helpers, controllerFactory);
expect(helpers.getUrlParameter).toHaveBeenCalledWith('p');
expect(helpers.getUrlParameter).toHaveBeenCalledWith('u');
})
});
When I run au test , I get this error:
Chrome 53.0.2785 (Windows 7 0.0.0) ERROR Error: Could not forget to add ".plugin (" aurelia-validation ") to your main.js? In C: /Users/olefebvre/Source/Repos/chatle.aurelia/ wwwroot / scripts / app-bundle.js: 2616
How to fix it?
The full project is on github at https://github.com/aguacongas/chatle.aurelia
UPDATE
( , )
:
name.html
<template>
<div validation-errors.bind="userNameErrors" class.bind="userNameErrors.length ? 'has-error' : ''">
<input class="form-control" name="UserName" value.bind="userName & validate" />
<span class="help-block" repeat.for="errorInfo of userNameErrors">
${errorInfo.error.message}
<span>
</div>
</template>
name.ts
import { autoinject, bindable, bindingMode } from 'aurelia-framework';
import { ValidationControllerFactory, ValidationController, ValidationRules } from 'aurelia-validation';
import { LoginService } from '../services/login.service';
@autoinject
export class UserName {
@bindable({ defaultBindingMode: bindingMode.twoWay })
userName: string;
controller: ValidationController;
constructor(private service: LoginService, controllerFactory: ValidationControllerFactory) {
this.controller = controllerFactory.createForCurrentScope();
}
userNameAvailable(value: string) {
return new Promise<boolean>(resolve => {
this.service.exists(value)
.then(r => resolve(!r));
})
}
}
ValidationRules
.ensure((c: UserName) => c.userName)
.satisfies((value, obj) => obj.userNameAvailable(value))
.withMessage('This user name already exists, please choose another one')
.on(UserName);
:
import {StageComponent} from 'aurelia-testing';
import {bootstrap} from 'aurelia-bootstrapper';
describe('user-name component specs', () => {
let component;
beforeEach(() => {
component = StageComponent
.withResources('components/user-name')
.inView('<user-name userName.bind="firstName"></user-namet>')
.boundTo({ firstName: 'Test' });
});
it('should render first name', done => {
component.create(bootstrap).then(() => {
const nameElement = document.querySelector('.form-control');
expect(nameElement.attributes['value']).toBe('Test');
done();
});
});
afterEach(() => {
component.dispose();
});
});
au test, ,
WARN: '% cUnhandled rejection : ".plugin('aurelia-validation)" main.js? FluentEnsure.assertInitialized(http://localhost:9876/base/wwwroot/scripts/app-bundle.js?f3fa3f9ce9b587af8455ab05a0d491f872123546:9:197927) FluentEnsure.ensure(http://localhost:9876/base/wwwroot/scripts/app-bundle.js?f3fa3f9ce9b587af8455ab05a0d491f872123546:9:196845) . ValidationRules.ensure(http://localhost:9876/base/wwwroot/scripts/app-bundle.js?f3fa3f9ce9b587af8455ab05a0d491f872123546:9:198743) . (http://localhost:9876/base/wwwroot/scripts/app-bundle.js?f3fa3f9ce9b587af8455ab05a0d491f872123546:9:95049) Object.execCb(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?8e5718043dfbefd1c3ad0ea29315a48c1ff7a645:3785:299) Object.check(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?8e5718043dfbefd1c3ad0ea29315a48c1ff7a645:3774:12) Object.enable(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?8e5718043dfbefd1c3ad0ea29315a48c1ff7a645:3779:58) Object.enable(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?8e5718043dfbefd1c3ad0ea29315a48c1ff7a645:3783:433) . (http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?8e5718043dfbefd1c3ad0ea29315a48c1ff7a645:3778:436) http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?8e5718043dfbefd1c3ad0ea29315a48c1ff7a645:3763:140 y (http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?8e5718043dfbefd1c3ad0ea29315a48c1ff7a645:3762:207) Object.enable(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?8e5718043dfbefd1c3ad0ea29315a48c1ff7a645:3777:469) Object.init(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?8e5718043dfbefd1c3ad0ea29315a48c1ff7a645:3772:154) http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?8e5718043dfbefd1c3ad0ea29315a48c1ff7a645:3782:308
2
, :
import { Aurelia } from 'aurelia-framework';
import { StageComponent } from 'aurelia-testing';
import { bootstrap } from 'aurelia-bootstrapper';
describe('user-name component specs', () => {
let component;
beforeEach(() => {
component = StageComponent
.withResources('components/user-name')
.inView('<user-name userName.bind="firstName"></user-namet>')
.boundTo({ firstName: 'Test' });
// bootstrap function call the component configure function with an Aurelia instance
component.configure = (aurelia:Aurelia) => {
aurelia.use
.standardConfiguration()
.plugin('aurelia-validation');
}
});
it('should render user name', done => {
component.create(bootstrap).then(() => {
const nameElement = document.querySelector('.form-control');
expect(nameElement['value']).toBe('Test');
done();
});
});
afterEach(() => {
component.dispose();
});
});
:
WARN: '% cUnhandled rejection : : () {__ cov_YrDLEqGJfOMLMH3Hdk8_ZA.f [ '231'] ++; __ cov_YrDLEqGJfOMLMH3Hdk8_ZA.s [ '724'] ++; c.userName;} ValidationParser.getAccessorExpression(http://localhost:9876/base/wwwroot/scripts/app-bundle.js?4e630e49e067afd65b1f5906dc4064c434c5e5df:9:177914) ValidationParser.parseProperty(http://localhost:9876/base/wwwroot/scripts/app-bundle.js?4e630e49e067afd65b1f5906dc4064c434c5e5df:9:178586) FluentEnsure.ensure(http://localhost:9876/base/wwwroot/scripts/app-bundle.js?4e630e49e067afd65b1f5906dc4064c434c5e5df:9:196210) Function.ValidationRules.ensure(http://localhost:9876/base/wwwroot/scripts/app-bundle.js?4e630e49e067afd65b1f5906dc4064c434c5e5df:9:197995) . (http://localhost:9876/base/wwwroot/scripts/app-bundle.js?4e630e49e067afd65b1f5906dc4064c434c5e5df:9:94307) Object.execCb(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:3785:299) Object.check(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:3774:12) Object.enable(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:3779:58) Object.enable(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:3783:433) . (http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:3778:436) http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:3763:140 y (http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:3762:207) Object.enable(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:3777:469) Object.init(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:3772:154) http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:3782:308 : DefaultLoader.loadModule(http://localhost:9876/base/wwwroot/scripts/vendor-bundle.js?89c5527ca11655b8716186a7a911ca39c4069f47:11444:14)
3
, , app-bundle.js , , :
preprocessors: {
[project.unitTestRunner.source]: [project.transpiler.id],
},