Best JavaScript code snippet using ng-mocks
vlocity_ins__templatecompiler.js
Source:vlocity_ins__templatecompiler.js
1/**2 * An OS component.3 */4export default class TemplateCompiler {5 compileZip(json, action) {6 return this.createZipToDeploy(json, action);7 }8 createZipToDeploy(json, action) {9 return new Promise((resolve, reject) => {10 let type;11 let url = '';12 var zip = new JSZip();13 if(action === "download") {14 type = 'blob';15 } else {16 url = 'lwc/';17 type = 'base64';18 zip.file(19 'package.xml',20 `<?xml version="1.0" encoding="UTF-8"?>21 <Package xmlns="http://soap.sforce.com/2006/04/metadata">22 <types>23 <members>${json.name}</members>24 <name>LightningComponentBundle</name>25 </types>26 <version>45.0</version>27 </Package>`28 );29 }30 json.files.forEach(function (file) {31 zip.file(`${url}${json.name}/${file.name}`, `${file.source}`);32 })33 zip.generateAsync({ type: type })34 .then(content => {35 resolve(content);36 });37 });38 }39 getDownloadableZip(zipFile, json) {40 return new Promise((resolve, reject) => {41 var zip = new JSZip();42 var newZip = new JSZip();43 var _this = this;44 45 this.zipLoadAsync(zip, zipFile).then(function(retrievedZip){46 retrievedZip.files["staticresources/lwc_offplatform.resource"].async("base64").then(function(zipFile){47 _this.zipLoadAsync(zip, zipFile).then(function(resourceFolder){ 48 _this.getFileSource(resourceFolder, newZip, json).then(function(zip){49 json.files.forEach(function (file) {50 zip.file(`src/c/${json.name}/${file.name}`, `${file.source}`);51 });52 zip.file('.npmrc','registry=https://npm.lwcjs.org');53 zip.file('.yarnrc','registry "https://npm.lwcjs.org"');54 55 zip.generateAsync({ type: "blob" })56 .then(content => {57 resolve(content);58 });59 });60 }, function(e){61 reject(e);62 });63 }, function(e){64 reject(e);65 });66 }, function(e){67 reject(e);68 })69 70 71 72 });73 }74 zipLoadAsync(zip, zipFile){75 return new Promise((resolve, reject) => {76 zip.loadAsync(zipFile, {base64: true,createFolders: true}).then(function(a){77 resolve(a);78 }, function (e) {79 reject(e);80 });81 });82 }83 getFileSource(resourceFolder, zip, json) {84 return new Promise((resolve, reject) => {85 const promises = [];86 let paths = [];87 resourceFolder.forEach(function(path,fileObject){88 promises.push(fileObject.async("string"));89 paths.push(path);90 });91 try {92 Promise.all(promises).then(function (data) {93 paths.map((item, index) => { 94 let obj = {};95 obj[item] = data[index];96 data[index] = data[index].replace(/\${lwc_name}/g, json.name);97 zip.file(item, data[index])98 return obj;99 });100 resolve(zip);101 });102 } catch(e) {103 reject(e);104 }105 106 });107 }108 getJs(json) {109 return `import { Element, api } from 'engine';110 import { OMNIDEF } from './${this.getLwcName(json)}_def.js';111 /**112 * IMPORTANT! Generated class DO NOT MODIFY113 */114 export default class ${json.bpType}${json.bpSubType}${json.bpLang} extends Element {115 @api definition = OMNIDEF;116 @api value = {};117 }118 `;119 }120 getJson(json) {121 return 'export const OMNIDEF = ' + JSON.stringify(json) + ';';122 }123 getHtml(json) {124 return `<template><!-- IMPORTANT: This is a generated file. Do not modify it! -->${this.buildOmniHtml(json)}</template>`;125 }126 getMetaXml(json) {127 return `<?xml version="1.0" encoding="UTF-8"?>128 <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">129 <apiVersion>45.0</apiVersion>130 <isExposed>false</isExposed>131 </LightningComponentBundle>`;132 }133 getLwcName(json) {134 return (json.bpType + '_' + json.bpSubType + '_' + json.bpLang).toLowerCase()135 }136 getTemplateHtml(html) {137 const omniscript = document.createElement('div');138 omniscript.innerHTML = html;139 return omniscript.innerHTML;140 }141 buildOmniHtml(omniDef) {142 const omniscript = document.createElement('c-omniscript');143 omniscript.setAttribute('type', omniDef.bpType);144 omniscript.setAttribute('subtype', omniDef.bpSubType);145 omniscript.setAttribute('language', omniDef.bpLang);146 omniscript.setAttribute('definition', '{definition}');147 omniscript.setAttribute('value', '{value}');148 omniDef.children.forEach(child => {149 omniscript.appendChild(this.buildOmniStepHtml(child));150 });151 const builtHtml = omniscript.outerHTML;152 // now process the html string for attributes which are name="{something}" and remove the quotes153 return builtHtml.replace(/="({[^}]*})"/g, '=$1');154 }155 buildOmniStepHtml(stepDef) {156 const step = document.createElement('c-omniscript_step');157 step.setAttribute('name', stepDef.name);158 step.setAttribute('slot', 'omniscript_step');159 stepDef.children.forEach(child => {160 step.appendChild(this.buildOmniElement(child));161 });162 return step;163 }164 getAttributeNameForProp(propKey, elementDef) {165 switch (elementDef.componentName) {166 case 'lightning-formatted-rich-text':167 if (propKey === 'label') {168 return 'value';169 }170 return propKey;171 // allow follow through172 default: return propKey;173 }174 }175 buildOmniElement(childDef) {176 return childDef.eleArray.map(childElDef => {177 const childEl = document.createElement(childElDef.componentName);178 childEl.setAttribute('name', childElDef.name);179 Object.keys(childElDef.propSetMap).forEach(key => {180 if (childElDef.propSetMap[key]) {181 switch (key) {182 case 'controlWidth':183 childEl.setAttribute('class', 'slds-size_' + childElDef.propSetMap[key] + '-of-12 slds-p-horizontal_medium');184 break;185 default:186 if (typeof childElDef.propSetMap[key] === 'boolean') {187 childEl.setAttribute(this.getAttributeNameForProp(key, childElDef), key);188 } else {189 childEl.setAttribute(this.getAttributeNameForProp(key, childElDef), childElDef.propSetMap[key]);190 }191 }192 }193 });194 return childEl;195 })[0];196 }197}...
nested-check-parent.ts
Source:nested-check-parent.ts
1import { MockedDebugNode } from '../../mock-render/types';2import elDefCompare from './el-def-compare';3import elDefGetNode from './el-def-get-node';4import elDefGetParent from './el-def-get-parent';5const detectParent = (node: MockedDebugNode, parent: MockedDebugNode | undefined): MockedDebugNode | undefined => {6 if (parent) {7 return parent;8 }9 const expected = elDefGetParent(node);10 const currentParent = node.parent ? elDefGetNode(node.parent) : undefined;11 if (node.parent && elDefCompare(expected, currentParent)) {12 return node.parent;13 }14 for (const childNode of node.parent?.childNodes || []) {15 const childElDef = elDefGetNode(childNode);16 if (elDefCompare(expected, childElDef)) {17 return childNode;18 }19 }20 return undefined;21};...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!