2017-10-05 12:28:57 +00:00
import './dialog.js' ;
describe ( 'Component vnDialog' , ( ) => {
let $componentController ;
let $element ;
beforeEach ( ( ) => {
angular . mock . module ( 'client' ) ;
} ) ;
beforeEach ( angular . mock . inject ( _$componentController _ => {
$componentController = _$componentController _ ;
$element = angular . element ( '<div></div>' ) ;
} ) ) ;
describe ( 'show()' , ( ) => {
it ( ` should define keypressHandler function, call addEventListener function and define element.style.display to block then call onOpen function ` , ( ) => {
window . innerHeight = 600 ;
window . innerWidth = 800 ;
let controller = $componentController ( 'vnDialog' , { $element } , { onOpen : ( ) => { } , dialog : { style : { } , offsetWidth : 780 , offsetHeight : 581 } } ) ;
spyOn ( controller . document , 'addEventListener' ) ;
spyOn ( controller , 'onOpen' ) ;
controller . show ( ) ;
expect ( controller . keypressHandler ) . toBeDefined ( ) ;
expect ( controller . document . addEventListener ) . toHaveBeenCalledWith ( 'keypress' , controller . keypressHandler ) ;
expect ( controller . element . style . display ) . toEqual ( 'block' ) ;
expect ( controller . onOpen ) . toHaveBeenCalledWith ( ) ;
} ) ;
it ( ` should define keypressHandler function, call addEventListener function and define element.style.display to block and never call onOpen function ` , ( ) => {
window . innerHeight = 600 ;
window . innerWidth = 800 ;
let controller = $componentController ( 'vnDialog' , { $element } , { dialog : { style : { } , offsetWidth : 781 , offsetHeight : 581 } } ) ;
spyOn ( controller . document , 'addEventListener' ) ;
controller . show ( ) ;
expect ( controller . keypressHandler ) . toBeDefined ( ) ;
expect ( controller . document . addEventListener ) . toHaveBeenCalledWith ( 'keypress' , controller . keypressHandler ) ;
expect ( controller . element . style . display ) . toEqual ( 'block' ) ;
expect ( controller . onOpen ) . not . toBeDefined ( ) ;
} ) ;
} ) ;
describe ( 'hide()' , ( ) => {
it ( ` should call fireResponse() and realHide() ` , ( ) => {
let controller = $componentController ( 'vnDialog' , { $element } ) ;
spyOn ( controller , 'fireResponse' ) ;
spyOn ( controller , 'realHide' ) ;
controller . hide ( ) ;
expect ( controller . fireResponse ) . toHaveBeenCalledWith ( ) ;
expect ( controller . realHide ) . toHaveBeenCalledWith ( ) ;
} ) ;
} ) ;
describe ( 'fireResponse()' , ( ) => {
it ( ` should return cancel as false ` , ( ) => {
let controller = $componentController ( 'vnDialog' , { $element } ) ;
let result = controller . fireResponse ( 'I am the answer!' ) ;
expect ( controller . onResponse ) . not . toBeDefined ( ) ;
expect ( result ) . toEqual ( false ) ;
} ) ;
it ( ` should return onResponse() ` , ( ) => {
let text = 'I am the answer!' ;
let controller = $componentController ( 'vnDialog' , { $element } , { onResponse : ( ) => {
return { response : text } ;
} } ) ;
let result = controller . fireResponse ( text ) ;
expect ( result . response ) . toEqual ( text ) ;
} ) ;
} ) ;
describe ( 'realHide()' , ( ) => {
it ( ` should set element.style.display and lastEvent properties and call removeEvenListener() ` , ( ) => {
let controller = $componentController ( 'vnDialog' , { $element } ) ;
spyOn ( controller . document , 'removeEventListener' ) ;
expect ( controller . element . style . display ) . not . toEqual ( 'none' ) ;
expect ( controller . lastEvent ) . not . toBeDefined ( ) ;
controller . realHide ( ) ;
expect ( controller . element . style . display ) . toEqual ( 'none' ) ;
expect ( controller . document . removeEventListener ) . toHaveBeenCalledWith ( 'keypress' , controller . keypressHandler ) ;
expect ( controller . lastEvent ) . toEqual ( null ) ;
} ) ;
} ) ;
2017-10-06 13:33:21 +00:00
describe ( 'onButtonClick()' , ( ) => {
it ( ` should call realHide if cancel isn't false ` , ( ) => {
let controller = $componentController ( 'vnDialog' , { $element } ) ;
controller . element = document . createElement ( 'div' ) ;
controller . element . className = 'tpl-buttons' ;
let childElement = document . createElement ( 'div' ) ;
childElement . className = 'button-bar' ;
controller . element . appendChild ( childElement ) ;
let event = { target : controller . element , attribute : true } ;
spyOn ( controller , 'realHide' ) ;
spyOn ( controller , 'fireResponse' ) . and . returnValue ( true ) ;
controller . onButtonClick ( event ) ;
expect ( controller . realHide ) . toHaveBeenCalledWith ( ) ;
} ) ;
it ( ` should call fireResponse with the value of response ` , ( ) => {
let controller = $componentController ( 'vnDialog' , { $element } ) ;
controller . element = document . createElement ( 'div' ) ;
controller . element . className = 'tpl-buttons' ;
let childElement = document . createElement ( 'div' ) ;
childElement . className = 'button-bar' ;
controller . element . appendChild ( childElement ) ;
let attribute = document . createAttribute ( 'response' ) ;
attribute . value = 'I am the response!' ;
controller . element . setAttributeNode ( attribute ) ;
spyOn ( controller , 'fireResponse' ) ;
2017-10-06 13:36:25 +00:00
let event = { target : controller . element } ;
2017-10-06 13:33:21 +00:00
controller . onButtonClick ( event ) ;
expect ( controller . fireResponse ) . toHaveBeenCalledWith ( 'I am the response!' ) ;
} ) ;
} ) ;
2017-10-06 14:30:51 +00:00
describe ( 'onDialogMouseDown()' , ( ) => {
it ( ` should set controller's lastEvent property ` , ( ) => {
let controller = $componentController ( 'vnDialog' , { $element } ) ;
controller . element = document . createElement ( 'div' ) ;
let event = { target : controller . element } ;
controller . onDialogMouseDown ( event ) ;
expect ( controller . lastEvent ) . toEqual ( event ) ;
} ) ;
} ) ;
describe ( 'onBackgroundMouseDown()' , ( ) => {
it ( ` shouldn't call hide() function as event equals lastEvent ` , ( ) => {
let controller = $componentController ( 'vnDialog' , { $element } ) ;
controller . element = document . createElement ( 'div' ) ;
let event = { target : controller . element } ;
controller . lastEvent = event ;
spyOn ( controller , 'hide' ) ;
controller . onBackgroundMouseDown ( event ) ;
expect ( controller . hide ) . not . toHaveBeenCalledWith ( ) ;
} ) ;
it ( ` should call hide() function as event doesn't equal lastEvent ` , ( ) => {
let controller = $componentController ( 'vnDialog' , { $element } ) ;
controller . element = document . createElement ( 'div' ) ;
let event = { target : controller . element } ;
controller . lastEvent = event ;
controller . lastEvent = 'the singularity event!' ;
spyOn ( controller , 'hide' ) ;
controller . onBackgroundMouseDown ( event ) ;
expect ( controller . hide ) . toHaveBeenCalledWith ( ) ;
} ) ;
} ) ;
2017-10-09 12:30:23 +00:00
describe ( 'onKeypress()' , ( ) => {
it ( ` should call hide() if the key pressed equal the code 27 ` , ( ) => {
let controller = $componentController ( 'vnDialog' , { $element } ) ;
controller . element = document . createElement ( 'div' ) ;
let event = { target : controller . element } ;
event . keyCode = 27 ;
spyOn ( controller , 'hide' ) ;
controller . onKeypress ( event ) ;
expect ( controller . hide ) . toHaveBeenCalledWith ( ) ;
} ) ;
it ( ` should't call hide() as the key pressed equal the code 999 ` , ( ) => {
let controller = $componentController ( 'vnDialog' , { $element } ) ;
controller . element = document . createElement ( 'div' ) ;
let event = { target : controller . element } ;
event . keyCode = 999 ;
spyOn ( controller , 'hide' ) ;
controller . onKeypress ( event ) ;
expect ( controller . hide ) . not . toHaveBeenCalledWith ( ) ;
} ) ;
} ) ;
2017-10-05 12:28:57 +00:00
} ) ;