kentico-cloud-delivery
Version:
Official Kentico Cloud Delivery SDK
155 lines • 7.3 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { ContentItem, ContentItemSystemAttributes, Fields, Link, richTextResolver, TypeResolver, urlSlugResolver, getParserAdapter } from '../../../lib';
var ActorMock = /** @class */ (function (_super) {
__extends(ActorMock, _super);
function ActorMock() {
return _super.call(this) || this;
}
ActorMock.prototype.setProperties = function (id, codename, firstName) {
var _this = this;
this.firstName = new Fields.TextField('firstName', firstName);
this.system = new ContentItemSystemAttributes({
id: id,
name: 'name',
codename: codename,
type: 'actor',
sitemapLocations: [],
language: 'en',
lastModified: new Date()
});
this.url = new Fields.UrlSlugField('name', codename, {
resolveUrl: function () { return urlSlugResolver.resolveUrl({
fieldName: 'name',
type: 'type',
item: _this,
linkResolver: function (link) {
return "/actor-rt/" + link.urlSlug;
},
enableAdvancedLogging: true,
fieldValue: codename
}); }
});
};
return ActorMock;
}(ContentItem));
describe('RichTextField', function () {
// prepare config & type resolver
var typeResolvers = [
new TypeResolver('actor', function () { return new ActorMock(); })
];
var config = {
projectId: '',
typeResolvers: typeResolvers
};
// prepare modular items
var modularItems = [];
var tomHardyId = 'd1557cb1-d7ec-4d04-9742-f86b52bc34fc';
var joelEdgertonId = '3294e4b0-e58b-49d7-85fa-5bc9a86556ec';
var tomHardy = new ActorMock();
tomHardy.setProperties(tomHardyId, 'tom_hardy', 'Tom');
var joelEdgerton = new ActorMock();
joelEdgerton.setProperties(joelEdgertonId, 'joel_edgerton', 'Joel');
// prepare links
var links = [
new Link({
itemId: tomHardy.system.id,
codename: tomHardy.system.codename,
type: tomHardy.system.type,
urlSlug: 'slug_for_tom'
}),
new Link({
itemId: joelEdgerton.system.id,
codename: joelEdgerton.system.codename,
type: joelEdgerton.system.type,
urlSlug: 'slug_for_joel'
})
];
modularItems.push(tomHardy);
modularItems.push(joelEdgerton);
// prepare html
// tslint:disable:max-line-length
var html = "\n <p>The youngest son of an alcoholic former boxer returns home, where he's trained by his father for competition in a mixed martial arts tournament - a path that puts the fighter on a collision course with his estranged, older brother.</p>\n<p>Stars: </p>\n<object type=\"application/kenticocloud\" data-type=\"item\" data-codename=\"tom_hardy\"></object>\n<object type=\"application/kenticocloud\" data-type=\"item\" data-codename=\"joel_edgerton\"></object>\n<p>See more in profile of <a data-item-id=\"3294e4b0-e58b-49d7-85fa-5bc9a86556ec\" href=\"\">Joel Edgerton</a> and <a data-item-id=\"d1557cb1-d7ec-4d04-9742-f86b52bc34fc\" href=\"\">Tom Hardy</a></p>\n ";
var field = new Fields.RichTextField('name', html, {
links: links,
resolveHtml: function () { return richTextResolver.resolveHtml(html, {
enableAdvancedLogging: false,
links: links,
modularItems: modularItems,
typeResolvers: config.typeResolvers,
richTextHtmlParser: getParserAdapter(),
modularContentWrapperClasses: ['kc-wrapper-class'],
modularContentWrapperTag: 'kcelem',
queryConfig: {
richTextResolver: function (item) {
return "<p class=\"testing_richtext\">" + item.firstName.text + "</p>";
},
linkResolver: function (link) { return '/actor-rt/' + link.urlSlug; }
}
}); }
});
it("checks name", function () {
expect(field.name).toEqual('name');
});
it("checks value", function () {
expect(field.value).toEqual(html);
});
it("checks that html contains resolved modular content #1", function () {
var expectedHtml = "<p class=\"testing_richtext\">Tom</p>";
expect(field.getHtml()).toContain(expectedHtml);
});
it("checks that html contains resolved modular content #2", function () {
var expectedHtml = "<p class=\"testing_richtext\">Joel</p>";
expect(field.getHtml()).toContain(expectedHtml);
});
it("checks that html contains resolved url #1", function () {
var expectedHtml = "/actor-rt/slug_for_tom";
expect(field.getHtml()).toContain(expectedHtml);
});
it("checks that html contains resolved url #2", function () {
var expectedHtml = "/actor-rt/slug_for_joel";
expect(field.getHtml()).toContain(expectedHtml);
});
it("checks that html contains propper modular item wrapper", function () {
var elem = "kcelem";
expect(field.getHtml()).toContain(elem);
});
it("checks that html contains propper modular item class", function () {
var wrapperClass = "kc-wrapper-class";
expect(field.getHtml()).toContain(wrapperClass);
});
it("checks that links are present in rich text field", function () {
expect(field.links.length).toEqual(links.length);
});
it("checks that links are resolved even if the rich text resolver is not set", function () {
var fieldWithoutRichTextResolver = new Fields.RichTextField('name', html, {
links: links,
resolveHtml: function () { return richTextResolver.resolveHtml(html, {
enableAdvancedLogging: false,
links: links,
modularItems: modularItems,
typeResolvers: config.typeResolvers,
richTextHtmlParser: getParserAdapter(),
modularContentWrapperClasses: ['kc-wrapper-class'],
modularContentWrapperTag: 'kc-item-wrapper',
queryConfig: {
richTextResolver: undefined,
linkResolver: function (link) { return '/actor-rt/' + link.urlSlug; }
}
}); }
});
var expectedHtml1 = "/actor-rt/slug_for_joel";
var expectedHtml2 = "/actor-rt/slug_for_tom";
expect(fieldWithoutRichTextResolver.getHtml()).toContain(expectedHtml1);
expect(fieldWithoutRichTextResolver.getHtml()).toContain(expectedHtml2);
});
});
//# sourceMappingURL=rich-text.spec.js.map