/*______________ | ______ | U I Z E J A V A S C R I P T F R A M E W O R K | / / | --------------------------------------------------- | / O / | MODULE : Uize.Test.Uize.Dom.Classes Class | / / / | | / / / /| | ONLINE : http://uize.com | /____/ /__/_| | COPYRIGHT : (c)2010-2016 UIZE | /___ | LICENSE : Available under MIT License or GNU General Public License |_______________| http://uize.com/license.html */ /* Module Meta Data type: Test importance: 3 codeCompleteness: 100 docCompleteness: 100 */ /*? Introduction The =Uize.Test.Uize.Dom.Classes= module defines a suite of unit tests for the =Uize.Dom.Classes= module. *DEVELOPERS:* `Chris van Rensburg` */ Uize.module ({ name:'Uize.Test.Uize.Dom.Classes', builder:function () { 'use strict'; function _mockNodeWithClassName (_className) { /* NOTE: returns an object designed to fool the Uize.Dom.Basics.isNode method into thinking it's a node, so we can test the classes methods without having to have a browser or a document, allowing this test module to run in Windows Script Host or other non-browser host */ return { className:_className, getAttribute:Uize.nop }; } function _modifyClassesStaticMethodTest (_methodFullName,_cases,_testProperties) { var _hostAndProperty = Uize.Test.splitHostAndProperty (_methodFullName), _methodHostName = _hostAndProperty.host, _methodName = _hostAndProperty.property, _test = [Uize.Test.staticPropertyTest (_methodFullName,'function')] ; function _getCaseTest (_case) { return ( Uize.isArray (_case) ? { title:_case [0], test:function () { var _mockNode = _mockNodeWithClassName (_case [1]), _methodHost = Uize.getModuleByName (_methodHostName), _arguments = _case [2] ; _methodHost [_methodName].apply ( _methodHost, [_mockNode].concat (Uize.isArray (_arguments) ? _arguments : [_arguments]) ); return this.expect (_case [3],_mockNode.className); } } : _case ); } for (var _caseNo = -1, _casesLength = _cases.length; ++_caseNo < _casesLength;) _test.push (_getCaseTest (_cases [_caseNo])) ; return Uize.Test.resolve ( Uize.copyInto ( { title:'STATIC METHOD TEST: ' + _methodFullName, test:_test }, _testProperties ) ); } function _modifyClassesStaticMethodsTest (_staticMethodsTest) { var _test = []; for ( var _staticMethodTestNo = -1, _staticMethodsTestLength = _staticMethodsTest.length; ++_staticMethodTestNo < _staticMethodsTestLength; ) { var _staticMethodTest = _staticMethodsTest [_staticMethodTestNo]; _test.push ( Uize.isArray (_staticMethodTest) ? _modifyClassesStaticMethodTest.apply (this,_staticMethodTest) : _staticMethodTest ); } return Uize.Test.resolve ({title:'Modify Classes Static Method Tests',test:_test}); } return Uize.Test.resolve ({ title:'Test for Uize.Dom.Classes Module', test:[ Uize.Test.requiredModulesTest ('Uize.Dom.Classes'), Uize.Test.staticMethodsTest ([ ['Uize.Dom.Classes.getState',[ /*** test handling of invalid node cases ***/ ['The integer -1 is returned when the node is null', [null,['disabled','enabled']], -1 ], ['The integer -1 is returned when the node is undefined', [undefined,['disabled','enabled']], -1 ], /*** test support for boolean state values ***/ ['The boolean false is returned when the node\'s className string contains the first of two state classes', [_mockNodeWithClassName ('BEFORE disabled AFTER'),['disabled','enabled']], false ], ['The boolean true is returned when the node\'s className string contains the second of two state classes', [_mockNodeWithClassName ('BEFORE enabled AFTER'),['disabled','enabled']], true ], /*** test support for integer state values (i.e. more than two states) ***/ ['The integer 0 is returned when the node\'s className string contains the first of three state classes', [_mockNodeWithClassName ('BEFORE warning AFTER'),['warning','nonFatalError','fatalError']], 0 ], ['The integer 1 is returned when the node\'s className string contains the second of three state classes', [_mockNodeWithClassName ('BEFORE nonFatalError AFTER'),['warning','nonFatalError','fatalError']], 1 ], ['The integer 2 is returned when the node\'s className string contains the third of three state classes', [_mockNodeWithClassName ('BEFORE fatalError AFTER'),['warning','nonFatalError','fatalError']], 2 ], /*** test handling of no state class present ***/ ['The integer -1 is returned when the node\'s className string contains neither of two state classes', [_mockNodeWithClassName ('BEFORE AFTER'),['disabled','enabled']], -1 ], ['The integer -1 is returned when the node\'s className string contains none of three state classes', [_mockNodeWithClassName ('BEFORE AFTER'),['warning','nonFatalError','fatalError']], -1 ], ['Class matching is case sensitive when getting a state from a node\'s className', [_mockNodeWithClassName ('ENABLED'),['disabled','enabled']], -1 ], /*** test handling when class for one of the states is an empty string ***/ ['The boolean false is returned when the first of the state classes is an empty string and the node\'s className string does not contain the second state class', [_mockNodeWithClassName ('populated featured'),['','selected']], false ], ['The boolean true is returned when the second of the state classes is an empty string and the node\'s className string does not contain the first state class', [_mockNodeWithClassName ('populated featured'),['selected','']], true ], /*** test support for state classes being specified using a string ***/ /*** test support for multiple state classes being specified using a comma-separated string ***/ ['The boolean false is returned when the node\'s className string contains the first of two state classes specified using a comma-separated string', [_mockNodeWithClassName ('BEFORE disabled AFTER'),'disabled,enabled'], false ], ['The boolean true is returned when the node\'s className string contains the second of two state classes', [_mockNodeWithClassName ('BEFORE enabled AFTER'),'disabled,enabled'], true ], /*** test handling of a single state class being specified using a string ***/ ['The boolean false is returned when a state class specified using a string isn\'t contained inside the node\'s className string', [_mockNodeWithClassName ('populated featured'),'selected'], false ], ['The boolean true is returned when a state class specified using a string is contained inside the node\'s className string', [_mockNodeWithClassName ('populated selected featured'),'selected'], true ], ['The integer -1 is returned when an empty string is specified for the state class', [_mockNodeWithClassName ('enabled'),''], -1 ] ]], ['Uize.Dom.Classes.hasClass',[ /*** test handling of invalid node cases ***/ ['The boolean false is returned when the node is null', [null,['selected']], false ], ['The boolean false is returned when the node is undefined', [undefined,['selected']], false ], /*** test when specified class is present ***/ ['The boolean true is returned when the specified class is the first class of several classes in the node\'s className string', [_mockNodeWithClassName ('populated selected featured'),['populated']], true ], ['The boolean true is returned when the specified class is the second class of several classes in the node\'s className string', [_mockNodeWithClassName ('populated selected featured'),['selected']], true ], ['The boolean true is returned when the specified class is the last class of several classes in the node\'s className string', [_mockNodeWithClassName ('populated selected featured'),['featured']], true ], /*** test when specified class is absent ***/ ['The value false is returned when the node\'s className string does not contain the specified class', [_mockNodeWithClassName ('populated featured'),['selected']], false ], ['Class matching is case sensitive when testing if a class is present in a node\'s className string', [_mockNodeWithClassName ('populated selected featured'),['SELECTED']], false ], /*** miscellaneous ***/ ['The boolean false is returned when the class that is being tested for is an empty string', [_mockNodeWithClassName ('populated selected featured'),['']], false ] ]] ]), _modifyClassesStaticMethodsTest ([ ['Uize.Dom.Classes.setState',[ ['Setting state for a class that is the empty string has no effect', 'populated selected featured', ['',true], 'populated selected featured' ], /*** test adding a class by setting its state to true ***/ ['Test that setting state for a class to true on a node that has no classes works correctly', '', ['selected',true], 'selected' ], ['Setting state for a class to true on a node that already has that class doesn\'t result in a duplicate', 'populated selected', ['selected',true], 'populated selected' ], ['Setting state for a class to true on a node that already has a different class results in the new class being appended, with a single space as separator', 'populated', ['selected',true], 'populated selected' ], ['Class matching is case sensitive when setting state for a class to true', 'populated selected', ['SELECTED',true], 'populated selected SELECTED' ], ['Setting state for a class to true where that class that is an empty string results in no change', 'populated selected', ['',true], 'populated selected' ], /*** test removing a class by setting its state to false ***/ ['Test that setting state for a class to false on a node that has no classes works correctly', '', ['selected',false], '' ], ['Test that setting state for a class to false where that class is at the beginning of a node\'s className string is handled correctly', 'populated selected featured', ['populated',false], 'selected featured' ], ['Test that setting state for a class to false where that class is in the middle of a node\'s className string is handled correctly', 'populated selected featured', ['selected',false], 'populated featured' ], ['Test that setting state for a class to false where that class at the end of a node\'s className string is handled correctly', 'populated selected featured', ['featured',false], 'populated selected' ], ['Setting state for a class to false where that class isn\'t in a node\'s className string results in no change', 'populated selected featured', ['BLAH',false], 'populated selected featured' ], ['Setting state for a class to false where that class is an empty string results in no change', 'populated selected featured', ['',false], 'populated selected featured' ], ['Setting state for a class to false where that class is the only class in a node\'s className string results in an empty className string', 'populated', ['populated',false], '' ], ['Class matching is case sensitive when setting state for a class to false', 'populated selected featured', ['FEATURED',false], 'populated selected featured' ], /*** test support for two state classes ***/ ['Test that setting state to false where there are two state classes and where neither class is in the node\'s className string is handled correctly', '', [['disabled','enabled'],false], 'disabled' ], ['Test that setting state to false where there are two state classes and where the false state class is in the node\'s className string is handled correctly', 'disabled', [['disabled','enabled'],false], 'disabled' ], ['Test that setting state to false where there are two state classes and where the true state class is in the node\'s className string is handled correctly', 'enabled', [['disabled','enabled'],false], 'disabled' ], ['Test that setting state to true where there are two state classes and where neither class is in the node\'s className string is handled correctly', '', [['disabled','enabled'],true], 'enabled' ], ['Test that setting state to true where there are two state classes and where the false state class is in the node\'s className string is handled correctly', 'disabled', [['disabled','enabled'],true], 'enabled' ], ['Test that setting state to true where there are two state classes and where the true state class is in the node\'s className string is handled correctly', 'enabled', [['disabled','enabled'],true], 'enabled' ], /*** test support for three state classes ***/ /*** setting state to 0 ***/ ['Test that setting state to 0 where there are three state classes and where none of the classes are in the node\'s className string is handled correctly', '', [['state0Class','state1Class','state2Class'],0], 'state0Class' ], ['Test that setting state to 0 where there are three state classes and where the 0 state class is in the node\'s className string is handled correctly', 'state0Class', [['state0Class','state1Class','state2Class'],0], 'state0Class' ], ['Test that setting state to 0 where there are three state classes and where the 1 state class is in the node\'s className string is handled correctly', 'state1Class', [['state0Class','state1Class','state2Class'],0], 'state0Class' ], ['Test that setting state to 0 where there are three state classes and where the 2 state class is in the node\'s className string is handled correctly', 'state2Class', [['state0Class','state1Class','state2Class'],0], 'state0Class' ], /*** setting state to 1 ***/ ['Test that setting state to 1 where there are three state classes and where none of the classes are in the node\'s className string is handled correctly', '', [['state0Class','state1Class','state2Class'],1], 'state1Class' ], ['Test that setting state to 1 where there are three state classes and where the 0 state class is in the node\'s className string is handled correctly', 'state0Class', [['state0Class','state1Class','state2Class'],1], 'state1Class' ], ['Test that setting state to 1 where there are three state classes and where the 1 state class is in the node\'s className string is handled correctly', 'state1Class', [['state0Class','state1Class','state2Class'],1], 'state1Class' ], ['Test that setting state to 1 where there are three state classes and where the 2 state class is in the node\'s className string is handled correctly', 'state2Class', [['state0Class','state1Class','state2Class'],1], 'state1Class' ], /*** setting state to 2 ***/ ['Test that setting state to 2 where there are three state classes and where none of the classes are in the node\'s className string is handled correctly', '', [['state0Class','state1Class','state2Class'],2], 'state2Class' ], ['Test that setting state to 2 where there are three state classes and where the 0 state class is in the node\'s className string is handled correctly', 'state0Class', [['state0Class','state1Class','state2Class'],2], 'state2Class' ], ['Test that setting state to 2 where there are three state classes and where the 1 state class is in the node\'s className string is handled correctly', 'state1Class', [['state0Class','state1Class','state2Class'],2], 'state2Class' ], ['Test that setting state to 2 where there are three state classes and where the 2 state class is in the node\'s className string is handled correctly', 'state2Class', [['state0Class','state1Class','state2Class'],2], 'state2Class' ], /*** test support for more than one state class being an empty string ***/ /*** test setting state to a state whose state class is a non-empty string, where all other state classes are empty strings ***/ ['Test that setting state to 0 where there are four state classes, where all are empty strings except for the 0 state class, and where the 0 state class is *not* in the node\'s className string is handled correctly', 'ENABLED', [['stateClass','','',''],0], 'ENABLED stateClass' ], ['Test that setting state to 1 where there are four state classes, where all are empty strings except for the 1 state class, and where the 1 state class is *not* in the node\'s className string is handled correctly', 'ENABLED', [['','stateClass','',''],1], 'ENABLED stateClass' ], ['Test that setting state to 2 where there are four state classes, where all are empty strings except for the 2 state class, and where the 2 state class is *not* in the node\'s className string is handled correctly', 'ENABLED', [['','','stateClass',''],2], 'ENABLED stateClass' ], ['Test that setting state to 3 where there are four state classes, where all are empty strings except for the 3 state class, and where the 3 state class is *not* in the node\'s className string is handled correctly', 'ENABLED', [['','','','stateClass'],3], 'ENABLED stateClass' ], /*** test setting state to a state whose state class is an empty string, and where there are three other state classes, two of which are empty strings, and one of which is a non-empty string that is in the node's className string ***/ ['Test that setting state to 0 where there are four state classes, where all are empty strings except for one state class which is in the node\'s className string, and where the 0 state class is an empty string, is handled correctly', 'BEFORE stateClass AFTER', [['','','','stateClass'],0], 'BEFORE AFTER' ], ['Test that setting state to 1 where there are four state classes, where all are empty strings except for one state class which is in the node\'s className string, and where the 1 state class is an empty string, is handled correctly', 'BEFORE stateClass AFTER', [['','','','stateClass'],1], 'BEFORE AFTER' ], ['Test that setting state to 2 where there are four state classes, where all are empty strings except for one state class which is in the node\'s className string, and where the 2 state class is an empty string, is handled correctly', 'BEFORE stateClass AFTER', [['','','','stateClass'],2], 'BEFORE AFTER' ], ['Test that setting state to 3 where there are four state classes, where all are empty strings except for one state class which is in the node\'s className string, and where the 3 state class is an empty string, is handled correctly', 'BEFORE stateClass AFTER', [['stateClass','','',''],3], 'BEFORE AFTER' ] ]], ['Uize.Dom.Classes.removeState',[ /*** test support for first state class being empty string ***/ ['Removing a state when none of the state classes is present in the node\'s className string results in no change', 'BEFORE AFTER', 'selected', 'BEFORE AFTER' ], ['Test that removing a state that is specified using a string with only one state class (the first class is implicit as the empty string) is handled correctly', 'BEFORE selected AFTER', 'selected', 'BEFORE AFTER' ], /*** test support for two state classes ***/ ['Removing a state where there are two non-empty state classes and where none of those classes is in the node\'s className string results in no change', 'BEFORE AFTER', [['disabled','enabled']], 'BEFORE AFTER' ], ['Removing a state where there are two non-empty state classes and where the first of those classes is in the node\'s className string results in it being removed', 'BEFORE disabled AFTER', [['disabled','enabled']], 'BEFORE AFTER' ], ['Removing a state where there are two non-empty state classes and where the second of those classes is in the node\'s className string results in it being removed', 'BEFORE enabled AFTER', [['disabled','enabled']], 'BEFORE AFTER' ], /*** test support for three state classes ***/ ['Removing a state where there are three non-empty state classes and where none of those classes is in the node\'s className string results in no change', 'BEFORE AFTER', [['state0Class','state1Class','state2Class']], 'BEFORE AFTER' ], ['Removing a state where there are three non-empty state classes and where the first of those classes is in the node\'s className string results in it being removed', 'BEFORE state0Class AFTER', [['state0Class','state1Class','state2Class']], 'BEFORE AFTER' ], ['Removing a state where there are three non-empty state classes and where the second of those classes is in the node\'s className string results in it being removed', 'BEFORE state1Class AFTER', [['state0Class','state1Class','state2Class']], 'BEFORE AFTER' ], ['Removing a state where there are three non-empty state classes and where the third of those classes is in the node\'s className string results in it being removed', 'BEFORE state2Class AFTER', [['state0Class','state1Class','state2Class']], 'BEFORE AFTER' ], /*** miscellaneous ***/ ['Class matching is case sensitive when removing a state', 'BEFORE selected AFTER', ['SELECTED'], 'BEFORE selected AFTER' ] ]], ['Uize.Dom.Classes.toggleState',[ ['Toggling state for a class that is the empty string has no effect', 'populated selected featured', [''], 'populated selected featured' ], /*** test support for state classes being specified as a string ***/ ['Test that toggling state where state classes is specified using a string and where the node\'s className string is empty is handled correctly', '', ['featured'], 'featured' ], ['Test that toggling state where state classes is specified using a string and where the node\'s className string is the true state class is handled correctly', 'featured', ['featured'], '' ], ['Toggling state where state classes is specified using a string, where the node\'s className string has classes but not the true state class, results in the true state class being appended', 'populated selected', ['featured'], 'populated selected featured' ], ['Test that toggling state where state classes is specified using a string, and where the true state class is at the beginning of the node\'s className string results in it being removed correctly', 'populated selected featured', ['populated'], 'selected featured' ], ['Test that toggling state where state classes is specified using a string, and where the true state class is in the middle of the node\'s className string results in it being removed correctly', 'populated selected featured', ['selected'], 'populated featured' ], ['Test that toggling state where state classes is specified using a string, and where the true state class is at the end of the node\'s className string results in it being removed correctly', 'populated selected featured', ['featured'], 'populated selected' ], /*** test support for two state classes ***/ ['Toggling state where there are two states, and where none of the state classes is in a node\'s className string results in the first state class being appended, with a single space as separator', 'BEFORE AFTER', [['disabled','enabled']], 'BEFORE AFTER disabled' ], ['Toggling state where there are two states, and where the first state class is in a node\'s className string results in it being replaced with the second state class', 'BEFORE disabled AFTER', [['disabled','enabled']], 'BEFORE enabled AFTER' ], ['Toggling state where there are two states, and where the second state class is in a node\'s className string results in it being replaced with the first state class', 'BEFORE enabled AFTER', [['disabled','enabled']], 'BEFORE disabled AFTER' ], /*** test support for three state classes ***/ ['Toggling state where there are three states, and where none of the state classes is in a node\'s className string results in the first state class being appended, with a single space as separator', 'BEFORE AFTER', [['state0Class','state1Class','state2Class']], 'BEFORE AFTER state0Class' ], ['Toggling state where there are three states, and where the first state class is in a node\'s className string results in it being replaced with the second state class', 'BEFORE state0Class AFTER', [['state0Class','state1Class','state2Class']], 'BEFORE state1Class AFTER' ], ['Toggling state where there are three states, and where the second state class is in a node\'s className string results in it being replaced with the third state class', 'BEFORE state1Class AFTER', [['state0Class','state1Class','state2Class']], 'BEFORE state2Class AFTER' ], ['That toggling state where there are three states, and where the third state class is in a node\'s className string results in it being replaced with the first state class', 'BEFORE state2Class AFTER', [['state0Class','state1Class','state2Class']], 'BEFORE state0Class AFTER' ] ]], ['Uize.Dom.Classes.addClass',[ ['Test that adding a class to a node that has no classes works correctly', '', 'selected', 'selected' ], ['Adding a class to a node that already has that class doesn\'t result in a duplicate', 'populated selected', 'selected', 'populated selected' ], ['Adding a class to a node that already has a different class results in the new class being appended, with a single space as separator', 'populated', 'selected', 'populated selected' ], ['Class matching is case sensitive when adding a class', 'populated selected', 'SELECTED', 'populated selected SELECTED' ], ['Adding a class that is an empty string results in no change', 'populated selected', '', 'populated selected' ] ]], ['Uize.Dom.Classes.removeClass',[ ['Test that removing a class from a node that has no classes works correctly', '', 'selected', '' ], ['Test that removing a class that is at the beginning of a node\'s className string is handled correctly', 'populated selected featured', 'populated', 'selected featured' ], ['Test that removing a class that is in the middle of a node\'s className string is handled correctly', 'populated selected featured', 'selected', 'populated featured' ], ['Test that removing a class that is at the end of a node\'s className string is handled correctly', 'populated selected featured', 'featured', 'populated selected' ], ['Removing a class that isn\'t in a node\'s className string results in no change', 'populated selected featured', 'BLAH', 'populated selected featured' ], ['Removing a class that is an empty string results in no change', 'populated selected featured', '', 'populated selected featured' ], ['Removing the only class in a node\'s className string results in an empty className string', 'populated', 'populated', '' ], ['Class matching is case sensitive when removing a class', 'populated selected featured', 'FEATURED', 'populated selected featured' ] ]], ['Uize.Dom.Classes.toggleClass',[ /*** test when specified class is present ***/ ['Test that toggling a class that is at the beginning of a node\'s className string is handled correctly', 'populated selected featured', 'populated', 'selected featured' ], ['Test that toggling a class that is in the middle of a node\'s className string is handled correctly', 'populated selected featured', 'selected', 'populated featured' ], ['Test that toggling a class that is at the end of a node\'s className string is handled correctly', 'populated selected featured', 'featured', 'populated selected' ], /*** test when specified class is absent ***/ ['Test that toggling a class for a node that has no classes works correctly', '', 'selected', 'selected' ], ['Toggling a class that isn\'t present for a node that has different classes in its className string results in the toggle class being appended, with a single space as separator', 'populated selected', 'featured', 'populated selected featured' ], /*** miscellaneous ***/ ['Class matching is case sensitive when toggling a class', 'populated selected featured', 'FEATURED', 'populated selected featured FEATURED' ], ['Toggling a class that is an empty string results in no change', 'populated selected featured', '', 'populated selected featured' ] ]] ]) ] }); } });