Add full-text search
Add a search box with a quick results popover, as well full results in the sidebar on pressing enter.
This commit is contained in:
147
tests/acceptance/search-test.js
Normal file
147
tests/acceptance/search-test.js
Normal file
@@ -0,0 +1,147 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { visit, currentURL } from '@ember/test-helpers';
|
||||
import { setupApplicationTest } from 'marco/tests/helpers';
|
||||
import Service from '@ember/service';
|
||||
|
||||
module('Acceptance | search', function (hooks) {
|
||||
setupApplicationTest(hooks);
|
||||
|
||||
test('visiting /search with q parameter performs text search', async function (assert) {
|
||||
// Mock Photon Service
|
||||
class MockPhotonService extends Service {
|
||||
async search(query) {
|
||||
if (query === 'Berlin') {
|
||||
return [
|
||||
{
|
||||
title: 'Berlin',
|
||||
lat: 52.52,
|
||||
lon: 13.405,
|
||||
osmId: '123',
|
||||
osmType: 'R',
|
||||
description: 'City in Germany',
|
||||
},
|
||||
{
|
||||
title: 'Berlin Alexanderplatz',
|
||||
lat: 52.521,
|
||||
lon: 13.41,
|
||||
osmId: '456',
|
||||
osmType: 'N',
|
||||
description: 'Square in Berlin',
|
||||
},
|
||||
];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
this.owner.register('service:photon', MockPhotonService);
|
||||
|
||||
// Mock Storage Service (empty)
|
||||
class MockStorageService extends Service {
|
||||
savedPlaces = [];
|
||||
findPlaceById() {
|
||||
return null;
|
||||
}
|
||||
rs = {
|
||||
on: () => {},
|
||||
};
|
||||
// Add placesInView since map component accesses it
|
||||
placesInView = [];
|
||||
loadPlacesInBounds() {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
this.owner.register('service:storage', MockStorageService);
|
||||
|
||||
await visit('/search?q=Berlin');
|
||||
|
||||
assert.strictEqual(currentURL(), '/search?q=Berlin');
|
||||
assert.dom('.places-list li').exists({ count: 2 });
|
||||
assert.dom('.places-list li:first-child .place-name').hasText('Berlin');
|
||||
});
|
||||
|
||||
test('visiting /search with lat/lon performs nearby search', async function (assert) {
|
||||
// Mock Osm Service
|
||||
class MockOsmService extends Service {
|
||||
async getNearbyPois() {
|
||||
return [
|
||||
{
|
||||
title: 'Nearby Cafe',
|
||||
lat: 52.521,
|
||||
lon: 13.406,
|
||||
osmId: '789',
|
||||
osmType: 'N',
|
||||
_distance: 100, // Pre-calculated or ignored if mocked
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
this.owner.register('service:osm', MockOsmService);
|
||||
|
||||
// Mock Storage Service (empty)
|
||||
class MockStorageService extends Service {
|
||||
savedPlaces = [];
|
||||
findPlaceById() {
|
||||
return null;
|
||||
}
|
||||
rs = {
|
||||
on: () => {},
|
||||
};
|
||||
// Add placesInView since map component accesses it
|
||||
placesInView = [];
|
||||
loadPlacesInBounds() {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
this.owner.register('service:storage', MockStorageService);
|
||||
|
||||
await visit('/search?lat=52.52&lon=13.405');
|
||||
|
||||
assert.strictEqual(currentURL(), '/search?lat=52.52&lon=13.405');
|
||||
assert.dom('.places-list li').exists({ count: 1 });
|
||||
assert.dom('.places-list li .place-name').hasText('Nearby Cafe');
|
||||
});
|
||||
|
||||
test('local bookmarks are merged into search results', async function (assert) {
|
||||
// Mock Photon Service
|
||||
class MockPhotonService extends Service {
|
||||
async search() {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
this.owner.register('service:photon', MockPhotonService);
|
||||
|
||||
// Mock Storage Service with a bookmark
|
||||
class MockStorageService extends Service {
|
||||
savedPlaces = [
|
||||
{
|
||||
title: 'My Secret Base',
|
||||
lat: 50.0,
|
||||
lon: 10.0,
|
||||
osmId: '999',
|
||||
osmType: 'N',
|
||||
description: 'Top Secret',
|
||||
},
|
||||
];
|
||||
findPlaceById(id) {
|
||||
if (id === '999') return this.savedPlaces[0];
|
||||
return null;
|
||||
}
|
||||
rs = {
|
||||
on: () => {},
|
||||
};
|
||||
placesInView = [];
|
||||
loadPlacesInBounds() {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
this.owner.register('service:storage', MockStorageService);
|
||||
|
||||
await visit('/search?q=Secret');
|
||||
|
||||
assert.strictEqual(currentURL(), '/search?q=Secret');
|
||||
assert.dom('.places-list li').exists({ count: 1 });
|
||||
assert.dom('.places-list li .place-name').hasText('My Secret Base');
|
||||
});
|
||||
});
|
||||
19
tests/integration/components/app-header-test.gjs
Normal file
19
tests/integration/components/app-header-test.gjs
Normal file
@@ -0,0 +1,19 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'marco/tests/helpers';
|
||||
import { render } from '@ember/test-helpers';
|
||||
import AppHeader from 'marco/components/app-header';
|
||||
|
||||
module('Integration | Component | app-header', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test('it renders the search box', async function (assert) {
|
||||
this.noop = () => {};
|
||||
await render(
|
||||
<template><AppHeader @onToggleMenu={{this.noop}} /></template>
|
||||
);
|
||||
|
||||
assert.dom('header.app-header').exists();
|
||||
assert.dom('.search-box').exists('Search box is present in the header');
|
||||
assert.dom('.menu-btn').exists('Menu button is present');
|
||||
});
|
||||
});
|
||||
37
tests/integration/components/place-details-test.gjs
Normal file
37
tests/integration/components/place-details-test.gjs
Normal file
@@ -0,0 +1,37 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'marco/tests/helpers';
|
||||
import { render } from '@ember/test-helpers';
|
||||
import PlaceDetails from 'marco/components/place-details';
|
||||
|
||||
module('Integration | Component | place-details', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test('it formats coordinates correctly', async function (assert) {
|
||||
const place = {
|
||||
title: 'Test Place',
|
||||
lat: 52.520006789,
|
||||
lon: 13.404954123,
|
||||
description: 'A place for testing.',
|
||||
};
|
||||
|
||||
await render(<template><PlaceDetails @place={{place}} /></template>);
|
||||
|
||||
assert.dom('.place-details').exists();
|
||||
assert.dom('.place-details h3').hasText('Test Place');
|
||||
|
||||
// Check for the formatted coordinates link text
|
||||
// "52.520007, 13.404954" (rounded)
|
||||
assert.dom('.meta-info a[href*="geo:"]').hasText('52.520007, 13.404954');
|
||||
});
|
||||
|
||||
test('it handles missing coordinates gracefully', async function (assert) {
|
||||
const place = {
|
||||
title: 'Place without Coords',
|
||||
};
|
||||
|
||||
await render(<template><PlaceDetails @place={{place}} /></template>);
|
||||
|
||||
assert.dom('.place-details h3').hasText('Place without Coords');
|
||||
assert.dom('.meta-info a[href*="geo:"]').doesNotExist();
|
||||
});
|
||||
});
|
||||
128
tests/integration/components/search-box-test.gjs
Normal file
128
tests/integration/components/search-box-test.gjs
Normal file
@@ -0,0 +1,128 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'marco/tests/helpers';
|
||||
import { render, fillIn, click, waitFor } from '@ember/test-helpers';
|
||||
import SearchBox from 'marco/components/search-box';
|
||||
import Service from '@ember/service';
|
||||
|
||||
module('Integration | Component | search-box', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test('it renders and handles search input', async function (assert) {
|
||||
// Mock Photon Service
|
||||
class MockPhotonService extends Service {
|
||||
async search(query) {
|
||||
if (query === 'test') {
|
||||
return [
|
||||
{
|
||||
title: 'Test Place',
|
||||
description: 'A test description',
|
||||
lat: 10,
|
||||
lon: 20,
|
||||
osmId: '123',
|
||||
osmType: 'node',
|
||||
},
|
||||
];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
this.owner.register('service:photon', MockPhotonService);
|
||||
|
||||
// Mock Router Service
|
||||
class MockRouterService extends Service {
|
||||
transitionTo(routeName, ...args) {
|
||||
assert.step(`transitionTo: ${routeName} ${JSON.stringify(args)}`);
|
||||
}
|
||||
}
|
||||
this.owner.register('service:router', MockRouterService);
|
||||
|
||||
await render(<template><SearchBox /></template>);
|
||||
|
||||
assert.dom('.search-input').exists();
|
||||
assert.dom('.search-results-popover').doesNotExist();
|
||||
|
||||
// Type 'test'
|
||||
await fillIn('.search-input', 'test');
|
||||
|
||||
// Wait for debounce and async search
|
||||
await waitFor('.search-results-popover', { timeout: 2000 });
|
||||
|
||||
assert.dom('.search-result-item').exists({ count: 1 });
|
||||
assert.dom('.result-title').hasText('Test Place');
|
||||
assert.dom('.result-desc').hasText('A test description');
|
||||
|
||||
// Click result
|
||||
await click('.search-result-item');
|
||||
|
||||
assert.verifySteps(['transitionTo: place ["osm:node:123"]']);
|
||||
assert
|
||||
.dom('.search-results-popover')
|
||||
.doesNotExist('Popover closes after selection');
|
||||
});
|
||||
|
||||
test('it handles submit for full search', async function (assert) {
|
||||
// Mock Photon Service
|
||||
class MockPhotonService extends Service {
|
||||
async search() {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
this.owner.register('service:photon', MockPhotonService);
|
||||
|
||||
// Mock MapUi Service
|
||||
class MockMapUiService extends Service {
|
||||
currentCenter = { lat: 52.52, lon: 13.405 };
|
||||
}
|
||||
this.owner.register('service:map-ui', MockMapUiService);
|
||||
|
||||
// Mock Router Service
|
||||
class MockRouterService extends Service {
|
||||
transitionTo(routeName, options) {
|
||||
assert.step(
|
||||
`transitionTo: ${routeName} ${JSON.stringify(options)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
this.owner.register('service:router', MockRouterService);
|
||||
|
||||
await render(<template><SearchBox /></template>);
|
||||
|
||||
await fillIn('.search-input', 'berlin');
|
||||
await click('.search-input'); // Focus
|
||||
// Trigger submit event on the form
|
||||
await this.element
|
||||
.querySelector('form')
|
||||
.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));
|
||||
|
||||
assert.verifySteps([
|
||||
'transitionTo: search {"queryParams":{"q":"berlin","selected":null,"lat":"52.5200","lon":"13.4050"}}',
|
||||
]);
|
||||
});
|
||||
|
||||
test('it uses map center for biased search', async function (assert) {
|
||||
// Mock MapUi Service
|
||||
class MockMapUiService extends Service {
|
||||
currentCenter = { lat: 52.52, lon: 13.405 };
|
||||
}
|
||||
this.owner.register('service:map-ui', MockMapUiService);
|
||||
|
||||
// Mock Photon Service
|
||||
class MockPhotonService extends Service {
|
||||
async search(query, lat, lon) {
|
||||
assert.step(`search: ${query}, ${lat}, ${lon}`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
this.owner.register('service:photon', MockPhotonService);
|
||||
|
||||
await render(<template><SearchBox /></template>);
|
||||
|
||||
await fillIn('.search-input', 'cafe');
|
||||
|
||||
// Wait for debounce (300ms) + execution
|
||||
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
await delay(400);
|
||||
|
||||
assert.verifySteps(['search: cafe, 52.52, 13.405']);
|
||||
});
|
||||
});
|
||||
@@ -9,6 +9,34 @@ module('Unit | Service | photon', function (hooks) {
|
||||
assert.ok(service);
|
||||
});
|
||||
|
||||
test('search truncates coordinates to 4 decimal places', async function (assert) {
|
||||
let service = this.owner.lookup('service:photon');
|
||||
const originalFetch = window.fetch;
|
||||
|
||||
let capturedUrl;
|
||||
window.fetch = async (url) => {
|
||||
capturedUrl = url;
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({ features: [] }),
|
||||
};
|
||||
};
|
||||
|
||||
try {
|
||||
await service.search('Test', 52.123456, 13.987654);
|
||||
assert.ok(
|
||||
capturedUrl.includes('lat=52.1235'),
|
||||
'lat is rounded to 4 decimals'
|
||||
);
|
||||
assert.ok(
|
||||
capturedUrl.includes('lon=13.9877'),
|
||||
'lon is rounded to 4 decimals'
|
||||
);
|
||||
} finally {
|
||||
window.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test('search handles successful response', async function (assert) {
|
||||
let service = this.owner.lookup('service:photon');
|
||||
|
||||
@@ -43,6 +71,7 @@ module('Unit | Service | photon', function (hooks) {
|
||||
assert.strictEqual(results[0].lat, 52.5);
|
||||
assert.strictEqual(results[0].lon, 13.4);
|
||||
assert.strictEqual(results[0].description, 'Test City, Test Country');
|
||||
assert.strictEqual(results[0].osmType, 'node', 'Normalizes N to node');
|
||||
} finally {
|
||||
window.fetch = originalFetch;
|
||||
}
|
||||
@@ -87,4 +116,22 @@ module('Unit | Service | photon', function (hooks) {
|
||||
assert.strictEqual(result.lat, 20);
|
||||
assert.strictEqual(result.lon, 10);
|
||||
});
|
||||
|
||||
test('normalizeFeature normalizes OSM types correctly', function (assert) {
|
||||
let service = this.owner.lookup('service:photon');
|
||||
|
||||
const checkType = (input, expected) => {
|
||||
const feature = {
|
||||
properties: { osm_type: input, name: 'Test' },
|
||||
geometry: { coordinates: [0, 0] },
|
||||
};
|
||||
const result = service.normalizeFeature(feature);
|
||||
assert.strictEqual(result.osmType, expected, `${input} -> ${expected}`);
|
||||
};
|
||||
|
||||
checkType('N', 'node');
|
||||
checkType('W', 'way');
|
||||
checkType('R', 'relation');
|
||||
checkType('unknown', 'unknown'); // Fallback
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user