Skip to content

Commit

Permalink
Merge pull request #17 from lyt9304/adapt-to-ie8
Browse files Browse the repository at this point in the history
将单元测试代码适配到 IE8 + jQuery 环境
  • Loading branch information
cssmagic committed Apr 27, 2016
2 parents dfd305a + 560e100 commit 0a18278
Show file tree
Hide file tree
Showing 13 changed files with 522 additions and 339 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
"zepto.js": "1.*"
},
"devDependencies": {
"jquery": "*",
"action": "0.3.2",
"underscore-template": "0.3.0",
"underscore.string": "2.*",
"mocha.css": "0.1.0",
"mocha": "1.*",
"chai": "1.*"
"expect.js": "^0.3.1"
}
}
45 changes: 45 additions & 0 deletions test/_sandbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing Sandbox - UT - Gearbox</title>
</head>

<body>
<h1>Testing Sandbox</h1>
<script src="../bower_components/underscore/underscore.js"></script>
<script src="../bower_components/jquery/dist/jquery.js"></script>
<script src="../dist/gearbox.js"></script>
<script>
void function () {
'use strict'

var conditions = JSON.parse(window.name || '{}')
var testId = conditions.testId
var keysToBeTested = conditions.keysToBeTested
if (!testId) return

var result = {}
// `JSON.stringify()` drops undefined keys in objects, so we need save these keys separately.
var emptyKeys = []

_.each(keysToBeTested, function (item) {
var value = _.url.getParam(item)
if (typeof value === 'undefined') {
emptyKeys.push(item)
} else {
result[item] = value
}
})

var data = {
testId: testId,
result: result,
emptyKeys: emptyKeys
}
parent.postMessage(JSON.stringify(data), '*')

}()
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion test/test-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Action', function () {
$wrapper.remove()
})
beforeEach(function () {
actionName = Date.now().toString(36)
actionName = new Date().getTime().toString(36)
randomKey = Math.random().toString(36)
})
it('gets action name from `href`', function (done) {
Expand Down
3 changes: 1 addition & 2 deletions test/test-dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ <h1>UT (Dev) - Gearbox</h1>
<script src="../bower_components/zepto.js/dist/zepto.js"></script>
<!-- testing framework -->
<script src="../bower_components/mocha/mocha.js"></script>
<script src="../bower_components/chai/chai.js"></script>
<script src="../bower_components/expect.js/index.js"></script>
<script>
mocha.setup('bdd')
mocha.checkLeaks()
var expect = chai.expect
</script>
<!-- source code -->
<script src="../src/core.js"></script>
Expand Down
45 changes: 45 additions & 0 deletions test/test-dist-trad-jquery.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>UT (Trad) (jQuery) - Gearbox</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" href="../bower_components/mocha/mocha.css">
<link rel="stylesheet" href="../bower_components/mocha.css/src/mocha.css">
</head>
<body>
<header>
<h1>UT (Trad) (jQuery) - Gearbox</h1>
<p><a href="https://github.com/CMUI/gearbox">View on GitHub</a></p>
</header>
<div id="mocha"></div>
<!-- deps -->
<script src="../bower_components/underscore/underscore.js"></script>
<script src="../bower_components/jquery/dist/jquery.js"></script>
<!-- testing framework -->
<script src="../bower_components/mocha/mocha.js"></script>
<script src="../bower_components/expect.js/index.js"></script>
<script>
mocha.setup('bdd')
mocha.checkLeaks()
</script>
<!-- traditional package -->
<script src="../dist/gearbox.js"></script>
<!-- test-case -->
<script src="test-str-alt.js"></script>
<script src="test-str.js"></script>
<script src="test-root.js"></script>
<script src="test-ua.js"></script>
<script src="test-url.js"></script>
<script src="test-dom.js"></script>
<script src="test-action.js"></script>
<script src="test-template.js"></script>
<!-- init -->
<script>
// IE 会把 iframe 的 ID 映射为 window 上的一个属性, 导致全局变量泄漏.
// 因此需要在 mocha 中提前注册一下 iframe 的 ID.
mocha.globals(['sandbox'])
mocha.run()
</script>
</body>
</html>
3 changes: 1 addition & 2 deletions test/test-dist-trad.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ <h1>UT (Trad) - Gearbox</h1>
<script src="../bower_components/zepto.js/dist/zepto.js"></script>
<!-- testing framework -->
<script src="../bower_components/mocha/mocha.js"></script>
<script src="../bower_components/chai/chai.js"></script>
<script src="../bower_components/expect.js/index.js"></script>
<script>
mocha.setup('bdd')
mocha.checkLeaks()
var expect = chai.expect
</script>
<!-- traditional package -->
<script src="../dist/gearbox.js"></script>
Expand Down
24 changes: 12 additions & 12 deletions test/test-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ describe('DOM', function () {
describe('Shortcuts', function () {
describe('_.dom.$win', function () {
it('is $collection of `window` object', function () {
expect(_.dom.is$Element(_.dom.$win)).to.be.true
expect(_.dom.is$Element(_.dom.$win)).to.equal(true)
expect(_.dom.$win[0]).to.equal(window)
})
})
describe('_.dom.$root', function () {
it('is a $collection of `document.documentElement` object', function () {
expect(_.dom.is$Element(_.dom.$root)).to.be.true
expect(_.dom.is$Element(_.dom.$root)).to.equal(true)
expect(_.dom.$root[0]).to.equal(document.documentElement)
expect(_.dom.$root[0].tagName.toUpperCase()).to.equal('HTML')
})
})
describe('_.dom.$body', function () {
it('is a $collection of `document.body` object', function () {
expect(_.dom.is$Element(_.dom.$body)).to.be.true
expect(_.dom.is$Element(_.dom.$body)).to.equal(true)
expect(_.dom.$body[0]).to.equal(document.body)
})
})
Expand All @@ -26,26 +26,26 @@ describe('DOM', function () {
it('checks if it\'s $collection', function () {
var arg
arg = $()
expect(_.dom.is$Element(arg)).to.be.true
expect(_.dom.is$Element(arg)).to.equal(true)
arg = $(window)
expect(_.dom.is$Element(arg)).to.be.true
expect(_.dom.is$Element(arg)).to.equal(true)
})
it('returns `false` if bad type of param', function () {
var arg
arg = undefined
expect(_.dom.is$Element(arg)).to.be.false
expect(_.dom.is$Element(arg)).to.equal(false)
arg = null
expect(_.dom.is$Element(arg)).to.be.false
expect(_.dom.is$Element(arg)).to.equal(false)
arg = 0
expect(_.dom.is$Element(arg)).to.be.false
expect(_.dom.is$Element(arg)).to.equal(false)
arg = true
expect(_.dom.is$Element(arg)).to.be.false
expect(_.dom.is$Element(arg)).to.equal(false)
arg = {}
expect(_.dom.is$Element(arg)).to.be.false
expect(_.dom.is$Element(arg)).to.equal(false)
arg = []
expect(_.dom.is$Element(arg)).to.be.false
expect(_.dom.is$Element(arg)).to.equal(false)
arg = document.documentElement
expect(_.dom.is$Element(arg)).to.be.false
expect(_.dom.is$Element(arg)).to.equal(false)
})
})
})
Expand Down
38 changes: 19 additions & 19 deletions test/test-root.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
describe('(Root)', function () {
describe('_.isPlainObject()', function () {
it('is an alias of $\'s same api', function () {
expect($.isPlainObject === _.isPlainObject).to.be.true
expect($.isPlainObject === _.isPlainObject).to.equal(true)
})
it('does basic functionality', function () {
var arg
arg = {}
expect(_.isPlainObject(arg)).to.be.true
expect(_.isPlainObject(arg)).to.equal(true)
arg = {foo: 1, bar: 2}
expect(_.isPlainObject(arg)).to.be.true
expect(_.isPlainObject(arg)).to.equal(true)
arg = new Object()
expect(_.isPlainObject(arg)).to.be.true
expect(_.isPlainObject(arg)).to.equal(true)
arg = /foobar/i
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = new Date()
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = new String('foobar')
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = window
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = document.documentElement
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
})
it('returns `false` if input other types', function () {
var arg
arg = undefined
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = null
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = 0
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = 'foobar'
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = true
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = []
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = it
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = String
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
arg = Date
expect(_.isPlainObject(arg)).to.be.false
expect(_.isPlainObject(arg)).to.equal(false)
})
})

describe('_.$()', function () {
it('does basic functionality same as $()', function () {
var elem = document.getElementById('mocha')
var obj = _.$(elem)
expect(obj).to.deep.equal($(elem))
expect(obj).to.eql($(elem))
})
it('returns directly if already $collection', function () {
var obj = $('#mocha')
Expand Down
Loading

0 comments on commit 0a18278

Please sign in to comment.