Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

inline script src into fixture so that the event binding will actually work #156

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/jasmine-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,33 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) {
var self = this
, url = this.makeFixtureUrl_(relativeUrl)
, htmlText = ''
, request = $.ajax({
async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
cache: false,
url: url,
success: function (data, status, $xhr) {
self.fixturesCache_[relativeUrl] = $xhr.responseText
htmlText = $xhr.responseText
},
error: function (jqXHR, status, errorThrown) {
throw new Error('Fixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')')
}
})
var scripts = $($.parseHTML(htmlText, true)).find('script') || [];
scripts.each(function(){
$.ajax({
async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
cache: false,
url: $(this).attr('src'),
success: function (data, status, $xhr) {
htmlText += '<script>' + $xhr.responseText + '</script>'
},
error: function (jqXHR, status, errorThrown) {
throw new Error('Script could not be loaded: ' + scriptSrc + ' (status: ' + status + ', message: ' + errorThrown.message + ')')
}
});
})
self.fixturesCache_[relativeUrl] = htmlText;
}

jasmine.Fixtures.prototype.makeFixtureUrl_ = function (relativeUrl){
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/fixture_with_javascript.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="anchor_01"><script src="spec/fixtures/javascripts/jasmine_javascript_click.js"></script><script src="spec/fixtures/javascripts/jasmine_javascript_hover.js"></script></div>
1 change: 1 addition & 0 deletions spec/fixtures/javascripts/jasmine_javascript_click.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(function (){ $('#anchor_01').click(function(){ $(this).addClass('foo'); }) });
1 change: 1 addition & 0 deletions spec/fixtures/javascripts/jasmine_javascript_hover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(function (){ $('#anchor_01').on('hover', function(){ $(this).addClass('bar'); }) });
26 changes: 24 additions & 2 deletions spec/suites/jasmine-jquery-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ describe("jasmine.Fixtures", function () {
expect($("#anchor_01")).toHaveClass('foo')
})
})

})

describe("appendLoad", function () {
Expand Down Expand Up @@ -399,6 +398,30 @@ describe("jasmine.Fixtures using real AJAX call", function () {
}).toThrow()
})
})

describe("when fixture contains an <script src='to/your/source'> tag", function () {
var fixtureUrl = "fixture_with_javascript.html"

it("should load javascript in fixture", function () {
var fixtureContent = jasmine.getFixtures().read(fixtureUrl)
expect(fixtureContent).toContain('$(function (){ $(\'#anchor_01\').click(function(){ $(this).addClass(\'foo\'); }) });')
})

it("should load content of fixture file and javascript and bind events", function () {
jasmine.getFixtures().load(fixtureUrl)
$('#anchor_01').click()
expect($("#anchor_01")).toHaveClass('foo')
})

it("should load multiple javascripts and bind events in fixture", function () {
jasmine.getFixtures().load(fixtureUrl)
$('#anchor_01').click()
$('#anchor_01').trigger('hover')
expect($("#anchor_01")).toHaveClass('foo')
expect($("#anchor_01")).toHaveClass('bar')
})
})

})


Expand Down Expand Up @@ -1590,7 +1613,6 @@ describe("jasmine.StyleFixtures using real AJAX call", function () {
})
})


describe("jasmine.JSONFixtures", function () {
var ajaxData = {a:1, b:2, arr: [1,2,'stuff'], hsh: { blurp: 8, blop: 'blip' }}
var moreAjaxData = [1,2,'stuff']
Expand Down