delegate.html revision 762404fab9a26ffe78629af69b947a582b2ebf1a
3215b14029b163463e7225627b6781e4a049f3d8Caridy<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
3215b14029b163463e7225627b6781e4a049f3d8Caridy<html>
3215b14029b163463e7225627b6781e4a049f3d8Caridy<head>
3215b14029b163463e7225627b6781e4a049f3d8Caridy <title>YUI Event Delegate Tests</title>
3215b14029b163463e7225627b6781e4a049f3d8Caridy</head>
3215b14029b163463e7225627b6781e4a049f3d8Caridy<body class="yui-skin-sam">
3215b14029b163463e7225627b6781e4a049f3d8Caridy <div id="doc">
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy <div id="mod1">
3215b14029b163463e7225627b6781e4a049f3d8Caridy <div class="hd"><h3 class="title">H3 - Title</h3></div>
3215b14029b163463e7225627b6781e4a049f3d8Caridy <div class="bd">
3215b14029b163463e7225627b6781e4a049f3d8Caridy <p>simple parragraph with a link <a href="#"id="firstlink">simple link</a></p>
3215b14029b163463e7225627b6781e4a049f3d8Caridy <p>another parragraph 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 </div>
3215b14029b163463e7225627b6781e4a049f3d8Caridy </div>
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy </div>
3215b14029b163463e7225627b6781e4a049f3d8Caridy <!-- YUI3 Core //-->
329980a34cfc56e4b608da4d695b771cc1000250Caridy <script type="text/javascript" src="/build/yui/yui-debug.js"></script>
3215b14029b163463e7225627b6781e4a049f3d8Caridy <!-- Initialization process //-->
3215b14029b163463e7225627b6781e4a049f3d8Caridy <script type="text/javascript">
3215b14029b163463e7225627b6781e4a049f3d8Caridy YUI({
3215b14029b163463e7225627b6781e4a049f3d8Caridy base: "/build/",
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
3215b14029b163463e7225627b6781e4a049f3d8Caridy // creating the console...
3215b14029b163463e7225627b6781e4a049f3d8Caridy (new Y.Console()).render();
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy // starting the testing process
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy // add the test cases and suites
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Test.Runner.add(new Y.Test.Case({
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy name: "Event Delegate Tests",
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_simple_delegate_for_anchors: function(){
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy var foo = false, t;
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy t = e.target;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click', 'a');
3215b14029b163463e7225627b6781e4a049f3d8Caridy
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]");
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.areEqual(t, Y.get('#firstlink'), "event delegate works but the target is an incorrect node, should be the matching node");
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy },
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_checking_delegation_target: function(){
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy var foo = false, t;
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy t = e.target;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click', 'a');
3215b14029b163463e7225627b6781e4a049f3d8Caridy
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
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.areEqual(t, Y.get('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching node");
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy },
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_including_container_in_selector: function(){
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy var foo = false, t;
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy t = e.target;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click', '#mod1 a');
3215b14029b163463e7225627b6781e4a049f3d8Caridy
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
3215b14029b163463e7225627b6781e4a049f3d8Caridy },
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_targeting_container_without_selectors: function(){
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy var foo = false, t;
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy t = e.target;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click');
3215b14029b163463e7225627b6781e4a049f3d8Caridy
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 },
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_multiple_selectors_one_match: function(){
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy var foo = false, t;
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy t = e.target;
329980a34cfc56e4b608da4d695b771cc1000250Caridy }, '#mod1', 'click', 'a,a span');
3215b14029b163463e7225627b6781e4a049f3d8Caridy
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
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy },
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy test_multiple_delegate_matches: function(){
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy var foo1 = false, foo2 = false, t1, t2;
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo1 = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy t1 = e.target;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click', 'a');
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.on('delegate', function(e) {
3215b14029b163463e7225627b6781e4a049f3d8Caridy foo2 = true;
3215b14029b163463e7225627b6781e4a049f3d8Caridy t2 = e.target;
3215b14029b163463e7225627b6781e4a049f3d8Caridy }, '#mod1', 'click', 'a span');
3215b14029b163463e7225627b6781e4a049f3d8Caridy
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]");
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.areEqual(t1, Y.get('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Assert.areEqual(t2, Y.get('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
3215b14029b163463e7225627b6781e4a049f3d8Caridy
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy },
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy test_bubble_up_after_delegate: function(){
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy var foo1 = false, foo2 = false, t1, t2;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.on('delegate', function(e) {
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy foo1 = true;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy t1 = e.target;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy }, '#mod1', 'click', 'a');
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.on('click', function(e) {
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy foo2 = true;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy t2 = e.target;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy }, '#doc');
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
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");
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.Assert.areEqual(t1, Y.get('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.Assert.areEqual(t2, Y.get('#spanwithinlink'), "event delegate works but it doesn't restore e.target to the original value.");
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy },
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy test_bubble_up_after_delegate_halt: function(){
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy var foo1 = false, foo2 = false;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.on('delegate', function(e) {
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy foo1 = true;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy e.halt();
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy }, '#mod1', 'click', 'a');
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy Y.on('click', function(e) {
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy foo2 = true;
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy }, '#doc');
762404fab9a26ffe78629af69b947a582b2ebf1aCaridy
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 }
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy /*
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 */
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy }));
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy //run all tests
3215b14029b163463e7225627b6781e4a049f3d8Caridy Y.Test.Runner.run();
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy /* finishing the testing process */
3215b14029b163463e7225627b6781e4a049f3d8Caridy
3215b14029b163463e7225627b6781e4a049f3d8Caridy });
3215b14029b163463e7225627b6781e4a049f3d8Caridy </script>
3215b14029b163463e7225627b6781e4a049f3d8Caridy</body>
3215b14029b163463e7225627b6781e4a049f3d8Caridy</html>