UNPKG

@umbraco/playwright-testhelpers

Version:

Test helpers for making playwright tests for Umbraco solutions

273 lines 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplateApiHelper = void 0; const AliasHelper_1 = require("./AliasHelper"); class TemplateApiHelper { api; constructor(api) { this.api = api; } async get(id) { const response = await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/template/' + id); const json = await response.json(); if (json !== null) { return json; } return null; } async doesExist(id) { const response = await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/template/' + id); return response.status() === 200; } async create(name, alias, content) { const templateData = { "name": name, "alias": alias, "content": content }; const response = await this.api.post(this.api.baseUrl + '/umbraco/management/api/v1/template', templateData); // Returns the id of the created template return response.headers().location.split("/").pop(); } async delete(id) { return await this.api.delete(this.api.baseUrl + '/umbraco/management/api/v1/template/' + id); } async update(id, template) { return await this.api.put(this.api.baseUrl + '/umbraco/management/api/v1/template/' + id, template); } async getChildren(id) { const response = await this.api.get(`${this.api.baseUrl}/umbraco/management/api/v1/tree/template/children?parentId=${id}&skip=0&take=10000`); const items = await response.json(); return items.items; } async getItems(ids) { let idArray = 'id=' + ids[0]; let i; for (i = 1; i < ids.length; ++i) { idArray += '&id=' + ids[i]; } const response = await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/tree/template/item?' + idArray); const json = await response.json(); if (json !== null) { return json; } return null; } async getAllAtRoot() { return await this.api.get(this.api.baseUrl + '/umbraco/management/api/v1/tree/template/root?skip=0&take=10000'); } async doesNameExist(name) { return await this.getByName(name); } async recurseDeleteChildren(id) { const items = await this.getChildren(id); for (const child of items) { if (child.isContainer || child.hasChildren) { await this.recurseDeleteChildren(child.id); } else { await this.delete(child.id); } } return await this.delete(id); } async recurseChildren(name, id, toDelete) { const items = await this.getChildren(id); for (const child of items) { if (child.name === name) { if (!toDelete) { return await this.get(child.id); } if (child.isContainer || child.hasChildren) { return await this.recurseDeleteChildren(child.id); } else { return await this.delete(child.id); } } else if (child.isContainer || child.hasChildren) { await this.recurseChildren(name, child.id, toDelete); } } return false; } async getByName(name) { const rootTemplates = await this.getAllAtRoot(); const jsonTemplates = await rootTemplates.json(); for (const template of jsonTemplates.items) { if (template.name === name) { return this.get(template.id); } else if (template.isContainer || template.hasChildren) { const result = await this.recurseChildren(name, template.id, false); if (result) { return result; } } } return false; } async ensureNameNotExists(name) { const rootTemplates = await this.getAllAtRoot(); const jsonTemplates = await rootTemplates.json(); for (const template of jsonTemplates.items) { if (template.name === name) { if (template.isContainer || template.hasChildren) { await this.recurseDeleteChildren(template.id); } await this.delete(template.id); } else { if (template.isContainer || template.hasChildren) { await this.recurseChildren(name, template.id, true); } } } } async createDefaultTemplate(name) { await this.ensureNameNotExists(name); const alias = AliasHelper_1.AliasHelper.toAlias(name); return await this.create(name, alias, ''); } async createTemplateWithDisplayingValue(name, templateContent) { await this.ensureNameNotExists(name); const alias = AliasHelper_1.AliasHelper.toAlias(name); const content = '@using Umbraco.Cms.Web.Common.PublishedModels;' + '\n@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage;' + '\n@{' + '\n\tLayout = null;' + '\n}' + '\n<div data-mark="content-render-value">' + templateContent + '\n</div>\n'; const templateId = await this.create(name, alias, content); return templateId === undefined ? '' : templateId; } async createTemplateWithDisplayingStringValue(name, valueAlias) { const templateContent = '\n@{' + '\n\tif (Model.HasValue("' + valueAlias + '")){' + '\n\t\t<p>@(Model.Value<string>("' + valueAlias + '"))</p>' + '\n\t}' + '\n}'; return this.createTemplateWithDisplayingValue(name, templateContent); } async createTemplateWithDisplayingMulitpleStringValue(name, valueAlias) { const templateContent = '\n@if(Model.HasValue("' + valueAlias + '"))' + '\n{' + '\nvar items = Model.Value<IEnumerable<string>>("' + valueAlias + '");' + '\n\t<ul>' + '\n\t\t@foreach(var item in items)' + '\n\t\t{' + '\n\t\t\t<li>@item</li>' + '\n\t\t}' + '\n\t</ul>' + '\n}'; return this.createTemplateWithDisplayingValue(name, templateContent); } async createTemplateWithDisplayingApprovedColorValue(name, valueAlias, useLabel = true) { let templateContent = ''; if (useLabel) { templateContent = '\n@using Umbraco.Cms.Core.PropertyEditors.ValueConverters' + '\n@{' + '\n\tvar hexColor = Model.Value("' + valueAlias + '");' + '\n\tvar colorLabel = Model.Value<ColorPickerValueConverter.PickedColor>("' + valueAlias + '").Label;' + '\n\tif (hexColor != null)' + '\n\t{' + '\n\t\t<div style="background-color: #@hexColor">@colorLabel</div>' + '\n\t}' + '\n}'; } else { templateContent = '\n@using Umbraco.Cms.Core.PropertyEditors.ValueConverters' + '\n@{' + '\n\tvar hexColor = Model.Value("' + valueAlias + '");' + '\n\tif (hexColor != null)' + '\n\t{' + '\n\t\t<div style="background-color: #@hexColor">@hexColor</div>' + '\n\t}' + '\n}'; } return this.createTemplateWithDisplayingValue(name, templateContent); } async createTemplateWithDisplayingImageCropperValue(name, valueAlias, cropName) { const templateContent = '\n@using Umbraco.Cms.Core.PropertyEditors.ValueConverters' + '\n\t<img src="@Url.GetCropUrl(Model.Value<ImageCropperValue>("' + valueAlias + '"), "' + cropName + '")" />'; return this.createTemplateWithDisplayingValue(name, templateContent); } async createTemplateWithDisplayingContentPickerValue(name, valueAlias) { const templateContent = '\n@{' + '\n\tIPublishedContent typedContentPicker = Model.Value<IPublishedContent>("' + valueAlias + '");' + '\n\tif (typedContentPicker != null)' + '\n\t{' + '\n\t\t<p>@typedContentPicker.Name</p>' + '\n\t}' + '\n}'; return this.createTemplateWithDisplayingValue(name, templateContent); } async createTemplateWithDisplayingUploadedFileValue(name, valueAlias) { const templateContent = '\n@using System.IO;' + '\n@{' + '\n\tif (Model.HasValue("' + valueAlias + '"))' + '\n\t{' + '\n\t\tvar myFile = Model.Value<string>("' + valueAlias + '");' + '\n\t\t<a href="@myFile">@System.IO.Path.GetFileName(myFile)</a>' + '\n\t}' + '\n}'; return this.createTemplateWithDisplayingValue(name, templateContent); } async createTemplateWithDisplayingMemberPickerValue(name, valueAlias) { const templateContent = '\n@{' + '\n\tif (Model.HasValue("' + valueAlias + '"))' + '\n\t{' + '\n\t\tvar member = Model.Value<IPublishedContent>("' + valueAlias + '");' + '\n\t\t@member.Name' + '\n\t}' + '\n}'; return this.createTemplateWithDisplayingValue(name, templateContent); } async createTemplateWithDisplayingMultiURLPickerValue(name, valueAlias) { const templateContent = '\n@using Umbraco.Cms.Core.Models' + '\n@{' + '\n\tvar links = Model.Value<IEnumerable<Link>>("' + valueAlias + '");' + '\n\tif (links.Any())' + '\n\t{' + '\n\t\t<ul>' + '\n\t\t\t@foreach (var link in links)' + '\n\t\t\t{' + '\n\t\t\t\t<li><a href="@link.Url" target="@link.Target">@link.Name</a></li>' + '\n\t\t\t}' + '\n\t\t</ul>' + '\n\t}' + '\n}'; return this.createTemplateWithDisplayingValue(name, templateContent); } async createTemplateWithDisplayingMultipleMediaPickerValue(name, valueAlias) { const templateContent = '\n@using Umbraco.Cms.Core.Models;' + '\n@{' + '\n\tvar medias = Model.Value<IEnumerable<MediaWithCrops>>("' + valueAlias + '");' + '\n\tif (medias.Any())' + '\n\t{' + '\n\t\t<ul>' + '\n\t\t\t@foreach (var media in medias)' + '\n\t\t\t{' + '\n\t\t\t\t<li><a href="@media.MediaUrl()">@media.Name</a></li>' + '\n\t\t\t}' + '\n\t\t</ul>' + '\n\t}' + '\n}'; return this.createTemplateWithDisplayingValue(name, templateContent); } async createTemplateUsingSiblingOfTypeMethod(name, documentTypeName) { const templateContent = '\n<ul>' + '\n\t@foreach(var item in Model.SiblingsOfType("' + AliasHelper_1.AliasHelper.toAlias(documentTypeName) + '"))' + '\n\t{' + '\n\t\t<li><a href="@item.Url()">@item.Name</a></li>' + '\n\t}' + '\n<ul>'; return this.createTemplateWithDisplayingValue(name, templateContent); } } exports.TemplateApiHelper = TemplateApiHelper; //# sourceMappingURL=TemplateApiHelper.js.map