Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #9473

Re: Is this the only way to unit-test an events abstraction?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!feeder.erje.net!diablo1.news.osn.de!news.osn.de!diablo2.news.osn.de!news.belwue.de!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <1658658.tIHn9WeWv6@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Tue, 20 Dec 2011 22:02:23 +0100
User-Agent KNode/4.4.11
Content-Transfer-Encoding 8Bit
Subject Re: Is this the only way to unit-test an events abstraction?
Newsgroups comp.lang.javascript
References <6567149.685.1324401412063.JavaMail.geo-discussion-forums@yqbo42>
Followup-To comp.lang.javascript
MIME-Version 1.0
Lines 130
NNTP-Posting-Date 20 Dec 2011 22:02:24 CET
NNTP-Posting-Host 6af8f586.newsspool2.arcor-online.net
X-Trace DXC=be[7beUL_g>=FQB?mjjV50A9EHlD;3Yc24Fo<]lROoR18kF<OcfhCO;`W;:Ca2SK74DZm8W4\YJN<;?f@h5gMfb<0HL@K4XoXn?nMdT]G0N8N7
X-Complaints-To usenet-abuse@arcor.de
Xref x330-a1.tempe.blueboxinc.net comp.lang.javascript:9473

Followups directed to: comp.lang.javascript

Show key headers only | View raw


storm.mark@gmail.com wrote:
^^^^^^^^^^^^^^^^^^^^
Please put your real name there.  In Google Groups, if you absolutely must 
use it (see below), by "subscribing" to the newsgroup.

> I have two questions:
> 
> 1.) How best to unit-test an event listener?
> 2.) Have I properly understood isHostMethod?
> 
> I'm trying to unit-test an event listener abstraction but the only way I
> can think to test it is against the window 'load' event.
> 
> I've a code example here: https://gist.github.com/1502326

First of all, contrary what Google Groups might suggest to you, this is a 
newsgroup, not a Web forum.  Primarily it is _not_ on the Web, but in a 
separate, plain-text based medium and a network separate from the Internet, 
called Network News (a subset of which is Usenet to which the `comp' 
hierarchy belongs).  So you should post the code that you are asking about, 
not just a URL to some Web site (even if it is a code repository).

You should also eschew Google Groups in the first place (except for 
research); it is *the* source of newsgroup spam, technically non-compliant 
postings, and therefore heavily filtered.

That said, your approach is based on the common misconception that 
addEventListener(), attachEvent() and proprietary event-handler properties 
would be semantically identical; they are not:

- *Several* event listeners can be added with element.addEventListener() in
  order of later execution.  In the listener context, `this' refers to
  the object that handled the event (the same as `element').  The listener
  is passed an event object reference.  The event object implements the
  standard attributes of the Event interface, like `target' and
  `relatedTarget', as properties, and methods like `preventDefault'.

- *Several* event listeners can be added with attachEvent().  Order of 
  later execution is _not_ guaranteed.  `this' refers to the same object
  as `window', _not_ to the object that handled the event.  The listener
  is _not_ passed an event object reference; that has be be acquired from
  window.event.  The event object does _not_ implement standard attributes,
  but has MSHTML-proprietary properties like `srcElement', `fromElement',
  `toElement', `returnValue' and `cancelBubble', respectively, instead.

- Only *one* (the primary) event listener can be added with proprietary 
  event-handler properties.  The existing property value will be
  *overwritten*.  The listener is passed an event object reference unless
  the implementation is MSHTML-based (IE).  The event object will have
  an implementation-dependent set of properties and methods.  Depending
  on the implementation, the return value of the listener will define
  whether the default action for the event is prevented (the event is
  canceled) or not.

Bottom line: Eschew attachEvent().
 
<http://www.w3.org/TR/DOM-Level-3-Events/#events-EventTarget>

<https://developer.mozilla.org/en/DOM_Client_Object_Cross-
Reference/DOM_Events> (I am only partially responsible for the current 
content and have not reviewed it yet; but it should give you a fair idea 
about what is going on).

<http://www.quirksmode.org/blog/archives/2005/08/addevent_consid.html>

Further, you should _not_ assume that because the object referred to by 
`this' (the global object) exposes the method, all objects must expose it, 
and that it must be working for all event types.  The same applies for the 
assumption of the opposite.  The more reliable approach, if slightly less 
efficient, is to use the approach that appears to be working when the event 
was created.

For an event-handling script that implements all of this and has 
(therefore?) survived bad storms (no pun intended), see 
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fdom%2Fevents.js>

> Also, I'm using David Mark's (@cinsoft) isHostMethod to detect the
> relevant host object support but was wondering if I had properly
> understood the concepts or not? So for example, am I right in thinking
> that a host object/method should be available under the following
> conditions (although I understand that these 'conditions' are unreliable):
> 
> - if typeof returns 'function' (for majority browsers)
> - if typeof returns 'unknown' (for IE < 9 where its implementation used
> ActiveX objects for native Functions) - if typeof returns 'object' and the
> value isn't 'null' (because ES3 specs have allowed null to return 'object'
> which is incorrect behaviour)

That is partially correct.  `typeof' is an *operator*, it does not *return* 
anything.  "unknown" may be the *result* of a `typeof' operation in *all* 
DOM implementations; there is no limit as to in which implementations it may 
be the result, or up to which MSHTML version it can be returned.  
ActiveX/COM has little to do with it; the point is those are *host* objects, 
and their `typeof' operation is allowed, by Specification, to result in any 
string (loosely speaking).  See ECMA-262 Ed. 3 and 5.1, sections 11.4.3, for 
details.

[JFTR: I am partially responsible for the way David's isHostMethod() looks 
today, for IIRC I have started this is…Method() business with isMethod() 
years ago (search the archives).  Curiously enough, I have recently 
(JSX@234) found it useful to "split" jsx.object.isMethod() so that there 
would be jsx.object.isNativeMethod() as well, as David did before, to tell 
user-defined event listeners from unsuitable objects apart in event.js.]
 
> If any of these conditions are true then that (again, unreliably) *should*
> mean that the specified host method is available to use.

Yes, it should.  Unfortunately, it is only so most of the time.  A notable 
exception is the postMessage() method discussed recently, which apparently 
existed before in MSHTML but had a very different implementation (not as 
specified in the HTML5 draft).

> But even still the host object/method might be implemented differently to
> how the specification dictates it should be and so it would be more
> accurate to do full 'feature detection' where I create the object and see
> if an event is triggered (or some similar test)?

If you mean feature-tests at runtime: No.  For you would need to create an 
event object as well, which firstly cannot be done everywhere, secondly 
would only create an event of one of the numerous event types if it worked, 
and thirdly you would require feature detection for that as well.  So that 
can only reduce your chances of getting a useful event library (0.5³ = 
0.125).


HTH

PointedEars
-- 
When all you know is jQuery, every problem looks $(olvable).

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Is this the only way to unit-test an events abstraction? storm.mark@gmail.com - 2011-12-20 09:16 -0800
  Re: Is this the only way to unit-test an events abstraction? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-20 22:02 +0100
    Re: Is this the only way to unit-test an events abstraction? storm.mark@gmail.com - 2011-12-21 01:27 -0800
      Re: Is this the only way to unit-test an events abstraction? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-21 13:42 +0100
    Re: Is this the only way to unit-test an events abstraction? John G Harris <john@nospam.demon.co.uk> - 2011-12-21 17:25 +0000
      Re: Is this the only way to unit-test an events abstraction? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-21 20:30 +0100
        Re: Is this the only way to unit-test an events abstraction? John G Harris <john@nospam.demon.co.uk> - 2011-12-22 15:22 +0000

csiph-web