This refactors the autocomplete Javascript to use a single dedicated /autocomplete.json endpoint instead of a bunch of separate endpoints. This simplifies the autocomplete Javascript by making it so that instead of calling a different endpoint for each type of query (for users, wiki pages, pools, artists, etc), then having to parse the results of each call to get the data we need, we can call a single endpoint that returns exactly what we need. This also means we don't have to parse searches clientside in order to autocomplete metatags. Instead we can just pass the search term to the server and let it parse the search, which is easy to do serverside. Finally, this makes autocomplete easier to test, and it makes it easier to add more sophisticated autocomplete behavior, since most of the logic lives serverside.
40 lines
1.3 KiB
Ruby
40 lines
1.3 KiB
Ruby
require "test_helper"
|
|
|
|
class AutocompleteControllerTest < ActionDispatch::IntegrationTest
|
|
def autocomplete(query, type)
|
|
get autocomplete_index_path(search: { query: query, type: type }), as: :json
|
|
assert_response :success
|
|
|
|
response.parsed_body.map { |result| result["value"] }
|
|
end
|
|
|
|
def assert_autocomplete_equals(expected_value, query, type)
|
|
assert_equal(expected_value, autocomplete(query, type))
|
|
end
|
|
|
|
context "Autocomplete controller" do
|
|
context "index action" do
|
|
setup do
|
|
create(:tag, name: "azur_lane")
|
|
end
|
|
|
|
should "work for opensearch queries" do
|
|
get autocomplete_index_path(search: { query: "azur", type: "opensearch" }), as: :json
|
|
|
|
assert_response :success
|
|
assert_equal(["azur", ["azur_lane"]], response.parsed_body)
|
|
end
|
|
|
|
should "work for tag queries" do
|
|
assert_autocomplete_equals(["azur_lane"], "azur", "tag_query")
|
|
assert_autocomplete_equals(["azur_lane"], "-azur", "tag_query")
|
|
assert_autocomplete_equals(["azur_lane"], "~azur", "tag_query")
|
|
assert_autocomplete_equals(["azur_lane"], "AZUR", "tag_query")
|
|
|
|
assert_autocomplete_equals(["rating:safe"], "rating:s", "tag_query")
|
|
assert_autocomplete_equals(["rating:safe"], "-rating:s", "tag_query")
|
|
end
|
|
end
|
|
end
|
|
end
|