Skip to content

Commit 13ddeec

Browse files
committed
Fix code tests for #8276 for real this time
Add utilFetchJson to get around some quirks of d3.json and use it for coreFileFetcher Load real general English locale strings at the beginning of code tests
1 parent 997b453 commit 13ddeec

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed

modules/core/file_fetcher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { json as d3_json } from 'd3-fetch';
1+
import { utilFetchJson } from '../util/util';
22

33
let _mainFileFetcher = coreFileFetcher(); // singleton
44

@@ -54,7 +54,7 @@ export function coreFileFetcher() {
5454

5555
let prom = _inflight[url];
5656
if (!prom) {
57-
_inflight[url] = prom = d3_json(url)
57+
_inflight[url] = prom = utilFetchJson(url)
5858
.then(result => {
5959
delete _inflight[url];
6060
if (!result) {

modules/util/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export { utilEntitySelector } from './util';
2626
export { utilEntityOrMemberSelector } from './util';
2727
export { utilEntityOrDeepMemberSelector } from './util';
2828
export { utilFastMouse } from './util';
29+
export { utilFetchJson } from './util';
2930
export { utilFunctor } from './util';
3031
export { utilGetAllNodes } from './util';
3132
export { utilGetSetValue } from './get_set_value';

modules/util/util.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,14 @@ export function utilUnicodeCharsCount(str) {
572572
export function utilUnicodeCharsTruncated(str, limit) {
573573
return Array.from(str).slice(0, limit).join('');
574574
}
575+
576+
// Variation of d3.json (https://github.com/d3/d3-fetch/blob/master/src/json.js)
577+
export function utilFetchJson(resourse, init) {
578+
return fetch(resourse, init)
579+
.then((response) => {
580+
// fetch in PhantomJS tests may return ok=false and status=0 even if it's okay
581+
if ((!response.ok && response.status !== 0) || !response.json) throw new Error(response.status + ' ' + response.statusText);
582+
if (response.status === 204 || response.status === 205) return;
583+
return response.json();
584+
});
585+
}

test/spec/spec_helpers.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ for (var k in iD.services) { delete iD.services[k]; }
77

88
// Run without data for speed (tests which need data can set it up themselves)
99

10+
iD.fileFetcher.assetPath('../dist/');
11+
1012
// Initializing `coreContext` will try loading the locale data and English locale strings:
11-
iD.fileFetcher.cache().locales = { en: { rtl: false, languageNames: {}, scriptNames: {} }};
12-
iD.fileFetcher.cache().locale_en = { en: {} };
13+
iD.fileFetcher.cache().locales = { en: { rtl: false, pct: 1}};
14+
iD.fileFetcher.cache().locales_index_tagging = { en: { rtl: false, pct: 1 } };
15+
iD.fileFetcher.cache().locale_tagging_en = { en: {} };
16+
iD.fileFetcher.cache().locales_index_general = { en: { rtl: false, pct: 1 } };
17+
// load the actual data for `iD.fileFetcher.cache().locale_general_en`
18+
iD.localizer.loadLocale('en', 'general', 'locales');
19+
1320
// Initializing `coreContext` initializes `_background`, which tries loading:
1421
iD.fileFetcher.cache().imagery = [];
1522
// Initializing `coreContext` initializes `_presets`, which tries loading:
@@ -24,7 +31,6 @@ iD.fileFetcher.cache().nsi_filters = { discardNames: [] };
2431
// Initializing `coreContext` initializes `_uploader`, which tries loading:
2532
iD.fileFetcher.cache().discarded = {};
2633

27-
2834
mocha.setup({
2935
timeout: 5000, // 5 sec
3036
ui: 'bdd',

test/spec/util/util.js

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -227,48 +227,29 @@ describe('iD.util', function() {
227227
});
228228

229229
describe('utilDisplayName', function() {
230-
before(function() {
231-
iD.fileFetcher.assetPath('dist/');
232-
});
233-
after(function() {
234-
iD.fileFetcher.assetPath('');
235-
});
236-
237230
it('returns the name if tagged with a name', function() {
238231
expect(iD.utilDisplayName({tags: {name: 'East Coast Greenway'}})).to.eql('East Coast Greenway');
239232
});
240-
it('distinguishes unnamed features by ref', function(done) {
241-
iD.localizer.loadLocale('en', 'general', 'locales').then(function() {
242-
expect(iD.utilDisplayName({tags: {ref: '66'}})).to.eql('66');
243-
done();
244-
});
245-
});
246-
it('distinguishes unnamed features by network or cycle_network', function(done) {
247-
iD.localizer.loadLocale('en', 'general', 'locales').then(function() {
248-
expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X'}})).to.eql('SORTA 3X');
249-
expect(iD.utilDisplayName({tags: {network: 'ncn', cycle_network: 'US:US', ref: '76'}})).to.eql('US:US 76');
250-
done();
251-
});
252-
});
253-
it('distinguishes unnamed routes by direction', function(done) {
254-
iD.localizer.loadLocale('en', 'general', 'locales').then(function() {
255-
expect(iD.utilDisplayName({tags: {network: 'US:US', ref: '66', direction: 'west', route: 'road'}})).to.eql('US:US 66 west');
256-
// Marguerite X: Counter-Clockwise
257-
expect(iD.utilDisplayName({tags: {network: 'Marguerite', ref: 'X', direction: 'anticlockwise', route: 'bus'}})).to.eql('Marguerite X anticlockwise');
258-
done();
259-
});
260-
});
261-
it('distinguishes unnamed routes by waypoints', function(done) {
262-
iD.localizer.loadLocale('en', 'general', 'locales').then(function() {
263-
expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', from: 'Downtown', route: 'bus'}})).to.eql('SORTA 3X');
264-
expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', to: 'Kings Island', route: 'bus'}})).to.eql('SORTA 3X');
265-
expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', via: 'Montgomery', route: 'bus'}})).to.eql('SORTA 3X');
266-
// Green Line: Old Ironsides => Winchester
267-
expect(iD.utilDisplayName({tags: {network: 'VTA', ref: 'Green', from: 'Old Ironsides', to: 'Winchester', route: 'bus'}})).to.eql('VTA Green from Old Ironsides to Winchester');
268-
// BART Yellow Line: Antioch => Pittsburg/Bay Point => SFO Airport => Millbrae
269-
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');
270-
done();
271-
});
233+
it('distinguishes unnamed features by ref', function() {
234+
expect(iD.utilDisplayName({tags: {ref: '66'}})).to.eql('66');
235+
});
236+
it('distinguishes unnamed features by network or cycle_network', function() {
237+
expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X'}})).to.eql('SORTA 3X');
238+
expect(iD.utilDisplayName({tags: {network: 'ncn', cycle_network: 'US:US', ref: '76'}})).to.eql('US:US 76');
239+
});
240+
it('distinguishes unnamed routes by direction', function() {
241+
expect(iD.utilDisplayName({tags: {network: 'US:US', ref: '66', direction: 'west', route: 'road'}})).to.eql('US:US 66 west');
242+
// Marguerite X: Counter-Clockwise
243+
expect(iD.utilDisplayName({tags: {network: 'Marguerite', ref: 'X', direction: 'anticlockwise', route: 'bus'}})).to.eql('Marguerite X anticlockwise');
244+
});
245+
it('distinguishes unnamed routes by waypoints', function() {
246+
expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', from: 'Downtown', route: 'bus'}})).to.eql('SORTA 3X');
247+
expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', to: 'Kings Island', route: 'bus'}})).to.eql('SORTA 3X');
248+
expect(iD.utilDisplayName({tags: {network: 'SORTA', ref: '3X', via: 'Montgomery', route: 'bus'}})).to.eql('SORTA 3X');
249+
// Green Line: Old Ironsides => Winchester
250+
expect(iD.utilDisplayName({tags: {network: 'VTA', ref: 'Green', from: 'Old Ironsides', to: 'Winchester', route: 'bus'}})).to.eql('VTA Green from Old Ironsides to Winchester');
251+
// BART Yellow Line: Antioch => Pittsburg/Bay Point => SFO Airport => Millbrae
252+
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');
272253
});
273254
});
274255
});

0 commit comments

Comments
 (0)