delegate.html revision 379e0306e00b6f0add52f96907da048c03a9f2a1
3215b14029b163463e7225627b6781e4a049f3d8Caridy<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots <div id="mod-header" class="hd"><h3 class="title">H3 - Title</h3></div>
5889c2819167de6bb3c1d1a1e3a5e6af060e8a95Adam Moore <p>simple paragraph with a link <a href="#"id="firstlink">simple link</a></p>
5889c2819167de6bb3c1d1a1e3a5e6af060e8a95Adam Moore <p>another paragraph with a complex link <a href="#" id="secondlink"><strong>strong within link</strong><img alt="fake image" id="fakeimage" /> - complex <span id="spanwithinlink">link</span></a></p>
3215b14029b163463e7225627b6781e4a049f3d8Caridy <!-- YUI3 Core //-->
329980a34cfc56e4b608da4d695b771cc1000250Caridy <script type="text/javascript" src="/build/yui/yui-debug.js"></script>
3215b14029b163463e7225627b6781e4a049f3d8Caridy <!-- Initialization process //-->
3215b14029b163463e7225627b6781e4a049f3d8Caridy filter: 'debug',
3215b14029b163463e7225627b6781e4a049f3d8Caridy combine: false,
3215b14029b163463e7225627b6781e4a049f3d8Caridy useConsole: true,
3215b14029b163463e7225627b6781e4a049f3d8Caridy logExclude: {Dom: true, Selector: true, Node: true, attribute: true, base: true, event: true, widget: true}
3215b14029b163463e7225627b6781e4a049f3d8Caridy }).use('console', 'test', 'event', 'event-simulate', function (Y) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy // creating the console...
0771d781138a507b3e657573703f511291640bf3Adam Moore // (new Y.Console()).render();
3215b14029b163463e7225627b6781e4a049f3d8Caridy // starting the testing process
3215b14029b163463e7225627b6781e4a049f3d8Caridy // add the test cases and suites
3215b14029b163463e7225627b6781e4a049f3d8Caridy name: "Event Delegate Tests",
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_simple_delegate_for_anchors: function(){
abf248eeaac94371535d800391168fff8d32bb44Adam Moore var foo = false, t, ct, boundEl;
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click', 'a');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Event.simulate (document.getElementById('firstlink'), 'click');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.isTrue(foo, "simple delegation fails, mod1 should pickup the event and test target [firstlink]");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore // Y.Assert.areEqual(t, Y.get('#firstlink'), "event delegate works but the target is an incorrect node, should be the matching node");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(ct, Y.get('#firstlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(t, Y.get('#firstlink'), "event delegate works but the target is an incorrect node, should be the actual click target");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(boundEl, Y.get('#mod1'), "event delegate works but the container property should be the bound element");
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots test_multiple_selectors: function () {
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots var foo = false, t, ct, boundEl;
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.on('delegate', function(e) {
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots }, '#mod1', 'click', '.hd,.bd');
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.Event.simulate(document.getElementById('mod-header'), 'click');
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.Assert.areEqual(ct, Y.get('#mod-header'), "event delegate works but the matched element is an incorrect node, should be the matching node");
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.Event.simulate(document.getElementById('mod-body'), 'click');
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.Assert.areEqual(ct, Y.get('#mod-body'), "event delegate works but the matched element is an incorrect node, should be the matching node");
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots test_document_as_container: function () {
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots var foo = false, t, ct, boundEl;
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.on('delegate', function(e) {
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots }, Y.get('#mod1').get('ownerDocument'), 'click', 'a');
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.Event.simulate (document.getElementById('firstlink'), 'click');
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.Assert.isTrue(foo, "simple delegation fails, mod1 should pickup the event and test target [firstlink]");
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.Assert.areEqual(ct, Y.get('#firstlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.Assert.areEqual(t, Y.get('#firstlink'), "event delegate works but the target is an incorrect node, should be the actual click target");
379e0306e00b6f0add52f96907da048c03a9f2a1Todd Kloots Y.Assert.areEqual(boundEl, Y.get('#mod1').get('ownerDocument'), "event delegate works but the container property should be the bound element");
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_checking_delegation_target: function(){
abf248eeaac94371535d800391168fff8d32bb44Adam Moore var foo = false, t, ct, boundEl;
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click', 'a');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Event.simulate (document.getElementById('fakeimage'), 'click');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.isTrue(foo, "delegation fails for an image within an anchor, mod1 should pickup the event and test target [secondlink]");
3215b14029b163463e7225627b6781e4a049f3d8Caridy // in this case, the target should be the anchor, and not the image
abf248eeaac94371535d800391168fff8d32bb44Adam Moore // Y.Assert.areEqual(t, Y.get('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching node");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore // this has been changed. the target is unchanged, the anchor is
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(ct, Y.get('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(t, Y.get('#fakeimage'), "event delegate works but the target is an incorrect node, should be the actual click target");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(boundEl, Y.get('#mod1'), "event delegate works but the container property should be the bound element");
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_including_container_in_selector: function(){
3215b14029b163463e7225627b6781e4a049f3d8Caridy var foo = false, t;
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click', '#mod1 a');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Event.simulate (document.getElementById('firstlink'), 'click');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.isFalse(foo, "delegation fails, the container (specified in the on) can not be part of the selectors");
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_targeting_container_without_selectors: function(){
3215b14029b163463e7225627b6781e4a049f3d8Caridy var foo = false, t;
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Event.simulate (document.getElementById('firstlink'), 'click');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.isFalse(foo, "delegation fails, delegation without at least one selector should never trigger an event");
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_multiple_selectors_one_match: function(){
3215b14029b163463e7225627b6781e4a049f3d8Caridy var foo = false, t;
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo = true;
329980a34cfc56e4b608da4d695b771cc1000250Caridy }, '#mod1', 'click', 'a,a span');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Event.simulate (document.getElementById('firstlink'), 'click');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.isTrue(foo, "multiple selectors fails, delegate should be able to match different selectors");
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.areEqual(t, Y.get('#firstlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_multiple_delegate_matches: function(){
abf248eeaac94371535d800391168fff8d32bb44Adam Moore var foo1 = false, foo2 = false, t1, t2, ct1, ct2;
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo1 = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click', 'a');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo2 = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click', 'a span');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Event.simulate (document.getElementById('spanwithinlink'), 'click');
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.isTrue(foo1, "first match fail, delegate should be able to match [a]");
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.Assert.isTrue(foo2, "second match fail, delegate should be able to match [a span]");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(ct1, Y.get('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching selector");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(t1, Y.get('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the clicked node");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(ct2, Y.get('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(t2, Y.get('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the clicked");
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy test_bubble_up_after_delegate: function(){
abf248eeaac94371535d800391168fff8d32bb44Adam Moore var foo1 = false, foo2 = false, t1, t2, ct;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.on('delegate', function(e) {
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy foo1 = true;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy }, '#mod1', 'click', 'a');
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.on('click', function(e) {
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy foo2 = true;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy }, '#doc');
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.Event.simulate (document.getElementById('spanwithinlink'), 'click');
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.Assert.isTrue(foo1, "first match fail, delegate should be able to match [a]");
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.Assert.isTrue(foo2, "second match fail, the event doesn't bubble up after the delegate routine");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore // Y.Assert.areEqual(t1, Y.get('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(ct, Y.get('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching selector");
abf248eeaac94371535d800391168fff8d32bb44Adam Moore Y.Assert.areEqual(t1, Y.get('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the actual target");
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.Assert.areEqual(t2, Y.get('#spanwithinlink'), "event delegate works but it doesn't restore e.target to the original value.");
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy test_bubble_up_after_delegate_halt: function(){
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy var foo1 = false, foo2 = false;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.on('delegate', function(e) {
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy foo1 = true;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy }, '#mod1', 'click', 'a');
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.on('click', function(e) {
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy foo2 = true;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy }, '#doc');
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.Event.simulate (document.getElementById('spanwithinlink'), 'click');
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.Assert.isTrue(foo1, "first match fail, delegate should be able to match [a]");
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.Assert.isFalse(foo2, "the listener for 'doc' got executed, which means that e.halt fails during the delegate routine");
3215b14029b163463e7225627b6781e4a049f3d8Caridy * Other things that I consider should be tested in the future:
3215b14029b163463e7225627b6781e4a049f3d8Caridy * - stopping the event, verifying the event ourside of the container
3215b14029b163463e7225627b6781e4a049f3d8Caridy * - stopping the event and verify what happen with multiple matches
3215b14029b163463e7225627b6781e4a049f3d8Caridy //run all tests
3215b14029b163463e7225627b6781e4a049f3d8Caridy /* finishing the testing process */