Skip to content

Commit

Permalink
Pretty intense refactor
Browse files Browse the repository at this point in the history
* Stop dropping stuff at top level jQuery. $.jasmine is now 
jasmine.fixture
* Internally reorganize methods a bit
* noConflict() method will now undo all global changes and side effects
*except* for the afterEach hook
* jasmine.fixture now has a `create` and an `affix` method. You can
also continue to use `window.affix` as you always have, but this may
be deprecated in the future for users to cache themselves (haven't
decided)
  • Loading branch information
searls committed Feb 15, 2014
1 parent 96d5ba5 commit cd1f609
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
33 changes: 18 additions & 15 deletions app/js/jasmine-fixture.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
root = @

originalJasmineFixture = root.jasmineFixture
originalJasmineDotFixture = root.jasmine?.fixture
originalAffix = root.affix

_ = (list) ->
inject: (iterator, memo) ->
memo = iterator(memo, item) for item in list

root.jasmineFixture = ($) ->
#--------------------------------------------------------
# #createNodes (jasmine-fixture 1.x)
$.fn.createNodes = createNodes = (selectorOptions, attach) ->
affix = (selectorOptions) ->
create.call(this, selectorOptions, true)

create = (selectorOptions, attach) ->
$top=null
_(selectorOptions.split(/[ ](?=[^\]]*?(?:\[|$))/)).inject(($parent, elementSelector) ->
return $parent if elementSelector == ">"
Expand All @@ -22,10 +24,12 @@
, $whatsTheRootOf(@))
$top

#--------------------------------------------------------
# #affix (jasmine-fixture 1.x)
$.fn.affix = root.affix = (selectorOptions) ->
createNodes.call(this, selectorOptions, true)
noConflict = ->
currentJasmineFixture = jasmine.fixture
root.jasmineFixture = originalJasmineFixture
root.jasmine?.fixture = originalJasmineDotFixture
root.affix = originalAffix
currentJasmineFixture

$whatsTheRootOf = (that) ->
if that.jquery?
Expand All @@ -35,17 +39,16 @@
else
$('<div id="jasmine_content"></div>').appendTo('body')

jasmineFixture = {affix, create, noConflict}
ewwSideEffects(jasmineFixture)
return jasmineFixture

ewwSideEffects = (jasmineFixture) ->
root.jasmine?.fixture = jasmineFixture
$.fn.affix = root.affix = jasmineFixture.affix
afterEach ->
$('#jasmine_content').remove()

$.jasmine =
noConflict: ->
root.jasmineFixture = originalJasmineFixture
root.affix = originalAffix
this

$.jasmine

if $
jasmineFixture = root.jasmineFixture($)
else
Expand Down
38 changes: 19 additions & 19 deletions spec/spec.coffee → spec/jasmine-fixture-spec.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe "jasmine-fixture", ->
describe "jasmine.fixture", ->

EXAMPLES = [
'article' #<article></article>
Expand Down Expand Up @@ -27,46 +27,46 @@ describe "jasmine-fixture", ->
'select[name="date[year]"]' #<select name="date[year]"></select>
]

describe "affix", ->

describe ".affix", ->
for selector in EXAMPLES
do (selector) ->
Then "#{selector} is added to the DOM", -> expect(selector).toInjectProperly()
Then "#{selector} does not pollute the DOM", -> expect($('body')).not.toHas(selector) #<--ensure no pollution is happening
describe "#{selector} working", ->
Then -> expect(selector).toInjectProperly()
Then -> expect($('body')).not.toHas(selector) #<--ensure no pollution is happening

context "non-ids with hash symbol", ->
Given -> affix('a[data-target-pane="#pane-id"]')
Then -> expect($('body')).not.toHas('#pane-id')

describe "createNodes", ->

describe ".create", ->
Given -> @subject = jasmine.fixture

for selector in EXAMPLES
do (selector) ->
Given "#{selector} is created", ->
@$result = $(window).createNodes(selector)
Then "#{selector} exists but is not added to the DOM", ->
expect(@$result.length).toBeGreaterThan(0)
expect(@$result.parent().length).toEqual(0)
expect($('body')).not.toHas(selector)
describe "#{selector} working", ->
When -> @$result = @subject.create(selector)
Then -> @$result.length > 0
And -> @$result.parent().length == 0
And -> expect($('body')).not.toHas(selector)

context "a plain old div", ->
Given -> @$result = $(window).createNodes('div')
When -> @$result = @subject.create('div')
Then -> expect(@$result).toIs('div')

context "raw attrs", ->
Given -> @$result = $(window).createNodes('[name=foo]')
When -> @$result = @subject.create('[name=foo]')
Then -> expect(@$result).toIs('[name=foo]')

context "nesting returns", ->
Given -> @$result = $(window).createNodes('table.sp_record tbody tr')
When -> @$result = @subject.create('table.sp_record tbody tr')
Then -> expect(@$result).toIs('table')

context "a siblings!", ->
Given -> @$result = $(window).createNodes('div h1+h2')
When -> @$result = @subject.create('div h1+h2')
Then -> expect(@$result.find('h1').siblings('h2')).toIs("h2")

context "chaining", ->
Given -> @$container = $(window).createNodes('.container')
When -> @$container = @subject.create('.container')
When -> @$result = @$container.affix('#content')
Then -> expect(@$container).toHas('#content')
Then -> expect(@$result).toIs('#content')
And -> expect(@$result).toIs('#content')
4 changes: 3 additions & 1 deletion spec/no-conflict-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ describe("#noConflict", function() {
beforeEach(function() {
cache = {
jasmineFixture: window.jasmineFixture,
affix: window.affix
affix: window.affix,
jasmineDotFixture: jasmine.fixture
};

subject = window.jasmineFixture($);
Expand All @@ -14,6 +15,7 @@ describe("#noConflict", function() {
afterEach(function() {
window.jasmineFixture = cache.jasmineFixture;
window.affix = cache.affix;
jasmine.fixture = cache.jasmineDotFixture;
});

it("returns itself when noConflicted", function() {
Expand Down

0 comments on commit cd1f609

Please sign in to comment.