kui-shell
Version:
This is the monorepo for Kui, the hybrid command-line/GUI electron-based Kubernetes tool
152 lines • 7.27 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert = require("assert");
const cli_1 = require("./cli");
const Selectors = require("./selectors");
const expectOK = (appAndCount, opt) => {
const app = appAndCount.app;
const N = appAndCount.count + 1;
return app.client
.waitForVisible(Selectors.PROMPT_N(N), cli_1.waitTimeout)
.then(() => app.client.getAttribute(Selectors.PROMPT_N(N), 'placeholder'))
.then(() => app.client.getValue(Selectors.PROMPT_N(N)))
.then(promptValue => {
if ((!opt || !opt.nonBlankPromptOk) && promptValue.length !== 0) {
console.error(`Expected prompt value to be empty: ${promptValue}`);
}
return promptValue;
})
.then(promptValue => {
if (!opt || !opt.nonBlankPromptOk)
assert.strictEqual(promptValue.length, 0);
})
.then(() => __awaiter(void 0, void 0, void 0, function* () {
if (opt && opt.expectError)
return false;
const html = yield app.client.getHTML(Selectors.OK_N(N - 1));
const okReg = new RegExp(process.env.OK) || /ok/;
assert.ok(okReg.test(html));
}))
.then(() => {
if (opt && opt.expectString) {
return app.client
.getText(Selectors.LIST_RESULTS_BY_NAME_N(N - 1))
.then(name => assert.strictEqual(name, opt.expectString));
}
else if (opt && opt.expectArray) {
return app.client
.getText(Selectors.LIST_RESULTS_BY_NAME_N(N - 1))
.then(name => (!Array.isArray(name) ? [name] : name))
.then(name => assert.ok(name !== opt.expectArray[0] && name.find(_ => _.indexOf(opt.expectArray[0]) >= 0)));
}
else if (opt && (opt.selector || opt.expect)) {
const selector = `${Selectors.OUTPUT_N(N - 1)} ${opt.selector || ''}`;
if (opt.elements) {
return app.client.elements(selector);
}
else {
return app.client.getText(selector).then(txt => {
if (opt.exact)
assert.strictEqual(txt, opt.expect);
else if (opt.expect) {
if (txt.indexOf(opt.expect) < 0) {
console.error(`Expected string not found expected=${opt.expect} idx=${txt.indexOf(opt.expect)} actual=${txt}`);
assert.ok(txt.indexOf(opt.expect) >= 0);
}
}
return opt.passthrough ? N - 1 : selector;
});
}
}
else if (opt && opt.expectJustOK === true) {
return app.client.waitUntil(() => __awaiter(void 0, void 0, void 0, function* () {
const txt = yield app.client.getText(Selectors.OUTPUT_N(N - 1));
const justOK = process.env.OK || 'ok';
return txt.length === 0 || txt === justOK;
}));
}
else {
return N - 1;
}
})
.then(res => (opt && (opt.selector || opt.passthrough) ? res : app));
};
exports.ok = (res) => __awaiter(void 0, void 0, void 0, function* () {
return expectOK(res, { passthrough: true })
.then(N => res.app.client.elements(Selectors.LIST_RESULTS_BY_NAME_N(N)))
.then(elts => assert.strictEqual(elts.value.length, 0))
.then(() => res.app);
});
exports.error = (statusCode, expect) => (res) => __awaiter(void 0, void 0, void 0, function* () {
return expectOK(res, {
selector: `.oops[data-status-code="${statusCode || 0}"]`,
expectError: true,
expect: expect
}).then(() => res.app);
});
exports.errorWithPassthrough = (statusCode, expect) => (res) => __awaiter(void 0, void 0, void 0, function* () {
return expectOK(res, {
selector: `.oops[data-status-code="${statusCode || 0}"]`,
expectError: true,
expect: expect,
passthrough: true
});
});
exports.blankWithOpts = (opts = {}) => (res) => __awaiter(void 0, void 0, void 0, function* () { return expectOK(res, Object.assign({ selector: '', expectError: true }, opts)); });
exports.blank = (res) => exports.blankWithOpts()(res);
exports.consoleToBeClear = (app) => {
return app.client.waitUntil(() => __awaiter(void 0, void 0, void 0, function* () {
return app.client.elements(Selectors.PROMPT_BLOCK).then(elements => elements.value.length === 1);
}));
};
exports.okWithCustom = (custom) => (res) => __awaiter(void 0, void 0, void 0, function* () { return expectOK(res, custom); });
exports.okWithTextContent = (expect, exact = false, failFast = true, sel = ' ') => (res) => __awaiter(void 0, void 0, void 0, function* () {
const selector = yield exports.okWithCustom({ selector: sel })(res);
const txt = yield cli_1.getTextContent(res.app, selector);
if (exact) {
assert.strictEqual(txt, expect);
return true;
}
else {
if (txt.indexOf(expect) < 0) {
console.error(`Expected string not found expected=${expect} idx=${txt.indexOf(expect)} actual=${txt}`);
if (failFast) {
assert.ok(txt.indexOf(expect) >= 0);
return true;
}
else {
return false;
}
}
}
});
exports.okWithString = (expect, exact = false) => (res) => __awaiter(void 0, void 0, void 0, function* () {
return exports.okWithCustom({ expect, exact })(res).catch((err1) => __awaiter(void 0, void 0, void 0, function* () {
return exports.okWithTextContent(expect, exact)(res).catch(() => {
throw err1;
});
}));
});
exports.okWithStringEventually = (expect, exact = false) => (res) => {
return res.app.client.waitUntil(() => {
try {
return exports.okWithString(expect, exact)(res);
}
catch (err) {
}
}, cli_1.waitTimeout);
};
exports.okWithAny = (res) => __awaiter(void 0, void 0, void 0, function* () { return expectOK(res); });
exports.okWithOnly = (entityName) => (res) => __awaiter(void 0, void 0, void 0, function* () { return expectOK(res, { expectString: entityName }); });
exports.okWith = (entityName) => (res) => __awaiter(void 0, void 0, void 0, function* () { return expectOK(res, { expectArray: [entityName] }); });
exports.justOK = (res) => __awaiter(void 0, void 0, void 0, function* () { return expectOK(res, { expectJustOK: true }).then(() => res.app); });
//# sourceMappingURL=repl-expect.js.map