Merge branch 'unit-test-client' into dev

This commit is contained in:
Carlos 2017-08-29 12:44:47 +02:00
commit 49a5b97e4d
9 changed files with 186 additions and 27 deletions

View File

@ -0,0 +1,48 @@
//TEST PENDING DUE TO MISSING TRIGGER ON FORM SUBMIT.
// import './address-create.js';
// describe('Component vnAddressCreate', () => {
// let $componentController;
// let $state;
// let $scope;
// let $httpBackend;
// let vnApp;
// beforeEach(() => {
// angular.mock.module('client');
// });
// beforeEach(angular.mock.inject(function(_$componentController_, $rootScope, _$httpBackend_,_$state_, _vnApp_) {
// $componentController = _$componentController_;
// $scope = $rootScope.$new();
// $httpBackend = _$httpBackend_;
// $state = _$state_;
// vnApp = _vnApp_;
// spyOn(vnApp, 'showError');
// }));
// it('should request to update the password', function() {
// let controller = $componentController('vnAddressCreate', {$httpBackend: $httpBackend, $scope: $scope, $state: $state});
// controller.address = {
// agencyFk: 23,
// city: "Valencia",
// clientFk: 3,
// consignee: "sd",
// enabled: false,
// id: 22490,
// mobile: "123456789",
// phone: "98765432",
// postcode: "12345",
// provinceFk: 63,
// street: "a"
// };
// $httpBackend.when('PATCH', '/client/api/Addresses/3').respond('done');
// $httpBackend.expectPATCH('/client/api/Addresses/3', {
// clientFk: 3,
// enabled: true
// });
// $httpBackend.flush();
// });
// });

View File

@ -10,7 +10,7 @@ export default class Controller {
this.billData = {}; this.billData = {};
this.copyData(); this.copyData();
} }
$onChanges(changes) { $onChanges() {
this.copyData(); this.copyData();
} }
copyData() { copyData() {

View File

@ -0,0 +1,59 @@
import './billing-data.js';
describe('Component vnClientBillingData', () => {
let $componentController;
let $scope;
let vnApp;
beforeEach(() => {
angular.mock.module('client');
});
beforeEach(angular.mock.inject(function(_$componentController_, $rootScope, _$q_, _vnApp_) {
$componentController = _$componentController_;
$scope = $rootScope.$new();
vnApp = _vnApp_;
spyOn(vnApp, 'showError');
}));
// describe('$onChanges()', () => {
// it(`should call copyData()`, () => {
// });
// });
describe('copyData()', () => {
it(`should pass client data to billData`, () => {
let controller = $componentController('vnClientBillingData', {$scope: $scope});
controller.client = {
payMethodFk: 'this',
iban: 'is',
dueDay: 'basically',
discount: 'nonsense',
credit: 'test',
creditInsurance: 'text!'
};
controller.copyData();
expect(controller.billData).toEqual(controller.client);
});
});
describe('submit()', () => {
it(`should call submit() on the watcher then receive a callback`, () => {
let controller = $componentController('vnClientBillingData', {$scope: $scope});
spyOn(controller, 'submit');
controller.submit();
expect(controller.submit).toHaveBeenCalled();
// controller.client = {
// payMethodFk: 'this',
// iban: 'is',
// dueDay: 'basically',
// discount: 'nonsense',
// credit: 'test',
// creditInsurance: 'text!'
// };
// controller.copyData();
// expect(controller.billData).toEqual(controller.client);
});
});
});

View File

@ -1,5 +1,5 @@
import {ng} from 'vendor'; import {ng} from 'vendor';
import 'core'; import 'core';
const ngModule = ng.module('client', []); const ngModule = ng.module('client', ['salix']);
export default ngModule; export default ngModule;

View File

@ -2,8 +2,7 @@
vn-id="watcher" vn-id="watcher"
url="/client/api/Accounts" url="/client/api/Accounts"
id-field="id" id-field="id"
data="$ctrl.client.account" data="$ctrl.account"
to="$ctrl.account"
form="form"> form="form">
</vn-watcher> </vn-watcher>
<form name="form" ng-submit="watcher.submit()" pad-medium> <form name="form" ng-submit="watcher.submit()" pad-medium>

View File

@ -1,23 +1,75 @@
import VNClientWebAccess from './web-access.js'; import './web-access.js';
describe('some component', () => {
let instance; describe('Component VnClientWebAccess', () => {
var $scope; let $componentController;
var $http; let $httpBackend;
var vnApp = {showError: jasmine.createSpy('showError')}; let $scope;
let vnApp;
beforeEach(() => { beforeEach(() => {
instance = new VNClientWebAccess($scope, $http, vnApp); angular.mock.module('client');
}); });
it('should throw an error when password is empty', () => { beforeEach(angular.mock.inject(function(_$componentController_, $rootScope, _$httpBackend_, _vnApp_) {
instance.newPassword = ''; $componentController = _$componentController_;
instance.onPassChange('ACCEPT'); $scope = $rootScope.$new();
expect(vnApp.showError).toHaveBeenCalledWith(`Passwords can't be empty`); $httpBackend = _$httpBackend_;
vnApp = _vnApp_;
spyOn(vnApp, 'showError');
}));
describe('$onChanges()', () => {
it(`should pass client's account to account`, () => {
let controller = $componentController('vnClientWebAccess', {$scope: $scope});
controller.client = {client: 'Bruce Wayne', account: 'Wayne Industries'};
controller.account = {};
controller.$onChanges();
expect(controller.account).toBe('Wayne Industries');
});
}); });
it('should throw an error when password repeat doesnt match', () => { describe('onPassOpen()', () => {
instance.newPassword = 'test'; it('should set passwords to empty values', () => {
instance.repeatPassword = 'notTheSame'; let controller = $componentController('vnClientWebAccess', {$scope: $scope});
instance.onPassChange('ACCEPT'); controller.newPassword = 'm24x8';
expect(vnApp.showError).toHaveBeenCalledWith(`Passwords don't match`); controller.repeatPassword = 'm24x8';
controller.onPassOpen();
expect(controller.newPassword).toBe('');
expect(controller.repeatPassword).toBe('');
});
});
describe('onPassChange()', () => {
it('should request to update the password', function() {
let controller = $componentController('vnClientWebAccess', {$scope: $scope});
controller.client = {id: '1234'};
controller.newPassword = 'm24x8';
controller.repeatPassword = 'm24x8';
$httpBackend.when('PATCH', '/client/api/Accounts/1234').respond('done');
$httpBackend.expectPATCH('/client/api/Accounts/1234', {password: 'm24x8'});
controller.onPassChange('ACCEPT');
$httpBackend.flush();
});
describe(`when password is empty`, () => {
it('should throw an error', () => {
let controller = $componentController('vnClientWebAccess', {$scope: $scope});
controller.client = {id: '1234'};
controller.newPassword = '';
controller.onPassChange('ACCEPT');
expect(vnApp.showError).toHaveBeenCalledWith(`Passwords can't be empty`);
});
});
describe(`when passwords don't match`, () => {
it('should throw an error', () => {
let controller = $componentController('vnClientWebAccess', {$scope: $scope});
controller.client = {id: '1234'};
controller.newPassword = 'm24x8';
controller.repeatPassword = 'notMatchingPassword';
controller.onPassChange('ACCEPT');
expect(vnApp.showError).toHaveBeenCalledWith(`Passwords don't match`);
});
});
}); });
}); });

View File

@ -116,7 +116,12 @@ export default class Watcher extends Component {
}); });
} }
writeData(json, resolve) { writeData(json, resolve) {
this.data = this.copyObject(json.data); Object.keys(this.data).forEach(
key => {
this.data[key] = json.data[key];
}
);
this.copyData(); this.copyData();
resolve(json); resolve(json);
} }
@ -135,7 +140,6 @@ export default class Watcher extends Component {
copyData() { copyData() {
this.orgData = this.copyObject(this.data); this.orgData = this.copyObject(this.data);
} }
copyObject(data) { copyObject(data) {
let copy = {}; let copy = {};
if (data) { if (data) {
@ -148,7 +152,6 @@ export default class Watcher extends Component {
} }
return copy; return copy;
} }
callback(transition) { callback(transition) {
if (!this.state && this.dataChanged()) { if (!this.state && this.dataChanged()) {
this.state = transition.to().name; this.state = transition.to().name;

View File

@ -1,5 +1,6 @@
// require all modules ending in ".spec" from the // require all modules ending in ".spec" from the
// current directory and all subdirectories // current directory and all subdirectories
import 'angular';
import 'angular-mocks';
var testsContext = require.context('./', true, /\.spec\.js$/); var testsContext = require.context('./', true, /\.spec\.js$/);
testsContext.keys().forEach(testsContext); testsContext.keys().forEach(testsContext);

View File

@ -46,16 +46,13 @@
"jasmine": "^2.7.0", "jasmine": "^2.7.0",
"karma": "^1.7.0", "karma": "^1.7.0",
"karma-chrome-launcher": "^2.2.0", "karma-chrome-launcher": "^2.2.0",
"karma-firefox-launcher": "^1.0.1",
"karma-jasmine": "^1.1.0", "karma-jasmine": "^1.1.0",
"karma-requirejs": "^1.1.0",
"karma-sourcemap-loader": "^0.3.7", "karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.4", "karma-webpack": "^2.0.4",
"merge-stream": "^1.0.1", "merge-stream": "^1.0.1",
"node-sass": "^3.11.0", "node-sass": "^3.11.0",
"pre-commit": "^1.1.3", "pre-commit": "^1.1.3",
"raw-loader": "*", "raw-loader": "*",
"requirejs": "^2.3.5",
"sass-loader": "^4.0.2", "sass-loader": "^4.0.2",
"style-loader": "^0.13.1", "style-loader": "^0.13.1",
"webpack": "^2.2.0", "webpack": "^2.2.0",