finished client side unit test for watcher
This commit is contained in:
parent
22ccf278a4
commit
56182fb4b1
|
@ -1,4 +1,5 @@
|
||||||
import './watcher.js';
|
import './watcher.js';
|
||||||
|
import getModifiedData from '../lib/modified';
|
||||||
|
|
||||||
describe('Component vnWatcher', () => {
|
describe('Component vnWatcher', () => {
|
||||||
let $componentController;
|
let $componentController;
|
||||||
|
@ -155,6 +156,142 @@ describe('Component vnWatcher', () => {
|
||||||
|
|
||||||
expect(controller.save.model).toEqual({info: 'new data'});
|
expect(controller.save.model).toEqual({info: 'new data'});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it(`should call controller.save.accept() then controller.writeData`, done => {
|
||||||
|
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
|
||||||
|
controller.save = {accept: () => {}};
|
||||||
|
controller.data = {originalInfo: 'original data', info: 'new data'};
|
||||||
|
controller.orgData = {originalInfo: 'original data'};
|
||||||
|
spyOn(controller.save, 'accept').and.returnValue(Promise.resolve());
|
||||||
|
spyOn(controller, 'writeData').and.callThrough();
|
||||||
|
controller.submit()
|
||||||
|
.then(() => {
|
||||||
|
expect(controller.save.accept).toHaveBeenCalledWith();
|
||||||
|
expect(controller.writeData).toHaveBeenCalledWith(jasmine.any(Object), jasmine.any(Function));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when id is defined', () => {
|
||||||
|
it(`should perform a query then call controller.writeData()`, () => {
|
||||||
|
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}, {dataChanged: () => {
|
||||||
|
return true;
|
||||||
|
}});
|
||||||
|
controller.data = {id: 2};
|
||||||
|
controller.orgData = {id: 1};
|
||||||
|
let changedData = getModifiedData(controller.data, controller.orgData);
|
||||||
|
controller.idField = 'id';
|
||||||
|
controller.url = 'test.com';
|
||||||
|
let json = {data: 'some data'};
|
||||||
|
spyOn(controller, 'writeData').and.callThrough();
|
||||||
|
$httpBackend.whenPATCH(`${controller.url}/1`, changedData).respond(json);
|
||||||
|
$httpBackend.expectPATCH(`${controller.url}/1`);
|
||||||
|
controller.submit()
|
||||||
|
.then(() => {
|
||||||
|
expect(controller.writeData).toHaveBeenCalledWith(jasmine.any(Object), jasmine.any(Function));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
$httpBackend.flush();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should perform a POST query then call controller.writeData()`, () => {
|
||||||
|
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}, {dataChanged: () => {
|
||||||
|
return true;
|
||||||
|
}});
|
||||||
|
controller.data = {id: 2};
|
||||||
|
controller.orgData = {id: 1};
|
||||||
|
controller.url = 'test.com';
|
||||||
|
let json = {data: 'some data'};
|
||||||
|
spyOn(controller, 'writeData').and.callThrough();
|
||||||
|
$httpBackend.whenPOST(`${controller.url}`, controller.data).respond(json);
|
||||||
|
$httpBackend.expectPOST(`${controller.url}`, controller.data);
|
||||||
|
controller.submit()
|
||||||
|
.then(() => {
|
||||||
|
expect(controller.writeData).toHaveBeenCalledWith(jasmine.any(Object), jasmine.any(Function));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
$httpBackend.flush();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('writeData()', () => {
|
||||||
|
it(`should call Object.asssign() function over controllers.data with json.data, then call updateOriginalData function and finally call resolve() function`, () => {
|
||||||
|
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
|
||||||
|
spyOn(controller, 'updateOriginalData');
|
||||||
|
controller.data = {};
|
||||||
|
let json = {data: 'some data'};
|
||||||
|
let resolve = jasmine.createSpy('resolve');
|
||||||
|
controller.writeData(json, resolve);
|
||||||
|
|
||||||
|
expect(controller.updateOriginalData).toHaveBeenCalledWith();
|
||||||
|
expect(resolve).toHaveBeenCalledWith(json);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('copyInNewObject()', () => {
|
||||||
|
it(`should return newCopy object if data was an object`, () => {
|
||||||
|
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
|
||||||
|
let data = {id: 1, Heroname: 'Batman', name: 'Bruce Wayne'};
|
||||||
|
let result = controller.copyInNewObject(data);
|
||||||
|
|
||||||
|
expect(result).toEqual(data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('callback()', () => {
|
||||||
|
describe(`when dataChanged() returns true and there's no state in the controller`, () => {
|
||||||
|
it(`should define controller.state, call controller.$scope.confirm.show() and return false`, () => {
|
||||||
|
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
|
||||||
|
$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');
|
||||||
|
expect(controller.$scope.confirm.show).toHaveBeenCalledWith();
|
||||||
|
expect(result).toBeFalsy();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe(`when dataChanged() returns false and/or there's a state in the controller`, () => {
|
||||||
|
it(`should return true`, () => {
|
||||||
|
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
|
||||||
|
$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()`, () => {
|
||||||
|
describe(`when response is ACCEPT`, () => {
|
||||||
|
it(`should call Object.assing on controlle.data with controller.orgData then call go() on state`, () => {
|
||||||
|
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
|
||||||
|
let response = 'ACCEPT';
|
||||||
|
controller.data = {};
|
||||||
|
controller.orgData = {name: 'Batman'};
|
||||||
|
controller.$state = {go: jasmine.createSpy('go')};
|
||||||
|
controller.state = 'Batman';
|
||||||
|
controller.onConfirmResponse(response);
|
||||||
|
|
||||||
|
expect(controller.data).toEqual(controller.orgData);
|
||||||
|
expect(controller.$state.go).toHaveBeenCalledWith(controller.state);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue