Skip to content

Commit

Permalink
Fix #8 - version 0.3.0 - call passed in select/change handlers. Upgra…
Browse files Browse the repository at this point in the history
…de Jasmine to 2.x code.
  • Loading branch information
rniemeyer committed Dec 26, 2014
1 parent 2d4f3d7 commit 8e42e99
Show file tree
Hide file tree
Showing 12 changed files with 2,782 additions and 2,932 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = function(grunt) {
options : {
specs : "spec/*.js",
vendor: [
"bower_components/jquery/jquery.min.js",
"bower_components/jquery/dist/jquery.min.js",
"bower_components/jqueryui/ui/jquery-ui.js",
"bower_components/knockout.js/knockout.js"
],
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-jqAutocomplete",
"version": "0.2.1",
"version": "0.3.0",
"main": "build/knockout-jqAutocomplete.min.js",
"ignore": [
"examples",
Expand Down
17 changes: 15 additions & 2 deletions build/knockout-jqAutocomplete.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// knockout-jqAutocomplete 0.2.1 | (c) 2013 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
// knockout-jqAutocomplete 0.3.0 | (c) 2014 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
;(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD anonymous module
Expand All @@ -14,7 +14,8 @@

//binding's init function
this.init = function(element, valueAccessor, allBindings, data, context) {
var options = unwrap(valueAccessor()),
var existingSelect, existingChange,
options = unwrap(valueAccessor()),
config = {},
filter = typeof options.filter === "function" ? options.filter : self.defaultFilter;

Expand All @@ -40,6 +41,10 @@
config.source = self.processOptions.bind(self, valueAccessor, filter, options.source);
}

//save any passed in select/change calls
existingSelect = typeof config.select === "function" && config.select;
existingChange = typeof config.change === "function" && config.change;

//handle updating the actual value
config.select = function(event, ui) {
if (ui.item && ui.item.actual) {
Expand All @@ -49,6 +54,10 @@
options.dataValue(ui.item.data);
}
}

if (existingSelect) {
existingSelect.apply(this, arguments);
}
};

//user made a change without selecting a value from the list
Expand All @@ -60,6 +69,10 @@
options.dataValue(null);
}
}

if (existingChange) {
existingChange.apply(this, arguments);
}
};

//initialize the widget
Expand Down
4 changes: 2 additions & 2 deletions build/knockout-jqAutocomplete.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-jqAutocomplete",
"version": "0.2.1",
"version": "0.3.0",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-uglify": "0.x.x",
Expand Down
69 changes: 60 additions & 9 deletions spec/knockout-jqAutocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ describe("knockout-jqAutocomplete", function(){
it("should apply additional options passed in the binding", function() {
instance.init(input, function() {
return {
delay: 1000
options: {
delay: 1000
}
};
});

expect($input.autocomplete("option", "delay"), 1000);
expect($input.autocomplete("option", "delay")).toEqual(1000);
});

it("should apply global options", function() {
Expand All @@ -83,7 +85,7 @@ describe("knockout-jqAutocomplete", function(){
return {};
});

expect($input.autocomplete("option", "delay"), 2000);
expect($input.autocomplete("option", "delay")).toEqual(2000);
});

it("should override global options with local options", function() {
Expand All @@ -97,7 +99,7 @@ describe("knockout-jqAutocomplete", function(){
};
});

expect($input.autocomplete("option", "delay"), 2000);
expect($input.autocomplete("option", "delay")).toEqual(2000);
});

it("should destroy the widget when the node is removed by KO", function() {
Expand Down Expand Up @@ -198,7 +200,7 @@ describe("knockout-jqAutocomplete", function(){

instance.processOptions(valueAccessor, null, data, request, response);

responseData = response.mostRecentCall.args[0];
responseData = response.calls.mostRecent().args[0];

expect(responseData.length).toEqual(3);
expect(JSON.stringify(responseData[0])).toEqual('{"label":"one","value":"one","actual":"one","data":"one"}');
Expand All @@ -222,7 +224,7 @@ describe("knockout-jqAutocomplete", function(){

instance.processOptions(valueAccessor, instance.defaultFilter, data, request, response);

responseData = response.mostRecentCall.args[0];
responseData = response.calls.mostRecent().args[0];

expect(responseData.length).toEqual(2);
expect(JSON.stringify(responseData[0])).toEqual('{"label":"two","value":"two","actual":"two","data":"two"}');
Expand All @@ -245,7 +247,7 @@ describe("knockout-jqAutocomplete", function(){

instance.processOptions(valueAccessor, instance.defaultFilter, data, request, response);

responseData = response.mostRecentCall.args[0];
responseData = response.calls.mostRecent().args[0];

expect(responseData.length).toEqual(2);
expect(JSON.stringify(responseData[0])).toEqual('{"label":"two","value":"two","actual":"two","data":"two"}');
Expand All @@ -264,7 +266,7 @@ describe("knockout-jqAutocomplete", function(){

instance.processOptions(valueAccessor, instance.defaultFilter, data, request, response);

responseData = response.mostRecentCall.args[0];
responseData = response.calls.mostRecent().args[0];

expect(responseData.length).toEqual(0);
});
Expand Down Expand Up @@ -300,7 +302,7 @@ describe("knockout-jqAutocomplete", function(){

instance.processOptions(valueAccessor, null, data, request, response);

responseData = response.mostRecentCall.args[0];
responseData = response.calls.mostRecent().args[0];

expect(responseData.length).toEqual(3);
expect(responseData[0].label).toEqual("1-two");
Expand Down Expand Up @@ -872,5 +874,54 @@ describe("knockout-jqAutocomplete", function(){

expect(input.value).toEqual("updated");
});

it("should call a passed in \"select\" function", function() {
var $listItems,
items = ["one", "two", "three"],
value = ko.observable(),
handler = jasmine.createSpy();

ko.applyBindingsToNode(input, {
jqAuto: {
value: value,
source: items,
options: {
select: handler
}
}
});

$input.autocomplete("search", "t");

$listItems = $("ul.ui-autocomplete li");

$listItems.first("a").click();

expect(handler.calls.count()).toEqual(1);
expect(handler.calls.argsFor(0)[0].type).toEqual("autocompleteselect");
expect("item" in handler.calls.argsFor(0)[1]).toBeTruthy();
});

it("should call a passed in \"change\" function", function() {
var items = [],
value = ko.observable("testing"),
handler = jasmine.createSpy();

ko.applyBindingsToNode(input, {
jqAuto: {
value: value,
source: items,
options: {
change: handler
}
}
});

$input.val("test").blur();

expect(handler.calls.count()).toEqual(1);
expect(handler.calls.argsFor(0)[0].type).toEqual("autocompletechange");
expect("item" in handler.calls.argsFor(0)[1]).toBeTruthy();
});
});
});
120 changes: 120 additions & 0 deletions spec/lib/boot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
Starting with version 2.0, this file "boots" Jasmine, performing all of the necessary initialization before executing the loaded environment and all of a project's specs. This file should be loaded after `jasmine.js`, but before any project source files or spec files are loaded. Thus this file can also be used to customize Jasmine for a project.
If a project is using Jasmine via the standalone distribution, this file can be customized directly. If a project is using Jasmine via the [Ruby gem][jasmine-gem], this file can be copied into the support directory via `jasmine copy_boot_js`. Other environments (e.g., Python) will have different mechanisms.
The location of `boot.js` can be specified and/or overridden in `jasmine.yml`.
[jasmine-gem]: http://github.com/pivotal/jasmine-gem
*/

(function() {

/**
* ## Require & Instantiate
*
* Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference.
*/
window.jasmine = jasmineRequire.core(jasmineRequire);

/**
* Since this is being run in a browser and the results should populate to an HTML page, require the HTML-specific Jasmine code, injecting the same reference.
*/
jasmineRequire.html(jasmine);

/**
* Create the Jasmine environment. This is used to run all specs in a project.
*/
var env = jasmine.getEnv();

/**
* ## The Global Interface
*
* Build up the functions that will be exposed as the Jasmine public interface. A project can customize, rename or alias any of these functions as desired, provided the implementation remains unchanged.
*/
var jasmineInterface = jasmineRequire.interface(jasmine, env);

/**
* Add all of the Jasmine global/public interface to the proper global, so a project can use the public interface directly. For example, calling `describe` in specs instead of `jasmine.getEnv().describe`.
*/
if (typeof window == "undefined" && typeof exports == "object") {
extend(exports, jasmineInterface);
} else {
extend(window, jasmineInterface);
}

/**
* ## Runner Parameters
*
* More browser specific code - wrap the query string in an object and to allow for getting/setting parameters from the runner user interface.
*/

var queryString = new jasmine.QueryString({
getWindowLocation: function() { return window.location; }
});

var catchingExceptions = queryString.getParam("catch");
env.catchExceptions(typeof catchingExceptions === "undefined" ? true : catchingExceptions);

/**
* ## Reporters
* The `HtmlReporter` builds all of the HTML UI for the runner page. This reporter paints the dots, stars, and x's for specs, as well as all spec names and all failures (if any).
*/
var htmlReporter = new jasmine.HtmlReporter({
env: env,
onRaiseExceptionsClick: function() { queryString.setParam("catch", !env.catchingExceptions()); },
getContainer: function() { return document.body; },
createElement: function() { return document.createElement.apply(document, arguments); },
createTextNode: function() { return document.createTextNode.apply(document, arguments); },
timer: new jasmine.Timer()
});

/**
* The `jsApiReporter` also receives spec results, and is used by any environment that needs to extract the results from JavaScript.
*/
env.addReporter(jasmineInterface.jsApiReporter);
env.addReporter(htmlReporter);

/**
* Filter which specs will be run by matching the start of the full name against the `spec` query param.
*/
var specFilter = new jasmine.HtmlSpecFilter({
filterString: function() { return queryString.getParam("spec"); }
});

env.specFilter = function(spec) {
return specFilter.matches(spec.getFullName());
};

/**
* Setting up timing functions to be able to be overridden. Certain browsers (Safari, IE 8, phantomjs) require this hack.
*/
window.setTimeout = window.setTimeout;
window.setInterval = window.setInterval;
window.clearTimeout = window.clearTimeout;
window.clearInterval = window.clearInterval;

/**
* ## Execution
*
* Replace the browser window's `onload`, ensure it's called, and then run all of the loaded specs. This includes initializing the `HtmlReporter` instance and then executing the loaded Jasmine environment. All of this will happen after all of the specs are loaded.
*/
var currentWindowOnload = window.onload;

window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
htmlReporter.initialize();
env.execute();
};

/**
* Helper function for readability above.
*/
function extend(destination, source) {
for (var property in source) destination[property] = source[property];
return destination;
}

}());
Loading

0 comments on commit 8e42e99

Please sign in to comment.