@web-atoms/core
Version:
159 lines (158 loc) • 5.68 kB
JavaScript
System.register(["tslib", "../App", "../core/AtomUri", "../di/RegisterSingleton", "../view-model/AtomWindowViewModel", "./NavigationService"], function (_export, _context) {
"use strict";
var __awaiter, __decorate, __metadata, App, AtomUri, RegisterSingleton, AtomWindowViewModel, NavigationService, MockConfirmViewModel, MockNavigationService;
_export("MockConfirmViewModel", void 0);
return {
setters: [function (_tslib) {
__awaiter = _tslib.__awaiter;
__decorate = _tslib.__decorate;
__metadata = _tslib.__metadata;
}, function (_App) {
App = _App.App;
}, function (_coreAtomUri) {
AtomUri = _coreAtomUri.AtomUri;
}, function (_diRegisterSingleton) {
RegisterSingleton = _diRegisterSingleton.RegisterSingleton;
}, function (_viewModelAtomWindowViewModel) {
AtomWindowViewModel = _viewModelAtomWindowViewModel.AtomWindowViewModel;
}, function (_NavigationService) {
NavigationService = _NavigationService.NavigationService;
}],
execute: function () {
_export("MockConfirmViewModel", MockConfirmViewModel = class MockConfirmViewModel extends AtomWindowViewModel {});
_export("MockNavigationService", MockNavigationService = class MockNavigationService extends NavigationService {
get location() {
return this.mLocation;
}
set location(v) {
if (JSON.stringify(this.location) === JSON.stringify(v)) {
return;
}
this.history.push(this.mLocation);
this.mLocation = v;
}
constructor(app) {
super(app);
this.windowStack = [];
this.history = [];
this.mLocation = new AtomUri("");
}
refresh() {}
navigate(url) {
this.location = new AtomUri(url);
}
back() {
if (this.history.length) {
const top = this.history.pop();
this.location = top;
this.history.pop();
}
}
alert(msg, title) {
return this.openTestWindow(`__AlertWindow_${msg}`, {
message: msg,
title
});
}
confirm(msg, title) {
return this.openTestWindow(`__ConfirmWindow_${msg}`, {
message: msg,
title
});
}
openPage(pageName, p) {
return this.openTestWindow(pageName, p);
}
notify(message, title) {
return __awaiter(this, void 0, void 0, function* () {
const url = `__AlertNotification_${message}`;
const w = this.windowStack.find(x => x.windowType === message);
if (!w) {
throw new Error(`No notification registered for "${message}"`);
}
w.action({});
});
}
openTestWindow(c, p) {
const url = c instanceof AtomUri ? c : new AtomUri(c);
if (p) {
for (const key in p) {
if (p.hasOwnProperty(key)) {
const element = p[key];
url.query[key] = element;
}
}
}
return new Promise((resolve, reject) => {
const w = this.windowStack.find(x => x.windowType === url.path);
if (!w) {
const ex = new Error(`No window registered for "${c} with ${p ? JSON.stringify(p, undefined, 2) : ""}"`);
reject(ex);
return;
}
setTimeout(() => {
try {
const vm = new AtomWindowViewModel(this.app);
for (const key in url.query) {
if (url.query.hasOwnProperty(key)) {
const element = url.query[key];
vm[key] = element;
}
}
resolve(w.action(vm));
} catch (e) {
reject(e);
}
}, 5);
});
}
expectAlert(msg) {
return this.expectWindow(`__AlertWindow_${msg}`, vm => true);
}
expectNotification(msg) {
return this.expectWindow(`__AlertNotification_${msg}`, vm => true);
}
expectConfirm(msg, action) {
return this.expectWindow(`__ConfirmWindow_${msg}`, action);
}
expectPopup(popupType, windowAction) {
return this.expectWindow(popupType, windowAction);
}
expectWindow(windowType, windowAction) {
const registration = {
windowType,
action: windowAction
};
registration.action = vm => {
this.removeRegistration(registration);
return windowAction(vm);
};
this.windowStack.push(registration);
return {
dispose: () => {
this.removeRegistration(registration);
}
};
}
removeRegistration(r) {
this.windowStack = this.windowStack.filter(x => x !== r);
}
assert() {
if (!this.windowStack.length) {
return;
}
throw new Error(`Expected windows did not open ${this.windowStack.map(x => `"${x.windowType}"`).join(",")}`);
}
registerForPopup() {}
forceRemove(view) {
throw new Error("Method not implemented.");
}
openWindow(url) {
return this.openTestWindow(url);
}
});
_export("MockNavigationService", MockNavigationService = __decorate([RegisterSingleton, __metadata("design:paramtypes", [App])], MockNavigationService));
}
};
});
//# sourceMappingURL=MockNavigationService.js.map