|
| 1 | +module Rails3JQueryAutocomplete |
| 2 | + module Test |
| 3 | + |
| 4 | + include Shoulda::InstanceMethods |
| 5 | + extend Shoulda::ClassMethods |
| 6 | + include Shoulda::Assertions |
| 7 | + extend Shoulda::Macros |
| 8 | + include Shoulda::Helpers |
| 9 | + |
| 10 | + context "the autocomplete gem" do |
| 11 | + |
| 12 | + should "be able to access the autocomplete action regardless of the quality of param[:term]" do |
| 13 | + get :autocomplete_movie_name |
| 14 | + assert_response :success |
| 15 | + |
| 16 | + get :autocomplete_movie_name, :term => '' |
| 17 | + assert_response :success |
| 18 | + |
| 19 | + get :autocomplete_movie_name, :term => nil |
| 20 | + assert_response :success |
| 21 | + |
| 22 | + get :autocomplete_movie_name, :term => 'Al' |
| 23 | + assert_response :success |
| 24 | + end |
| 25 | + |
| 26 | + should "respond with expected json" do |
| 27 | + get :autocomplete_movie_name, :term => 'Al' |
| 28 | + json_response = JSON.parse(@response.body) |
| 29 | + assert_equal(json_response.first["label"], @movie1.name) |
| 30 | + assert_equal(json_response.first["value"], @movie1.name) |
| 31 | + assert_equal(json_response.first["id"].to_s, @movie1.id.to_s) |
| 32 | + end |
| 33 | + |
| 34 | + should "return results in alphabetical order by default" do |
| 35 | + get :autocomplete_movie_name, :term => 'Al' |
| 36 | + json_response = JSON.parse(@response.body) |
| 37 | + assert_equal(json_response.first["label"], "Alpha") |
| 38 | + assert_equal(json_response.last["label"], "Alzpha") |
| 39 | + end |
| 40 | + |
| 41 | + should "be able to sort in other ways if desired" do |
| 42 | + @controller.class_eval do |
| 43 | + autocomplete :movie, :name, {:order => "name DESC"} |
| 44 | + end |
| 45 | + |
| 46 | + get :autocomplete_movie_name, :term => 'Al' |
| 47 | + json_response = JSON.parse(@response.body) |
| 48 | + assert_equal(json_response.first["label"], "Alzpha") |
| 49 | + assert_equal(json_response.last["label"], "Alpha") |
| 50 | + end |
| 51 | + |
| 52 | + should "be able to limit the results" do |
| 53 | + @controller.class_eval do |
| 54 | + autocomplete :movie, :name, {:limit => 1} |
| 55 | + end |
| 56 | + |
| 57 | + get :autocomplete_movie_name, :term => 'Al' |
| 58 | + json_response = JSON.parse(@response.body) |
| 59 | + assert_equal(json_response.length, 1) |
| 60 | + end |
| 61 | + |
| 62 | + should "ignore case of search term and results" do |
| 63 | + @movie = @movie_class.create(:name => 'aLpHa') |
| 64 | + |
| 65 | + @controller.class_eval do |
| 66 | + autocomplete :movie, :name |
| 67 | + end |
| 68 | + |
| 69 | + get :autocomplete_movie_name, :term => 'Al' |
| 70 | + json_response = JSON.parse(@response.body) |
| 71 | + assert_equal(json_response.length, @movie_class.count) |
| 72 | + assert_equal(json_response.last["label"], 'aLpHa') |
| 73 | + end |
| 74 | + |
| 75 | + should "match term to letters in middle of words when full-text search is on" do |
| 76 | + @controller.class_eval do |
| 77 | + autocomplete :movie, :name, {:full => true} |
| 78 | + end |
| 79 | + |
| 80 | + get :autocomplete_movie_name, :term => 'ph' |
| 81 | + json_response = JSON.parse(@response.body) |
| 82 | + assert_equal(json_response.length, @movie_class.count) |
| 83 | + assert_equal(json_response.first["label"], @movie1.name) |
| 84 | + end |
| 85 | + |
| 86 | + should "be able to customize what is displayed" do |
| 87 | + @controller.class_eval do |
| 88 | + autocomplete :movie, :name, {:display_value => :display_name} |
| 89 | + end |
| 90 | + |
| 91 | + get :autocomplete_movie_name, :term => 'Al' |
| 92 | + |
| 93 | + json_response = JSON.parse(@response.body) |
| 94 | + |
| 95 | + assert_equal(@movie1.display_name, json_response.first["label"]) |
| 96 | + assert_equal(@movie1.display_name, json_response.first["value"]) |
| 97 | + assert_equal(@movie1.id.to_s, json_response.first["id"].to_s) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments