require 'spec_helper' describe "Did You Mean" do before { visit root_path } describe "searching all fields" do it "should have suggestions" do fill_in "q", with: 'politica' click_button 'search' expect(page).to have_content("Did you mean") click_link 'policy' within ("#sortAndPerPage") do expect(page).to have_content "1 - 2 of 2" end end end describe "for a title search" do before { select 'Title', from: 'search_field' } it "should have suggestions" do # yehudiyam is one letter away from a title word fill_in "q", with: 'yehudiyam' click_button 'search' expect(page).to have_content("Did you mean") click_link 'yehudiyim' within ("#sortAndPerPage") do expect(page).to have_content "1 to 1 of 1" end within ("select#search_field") do expect(page).to have_selector("option[selected]", text: "Title") end end end describe "for an author search" do before { select 'Author', from: 'search_field' } it "should have suggestions" do # shirma is one letter away from an author word fill_in "q", with: 'shirma' click_button 'search' expect(page).to have_content("Did you mean") click_link 'sharma' within ("#sortAndPerPage") do expect(page).to have_content "1 to 1 of 1" end within ("select#search_field") do expect(page).to have_selector("option[selected]", text: "Author") end end end describe "for an subject search" do before { select 'Subject', from: 'search_field' } it "should have suggestions" do # wome is one letter away from an author word fill_in "q", with: 'wome' click_button 'search' expect(page).to have_content("Did you mean") click_link 'women' within ("#sortAndPerPage") do expect(page).to have_content "1 - 3 of 3" end within ("select#search_field") do expect(page).to have_selector("option[selected]", text: "Subject") end end end describe "a multiword query" do it "should not have suggestions if there are no matches" do fill_in "q", with: 'ooofda ooofda' click_button 'search' expect(page).to_not have_content("Did you mean") end it "should have separate suggestions" do fill_in "q", with: 'politica boo' click_button 'search' within(".suggest") do expect(page).to have_content("Did you mean") expect(page).to have_link('policy') expect(page).to have_link('bon') expect(page).not_to have_link('policy bon') end click_link 'bon' within ("#sortAndPerPage") do expect(page).to have_content "1 to 1 of 1" end end it "should ignore repeated terms" do fill_in "q", with: 'boo boo' click_button 'search' within(".suggest") do expect(page).to have_content("Did you mean") expect(page).to have_link('bon', count: 1) expect(page).not_to have_link('bon bon') end end end it "should show suggestions if there aren't many hits" do fill_in "q", with: 'ayaz' click_button 'search' expect(page).to have_content("Did you mean") click_link 'bya' within ("#sortAndPerPage") do expect(page).to have_content "1 - 3 of 3" end end it "should should not show suggestions if there are many results" do # histori gives 9 results in 30 record demo index fill_in "q", with: 'histori' click_button 'search' expect(page).to_not have_content("Did you mean") end it "should should show suggestions if at the threshold number" do # polit gives 5 results in 30 record demo index - 5 is default cutoff fill_in "q", with: 'polit' click_button 'search' expect(page).to have_content("Did you mean") end end