2017-10-13 16:26:38 +00:00
import './watcher.js' ;
2018-02-10 15:18:01 +00:00
import getModifiedData from '../../lib/modified' ;
2017-10-13 16:26:38 +00:00
describe ( 'Component vnWatcher' , ( ) => {
let $componentController ;
let $scope ;
let $element ;
let $state ;
let $transitions ;
let $httpBackend ;
let vnApp ;
let $translate ;
2017-10-17 12:24:40 +00:00
let controller ;
2018-01-09 12:49:15 +00:00
let $attrs ;
2017-10-13 16:26:38 +00:00
beforeEach ( ( ) => {
angular . mock . module ( 'client' ) ;
} ) ;
beforeEach ( angular . mock . inject ( ( _$componentController _ , $rootScope , _$state _ , _$transitions _ , _$httpBackend _ , _vnApp _ , _$translate _ ) => {
$componentController = _$componentController _ ;
$scope = $rootScope . $new ( ) ;
$element = angular . element ( '<div></div>' ) ;
$state = _$state _ ;
vnApp = _vnApp _ ;
$transitions = _$transitions _ ;
$httpBackend = _$httpBackend _ ;
2018-01-30 13:48:21 +00:00
$httpBackend . when ( 'GET' , /\/locale\/\w+\/[a-z]{2}\.json/ ) . respond ( { } ) ;
2017-10-13 16:26:38 +00:00
$translate = _$translate _ ;
2018-01-09 12:49:15 +00:00
$attrs = {
save : "patch"
} ;
controller = $componentController ( 'vnWatcher' , { $scope , $element , $state , vnApp , $transitions , $httpBackend , $translate , $attrs } ) ;
2017-10-13 16:26:38 +00:00
} ) ) ;
2017-10-16 13:20:40 +00:00
describe ( '$onInit()' , ( ) => {
2017-10-13 16:26:38 +00:00
it ( ` should call fetchData() if controllers get and url properties are defined ` , ( ) => {
controller . get = ( ) => { } ;
controller . url = 'test.com' ;
spyOn ( controller , 'fetchData' ) ;
controller . $onInit ( ) ;
expect ( controller . fetchData ) . toHaveBeenCalledWith ( ) ;
} ) ;
it ( ` should throw an error if $ onInit is called without url defined ` , ( ) => {
controller . get = ( ) => { } ;
expect ( function ( ) {
controller . $onInit ( ) ;
} ) . toThrow ( new Error ( 'Error: Parameter url ommitted' ) ) ;
} ) ;
} ) ;
2017-10-16 13:20:40 +00:00
describe ( '$onChanges()' , ( ) => {
2017-10-13 16:26:38 +00:00
it ( ` should call updateOriginalData() if controllers data is defined ` , ( ) => {
controller . data = [ ] ;
spyOn ( controller , 'updateOriginalData' ) ;
controller . $onChanges ( ) ;
expect ( controller . updateOriginalData ) . toHaveBeenCalledWith ( ) ;
} ) ;
} ) ;
2017-10-16 13:20:40 +00:00
describe ( '$onDestroy()' , ( ) => {
it ( ` should call deregisterCallback() ` , ( ) => {
2017-10-13 16:26:38 +00:00
spyOn ( controller , 'deregisterCallback' ) ;
controller . $onDestroy ( ) ;
expect ( controller . deregisterCallback ) . toHaveBeenCalledWith ( ) ;
} ) ;
} ) ;
2017-10-16 13:20:40 +00:00
describe ( 'fetchData()' , ( ) => {
it ( ` should perform a query then store the received data into controller.data and call updateOriginalData() ` , ( ) => {
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 => {
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 => {
spyOn ( controller , 'submit' ) . and . returnValue ( Promise . resolve ( ) ) ;
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 )
. then ( ( ) => {
expect ( controller . submit ) . toHaveBeenCalledWith ( ) ;
2017-12-04 07:17:29 +00:00
expect ( controller . $state . go ) . toHaveBeenCalledWith ( state , { } ) ;
2017-10-16 13:20:40 +00:00
done ( ) ;
} ) ;
} ) ;
} ) ;
describe ( 'submit()' , ( ) => {
describe ( 'when controller.form' , ( ) => {
it ( ` should call controller.form.setSubminted if controller.form is defined ` , ( ) => {
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 ` , ( ) => {
controller . form = { $setSubmitted : ( ) => { } } ;
spyOn ( controller , 'invalidForm' ) ;
controller . submit ( ) ;
expect ( controller . invalidForm ) . toHaveBeenCalledWith ( jasmine . any ( Function ) ) ;
} ) ;
} ) ;
describe ( 'when !controller.dataChanged()' , ( ) => {
it ( ` should call controller.noChanges() ` , ( ) => {
spyOn ( controller , 'noChanges' ) ;
controller . submit ( ) ;
expect ( controller . noChanges ) . toHaveBeenCalledWith ( jasmine . any ( Function ) ) ;
} ) ;
} ) ;
describe ( 'when controller.save()' , ( ) => {
it ( ` should set controller.save.model property ` , ( ) => {
2018-02-01 12:28:45 +00:00
controller . save = { accept : ( ) => { } } ;
2017-10-16 13:20:40 +00:00
controller . data = { originalInfo : 'original data' , info : 'new data' } ;
controller . orgData = { originalInfo : 'original data' } ;
controller . submit ( ) ;
expect ( controller . save . model ) . toEqual ( { info : 'new data' } ) ;
} ) ;
2017-10-17 10:27:32 +00:00
it ( ` should call controller.save.accept() then controller.writeData ` , done => {
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() ` , ( ) => {
2017-10-17 12:24:40 +00:00
controller . dataChanged = ( ) => {
2017-10-17 10:27:32 +00:00
return true ;
2017-10-17 12:24:40 +00:00
} ;
2017-10-17 10:27:32 +00:00
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() ` , ( ) => {
2017-10-17 12:24:40 +00:00
controller . dataChanged = ( ) => {
2017-10-17 10:27:32 +00:00
return true ;
2017-10-17 12:24:40 +00:00
} ;
2017-10-17 10:27:32 +00:00
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 ` , ( ) => {
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 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 ` , ( ) => {
$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 ` , ( ) => {
$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 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 ) ;
} ) ;
2017-10-16 13:20:40 +00:00
} ) ;
2017-10-17 10:33:37 +00:00
describe ( ` when response is not ACCEPT ` , ( ) => {
it ( ` should set controller.state to null ` , ( ) => {
let response = 'anything but ACCEPT' ;
controller . state = 'Batman' ;
controller . onConfirmResponse ( response ) ;
expect ( controller . state ) . toBeFalsy ( ) ;
} ) ;
} ) ;
2017-10-16 13:20:40 +00:00
} ) ;
2017-10-13 16:26:38 +00:00
} ) ;