Skip to content

Commit a04c7fe

Browse files
committed
Merge pull request harvesthq#2287 from harvesthq/jasmine-tests-on-ci
Jasmine tests on ci
2 parents 8897bbc + e7c26a4 commit a04c7fe

File tree

6 files changed

+159
-1
lines changed

6 files changed

+159
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ chosen.zip
99
.ruby-version
1010
.rbenv-gemsets
1111
.grunt
12+
_SpecRunner.html
13+
spec/public

Gruntfile.coffee

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ This file is generated by `grunt build`, do not edit it by hand.
4444
files:
4545
'public/chosen.jquery.js': ['coffee/lib/select-parser.coffee', 'coffee/lib/abstract-chosen.coffee', 'coffee/chosen.jquery.coffee']
4646
'public/chosen.proto.js': ['coffee/lib/select-parser.coffee', 'coffee/lib/abstract-chosen.coffee', 'coffee/chosen.proto.coffee']
47+
'spec/public/jquery_specs.js': 'spec/jquery/*.spec.coffee'
48+
'spec/public/proto_specs.js': 'spec/proto/*.spec.coffee'
4749

4850
uglify:
4951
options:
@@ -92,9 +94,25 @@ This file is generated by `grunt build`, do not edit it by hand.
9294
message: "Updated to new Chosen version #{version()}"
9395
src: ['**']
9496

97+
jasmine:
98+
jquery:
99+
src: [ 'public/chosen.jquery.js' ]
100+
options:
101+
vendor: [ 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js' ]
102+
specs: 'spec/public/jquery_specs.js'
103+
proto:
104+
src: [ 'public/chosen.proto.js' ]
105+
options:
106+
vendor: [
107+
'https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js',
108+
'public/docsupport/event.simulate.js'
109+
]
110+
specs: 'spec/public/proto_specs.js'
111+
95112
grunt.registerTask 'default', ['build']
96113
grunt.registerTask 'build', ['coffee', 'compass', 'concat', 'uglify', 'cssmin', 'package-bower']
97114
grunt.registerTask 'prep-release', ['build', 'dom_munger:latest_version', 'zip:chosen','package-jquery']
115+
grunt.registerTask 'test', ['coffee', 'jasmine']
98116

99117
grunt.registerTask 'publish-release', ['gh-pages']
100118

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"engines": {
1111
"node": ">=0.4.0"
1212
},
13+
"scripts": {
14+
"test": "grunt test"
15+
},
1316
"dependencies": {},
1417
"devDependencies": {
1518
"coffee-script": ">= 1.6",
@@ -18,10 +21,11 @@
1821
"grunt-contrib-compass": "~0.5.0",
1922
"grunt-contrib-concat": "~0.1.3",
2023
"grunt-contrib-cssmin": "~0.6.1",
21-
"grunt-gh-pages": "~0.10.0",
24+
"grunt-contrib-jasmine": "~0.5.1",
2225
"grunt-contrib-uglify": "~0.2.0",
2326
"grunt-contrib-watch": "~0.3.1",
2427
"grunt-dom-munger": "~2.0.1",
28+
"grunt-gh-pages": "~0.10.0",
2529
"grunt-zip": "~0.9.2",
2630
"load-grunt-tasks": "^3.0.0"
2731
},
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Event.simulate(@element, eventName[, options]) -> Element
3+
*
4+
* - @element: element to fire event on
5+
* - eventName: name of event to fire (only MouseEvents and HTMLEvents interfaces are supported)
6+
* - options: optional object to fine-tune event properties - pointerX, pointerY, ctrlKey, etc.
7+
*
8+
* $('foo').simulate('click'); // => fires "click" event on an element with id=foo
9+
*
10+
**/
11+
(function(){
12+
13+
var eventMatchers = {
14+
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
15+
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
16+
}
17+
var defaultOptions = {
18+
pointerX: 0,
19+
pointerY: 0,
20+
button: 0,
21+
ctrlKey: false,
22+
altKey: false,
23+
shiftKey: false,
24+
metaKey: false,
25+
bubbles: true,
26+
cancelable: true
27+
}
28+
29+
Event.simulate = function(element, eventName) {
30+
var options = Object.extend(Object.clone(defaultOptions), arguments[2] || { });
31+
var oEvent, eventType = null;
32+
33+
element = $(element);
34+
35+
for (var name in eventMatchers) {
36+
if (eventMatchers[name].test(eventName)) { eventType = name; break; }
37+
}
38+
39+
if (!eventType)
40+
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
41+
42+
if (document.createEvent) {
43+
oEvent = document.createEvent(eventType);
44+
if (eventType == 'HTMLEvents') {
45+
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
46+
}
47+
else {
48+
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
49+
options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
50+
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
51+
}
52+
element.dispatchEvent(oEvent);
53+
}
54+
else {
55+
options.clientX = options.pointerX;
56+
options.clientY = options.pointerY;
57+
oEvent = Object.extend(document.createEventObject(), options);
58+
element.fireEvent('on' + eventName, oEvent);
59+
}
60+
return element;
61+
}
62+
63+
Element.addMethods({ simulate: Event.simulate });
64+
})();

spec/jquery/basic.spec.coffee

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
describe "Basic setup", ->
2+
it "should add chosen to jQuery object", ->
3+
expect(jQuery.fn.chosen).toBeDefined()
4+
5+
it "should create very basic chosen", ->
6+
tmpl = "
7+
<select data-placeholder='Choose a Country...'>
8+
<option value=''></option>
9+
<option value='United States'>United States</option>
10+
<option value='United Kingdom'>United Kingdom</option>
11+
<option value='Afghanistan'>Afghanistan</option>
12+
</select>
13+
"
14+
div = $("<div>").html(tmpl)
15+
select = div.find("select")
16+
expect(select.size()).toBe(1)
17+
select.chosen()
18+
# very simple check that the necessary elements have been created
19+
["container", "container-single", "single", "default"].forEach (clazz)->
20+
el = div.find(".chosen-#{clazz}")
21+
expect(el.size()).toBe(1)
22+
23+
# test a few interactions
24+
expect(select.val()).toBe ""
25+
26+
container = div.find(".chosen-container")
27+
container.trigger("mousedown") # open the drop
28+
expect(container.hasClass("chosen-container-active")).toBe true
29+
#select an item
30+
container.find(".active-result").last().trigger("mouseup")
31+
32+
expect(select.val()).toBe "Afghanistan"
33+

spec/proto/basic.spec.coffee

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
describe "Basic setup", ->
2+
it "should add expose a Chosen global", ->
3+
expect(Chosen).toBeDefined()
4+
5+
it "should create very basic chosen", ->
6+
tmpl = "
7+
<select data-placeholder='Choose a Country...'>
8+
<option value=''></option>
9+
<option value='United States'>United States</option>
10+
<option value='United Kingdom'>United Kingdom</option>
11+
<option value='Afghanistan'>Afghanistan</option>
12+
</select>
13+
"
14+
15+
div = new Element("div")
16+
document.body.insert(div)
17+
div.update(tmpl)
18+
select = div.down("select")
19+
expect(select).toBeDefined()
20+
new Chosen(select)
21+
# very simple check that the necessary elements have been created
22+
["container", "container-single", "single", "default"].forEach (clazz)->
23+
el = div.down(".chosen-#{clazz}")
24+
expect(el).toBeDefined()
25+
26+
# test a few interactions
27+
expect($F(select)).toBe ""
28+
29+
container = div.down(".chosen-container")
30+
container.simulate("mousedown") # open the drop
31+
expect(container.hasClassName("chosen-container-active")).toBe true
32+
33+
#select an item
34+
container.select(".active-result").last().simulate("mouseup")
35+
36+
expect($F(select)).toBe "Afghanistan"
37+
div.remove()

0 commit comments

Comments
 (0)