Fix OSM data refreshes not immediately rendering
This commit is contained in:
@@ -432,4 +432,65 @@ module('Unit | Service | osm', function (hooks) {
|
||||
'Uses relations.json endpoint'
|
||||
);
|
||||
});
|
||||
|
||||
test('fetchOsmObject with forceFresh bypasses the cache and returns fresh data', async function (assert) {
|
||||
let service = this.owner.lookup('service:osm');
|
||||
|
||||
// Seed both caches so we can prove forceFresh skips them.
|
||||
service.cachedPlaces.set('node:5', {
|
||||
data: { title: 'Stale In-Memory', lat: 1, lon: 1 },
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
await service.localForage.set('osm-cache', 'node:5', {
|
||||
data: { title: 'Stale IndexedDB', lat: 1, lon: 1 },
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
service.fetchWithRetry = async () => ({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
elements: [
|
||||
{
|
||||
id: 5,
|
||||
type: 'node',
|
||||
lat: 2,
|
||||
lon: 3,
|
||||
tags: { name: 'Fresh From API' },
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await service.fetchOsmObject('5', 'node', {
|
||||
forceFresh: true,
|
||||
});
|
||||
|
||||
assert.strictEqual(
|
||||
result.title,
|
||||
'Fresh From API',
|
||||
'Returns fresh API data, not cached data'
|
||||
);
|
||||
assert.strictEqual(result.lat, 2);
|
||||
assert.strictEqual(result.lon, 3);
|
||||
});
|
||||
|
||||
test('fetchOsmObject without forceFresh returns the in-memory cache entry without hitting the API', async function (assert) {
|
||||
let service = this.owner.lookup('service:osm');
|
||||
|
||||
service.cachedPlaces.set('node:6', {
|
||||
data: { title: 'Warm In-Memory', lat: 4, lon: 5 },
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
let fetchCalled = 0;
|
||||
service.fetchWithRetry = async () => {
|
||||
fetchCalled++;
|
||||
return { ok: true, json: async () => ({ elements: [] }) };
|
||||
};
|
||||
|
||||
const result = await service.fetchOsmObject('6', 'node');
|
||||
|
||||
assert.strictEqual(fetchCalled, 0, 'API was not hit');
|
||||
assert.strictEqual(result.title, 'Warm In-Memory');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,8 +15,10 @@ module('Unit | Service | storage', function (hooks) {
|
||||
let service = this.owner.lookup('service:storage');
|
||||
|
||||
// Stub OSM Service
|
||||
let capturedOptions;
|
||||
class OsmStub extends Service {
|
||||
async fetchOsmObject(id, type) {
|
||||
async fetchOsmObject(id, type, options) {
|
||||
capturedOptions = options;
|
||||
return {
|
||||
osmId: id,
|
||||
osmType: type,
|
||||
@@ -49,6 +51,10 @@ module('Unit | Service | storage', function (hooks) {
|
||||
|
||||
assert.ok(updatePlaceCalled, 'updatePlace should be called');
|
||||
assert.strictEqual(result.lat, 52.5201, 'Latitude updated');
|
||||
assert.ok(
|
||||
capturedOptions?.forceFresh,
|
||||
'refreshPlace fetches fresh OSM data (forceFresh: true)'
|
||||
);
|
||||
});
|
||||
|
||||
test('refreshPlace ignores tiny coordinate drift', async function (assert) {
|
||||
|
||||
Reference in New Issue
Block a user