Skip to content

Commit

Permalink
Add prototype version of test.
Browse files Browse the repository at this point in the history
  • Loading branch information
adunkman committed Mar 28, 2015
1 parent fb506c2 commit 3a89587
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Gruntfile.coffee
Expand Up @@ -44,7 +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/specs.js': 'spec/*.spec.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 @@ -98,7 +99,15 @@ This file is generated by `grunt build`, do not edit it by hand.
src: [ 'public/chosen.jquery.js' ]
options:
vendor: [ 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js' ]
specs: 'spec/public/*.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']
Expand Down
64 changes: 64 additions & 0 deletions public/docsupport/event.simulate.js
@@ -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 });
})();
File renamed without changes.
37 changes: 37 additions & 0 deletions spec/proto/basic.spec.coffee
@@ -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 3a89587

Please sign in to comment.