@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
211 lines • 7.94 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RndInputBuilder = exports.RndHandoffBuilder = exports.RndDocumentBuilder = exports.RndMediaBuilder = exports.RndStartUpBuilder = exports.RndCarouselBuilder = exports.RndElementBuilder = exports.RndTextBuilder = exports.RndTopContentBuilder = exports.RndButtonsBuilder = exports.ContentCallbackBuilder = exports.rndBool = exports.rndStr = void 0;
const cms_1 = require("../cms");
const contents_1 = require("../contents");
const content_factories_1 = require("../factories/content-factories");
const index_1 = require("../index");
/**
* The Builder classes below create Content instances with minimal effort
* to speedup the implementation of unit tests by setting random values for the
* non specified fields.
* They are exported by the plugin so that the can be used in bots' unit tests
*/
function rndStr() {
return Math.random().toString();
}
exports.rndStr = rndStr;
function rndBool() {
return Math.random() >= 0.5;
}
exports.rndBool = rndBool;
class ContentCallbackBuilder {
withContentId(contentId) {
this.callback = index_1.ContentCallback.ofContentId(contentId);
return this;
}
build() {
return this.callback || new index_1.ContentCallback(cms_1.ContentType.TEXT, rndStr());
}
}
exports.ContentCallbackBuilder = ContentCallbackBuilder;
class RndButtonsBuilder {
constructor() {
this.buttons = [];
this.callbackBuilder = new ContentCallbackBuilder();
}
build() {
return this.buttons;
}
withCallback(callback) {
this.callback = callback;
return this;
}
withName(name) {
this.name = name;
return this;
}
withText(text) {
this.text = text;
return this;
}
addButton() {
var _a, _b, _c;
this.buttons.push(new index_1.Button(rndStr(), (_a = this.name) !== null && _a !== void 0 ? _a : rndStr(), (_b = this.text) !== null && _b !== void 0 ? _b : rndStr(), (_c = this.callback) !== null && _c !== void 0 ? _c : this.callbackBuilder.build()));
return this;
}
}
exports.RndButtonsBuilder = RndButtonsBuilder;
class RndTopContentBuilder {
constructor() {
this.keywords = [];
}
withRandomFields(builder) {
builder.shortText = rndStr();
builder.keywords = [rndStr(), rndStr()];
builder.followUp = rndBool()
? undefined
: new index_1.Text(new index_1.CommonFields(rndStr(), rndStr()), rndStr(), []);
}
}
exports.RndTopContentBuilder = RndTopContentBuilder;
class RndTextBuilder extends content_factories_1.TextBuilder {
constructor(name = rndStr(), text = rndStr()) {
super(rndStr(), name, text);
this.buttonsBuilder = new RndButtonsBuilder();
this.topComponentBuilder = new RndTopContentBuilder();
}
/** @deprecated use buttonsBuilder */
withButtonsBuilder() {
return this.buttonsBuilder;
}
build() {
const buttons = this.buttonsBuilder.build();
// TODO too complex. resurrect withButtonsBuilder?
if (buttons.length > 0) {
if (this.buttons.length > 0) {
throw new index_1.CmsException('Not supported to add buttons with both .buttons & buttonsBuilder');
}
this.buttonsBuilder.buttons = [];
this.buttons = buttons;
}
return super.build();
}
withRandomFields() {
this.buttonsBuilder.addButton().addButton();
this.topComponentBuilder.withRandomFields(this);
this.buttonsStyle = rndBool() ? index_1.ButtonStyle.QUICK_REPLY : index_1.ButtonStyle.BUTTON;
return this;
}
}
exports.RndTextBuilder = RndTextBuilder;
class RndElementBuilder extends content_factories_1.ElementBuilder {
constructor() {
super(rndStr());
}
withButtonsBuilder() {
if (this.buttons.length > 0) {
throw new index_1.CmsException('cannot use withButtonsBuilder if addButtons was previously called');
}
if (!this.buttonsBuilder) {
this.buttonsBuilder = new RndButtonsBuilder();
}
return this.buttonsBuilder;
}
withButtons(buttons) {
if (this.buttonsBuilder) {
throw new index_1.CmsException('cannot use addButtons if withButtonsBuilder was previously called');
}
return super.withButtons(buttons);
}
withRandomFields() {
if (!this.buttonsBuilder) {
this.buttonsBuilder = new RndButtonsBuilder().addButton().addButton();
}
this.title = rndStr();
this.subtitle = rndStr();
this.imgUrl = rndStr();
return this;
}
build() {
if (this.buttonsBuilder) {
this.buttons = this.buttonsBuilder.build();
}
return super.build();
}
}
exports.RndElementBuilder = RndElementBuilder;
class RndCarouselBuilder extends content_factories_1.CarouselBuilder {
constructor(name = rndStr()) {
super(rndStr(), name);
this.topComponentBuilder = new RndTopContentBuilder();
this.elementBuilder = new RndElementBuilder();
}
withRandomFields(numElements = 2) {
for (let i = 0; i < numElements; i++) {
this.elementBuilder.withRandomFields();
this.addElement();
}
this.topComponentBuilder.withRandomFields(this);
return this;
}
}
exports.RndCarouselBuilder = RndCarouselBuilder;
class RndStartUpBuilder extends content_factories_1.StartUpBuilder {
constructor(name = rndStr(), text = rndStr()) {
super(rndStr(), name, text);
this.topComponentBuilder = new RndTopContentBuilder();
this.buttonsBuilder = new RndButtonsBuilder();
}
withRandomFields() {
this.buttons = this.buttonsBuilder.addButton().addButton().build();
this.topComponentBuilder.withRandomFields(this);
return this;
}
}
exports.RndStartUpBuilder = RndStartUpBuilder;
class RndMediaBuilder extends content_factories_1.MediaBuilder {
constructor(name = rndStr(), mediaUrl = 'http://' + rndStr()) {
super(rndStr(), name, mediaUrl);
this.topComponentBuilder = new RndTopContentBuilder();
}
withRandomFields() {
this.topComponentBuilder.withRandomFields(this);
return this;
}
}
exports.RndMediaBuilder = RndMediaBuilder;
class RndDocumentBuilder extends content_factories_1.DocumentBuilder {
constructor(name = rndStr(), docUrl = 'http://' + rndStr()) {
super(rndStr(), name, docUrl);
this.topComponentBuilder = new RndTopContentBuilder();
}
withRandomFields() {
this.topComponentBuilder.withRandomFields(this);
return this;
}
}
exports.RndDocumentBuilder = RndDocumentBuilder;
class RndHandoffBuilder extends content_factories_1.HandoffBuilder {
constructor(name = rndStr(), onFinish = new ContentCallbackBuilder().build()) {
super(rndStr(), name, onFinish);
this.topComponentBuilder = new RndTopContentBuilder();
}
withRandomFields() {
this.message = new index_1.Text(new index_1.CommonFields(rndStr(), rndStr()), rndStr(), []);
this.failMessage = new index_1.Text(new index_1.CommonFields(rndStr(), rndStr()), rndStr(), []);
this.agent = new contents_1.HandoffAgentEmail(rndStr());
this.shadowing = rndBool();
this.topComponentBuilder.withRandomFields(this);
return this;
}
}
exports.RndHandoffBuilder = RndHandoffBuilder;
class RndInputBuilder extends content_factories_1.InputBuilder {
constructor(name = rndStr(), keywords = [rndStr(), rndStr()], target = new ContentCallbackBuilder().build()) {
super(rndStr(), name, rndStr(), keywords, target);
this.topComponentBuilder = new RndTopContentBuilder();
}
}
exports.RndInputBuilder = RndInputBuilder;
//# sourceMappingURL=builders.js.map