client side unit test for vnWatcher until submit[controller.save] plus small refactor for other files plus deletion of unused file

This commit is contained in:
Carlos 2017-10-16 15:20:40 +02:00
parent 496fd22b60
commit 5e1be3bc53
5 changed files with 104 additions and 19 deletions

View File

@ -33,11 +33,14 @@ describe('Client', () => {
it(`should request to GET the client notes`, () => {
let controller = $componentController('vnClientNotes', {$httpBackend: $httpBackend, $state: $state});
controller.client = {id: '1234'};
let json = JSON.stringify({where: {clientFk: '1234'}, order: 'created DESC'});
$httpBackend.when('GET', `/client/api/clientObservations?filter=${json}`).respond('ok');
$httpBackend.expectGET(`/client/api/clientObservations?filter=${json}`, {Accept: 'application/json, text/plain, */*'});
let jsonString = JSON.stringify({where: {clientFk: '1234'}, order: 'created DESC'});
let json = {data: 'some data'};
$httpBackend.when('GET', `/client/api/clientObservations?filter=${jsonString}`).respond(json);
$httpBackend.expectGET(`/client/api/clientObservations?filter=${jsonString}`, {Accept: 'application/json, text/plain, */*'});
controller.getObservation();
$httpBackend.flush();
expect(controller.observations).toEqual(json);
});
});

View File

@ -154,7 +154,7 @@ describe('Component vnAutocomplete', () => {
});
});
describe('findItem()', () => {
describe('findItems()', () => {
it(`should return items empty array if the controller does not provide a url and have no items defined`, () => {
let controller = $componentController('vnIconMenu', {$scope, $element, $httpBackend, $timeout});
controller.findItems('some search value');

View File

@ -59,7 +59,7 @@ export default class Watcher extends Component {
* Submits the data and goes back in the history.
*/
submitBack() {
this.submit().then(
return this.submit().then(
() => this.window.history.back()
);
}
@ -69,7 +69,7 @@ export default class Watcher extends Component {
* @param {String} state The state name
*/
submitGo(state) {
this.submit().then(
return this.submit().then(
() => this.$state.go(state)
);
}

View File

@ -25,7 +25,7 @@ describe('Component vnWatcher', () => {
$translate = _$translate_;
}));
describe('$onInit() ', () => {
describe('$onInit()', () => {
it(`should call fetchData() if controllers get and url properties are defined`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
controller.get = () => {};
@ -46,7 +46,7 @@ describe('Component vnWatcher', () => {
});
});
describe('$onChanges() ', () => {
describe('$onChanges()', () => {
it(`should call updateOriginalData() if controllers data is defined`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
controller.data = [];
@ -57,8 +57,8 @@ describe('Component vnWatcher', () => {
});
});
describe('$onDestroy() ', () => {
it(`should allways call deregisterCallback()`, () => {
describe('$onDestroy()', () => {
it(`should call deregisterCallback()`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
spyOn(controller, 'deregisterCallback');
controller.$onDestroy();
@ -66,4 +66,95 @@ describe('Component vnWatcher', () => {
expect(controller.deregisterCallback).toHaveBeenCalledWith();
});
});
describe('fetchData()', () => {
it(`should perform a query then store the received data into controller.data and call updateOriginalData()`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
spyOn(controller, 'updateOriginalData');
let json = {data: 'some data'};
controller.data = [1];
controller.idField = 0;
controller.url = 'test.com';
$httpBackend.whenGET('test.com/1').respond(json);
$httpBackend.expectGET('test.com/1');
controller.fetchData();
$httpBackend.flush();
expect(controller.data).toEqual({data: 'some data'});
expect(controller.updateOriginalData).toHaveBeenCalledWith();
});
});
describe('submitBack()', () => {
it(`should call controller.window.history.back() function after calling controllers submit() function`, done => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
spyOn(controller, 'submit').and.returnValue(Promise.resolve());
spyOn(controller.window.history, 'back');
controller.submitBack()
.then(() => {
expect(controller.submit).toHaveBeenCalledWith();
expect(controller.window.history.back).toHaveBeenCalledWith();
done();
});
});
});
describe('submitGo()', () => {
it(`should call controller.$state.go() function after calling controllers submit() function`, done => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
spyOn(controller, 'submit').and.returnValue(Promise.resolve());
spyOn(controller.$state, 'go');
let state = 'the state';
controller.submitGo(state)
.then(() => {
expect(controller.submit).toHaveBeenCalledWith();
expect(controller.$state.go).toHaveBeenCalledWith(state);
done();
});
});
});
describe('submit()', () => {
describe('when controller.form', () => {
it(`should call controller.form.setSubminted if controller.form is defined`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
controller.form = {$setSubmitted: () => {}};
spyOn(controller.form, '$setSubmitted');
controller.submit();
expect(controller.form.$setSubmitted).toHaveBeenCalledWith();
});
it(`should call controller.invalidForm if controller.form.$valid is not defined`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
controller.form = {$setSubmitted: () => {}};
spyOn(controller, 'invalidForm');
controller.submit();
expect(controller.invalidForm).toHaveBeenCalledWith(jasmine.any(Function));
});
});
describe('when !controller.dataChanged()', () => {
it(`should call controller.noChanges()`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
spyOn(controller, 'noChanges');
controller.submit();
expect(controller.noChanges).toHaveBeenCalledWith(jasmine.any(Function));
});
});
describe('when controller.save()', () => {
it(`should set controller.save.model property`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
controller.save = {};
controller.data = {originalInfo: 'original data', info: 'new data'};
controller.orgData = {originalInfo: 'original data'};
controller.submit();
expect(controller.save.model).toEqual({info: 'new data'});
});
});
});
});

View File

@ -1,9 +0,0 @@
const Jasmine = require('jasmine');
const jasmine = new Jasmine();
jasmine.loadConfig({
spec_dir: '',
spec_files: ['*[Ss]pec.js']
});
jasmine.execute();