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 $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 ;
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 _ ) => {
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-02-26 12:22:52 +00:00
it ( 'should call fetchData() if controllers get and url properties are defined' , ( ) => {
2017-10-13 16:26:38 +00:00
controller . get = ( ) => { } ;
controller . url = 'test.com' ;
2020-02-27 06:19:42 +00:00
jest . spyOn ( controller , 'fetchData' ) . mockReturnThis ( ) ;
2017-10-13 16:26:38 +00:00
controller . $onInit ( ) ;
expect ( controller . fetchData ) . toHaveBeenCalledWith ( ) ;
} ) ;
it ( ` should throw an error if $ onInit is called without url defined ` , ( ) => {
controller . get = ( ) => { } ;
expect ( function ( ) {
controller . $onInit ( ) ;
2018-05-31 09:52:39 +00:00
} ) . toThrowError ( /parameter/ ) ;
2017-10-13 16:26:38 +00:00
} ) ;
} ) ;
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() ` , ( ) => {
2020-02-26 12:22:52 +00:00
jest . spyOn ( controller , 'updateOriginalData' ) ;
2017-10-16 13:20:40 +00:00
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()' , ( ) => {
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
it ( ` should throw error if controller.dirty is true ` , ( ) => {
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
} ) ;
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
} ) ;
describe ( 'when id is defined' , ( ) => {
2018-05-25 15:25:35 +00:00
it ( ` should perform a query then call controller.writeData() ` , done => {
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' } ;
2020-02-26 12:22:52 +00:00
jest . spyOn ( controller , 'writeData' ) ;
2017-10-17 10:27:32 +00:00
$httpBackend . whenPATCH ( ` ${ controller . url } /1 ` , changedData ) . respond ( json ) ;
$httpBackend . expectPATCH ( ` ${ controller . url } /1 ` ) ;
2018-05-25 15:25:35 +00:00
controller . realSubmit ( )
2018-12-22 10:59:26 +00:00
. then ( ( ) => {
expect ( controller . writeData ) . toHaveBeenCalledWith ( jasmine . any ( Object ) , jasmine . any ( Function ) ) ;
done ( ) ;
} ) . catch ( done . fail ) ;
2017-10-17 10:27:32 +00:00
$httpBackend . flush ( ) ;
} ) ;
} ) ;
2018-05-25 15:25:35 +00:00
it ( ` should perform a POST query then call controller.writeData() ` , done => {
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' } ;
2020-02-26 12:22:52 +00:00
jest . spyOn ( controller , 'writeData' ) ;
2017-10-17 10:27:32 +00:00
$httpBackend . whenPOST ( ` ${ controller . url } ` , controller . data ) . respond ( json ) ;
$httpBackend . expectPOST ( ` ${ controller . url } ` , controller . data ) ;
2018-05-25 15:25:35 +00:00
controller . realSubmit ( )
2018-12-22 10:59:26 +00:00
. then ( ( ) => {
expect ( controller . writeData ) . toHaveBeenCalledWith ( jasmine . any ( Object ) , jasmine . any ( Function ) ) ;
done ( ) ;
} ) . catch ( done . fail ) ;
2017-10-17 10:27:32 +00:00
$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 ` , ( ) => {
2020-02-26 12:22:52 +00:00
jest . spyOn ( controller , 'updateOriginalData' ) ;
2017-10-17 10:27:32 +00:00
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 ( '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 ` , ( ) => {
2017-10-17 10:27:32 +00:00
it ( ` should call Object.assing on controlle.data with controller.orgData then call go() on state ` , ( ) => {
2019-10-30 15:57:14 +00:00
let response = 'accept' ;
2017-10-17 10:27:32 +00:00
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
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 ` , ( ) => {
2019-10-30 15:57:14 +00:00
let response = 'anything but accept' ;
2017-10-17 10:33:37 +00:00
controller . state = 'Batman' ;
controller . onConfirmResponse ( response ) ;
expect ( controller . state ) . toBeFalsy ( ) ;
} ) ;
} ) ;
2017-10-16 13:20:40 +00:00
} ) ;
2019-09-18 07:41:25 +00:00
describe ( ` loadOriginalData() ` , ( ) => {
it ( ` should iterate over the current data object, delete all properties then assign the ones from original data ` , ( ) => {
controller . data = { name : 'Bruce' } ;
controller . orgData = { name : 'Batman' } ;
controller . loadOriginalData ( ) ;
expect ( controller . data ) . toEqual ( controller . orgData ) ;
} ) ;
} ) ;
2017-10-13 16:26:38 +00:00
} ) ;