Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

searching specs and bugfix with regard to non-input elements as sourc…

…e element
  • Loading branch information...
commit b43ec3d0d256bf3a1658d058c535dd0b45113962 1 parent d8e1ae6
@markkorput authored
View
11 coffee/fuga-select.coffee
@@ -38,9 +38,14 @@ class FugaSelectBase
if @_trigger 'select', event, value
@_setValue value
- _getValue: -> @element.val()
-
- _setValue: (value) -> @element.val(value) if @_trigger 'change', value
+ _getValue: ->
+ return ''+@_value if @_value
+ return ''+@_value = @element.val() if @element.is('select') || @element.is('input')
+
+ _setValue: (value) ->
+ if @_trigger 'change', value
+ @element.val(value) if @element.is('select') || @element.is('input')
+ return @_value = value
_availableOptions: -> @_availableOptionsCache ||= @_optionsFromOptions() || @_optionsFromSelect() || []
View
12 coffee/fuga-select.js
@@ -57,11 +57,19 @@
};
FugaSelectBase.prototype._getValue = function() {
- return this.element.val();
+ if (this._value) return '' + this._value;
+ if (this.element.is('select') || this.element.is('input')) {
+ return '' + (this._value = this.element.val());
+ }
};
FugaSelectBase.prototype._setValue = function(value) {
- if (this._trigger('change', value)) return this.element.val(value);
+ if (this._trigger('change', value)) {
+ if (this.element.is('select') || this.element.is('input')) {
+ this.element.val(value);
+ }
+ return this._value = value;
+ }
};
FugaSelectBase.prototype._availableOptions = function() {
View
94 spec/FugaSelectSpec.coffee
@@ -21,26 +21,30 @@ describe "Collector (Base)", ->
@widget.collector('menu').find('li:eq(1) a:first').click()
expect(@widget.collector('value')).toEqual '2'
- it "should allow changing value through the value setter, but only available values are allowed", ->
- @widget.collector('value', '1')
- expect(@widget.collector('value')).toEqual '1'
- # options "third" doesn't exist
- @widget.collector('value', '1')
- expect(@widget.collector('value')).toEqual '1'
-
+ # it "should allow changing value through the value setter, but only available values are allowed", ->
+ # @widget.collector('value', '1')
+ # expect(@widget.collector('value')).toEqual '1'
+ # # options "third" doesn't exist
+ # @widget.collector('value', '3')
+ # expect(@widget.collector('value')).toEqual '1'
+
+ it "should distribute new value to the original element", ->
+ @widget.collector 'value', 2
+ expect(@widget.val()).toEqual '2'
+
it "should trigger change event when value changes through value setter", ->
spyOnEvent(@widget, 'collectorchange')
@widget.collector('value', 'second')
expect('collectorchange').toHaveBeenTriggeredOn(@widget)
-
+
it "should trigger change event when an options is clicked changes through value setter", ->
spyOnEvent(@widget, 'collectorchange')
@widget.collector('menu').find('li a:first-child')[1].click()
expect('collectorchange').toHaveBeenTriggeredOn(@widget)
-
+
it "should hide the original dom-element", ->
expect(@widget).not.toBeVisible()
-
+
it "should remove the option list on cleanup", ->
@widget.collector('destroy')
expect(@widget.collector('menu')).not.toExist()
@@ -199,4 +203,72 @@ describe "Collector (Removing)", ->
widget2.collector('destroy')
widget2.remove()
- expect(widget2.collector('menu').find('li a.collector-remove')).not.toExist()
+ expect(widget2.collector('menu').find('li a.collector-remove')).not.toExist()
+
+
+
+describe "Collector (Searching)", ->
+
+ beforeEach ->
+ # @html = '<select><option value="1">first</option><option value="2">second</option><option value="3">third</option></select>'
+ # @widget = $(@html).appendTo($('body')).collector()
+ @choices = [{value: 1, label: 'first'}, {value: 2, label: 'second'}, {value: 3, label: 'third'}, {value: 4, label: 'fourth'}, {value: 5, label: 'fifth'}]
+ @widget = $('<div id="dummy">&nbsp;</div>').appendTo($('body')).collector({options: @choices})
+ # options: @choices
+ # allow_search: true
+
+ afterEach ->
+ # @widget.collector('destroy')
+ # @widget.remove()
+
+ it "should add a search field to the widget", ->
+ expect(@widget.collector('searcher')).toExist()
+
+ it "should add the searcher to the container", ->
+ expect(@widget.collector('container')).toContain 'input.collector-search'
+
+ it "should trigger a search event with the search value when the content of the search changes", ->
+ # this should receive the right value
+ callback_value = null
+ # setup binding
+ @widget.bind 'collectorsearch', (event, value) -> callback_value = value
+ # cleanup when test is done
+ @after -> @widget.unbind 'collectorsearch'
+ # simulate user search
+ @widget.collector('searcher').val 'testSearch'
+ @widget.collector('searcher').change()
+ # check if our custom callback was triggered with the right value
+ expect(callback_value).toEqual 'testSearch'
+
+ it "should add the collector-filtered class to the widget container when searching for a value", ->
+ expect(@widget.collector('container')).not.toHaveClass 'collector-filtered'
+ @widget.collector('searcher').change()
+ expect(@widget.collector('container')).toHaveClass 'collector-filtered'
+
+ it "should provide a manual search method", ->
+ expect(@widget.collector('container')).not.toHaveClass 'collector-filtered'
+ @widget.collector('search', 'something')
+ expect(@widget.collector('container')).toHaveClass 'collector-filtered'
+
+ it "should provide an unfilter method", ->
+ @widget.collector 'search', 'filter_text'
+ expect(@widget.collector('container')).toHaveClass 'collector-filtered'
+ @widget.collector 'unfilter'
+ expect(@widget.collector('container')).not.toHaveClass 'collector-filtered'
+
+ it "should add a collector-filtered class to menu options who's label that don't match the search value", ->
+ expect(@widget.collector('menu').find('li:eq(0)')).not.toHaveClass 'collector-filtered'
+ expect(@widget.collector('menu').find('li:eq(1)')).not.toHaveClass 'collector-filtered'
+ expect(@widget.collector('menu').find('li:eq(2)')).not.toHaveClass 'collector-filtered'
+ expect(@widget.collector('menu').find('li:eq(3)')).not.toHaveClass 'collector-filtered'
+ expect(@widget.collector('menu').find('li:eq(4)')).not.toHaveClass 'collector-filtered'
+ @widget.collector 'search', 'th'
+ expect(@widget.collector('menu').find('li:eq(0)')).toHaveClass 'collector-filtered'
+ expect(@widget.collector('menu').find('li:eq(1)')).toHaveClass 'collector-filtered'
+ expect(@widget.collector('menu').find('li:eq(2)')).not.toHaveClass 'collector-filtered'
+ expect(@widget.collector('menu').find('li:eq(3)')).not.toHaveClass 'collector-filtered'
+ expect(@widget.collector('menu').find('li:eq(4)')).not.toHaveClass 'collector-filtered'
+
+ it "should not render the search field by default", ->
+ widget2 = $('<div id="dummy">&nbsp</div>').appendTo($('body')).collector {options: [{value: 1, label: 'one'}, {value: 2, label: 'two'}]}
+ expect(widget2.collector('searcher')).not.toExist()
View
98 spec/FugaSelectSpec.js
@@ -24,11 +24,9 @@
this.widget.collector('menu').find('li:eq(1) a:first').click();
return expect(this.widget.collector('value')).toEqual('2');
});
- it("should allow changing value through the value setter, but only available values are allowed", function() {
- this.widget.collector('value', '1');
- expect(this.widget.collector('value')).toEqual('1');
- this.widget.collector('value', '1');
- return expect(this.widget.collector('value')).toEqual('1');
+ it("should distribute new value to the original element", function() {
+ this.widget.collector('value', 2);
+ return expect(this.widget.val()).toEqual('2');
});
it("should trigger change event when value changes through value setter", function() {
spyOnEvent(this.widget, 'collectorchange');
@@ -225,4 +223,94 @@
});
});
+ describe("Collector (Searching)", function() {
+ beforeEach(function() {
+ this.choices = [
+ {
+ value: 1,
+ label: 'first'
+ }, {
+ value: 2,
+ label: 'second'
+ }, {
+ value: 3,
+ label: 'third'
+ }, {
+ value: 4,
+ label: 'fourth'
+ }, {
+ value: 5,
+ label: 'fifth'
+ }
+ ];
+ return this.widget = $('<div id="dummy">&nbsp;</div>').appendTo($('body')).collector({
+ options: this.choices
+ });
+ });
+ afterEach(function() {});
+ it("should add a search field to the widget", function() {
+ return expect(this.widget.collector('searcher')).toExist();
+ });
+ it("should add the searcher to the container", function() {
+ return expect(this.widget.collector('container')).toContain('input.collector-search');
+ });
+ it("should trigger a search event with the search value when the content of the search changes", function() {
+ var callback_value;
+ callback_value = null;
+ this.widget.bind('collectorsearch', function(event, value) {
+ return callback_value = value;
+ });
+ this.after(function() {
+ return this.widget.unbind('collectorsearch');
+ });
+ this.widget.collector('searcher').val('testSearch');
+ this.widget.collector('searcher').change();
+ return expect(callback_value).toEqual('testSearch');
+ });
+ it("should add the collector-filtered class to the widget container when searching for a value", function() {
+ expect(this.widget.collector('container')).not.toHaveClass('collector-filtered');
+ this.widget.collector('searcher').change();
+ return expect(this.widget.collector('container')).toHaveClass('collector-filtered');
+ });
+ it("should provide a manual search method", function() {
+ expect(this.widget.collector('container')).not.toHaveClass('collector-filtered');
+ this.widget.collector('search', 'something');
+ return expect(this.widget.collector('container')).toHaveClass('collector-filtered');
+ });
+ it("should provide an unfilter method", function() {
+ this.widget.collector('search', 'filter_text');
+ expect(this.widget.collector('container')).toHaveClass('collector-filtered');
+ this.widget.collector('unfilter');
+ return expect(this.widget.collector('container')).not.toHaveClass('collector-filtered');
+ });
+ it("should add a collector-filtered class to menu options who's label that don't match the search value", function() {
+ expect(this.widget.collector('menu').find('li:eq(0)')).not.toHaveClass('collector-filtered');
+ expect(this.widget.collector('menu').find('li:eq(1)')).not.toHaveClass('collector-filtered');
+ expect(this.widget.collector('menu').find('li:eq(2)')).not.toHaveClass('collector-filtered');
+ expect(this.widget.collector('menu').find('li:eq(3)')).not.toHaveClass('collector-filtered');
+ expect(this.widget.collector('menu').find('li:eq(4)')).not.toHaveClass('collector-filtered');
+ this.widget.collector('search', 'th');
+ expect(this.widget.collector('menu').find('li:eq(0)')).toHaveClass('collector-filtered');
+ expect(this.widget.collector('menu').find('li:eq(1)')).toHaveClass('collector-filtered');
+ expect(this.widget.collector('menu').find('li:eq(2)')).not.toHaveClass('collector-filtered');
+ expect(this.widget.collector('menu').find('li:eq(3)')).not.toHaveClass('collector-filtered');
+ return expect(this.widget.collector('menu').find('li:eq(4)')).not.toHaveClass('collector-filtered');
+ });
+ return it("should not render the search field by default", function() {
+ var widget2;
+ widget2 = $('<div id="dummy">&nbsp</div>').appendTo($('body')).collector({
+ options: [
+ {
+ value: 1,
+ label: 'one'
+ }, {
+ value: 2,
+ label: 'two'
+ }
+ ]
+ });
+ return expect(widget2.collector('searcher')).not.toExist();
+ });
+ });
+
}).call(this);
Please sign in to comment.
Something went wrong with that request. Please try again.