Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major unit tests cleanup #13853

Merged
merged 16 commits into from
Jul 7, 2014
Merged
29 changes: 17 additions & 12 deletions js/tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@
<script>
// See https://github.com/axemclion/grunt-saucelabs#test-result-details-with-qunit
var log = []
QUnit.done = function (test_results) {
var tests = log.map(function (details) {
return {
QUnit.done(function (testResults) {
var tests = []
for (var i = 0, len = log.length; i < len; i++) {
var details = log[i]
tests.push({
name: details.name,
result: details.result,
expected: details.expected,
actual: details.actual,
source: details.source
}
})
test_results.tests = tests
})
}
testResults.tests = tests

// Delaying results a bit because in real-world scenario you won't get them immediately
setTimeout(function () {
window.global_test_results = test_results
}, 2000)
}
window.global_test_results = testResults
})

QUnit.testStart(function (testDetails) {
QUnit.log = function (details) {
Expand All @@ -39,6 +38,12 @@
}
}
})

// Cleanup
QUnit.testDone(function () {
$('#qunit-fixture').empty()
$('#modal-test, .modal-backdrop').remove()
})
</script>

<!-- Plugin sources -->
Expand Down Expand Up @@ -70,7 +75,7 @@

</head>
<body>
<div>
<div id="qunit-container">
<div id="qunit"></div>
<div id="qunit-fixture"></div>
</div>
Expand Down
31 changes: 20 additions & 11 deletions js/tests/unit/affix.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,53 @@ $(function () {
})

test('should provide no conflict', function () {
ok(!$.fn.affix, 'affix was set back to undefined (org value)')
strictEqual($.fn.affix, undefined, 'affix was set back to undefined (org value)')
})

test('should return element', function () {
ok($(document.body).bootstrapAffix()[0] == document.body, 'document.body returned')
test('should return jquery collection containing the element', function () {
var $el = $('<div/>')
var $affix = $el.bootstrapAffix()
ok($affix instanceof $, 'returns jquery collection')
strictEqual($affix[0], $el[0], 'collection contains element')
})

test('should exit early if element is not visible', function () {
var $affix = $('<div style="display: none"></div>').bootstrapAffix()
var $affix = $('<div style="display: none"/>').bootstrapAffix()
$affix.data('bs.affix').checkPosition()
ok(!$affix.hasClass('affix'), 'affix class was not added')
})

test('should trigger affixed event after affix', function () {
stop()

var template = $('<div id="affixTarget"><ul><li>Please affix</li><li>And unaffix</li></ul></div><div id="affixAfter" style="height: 20000px; display:block;"></div>')
template.appendTo('body')
var templateHTML = '<div id="affixTarget">'
+ '<ul>'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 4 spaces here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These all have four spaces, the original templates that were spread across lines were all indented by four spaces, so I sticked to it. Should I move to two?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh, it's chill

+ '<li>Please affix</li>'
+ '<li>And unaffix</li>'
+ '</ul>'
+ '</div>'
+ '<div id="affixAfter" style="height: 20000px; display: block;"/>'
$(templateHTML).appendTo(document.body)

$('#affixTarget').bootstrapAffix({
offset: $('#affixTarget ul').position()
})

$('#affixTarget')
.on('affix.bs.affix', function () {
ok(true, 'affix event triggered')
ok(true, 'affix event fired')
}).on('affixed.bs.affix', function () {
ok(true, 'affixed event triggered')
$('#affixTarget').remove()
$('#affixAfter').remove()
ok(true, 'affixed event fired')
$('#affixTarget, #affixAfter').remove()
start()
})

setTimeout(function () {
window.scrollTo(0, document.body.scrollHeight)

setTimeout(function () {
window.scroll(0, 0)
}, 0)
}, 16) // for testing in a browser
}, 0)
})
})
46 changes: 23 additions & 23 deletions js/tests/unit/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,52 @@ $(function () {
})

test('should provide no conflict', function () {
ok(!$.fn.alert, 'alert was set back to undefined (org value)')
strictEqual($.fn.alert, undefined, 'alert was set back to undefined (org value)')
})

test('should return element', function () {
ok($(document.body).bootstrapAlert()[0] == document.body, 'document.body returned')
test('should return jquery collection containing the element', function () {
var $el = $('<div/>')
var $alert = $el.bootstrapAlert()
ok($alert instanceof $, 'returns jquery collection')
strictEqual($alert[0], $el[0], 'collection contains element')
})

test('should fade element out on clicking .close', function () {
var alertHTML = '<div class="alert-message warning fade in">' +
'<a class="close" href="#" data-dismiss="alert">×</a>' +
'<p><strong>Holy guacamole!</strong> Best check yo self, you\'re not looking too good.</p>' +
'</div>'
var alert = $(alertHTML).bootstrapAlert()
var alertHTML = '<div class="alert-message warning fade in">'
+ '<a class="close" href="#" data-dismiss="alert">×</a>'
+ '<p><strong>Holy guacamole!</strong> Best check yo self, you\'re not looking too good.</p>'
+ '</div>'
var $alert = $(alertHTML).bootstrapAlert()

alert.find('.close').click()
$alert.find('.close').click()

ok(!alert.hasClass('in'), 'remove .in class on .close click')
equal($alert.hasClass('in'), false, 'remove .in class on .close click')
})

test('should remove element when clicking .close', function () {
$.support.transition = false
var alertHTML = '<div class="alert-message warning fade in">'
+ '<a class="close" href="#" data-dismiss="alert">×</a>'
+ '<p><strong>Holy guacamole!</strong> Best check yo self, you\'re not looking too good.</p>'
+ '</div>'
var $alert = $(alertHTML).appendTo('#qunit-fixture').bootstrapAlert()

var alertHTML = '<div class="alert-message warning fade in">' +
'<a class="close" href="#" data-dismiss="alert">×</a>' +
'<p><strong>Holy guacamole!</strong> Best check yo self, you\'re not looking too good.</p>' +
'</div>'
var alert = $(alertHTML).appendTo('#qunit-fixture').bootstrapAlert()
notEqual($('#qunit-fixture').find('.alert-message').length, 0, 'element added to dom')

ok($('#qunit-fixture').find('.alert-message').length, 'element added to dom')
$alert.find('.close').click()

alert.find('.close').click()

ok(!$('#qunit-fixture').find('.alert-message').length, 'element removed from dom')
equal($('#qunit-fixture').find('.alert-message').length, 0, 'element removed from dom')
})

test('should not fire closed when close is prevented', function () {
$.support.transition = false
stop()
$('<div class="alert"/>')
.on('close.bs.alert', function (e) {
e.preventDefault()
ok(true)
ok(true, 'close event fired')
start()
})
.on('closed.bs.alert', function () {
ok(false)
ok(false, 'closed event fired')
})
.bootstrapAlert('close')
})
Expand Down
Loading