diff --git a/modules/core/file_fetcher.js b/modules/core/file_fetcher.js index a51b61c47..de0ba7d0d 100644 --- a/modules/core/file_fetcher.js +++ b/modules/core/file_fetcher.js @@ -1,4 +1,4 @@ -import { json as d3_json } from 'd3-fetch'; +import { utilFetchJson } from '../util/util'; let _mainFileFetcher = coreFileFetcher(); // singleton @@ -54,7 +54,7 @@ export function coreFileFetcher() { let prom = _inflight[url]; if (!prom) { - _inflight[url] = prom = d3_json(url) + _inflight[url] = prom = utilFetchJson(url) .then(result => { delete _inflight[url]; if (!result) { diff --git a/modules/util/index.js b/modules/util/index.js index d82208a15..683c476c2 100644 --- a/modules/util/index.js +++ b/modules/util/index.js @@ -26,6 +26,7 @@ export { utilEntitySelector } from './util'; export { utilEntityOrMemberSelector } from './util'; export { utilEntityOrDeepMemberSelector } from './util'; export { utilFastMouse } from './util'; +export { utilFetchJson } from './util'; export { utilFunctor } from './util'; export { utilGetAllNodes } from './util'; export { utilGetSetValue } from './get_set_value'; diff --git a/modules/util/util.js b/modules/util/util.js index 3865c560f..096d8d41c 100644 --- a/modules/util/util.js +++ b/modules/util/util.js @@ -572,3 +572,14 @@ export function utilUnicodeCharsCount(str) { export function utilUnicodeCharsTruncated(str, limit) { return Array.from(str).slice(0, limit).join(''); } + +// Variation of d3.json (https://github.com/d3/d3-fetch/blob/master/src/json.js) +export function utilFetchJson(resourse, init) { + return fetch(resourse, init) + .then((response) => { + // fetch in PhantomJS tests may return ok=false and status=0 even if it's okay + if ((!response.ok && response.status !== 0) || !response.json) throw new Error(response.status + ' ' + response.statusText); + if (response.status === 204 || response.status === 205) return; + return response.json(); + }); +} diff --git a/test/spec/spec_helpers.js b/test/spec/spec_helpers.js index 2f6898f0a..957334164 100644 --- a/test/spec/spec_helpers.js +++ b/test/spec/spec_helpers.js @@ -7,9 +7,16 @@ for (var k in iD.services) { delete iD.services[k]; } // Run without data for speed (tests which need data can set it up themselves) +iD.fileFetcher.assetPath('../dist/'); + // Initializing `coreContext` will try loading the locale data and English locale strings: -iD.fileFetcher.cache().locales = { en: { rtl: false, languageNames: {}, scriptNames: {} }}; -iD.fileFetcher.cache().locale_en = { en: {} }; +iD.fileFetcher.cache().locales = { en: { rtl: false, pct: 1}}; +iD.fileFetcher.cache().locales_index_tagging = { en: { rtl: false, pct: 1 } }; +iD.fileFetcher.cache().locale_tagging_en = { en: {} }; +iD.fileFetcher.cache().locales_index_general = { en: { rtl: false, pct: 1 } }; +// load the actual data for `iD.fileFetcher.cache().locale_general_en` +iD.localizer.loadLocale('en', 'general', 'locales'); + // Initializing `coreContext` initializes `_background`, which tries loading: iD.fileFetcher.cache().imagery = []; // Initializing `coreContext` initializes `_presets`, which tries loading: @@ -24,7 +31,6 @@ iD.fileFetcher.cache().nsi_filters = { discardNames: [] }; // Initializing `coreContext` initializes `_uploader`, which tries loading: iD.fileFetcher.cache().discarded = {}; - mocha.setup({ timeout: 5000, // 5 sec ui: 'bdd', diff --git a/test/spec/util/util.js b/test/spec/util/util.js index 29407d209..95f8e1c18 100644 --- a/test/spec/util/util.js +++ b/test/spec/util/util.js @@ -227,48 +227,29 @@ describe('iD.util', function() { }); describe('utilDisplayName', function() { - before(function() { - iD.fileFetcher.assetPath('dist/'); - }); - after(function() { - iD.fileFetcher.assetPath(''); - }); - it('returns the name if tagged with a name', function() { expect(iD.utilDisplayName({tags: {name: 'East Coast Greenway'}})).to.eql('East Coast Greenway'); }); - it('distinguishes unnamed features by ref', function(done) { - iD.localizer.loadLocale('en', 'general', 'locales').then(function() { - expect(iD.utilDisplayName({tags: {ref: '66'}})).to.eql('66'); - done(); - }); + it('distinguishes unnamed features by ref', function() { + expect(iD.utilDisplayName({tags: {ref: '66'}})).to.eql('66'); }); - it('distinguishes unnamed features by network or cycle_network', function(done) { - iD.localizer.loadLocale('en', 'general', 'locales').then(function() { - expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X'}})).to.eql('SORTA 3X'); - expect(iD.utilDisplayName({tags: {network: 'ncn', cycle_network: 'US:US', ref: '76'}})).to.eql('US:US 76'); - done(); - }); + it('distinguishes unnamed features by network or cycle_network', function() { + expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X'}})).to.eql('SORTA 3X'); + expect(iD.utilDisplayName({tags: {network: 'ncn', cycle_network: 'US:US', ref: '76'}})).to.eql('US:US 76'); }); - it('distinguishes unnamed routes by direction', function(done) { - iD.localizer.loadLocale('en', 'general', 'locales').then(function() { - expect(iD.utilDisplayName({tags: {network: 'US:US', ref: '66', direction: 'west', route: 'road'}})).to.eql('US:US 66 west'); - // Marguerite X: Counter-Clockwise - expect(iD.utilDisplayName({tags: {network: 'Marguerite', ref: 'X', direction: 'anticlockwise', route: 'bus'}})).to.eql('Marguerite X anticlockwise'); - done(); - }); + it('distinguishes unnamed routes by direction', function() { + expect(iD.utilDisplayName({tags: {network: 'US:US', ref: '66', direction: 'west', route: 'road'}})).to.eql('US:US 66 west'); + // Marguerite X: Counter-Clockwise + expect(iD.utilDisplayName({tags: {network: 'Marguerite', ref: 'X', direction: 'anticlockwise', route: 'bus'}})).to.eql('Marguerite X anticlockwise'); }); - it('distinguishes unnamed routes by waypoints', function(done) { - iD.localizer.loadLocale('en', 'general', 'locales').then(function() { - expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', from: 'Downtown', route: 'bus'}})).to.eql('SORTA 3X'); - expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', to: 'Kings Island', route: 'bus'}})).to.eql('SORTA 3X'); - expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', via: 'Montgomery', route: 'bus'}})).to.eql('SORTA 3X'); - // Green Line: Old Ironsides => Winchester - expect(iD.utilDisplayName({tags: {network: 'VTA', ref: 'Green', from: 'Old Ironsides', to: 'Winchester', route: 'bus'}})).to.eql('VTA Green from Old Ironsides to Winchester'); - // BART Yellow Line: Antioch => Pittsburg/Bay Point => SFO Airport => Millbrae - expect(iD.utilDisplayName({tags: {network: 'BART', ref: 'Yellow', from: 'Antioch', to: 'Millbrae', via: 'Pittsburg/Bay Point;San Francisco International Airport', route: 'subway'}})).to.eql('BART Yellow from Antioch to Millbrae via Pittsburg/Bay Point;San Francisco International Airport'); - done(); - }); + it('distinguishes unnamed routes by waypoints', function() { + expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', from: 'Downtown', route: 'bus'}})).to.eql('SORTA 3X'); + expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', to: 'Kings Island', route: 'bus'}})).to.eql('SORTA 3X'); + expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', via: 'Montgomery', route: 'bus'}})).to.eql('SORTA 3X'); + // Green Line: Old Ironsides => Winchester + expect(iD.utilDisplayName({tags: {network: 'VTA', ref: 'Green', from: 'Old Ironsides', to: 'Winchester', route: 'bus'}})).to.eql('VTA Green from Old Ironsides to Winchester'); + // BART Yellow Line: Antioch => Pittsburg/Bay Point => SFO Airport => Millbrae + expect(iD.utilDisplayName({tags: {network: 'BART', ref: 'Yellow', from: 'Antioch', to: 'Millbrae', via: 'Pittsburg/Bay Point;San Francisco International Airport', route: 'subway'}})).to.eql('BART Yellow from Antioch to Millbrae via Pittsburg/Bay Point;San Francisco International Airport'); }); }); });