Skip to content

Commit

Permalink
Merge pull request #2287 from harvesthq/jasmine-tests-on-ci
Browse files Browse the repository at this point in the history
Jasmine tests on ci
  • Loading branch information
pfiller committed Mar 28, 2015
2 parents 8897bbc + e7c26a4 commit a04c7fe
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ chosen.zip
.ruby-version
.rbenv-gemsets
.grunt
_SpecRunner.html
spec/public
18 changes: 18 additions & 0 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ This file is generated by `grunt build`, do not edit it by hand.
files:
'public/chosen.jquery.js': ['coffee/lib/select-parser.coffee', 'coffee/lib/abstract-chosen.coffee', 'coffee/chosen.jquery.coffee']
'public/chosen.proto.js': ['coffee/lib/select-parser.coffee', 'coffee/lib/abstract-chosen.coffee', 'coffee/chosen.proto.coffee']
'spec/public/jquery_specs.js': 'spec/jquery/*.spec.coffee'
'spec/public/proto_specs.js': 'spec/proto/*.spec.coffee'

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

jasmine:
jquery:
src: [ 'public/chosen.jquery.js' ]
options:
vendor: [ 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js' ]
specs: 'spec/public/jquery_specs.js'
proto:
src: [ 'public/chosen.proto.js' ]
options:
vendor: [
'https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js',
'public/docsupport/event.simulate.js'
]
specs: 'spec/public/proto_specs.js'

grunt.registerTask 'default', ['build']
grunt.registerTask 'build', ['coffee', 'compass', 'concat', 'uglify', 'cssmin', 'package-bower']
grunt.registerTask 'prep-release', ['build', 'dom_munger:latest_version', 'zip:chosen','package-jquery']
grunt.registerTask 'test', ['coffee', 'jasmine']

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

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"engines": {
"node": ">=0.4.0"
},
"scripts": {
"test": "grunt test"
},
"dependencies": {},
"devDependencies": {
"coffee-script": ">= 1.6",
Expand All @@ -18,10 +21,11 @@
"grunt-contrib-compass": "~0.5.0",
"grunt-contrib-concat": "~0.1.3",
"grunt-contrib-cssmin": "~0.6.1",
"grunt-gh-pages": "~0.10.0",
"grunt-contrib-jasmine": "~0.5.1",
"grunt-contrib-uglify": "~0.2.0",
"grunt-contrib-watch": "~0.3.1",
"grunt-dom-munger": "~2.0.1",
"grunt-gh-pages": "~0.10.0",
"grunt-zip": "~0.9.2",
"load-grunt-tasks": "^3.0.0"
},
Expand Down
64 changes: 64 additions & 0 deletions public/docsupport/event.simulate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Event.simulate(@element, eventName[, options]) -> Element
*
* - @element: element to fire event on
* - eventName: name of event to fire (only MouseEvents and HTMLEvents interfaces are supported)
* - options: optional object to fine-tune event properties - pointerX, pointerY, ctrlKey, etc.
*
* $('foo').simulate('click'); // => fires "click" event on an element with id=foo
*
**/
(function(){

var eventMatchers = {
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
pointerX: 0,
pointerY: 0,
button: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
bubbles: true,
cancelable: true
}

Event.simulate = function(element, eventName) {
var options = Object.extend(Object.clone(defaultOptions), arguments[2] || { });
var oEvent, eventType = null;

element = $(element);

for (var name in eventMatchers) {
if (eventMatchers[name].test(eventName)) { eventType = name; break; }
}

if (!eventType)
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

if (document.createEvent) {
oEvent = document.createEvent(eventType);
if (eventType == 'HTMLEvents') {
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
}
else {
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
}
element.dispatchEvent(oEvent);
}
else {
options.clientX = options.pointerX;
options.clientY = options.pointerY;
oEvent = Object.extend(document.createEventObject(), options);
element.fireEvent('on' + eventName, oEvent);
}
return element;
}

Element.addMethods({ simulate: Event.simulate });
})();
33 changes: 33 additions & 0 deletions spec/jquery/basic.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe "Basic setup", ->
it "should add chosen to jQuery object", ->
expect(jQuery.fn.chosen).toBeDefined()

it "should create very basic chosen", ->
tmpl = "
<select data-placeholder='Choose a Country...'>
<option value=''></option>
<option value='United States'>United States</option>
<option value='United Kingdom'>United Kingdom</option>
<option value='Afghanistan'>Afghanistan</option>
</select>
"
div = $("<div>").html(tmpl)
select = div.find("select")
expect(select.size()).toBe(1)
select.chosen()
# very simple check that the necessary elements have been created
["container", "container-single", "single", "default"].forEach (clazz)->
el = div.find(".chosen-#{clazz}")
expect(el.size()).toBe(1)

# test a few interactions
expect(select.val()).toBe ""

container = div.find(".chosen-container")
container.trigger("mousedown") # open the drop
expect(container.hasClass("chosen-container-active")).toBe true
#select an item
container.find(".active-result").last().trigger("mouseup")

expect(select.val()).toBe "Afghanistan"

37 changes: 37 additions & 0 deletions spec/proto/basic.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe "Basic setup", ->
it "should add expose a Chosen global", ->
expect(Chosen).toBeDefined()

it "should create very basic chosen", ->
tmpl = "
<select data-placeholder='Choose a Country...'>
<option value=''></option>
<option value='United States'>United States</option>
<option value='United Kingdom'>United Kingdom</option>
<option value='Afghanistan'>Afghanistan</option>
</select>
"

div = new Element("div")
document.body.insert(div)
div.update(tmpl)
select = div.down("select")
expect(select).toBeDefined()
new Chosen(select)
# very simple check that the necessary elements have been created
["container", "container-single", "single", "default"].forEach (clazz)->
el = div.down(".chosen-#{clazz}")
expect(el).toBeDefined()

# test a few interactions
expect($F(select)).toBe ""

container = div.down(".chosen-container")
container.simulate("mousedown") # open the drop
expect(container.hasClassName("chosen-container-active")).toBe true

#select an item
container.select(".active-result").last().simulate("mouseup")

expect($F(select)).toBe "Afghanistan"
div.remove()

0 comments on commit a04c7fe

Please sign in to comment.