2017-10-13 16:26:38 +00:00
|
|
|
import './watcher.js';
|
|
|
|
|
|
|
|
describe('Component vnWatcher', () => {
|
|
|
|
let $scope;
|
|
|
|
let $element;
|
|
|
|
let $state;
|
|
|
|
let $httpBackend;
|
2017-10-17 12:24:40 +00:00
|
|
|
let controller;
|
2018-01-09 12:49:15 +00:00
|
|
|
let $attrs;
|
2018-05-25 15:25:35 +00:00
|
|
|
let $q;
|
2020-03-09 08:00:03 +00:00
|
|
|
let data;
|
2017-10-13 16:26:38 +00:00
|
|
|
|
2019-10-24 22:53:53 +00:00
|
|
|
beforeEach(ngModule('vnCore'));
|
2017-10-13 16:26:38 +00:00
|
|
|
|
2020-07-23 14:46:16 +00:00
|
|
|
beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$state_, _$q_) => {
|
2020-03-09 08:00:03 +00:00
|
|
|
data = {
|
|
|
|
id: 1,
|
|
|
|
foo: 'bar'
|
|
|
|
};
|
|
|
|
|
2017-10-13 16:26:38 +00:00
|
|
|
$scope = $rootScope.$new();
|
|
|
|
$element = angular.element('<div></div>');
|
|
|
|
$state = _$state_;
|
2018-05-25 15:25:35 +00:00
|
|
|
$q = _$q_;
|
2018-12-22 10:59:26 +00:00
|
|
|
$httpBackend = _$httpBackend_;
|
2018-01-09 12:49:15 +00:00
|
|
|
$attrs = {
|
2018-12-22 10:59:26 +00:00
|
|
|
save: 'patch'
|
2018-01-09 12:49:15 +00:00
|
|
|
};
|
2018-12-22 10:59:26 +00:00
|
|
|
controller = $componentController('vnWatcher', {$scope, $element, $state, $httpBackend, $attrs, $q});
|
2017-10-13 16:26:38 +00:00
|
|
|
}));
|
|
|
|
|
2017-10-16 13:20:40 +00:00
|
|
|
describe('$onInit()', () => {
|
2020-03-09 08:00:03 +00:00
|
|
|
it('should set data empty by default', () => {
|
2017-10-13 16:26:38 +00:00
|
|
|
controller.$onInit();
|
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
expect(controller.data).toBeUndefined();
|
2017-10-13 16:26:38 +00:00
|
|
|
});
|
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
it('should set new data when insert mode is enabled', () => {
|
|
|
|
controller.insertMode = true;
|
|
|
|
controller.data = data;
|
2017-10-13 16:26:38 +00:00
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
controller.$onInit();
|
|
|
|
|
|
|
|
expect(controller.orgData).toEqual(data);
|
|
|
|
expect(controller.orgData).toEqual(data);
|
|
|
|
expect(controller.isNew).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call backend and fetch data if url and idValue properties are defined', () => {
|
|
|
|
controller.url = 'Foos';
|
|
|
|
controller.idValue = 1;
|
|
|
|
|
|
|
|
$httpBackend.expectGET('Foos/1').respond(data);
|
|
|
|
controller.$onInit();
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
expect(controller.orgData).toEqual(data);
|
|
|
|
expect(controller.orgData).toEqual(data);
|
|
|
|
expect(controller.orgData).not.toBe(controller.data);
|
2017-10-13 16:26:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
describe('fetch()', () => {
|
|
|
|
it(`should perform a query then store the received data into data property and make an snapshot into orgData`, () => {
|
|
|
|
controller.url = 'Bars';
|
|
|
|
controller.idValue = 1;
|
|
|
|
|
|
|
|
$httpBackend.expectGET('Bars/1').respond(data);
|
|
|
|
controller.$onInit();
|
2017-10-16 13:20:40 +00:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
expect(controller.orgData).toEqual(data);
|
|
|
|
expect(controller.orgData).toEqual(data);
|
|
|
|
expect(controller.orgData).not.toBe(controller.data);
|
2017-10-16 13:20:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('submitBack()', () => {
|
2020-02-26 12:22:52 +00:00
|
|
|
it('should call controller.window.history.back() function after calling controllers submit() function', done => {
|
|
|
|
jest.spyOn(controller, 'submit').mockReturnValue(Promise.resolve());
|
|
|
|
jest.spyOn(controller.window.history, 'back');
|
2017-10-16 13:20:40 +00:00
|
|
|
controller.submitBack()
|
2018-12-22 10:59:26 +00:00
|
|
|
.then(() => {
|
|
|
|
expect(controller.submit).toHaveBeenCalledWith();
|
|
|
|
expect(controller.window.history.back).toHaveBeenCalledWith();
|
|
|
|
done();
|
|
|
|
}).catch(done.fail);
|
2017-10-16 13:20:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('submitGo()', () => {
|
2020-02-26 12:22:52 +00:00
|
|
|
it('should call controller.$state.go() function after calling controllers submit() function', done => {
|
|
|
|
jest.spyOn(controller, 'submit').mockReturnValue(Promise.resolve());
|
|
|
|
jest.spyOn(controller.$state, 'go');
|
2017-12-04 07:17:29 +00:00
|
|
|
let state = 'the.State';
|
2017-10-16 13:20:40 +00:00
|
|
|
controller.submitGo(state)
|
2018-12-22 10:59:26 +00:00
|
|
|
.then(() => {
|
|
|
|
expect(controller.submit).toHaveBeenCalledWith();
|
|
|
|
expect(controller.$state.go).toHaveBeenCalledWith(state, {});
|
|
|
|
done();
|
|
|
|
}).catch(done.fail);
|
2017-10-16 13:20:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
describe('check()', () => {
|
|
|
|
it(`should throw error if controller.form is invalid`, () => {
|
|
|
|
controller.form = {$invalid: true};
|
2017-10-16 13:20:40 +00:00
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
expect(function() {
|
|
|
|
controller.check();
|
|
|
|
}).toThrowError();
|
|
|
|
});
|
|
|
|
});
|
2017-10-16 13:20:40 +00:00
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
describe('realSubmit()', () => {
|
|
|
|
describe('when controller.form', () => {
|
|
|
|
it(`should call controller.form.setSubmited if controller.form is defined`, () => {
|
|
|
|
controller.form = {
|
|
|
|
$setSubmitted: () => {},
|
|
|
|
$setPristine: () => {}
|
|
|
|
};
|
2020-02-26 12:22:52 +00:00
|
|
|
jest.spyOn(controller.form, '$setSubmitted');
|
2018-05-25 15:25:35 +00:00
|
|
|
controller.realSubmit();
|
2017-10-16 13:20:40 +00:00
|
|
|
|
2018-05-31 09:52:39 +00:00
|
|
|
expect(controller.form.$setSubmitted).toHaveBeenCalledWith();
|
2017-10-16 13:20:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when controller.save()', () => {
|
|
|
|
it(`should set controller.save.model property`, () => {
|
2018-05-25 15:25:35 +00:00
|
|
|
controller.save = {accept: () => $q.resolve()};
|
2017-10-16 13:20:40 +00:00
|
|
|
controller.data = {originalInfo: 'original data', info: 'new data'};
|
|
|
|
controller.orgData = {originalInfo: 'original data'};
|
2018-05-25 15:25:35 +00:00
|
|
|
controller.realSubmit();
|
2017-10-16 13:20:40 +00:00
|
|
|
|
|
|
|
expect(controller.save.model).toEqual({info: 'new data'});
|
|
|
|
});
|
2017-10-17 10:27:32 +00:00
|
|
|
});
|
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
describe('should perform a PATCH query and save the data', () => {
|
|
|
|
it(`should perform a query then call controller.writeData()`, () => {
|
|
|
|
controller.url = 'Foos';
|
|
|
|
controller.data = data;
|
|
|
|
|
|
|
|
const changedData = {baz: 'value'};
|
|
|
|
Object.assign(controller.data, changedData);
|
|
|
|
|
|
|
|
$httpBackend.expectPATCH('Foos/1', changedData).respond({newProp: 'some'});
|
|
|
|
controller.realSubmit();
|
2017-10-17 10:27:32 +00:00
|
|
|
$httpBackend.flush();
|
2020-03-09 08:00:03 +00:00
|
|
|
|
|
|
|
expect(controller.data).toEqual(Object.assign({}, data, changedData));
|
2017-10-17 10:27:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
it(`should perform a POST query and save the data`, () => {
|
|
|
|
controller.insertMode = true;
|
|
|
|
controller.url = 'Foos';
|
|
|
|
controller.data = data;
|
|
|
|
|
|
|
|
const changedData = {baz: 'value'};
|
|
|
|
Object.assign(controller.data, changedData);
|
|
|
|
|
|
|
|
$httpBackend.expectPOST('Foos', controller.data).respond({newProp: 'some'});
|
|
|
|
controller.realSubmit();
|
2017-10-17 10:27:32 +00:00
|
|
|
$httpBackend.flush();
|
2020-03-09 08:00:03 +00:00
|
|
|
|
|
|
|
expect(controller.data).toEqual(Object.assign({}, data, changedData));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('should perform a DELETE query and save empty data', () => {
|
|
|
|
it(`should perform a query then call controller.writeData()`, () => {
|
|
|
|
controller.url = 'Foos';
|
|
|
|
controller.data = data;
|
|
|
|
controller.delete();
|
|
|
|
|
|
|
|
$httpBackend.expectDELETE('Foos/1').respond();
|
|
|
|
controller.realSubmit();
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
expect(controller.data).toBeNull();
|
|
|
|
});
|
2017-10-17 10:27:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('writeData()', () => {
|
2020-03-09 08:00:03 +00:00
|
|
|
it(`should save data into orgData`, () => {
|
|
|
|
controller.data = data;
|
|
|
|
Object.assign(controller.data, {baz: 'value'});
|
|
|
|
|
|
|
|
controller.writeData({});
|
|
|
|
|
|
|
|
expect(controller.data).toEqual(controller.orgData);
|
2017-10-17 10:27:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('callback()', () => {
|
|
|
|
describe(`when dataChanged() returns true and there's no state in the controller`, () => {
|
2018-05-31 09:52:39 +00:00
|
|
|
it(`should define controller.state, call controller.$.confirm.show() and return false`, () => {
|
2017-10-17 10:27:32 +00:00
|
|
|
$scope.confirm = {show: jasmine.createSpy('show')};
|
|
|
|
controller.dataChanged = () => {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
controller.state = undefined;
|
|
|
|
let transition = {to: () => {
|
|
|
|
return {name: 'Batman'};
|
|
|
|
}};
|
|
|
|
let result = controller.callback(transition);
|
|
|
|
|
|
|
|
expect(controller.state).toEqual('Batman');
|
2018-05-31 09:52:39 +00:00
|
|
|
expect(controller.$.confirm.show).toHaveBeenCalledWith();
|
2017-10-17 10:27:32 +00:00
|
|
|
expect(result).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe(`when dataChanged() returns false and/or there's a state in the controller`, () => {
|
|
|
|
it(`should return true`, () => {
|
|
|
|
$scope.confirm = {show: jasmine.createSpy('show')};
|
|
|
|
controller.dataChanged = () => {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
controller.state = 'the state';
|
|
|
|
let transition = {to: () => {
|
|
|
|
return {name: 'Batman'};
|
|
|
|
}};
|
|
|
|
let result = controller.callback(transition);
|
|
|
|
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe(`onConfirmResponse()`, () => {
|
2019-10-30 15:57:14 +00:00
|
|
|
describe(`when response is accept`, () => {
|
2020-03-09 08:00:03 +00:00
|
|
|
it(`should reset data them go to state`, () => {
|
|
|
|
let data = {key: 'value'};
|
|
|
|
controller.data = data;
|
|
|
|
data.foo = 'bar';
|
2017-10-17 10:27:32 +00:00
|
|
|
controller.$state = {go: jasmine.createSpy('go')};
|
2020-03-09 08:00:03 +00:00
|
|
|
controller.state = 'foo.bar';
|
|
|
|
controller.onConfirmResponse('accept');
|
2017-10-17 10:27:32 +00:00
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
expect(controller.data).toEqual({key: 'value'});
|
2017-10-17 10:27:32 +00:00
|
|
|
expect(controller.$state.go).toHaveBeenCalledWith(controller.state);
|
|
|
|
});
|
2017-10-16 13:20:40 +00:00
|
|
|
});
|
2017-10-17 10:33:37 +00:00
|
|
|
|
2019-10-30 15:57:14 +00:00
|
|
|
describe(`when response is not accept`, () => {
|
2017-10-17 10:33:37 +00:00
|
|
|
it(`should set controller.state to null`, () => {
|
|
|
|
controller.state = 'Batman';
|
2020-03-09 08:00:03 +00:00
|
|
|
controller.onConfirmResponse('cancel');
|
2017-10-17 10:33:37 +00:00
|
|
|
|
|
|
|
expect(controller.state).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
2017-10-16 13:20:40 +00:00
|
|
|
});
|
2019-09-18 07:41:25 +00:00
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
describe(`reset()`, () => {
|
|
|
|
it(`should reset data as it was before changing it`, () => {
|
|
|
|
let data = {key: 'value'};
|
2019-09-18 07:41:25 +00:00
|
|
|
|
2020-03-09 08:00:03 +00:00
|
|
|
controller.data = data;
|
|
|
|
data.foo = 'bar';
|
|
|
|
controller.reset();
|
|
|
|
|
|
|
|
expect(data).toEqual({key: 'value'});
|
2019-09-18 07:41:25 +00:00
|
|
|
});
|
|
|
|
});
|
2017-10-13 16:26:38 +00:00
|
|
|
});
|