Skip to content

Commit fe99c5e

Browse files
authored
Map - add test on map ready callback and route adding fail
1 parent 050495e commit fe99c5e

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

packages/devextreme/js/__internal/ui/map/m_provider.dynamic.azure.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class AzureProvider extends DynamicProvider {
117117
return new Promise<void>((resolve) => {
118118
if (azureMapsLoaded()) {
119119
resolve();
120+
return;
120121
}
121122

122123
if (!azureMapsLoader) {

packages/devextreme/testing/helpers/forMap/azureMock.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
}
2323

2424
if(eventName === 'ready') {
25-
targetOrCallback();
25+
atlas.readyCallbackCalled = true;
26+
27+
if(!window.postponeMapReadyPromise) {
28+
callbackFun();
29+
} else {
30+
atlas.mapReadyResolve = callbackFun;
31+
}
2632
}
2733
},
2834
remove: (eventName) => {

packages/devextreme/testing/tests/DevExpress.ui.widgets/mapParts/azureTests.js

Lines changed: 113 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import $ from 'jquery';
44
import { MARKERS, ROUTES } from './utils.js';
55
import AzureProvider from '__internal/ui/map/m_provider.dynamic.azure';
66
import ajaxMock from '../../../helpers/ajaxMock.js';
7+
import errors from 'ui/widget/ui.errors';
78

89
import 'ui/map';
910

@@ -94,12 +95,13 @@ QUnit.module('map loading', moduleConfig, () => {
9495
setTimeout(function() {
9596
$('#map').dxMap({
9697
provider: 'azure',
97-
onReady: $.proxy(() => {
98-
assert.ok(window.atlas.Map.customFlag, 'map loaded without getting script');
99-
100-
done();
101-
}, this)
10298
});
99+
100+
setTimeout(() => {
101+
assert.ok(window.atlas.Map.customFlag, 'map loaded without getting script');
102+
103+
done();
104+
}, 50);
103105
});
104106
});
105107
});
@@ -151,6 +153,19 @@ QUnit.module('map loading', moduleConfig, () => {
151153
}
152154
});
153155
});
156+
157+
QUnit.test('map should add ready event handler to call map ready callback', function(assert) {
158+
const done = assert.async();
159+
160+
$('#map').dxMap({
161+
provider: 'azure',
162+
onReady: () => {
163+
assert.strictEqual(atlas.readyCallbackCalled, true, 'map ready callback was called');
164+
165+
done();
166+
}
167+
}).dxMap('instance');
168+
});
154169
});
155170

156171
QUnit.module('basic options', moduleConfig, () => {
@@ -1043,4 +1058,97 @@ QUnit.module('Routes', moduleConfig, () => {
10431058
}
10441059
}).dxMap('instance');
10451060
});
1061+
1062+
QUnit.test('Should log an error if get route request failed', function(assert) {
1063+
const done = assert.async();
1064+
const mapReadyDeferred = $.Deferred();
1065+
1066+
const map = $('#map').dxMap({
1067+
provider: 'azure',
1068+
onReady: () => {
1069+
mapReadyDeferred.resolve();
1070+
},
1071+
}).dxMap('instance');
1072+
1073+
const errorStub = sinon.stub(errors, 'log');
1074+
1075+
mapReadyDeferred.done(() => {
1076+
ajaxMock.clear();
1077+
ajaxMock.setup({
1078+
url: '/fakeAzureUrl',
1079+
responseText: {
1080+
routes: [{
1081+
legs: {
1082+
length: 1,
1083+
},
1084+
}],
1085+
},
1086+
});
1087+
1088+
map.addRoute(ROUTES[0]).done(() => {
1089+
const message = errorStub.args[0][1].message;
1090+
1091+
assert.ok(errorStub.withArgs('W1006').calledOnce, 'warning is logged');
1092+
assert.strictEqual(message, 'route.legs.flatMap is not a function', 'warning message text is correct');
1093+
1094+
errorStub.restore();
1095+
done();
1096+
});
1097+
});
1098+
});
1099+
1100+
QUnit.module('Postponed map ready', {
1101+
beforeEach: function() {
1102+
window.postponeMapReadyPromise = true;
1103+
},
1104+
afterEach: function() {
1105+
delete window.postponeMapReadyPromise;
1106+
}
1107+
}, () => {
1108+
QUnit.test('Route should not be added on init before map ready promise is resolved', function(assert) {
1109+
const done = assert.async();
1110+
const onRouteAdded = sinon.stub();
1111+
1112+
$('#map').dxMap({
1113+
provider: 'azure',
1114+
onReady: () => {
1115+
assert.strictEqual(onRouteAdded.callCount, 1, 'route was added after map is ready');
1116+
1117+
done();
1118+
},
1119+
onRouteAdded,
1120+
routes: [ROUTES[0]],
1121+
});
1122+
1123+
setTimeout(() => {
1124+
assert.strictEqual(onRouteAdded.callCount, 0, 'route was not added before map is ready');
1125+
1126+
atlas.mapReadyResolve();
1127+
}, 100);
1128+
});
1129+
1130+
QUnit.test('Add route method should not be called before map ready promise is resolved', function(assert) {
1131+
const done = assert.async();
1132+
const onRouteAdded = sinon.stub();
1133+
1134+
const map = $('#map').dxMap({
1135+
provider: 'azure',
1136+
onReady: () => {
1137+
assert.strictEqual(onRouteAdded.callCount, 1, 'route was added after map is ready');
1138+
1139+
done();
1140+
},
1141+
onRouteAdded,
1142+
routes: [ROUTES[0]],
1143+
}).dxMap('instance');
1144+
1145+
map.addRoute(ROUTES[0]);
1146+
1147+
setTimeout(() => {
1148+
assert.strictEqual(onRouteAdded.callCount, 0, 'route was not added before map is ready');
1149+
1150+
atlas.mapReadyResolve();
1151+
}, 100);
1152+
});
1153+
});
10461154
});

0 commit comments

Comments
 (0)