describe 'on()', -> fn = `on` it 'should throw an error on no input', -> try fn() catch e then err = e expect(err).toNotBe undefined it 'should expose an on.dom function after the first call', -> expect(typeof `on`.dom).toBe 'function' it 'should expose an on.query function after the first call', -> expect(typeof `on`.query).toBe 'function' it 'should expose an on.path_re function after the first call', -> expect(typeof `on`.path_re).toBe 'function' it 'should accept an object with "path_re", "dom", and/or "query" specs', -> what = fn( ready: (->), path_re: '/', dom: 'css *', query: true) expect(what).toEqual jasmine.any Array expect(what.length).toBeGreaterThan 2 describe 'on.dom(dom spec – see below for the three types of dom spec)', -> it 'should be a function after the first on() call', -> try `on()` catch e then err = e expect(err).toNotBe undefined it 'should expose on.dom.* functions once on.dom() has run once', -> try `on`.dom() fns = Object.keys(`on`.dom).join(',') expect(fns).toBe 'css*,css+,css?,css,xpath*,xpath+,xpath?,xpath!,xpath' describe 'on.dom(dom spec type 1: a selector string)', -> root = document.documentElement assertion = it describe 'on.dom("css… selection"): Array/Node, optional/not?', -> describe 'Array of Node:s (0+ occurrences):', -> assertion 'on.dom("css* NotFound") => []', -> expect(`on`.dom('css* NotFound')).toEqual [] assertion 'on.dom("css* html") => [root element]', -> expect(`on`.dom('css* html')).toEqual [root] assertion 'on.dom("css* *") => document.all (but as a proper Array)', -> what = `on`.dom("css* *") dall = [].slice.call document.all, 0 expect(what).toEqual dall describe 'Array of Node:s (1+ occurrences):', -> assertion 'on.dom("css+ html") => [root element]', -> expect(`on`.dom('css+ html')).toEqual [root] assertion 'on.dom("css+ NotFound") => undefined', -> expect(`on`.dom('css+ NotFound')).toBe undefined describe 'single optional Node, or null if not found:', -> assertion 'on.dom("css? *") => root element (= first match)', -> expect(`on`.dom('css? *')).toBe root assertion 'on.dom("css? NotFound") => null (not found)', -> expect(`on`.dom('css? NotFound')).toBe null describe 'single mandatory Node:', -> assertion 'on.dom("css *") => the root element', -> expect(`on`.dom('css *')).toBe root assertion 'on.dom("css NotFound") => undefined (unsatisfied)', -> expect(`on`.dom('css NotFound')).toBe undefined describe 'on.dom("xpath… selection"): Array/Node, optional/not?', -> describe 'xpath* => Array of Node:s (0+ occurrences):', -> assertion 'on.dom("xpath* /*") => [root element]', -> expect(`on`.dom('xpath* /*')).toEqual [root] assertion 'on.dom("xpath* /NotFound") => []', -> expect(`on`.dom('xpath* /NotFound')).toEqual [] describe 'xpath+ => Array of Node:s (1+ occurrences):', -> assertion 'on.dom("xpath+ /*") => [root element]', -> expect(`on`.dom('xpath+ /*')).toEqual [root] assertion 'on.dom("xpath+ /NotFound") => undefined', -> expect(`on`.dom('xpath+ /NotFound')).toBe undefined describe 'xpath? => single optional Node, or null if missing:', -> assertion 'on.dom("xpath? /NotFound") => null', -> expect(`on`.dom('xpath? /NotFound')).toBe null assertion 'on.dom("xpath? /*") => the root element', -> expect(`on`.dom('xpath? /*')).toBe root describe 'xpath => single mandatory Node:', -> assertion 'on.dom("xpath /*") => the root element', -> expect(`on`.dom('xpath /*')).toBe root assertion 'on.dom("xpath /NotFound") => undefined', -> expect(`on`.dom('xpath /NotFound')).toBe undefined assertion 'on.dom("xpath .") => the current document', -> expect(`on`.dom('xpath .')).toBe document describe '…or queries yielding Number/String/Boolean answers:', -> assertion 'on.dom("xpath count(/)") => 1', -> expect(`on`.dom('xpath count(/)')).toBe 1 assertion 'on.dom("xpath count(/NotFound)") => 0', -> expect(`on`.dom('xpath count(/NotFound)')).toBe 0 assertion 'on.dom("xpath name(/*)") => "html"', -> expect(`on`.dom('xpath name(/*)')).toBe 'html' assertion 'on.dom("xpath name(/)") => ""', -> expect(`on`.dom('xpath name(/)')).toBe '' assertion 'on.dom("xpath name(/*) = \'html\'") => true', -> expect(`on`.dom('xpath name(/*) = \'html\'')).toBe true assertion 'on.dom("xpath name(/*) = \'nope\'") => false', -> expect(`on`.dom('xpath name(/*) = \'nope\'')).toBe false describe 'xpath! makes assertions, requiring truthy answers:', -> assertion 'on.dom("xpath! count(/)") => 1', -> expect(`on`.dom('xpath count(/)')).toBe 1 assertion 'on.dom("xpath! count(/NotFound)") => undefined', -> expect(`on`.dom('xpath! count(/NotFound)')).toBe undefined assertion 'on.dom("xpath! name(/*)") => "html"', -> expect(`on`.dom('xpath! name(/*)')).toBe 'html' assertion 'on.dom("xpath! name(/)") => undefined', -> expect(`on`.dom('xpath! name(/)')).toBe undefined assertion 'on.dom("xpath! name(/*) = \'html\'") => true', -> expect(`on`.dom('xpath! name(/*) = \'html\'')).toBe true assertion 'on.dom("xpath! name(/*) = \'nope\'") => undefined', -> expect(`on`.dom('xpath! name(/*) = \'nope\'')).toBe undefined describe 'on.dom(dom spec type 2: an object showing the structure you want)', -> html = document.documentElement head = document.querySelector 'head' try `on()` # ensures there's an on.dom to call assertion = it pluralize = (n, noun) -> "#{n} #{noun}#{if n is 1 then '' else 's'}" assertion 'on.dom({}) => {} (fairly useless, but minimal, test case)', -> expect(`on`.dom({})).toEqual {} assertion 'on.dom({ h:"css head", H:"css html" }) => { h:head, H:html }', -> expect(`on`.dom({ h:"css head", H:"css html" })).toEqual { h:head, H:html } assertion 'on.dom({ h:"css head", f:"css? foot" }) => { h:head, f:null }', -> expect(`on`.dom({ h:"css head", f:"css? foot" })).toEqual { h:head, f:null } assertion 'on.dom({ h:"css head", f:"css foot" }) => undefined (no foot!)', -> expect(`on`.dom({ h:"css head", f:"css foot" })).toEqual undefined assertion 'on.dom({ x:"css* frame" }) => { x:[] } (frames optional here)', -> expect(`on`.dom({ x:"css* frame" })).toEqual { x:[] } assertion 'on.dom({ x:"css+ frame" }) => undefined (but mandatory here!)', -> expect(`on`.dom({ x:"css+ frame" })).toBe undefined assertion 'on.dom({ x:"css* script" }) => { x:[…all (>=0) script tags…] }', -> what = `on`.dom({ x:"css* script" }) expect(what.x).toEqual jasmine.any Array expect(what.x.every (s) -> s.nodeName is 'script') assertion 'on.dom({ x:"css+ script" }) => { x:[…all (>0) script tags…] }', -> what = `on`.dom({ x:"css+ script" }) expect(what.x).toEqual jasmine.any Array expect(what.x.length).toBeGreaterThan 0 expect(what.x.every (s) -> s.nodeName.toLowerCase() is 'script').toBe true assertion 'on.dom({ c:"xpath count(//script)" }) => {c:N} (any N is okay)', -> what = `on`.dom({ c:"xpath count(//script)" }) expect(what).toEqual jasmine.any Object expect(N = what.c).toEqual jasmine.any Number console.log "on.dom({ c: count(…) }) found #{pluralize N, 'script'}" delete what.c expect(what).toEqual {} assertion 'on.dom({ c:"xpath! count(//script)" }) => {c:N} (only N!=0 ok)', -> what = `on`.dom({ c:"xpath! count(//script)" }) expect(what.c).toBeGreaterThan 0 delete what.c expect(what).toEqual {} assertion 'on.dom({ c:"xpath! count(//missing)" }) => undefined (as N==0)', -> expect(`on`.dom({ c:"xpath! count(//missing)" })).toBe undefined assertion 'on.dom({ c:"xpath! count(//*) and /html" }) => { c:true }', -> expect(`on`.dom({ c:"xpath! count(//*) > 5 and /html" })).toEqual c: true describe 'on.dom(dom spec type 3: [context_spec, per_match_spec])', -> html = document.documentElement head = document.querySelector 'head' try `on()` # ensures there's an on.dom to call assertion = it assertion 'on.dom(["css* script[src]", "xpath string(@src)"]) => ["url"…]', -> what = `on`.dom(["css* script[src]", "xpath string(@src)"]) expect(what).toEqual jasmine.any Array expect(what.every (s) -> typeof s is 'string').toBe true assertion 'on.dom(["css? script:not([src])", "xpath string(.)"]) => "js…"', -> what = `on`.dom(["css? script:not([src])", "xpath string(.)"]) expect(typeof what).toBe 'string' desc = 'Code of first inline script tag' console.log "#{desc}:\n#{what}\n(#{desc} ends.)" assertion 'on.dom(["css? script:not([src])", "xpath! string(@src)"])' + ' => undefined (empty string is not truthy => not a match)', -> what = `on`.dom(["css? script:not([src])", "xpath! string(@src)"]) expect(what).toBe undefined assertion 'on.dom(["xpath /svg", "css* *"]) => undefined (not an svg doc)', -> expect(`on`.dom(["xpath /svg", "css* *"])).toBe undefined