diff --git a/app/assets/javascripts/discourse/lib/search.js.es6 b/app/assets/javascripts/discourse/lib/search.js.es6 index b0929ca60..348fe0f19 100644 --- a/app/assets/javascripts/discourse/lib/search.js.es6 +++ b/app/assets/javascripts/discourse/lib/search.js.es6 @@ -41,24 +41,26 @@ export function translateResults(results, opts) { results.resultTypes = []; // TODO: consider refactoring front end to take a better structure - [['topic','posts'],['user','users'],['category','categories']].forEach(function(pair){ - const type = pair[0], name = pair[1]; - if (results[name].length > 0) { - var result = { - results: results[name], - componentName: "search-result-" + ((opts.searchContext && opts.searchContext.type === 'topic' && type === 'topic') ? 'post' : type), - type, - more: r['more_' + name] - }; + if (r) { + [['topic','posts'],['user','users'],['category','categories']].forEach(function(pair){ + const type = pair[0], name = pair[1]; + if (results[name].length > 0) { + var result = { + results: results[name], + componentName: "search-result-" + ((opts.searchContext && opts.searchContext.type === 'topic' && type === 'topic') ? 'post' : type), + type, + more: r['more_' + name] + }; - if (result.more && name === "posts" && opts.fullSearchUrl) { - result.more = false; - result.moreUrl = opts.fullSearchUrl; + if (result.more && name === "posts" && opts.fullSearchUrl) { + result.more = false; + result.moreUrl = opts.fullSearchUrl; + } + + results.resultTypes.push(result); } - - results.resultTypes.push(result); - } - }); + }); + } const noResults = !!(results.topics.length === 0 && results.posts.length === 0 && diff --git a/test/javascripts/acceptance/search-full-test.js.es6 b/test/javascripts/acceptance/search-full-test.js.es6 index 96320ff0c..005ea036f 100644 --- a/test/javascripts/acceptance/search-full-test.js.es6 +++ b/test/javascripts/acceptance/search-full-test.js.es6 @@ -1,12 +1,21 @@ import { acceptance } from "helpers/qunit-helpers"; acceptance("Search - Full Page"); -// TODO: needs fixing (cc @sam) -// test("search", (assert) => { -// visit("/search?q=trout"); +test("perform various searches", assert => { + visit("/search"); -// andThen(() => { -// assert.ok(find('input.search').length > 0); -// assert.ok(find('.topic-list-item').length > 0); -// }); -// }); + andThen(() => { + assert.ok(find('input.search').length > 0); + assert.ok(find('.topic').length === 0); + }); + + fillIn('.search input', 'none'); + click('.search .btn-primary'); + + andThen(() => assert.ok(find('.topic').length === 0), 'has no results'); + + fillIn('.search input', 'posts'); + click('.search .btn-primary'); + + andThen(() => assert.ok(find('.topic').length === 1, 'has one post')); +}); diff --git a/test/javascripts/helpers/create-pretender.js.es6 b/test/javascripts/helpers/create-pretender.js.es6 index fc2c537c1..b1e340058 100644 --- a/test/javascripts/helpers/create-pretender.js.es6 +++ b/test/javascripts/helpers/create-pretender.js.es6 @@ -78,6 +78,19 @@ export default function() { this.get('/clicks/track', success); + this.get('/search', request => { + if (request.queryParams.q === 'posts') { + return response({ + posts: [{ + id: 1234 + }] + }); + } + + return response({}); + }); + + this.put('/users/eviltrout', () => response({ user: {} })); this.get("/t/280.json", () => response(fixturesByUrl['/t/280/1.json']));