Best JavaScript code snippet using ng-mocks
htmlparser.js
Source:htmlparser.js
1//htmlå符串转æ¢æuNodeèç¹2//by zhanyi3var htmlparser = UM.htmlparser = function (htmlstr,ignoreBlank) {4 var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>))/g,5 re_attr = /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g;6 //ieä¸åå¾çhtmlå¯è½ä¼æ\nåå¨ï¼è¦å»æï¼å¨å¤çreplace(/[\t\r\n]*/g,'');代ç é«éç\nä¸è½å»é¤7 var allowEmptyTags = {8 b:1,code:1,i:1,u:1,strike:1,s:1,tt:1,strong:1,q:1,samp:1,em:1,span:1,9 sub:1,img:1,sup:1,font:1,big:1,small:1,iframe:1,a:1,br:1,pre:110 };11 htmlstr = htmlstr.replace(new RegExp(domUtils.fillChar, 'g'), '');12 if(!ignoreBlank){13 htmlstr = htmlstr.replace(new RegExp('[\\r\\t\\n'+(ignoreBlank?'':' ')+']*<\/?(\\w+)\\s*(?:[^>]*)>[\\r\\t\\n'+(ignoreBlank?'':' ')+']*','g'), function(a,b){14 //brææ¶åç¬å¤ç15 if(b && allowEmptyTags[b.toLowerCase()]){16 return a.replace(/(^[\n\r]+)|([\n\r]+$)/g,'');17 }18 return a.replace(new RegExp('^[\\r\\n'+(ignoreBlank?'':' ')+']+'),'').replace(new RegExp('[\\r\\n'+(ignoreBlank?'':' ')+']+$'),'');19 });20 }21 var uNode = UM.uNode,22 needParentNode = {23 'td':'tr',24 'tr':['tbody','thead','tfoot'],25 'tbody':'table',26 'th':'tr',27 'thead':'table',28 'tfoot':'table',29 'caption':'table',30 'li':['ul', 'ol'],31 'dt':'dl',32 'dd':'dl',33 'option':'select'34 },35 needChild = {36 'ol':'li',37 'ul':'li'38 };39 function text(parent, data) {40 if(needChild[parent.tagName]){41 var tmpNode = uNode.createElement(needChild[parent.tagName]);42 parent.appendChild(tmpNode);43 tmpNode.appendChild(uNode.createText(data));44 parent = tmpNode;45 }else{46 parent.appendChild(uNode.createText(data));47 }48 }49 function element(parent, tagName, htmlattr) {50 var needParentTag;51 if (needParentTag = needParentNode[tagName]) {52 var tmpParent = parent,hasParent;53 while(tmpParent.type != 'root'){54 if(utils.isArray(needParentTag) ? utils.indexOf(needParentTag, tmpParent.tagName) != -1 : needParentTag == tmpParent.tagName){55 parent = tmpParent;56 hasParent = true;57 break;58 }59 tmpParent = tmpParent.parentNode;60 }61 if(!hasParent){62 parent = element(parent, utils.isArray(needParentTag) ? needParentTag[0] : needParentTag)63 }64 }65 //ædtdå¤çåµå¥66// if(parent.type != 'root' && !dtd[parent.tagName][tagName])67// parent = parent.parentNode;68 var elm = new uNode({69 parentNode:parent,70 type:'element',71 tagName:tagName.toLowerCase(),72 //æ¯èªéåçå¤çä¸ä¸73 children:dtd.$empty[tagName] ? null : []74 });75 //å¦æå±æ§åå¨ï¼å¤çå±æ§76 if (htmlattr) {77 var attrs = {}, match;78 while (match = re_attr.exec(htmlattr)) {79 attrs[match[1].toLowerCase()] = utils.unhtml(match[2] || match[3] || match[4])80 }81 elm.attrs = attrs;82 }83 parent.children.push(elm);84 //å¦ææ¯èªéåèç¹è¿åç¶äº²èç¹85 return dtd.$empty[tagName] ? parent : elm86 }87 function comment(parent, data) {88 parent.children.push(new uNode({89 type:'comment',90 data:data,91 parentNode:parent92 }));93 }94 var match, currentIndex = 0, nextIndex = 0;95 //è®¾ç½®æ ¹èç¹96 var root = new uNode({97 type:'root',98 children:[]99 });100 var currentParent = root;101 while (match = re_tag.exec(htmlstr)) {102 currentIndex = match.index;103 try{104 if (currentIndex > nextIndex) {105 //text node106 text(currentParent, htmlstr.slice(nextIndex, currentIndex));107 }108 if (match[3]) {109 if(dtd.$cdata[currentParent.tagName]){110 text(currentParent, match[0]);111 }else{112 //start tag113 currentParent = element(currentParent, match[3].toLowerCase(), match[4]);114 }115 } else if (match[1]) {116 if(currentParent.type != 'root'){117 if(dtd.$cdata[currentParent.tagName] && !dtd.$cdata[match[1]]){118 text(currentParent, match[0]);119 }else{120 var tmpParent = currentParent;121 while(currentParent.type == 'element' && currentParent.tagName != match[1].toLowerCase()){122 currentParent = currentParent.parentNode;123 if(currentParent.type == 'root'){124 currentParent = tmpParent;125 throw 'break'126 }127 }128 //end tag129 currentParent = currentParent.parentNode;130 }131 }132 } else if (match[2]) {133 //comment134 comment(currentParent, match[2])135 }136 }catch(e){}137 nextIndex = re_tag.lastIndex;138 }139 //å¦æç»ææ¯ææ¬ï¼å°±æå¯è½ä¸¢æï¼æ以è¿éæå¨å¤æä¸ä¸140 //ä¾å¦ <li>sdfsdfsdf<li>sdfsdfsdfsdf141 if (nextIndex < htmlstr.length) {142 text(currentParent, htmlstr.slice(nextIndex));143 }144 return root;...
form-wizard.js
Source:form-wizard.js
1define([2 "knockout",3 "ojL10n!resources/nls/generic",4 "ojs/ojbutton",5 "ojs/ojvalidationgroup"6], function(ko,ResourceBundle) {7 "use strict";8 return function(rootParams) {9 const self = this;10 let currentParent = -1,11 currentChild = -1,12 currentNode;13 self.resources = ResourceBundle;14 self.isFirst = ko.observable(true);15 self.isLast = ko.observable(false);16 self.validationTracker = ko.observable();17 self.currentComponent = ko.observable();18 self.currentData = ko.observable();19 self.steps = ko.observableArray();20 const maskVisited = function(current) {21 current.isVisited(true);22 current.isCurrent(false);23 },24 setFirstAndLast = function() {25 if (self.steps().length) {26 self.isFirst(false);27 self.isLast(false);28 if (currentParent === 0) {29 if (self.steps()[currentParent].clickable() || currentChild === 0) {30 self.isFirst(true);31 }32 }33 if (currentParent === self.steps().length - 1) {34 if (self.steps()[currentParent].clickable() || currentChild === self.steps()[currentParent].children.length - 1) {35 self.isLast(true);36 }37 }38 }39 },40 setCurrent = function(current, index, isChild) {41 if (currentNode) {42 currentNode.isCurrent(false);43 }44 currentNode = current;45 rootParams.baseModel.registerComponent(current.componentName, current.module);46 self.currentComponent(current.componentName);47 self.currentData(current.data);48 current.isCurrent(true);49 if (isChild) {50 currentChild = index;51 } else {52 currentParent = index;53 }54 setFirstAndLast();55 },56 steps = rootParams.steps.map(function(element, index) {57 element.isVisited = ko.observable(false);58 element.isCurrent = ko.observable(false);59 element.clickable = ko.observable(true);60 if (element.children) {61 element.children = element.children.map(function(child) {62 child.isVisited = ko.observable(false);63 child.isCurrent = ko.observable(false);64 return child;65 });66 element.clickable(false);67 if (index === 0) {68 currentParent = 0;69 setCurrent(element.children[0], 0, true);70 element.isVisited(true);71 }72 } else if (index === 0) {73 setCurrent(element, 0);74 }75 return element;76 });77 ko.utils.arrayPushAll(self.steps, steps);78 self.next = function() {79 if (!rootParams.baseModel.showComponentValidationErrors(document.getElementById("form-wizard"))) {80 return;81 }82 const process = currentNode.data.process ? currentNode.data.process() : Promise.resolve();83 process.then(function() {84 if (currentChild === -1) {85 maskVisited(steps[currentParent]);86 if (steps[currentParent + 1]) {87 if (steps[currentParent + 1].clickable()) {88 setCurrent(steps[currentParent + 1], currentParent + 1);89 } else if (steps[currentParent + 1].children) {90 currentParent++;91 steps[currentParent].isVisited(true);92 setCurrent(steps[currentParent].children[0], 0, true);93 }94 }95 } else {96 maskVisited(steps[currentParent].children[currentChild]);97 if (steps[currentParent].children[currentChild + 1]) {98 setCurrent(steps[currentParent].children[currentChild + 1], currentChild + 1, true);99 } else if (steps[currentParent + 1] && steps[currentParent + 1].clickable()) {100 currentParent++;101 currentChild = -1;102 setCurrent(steps[currentParent], currentParent);103 } else if (steps[currentParent + 1] && steps[currentParent + 1].children) {104 currentParent++;105 steps[currentParent].isVisited(true);106 setCurrent(steps[currentParent].children[0], 0, true);107 }108 }109 });110 };111 self.previous = function() {112 if (currentChild === -1) {113 if (steps[currentParent - 1]) {114 currentParent--;115 if (steps[currentParent].children) {116 setCurrent(steps[currentParent].children[steps[currentParent].children.length - 1], steps[currentParent].children.length - 1, true);117 } else if (steps[currentParent].clickable()) {118 setCurrent(steps[currentParent], currentParent);119 }120 }121 } else if (steps[currentParent].children[currentChild - 1]) {122 currentChild--;123 setCurrent(steps[currentParent].children[currentChild], currentChild, true);124 } else if (steps[currentParent - 1]) {125 currentParent--;126 if (steps[currentParent].children) {127 setCurrent(steps[currentParent].children[steps[currentParent].children.length - 1], steps[currentParent].children.length - 1, true);128 } else if (steps[currentParent].clickable()) {129 currentChild = -1;130 setCurrent(steps[currentParent], currentParent);131 }132 }133 };134 self.jump = function(parent, child) {135 currentParent = parent;136 if (rootParams.baseModel.isEmpty(child)) {137 currentChild = -1;138 setCurrent(steps[currentParent], currentParent);139 } else {140 setCurrent(steps[currentParent].children[child], child, true);141 }142 };143 };...
Using AI Code Generation
1import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';2import { ChildComponent } from './child.component';3import { ParentComponent } from './parent.component';4describe('ParentComponent', () => {5 beforeEach(() => MockBuilder(ParentComponent));6 it('should create', () => {7 const fixture = MockRender(ParentComponent);8 expect(fixture.point.componentInstance).toBeDefined();9 });10 it('should render child component', () => {11 const fixture = MockRender(ParentComponent);12 expect(ngMocks.find(ChildComponent)).toBeDefined();13 });14 it('should render child component', () => {15 const fixture = MockRender(ParentComponent);16 expect(ngMocks.find(ChildComponent)).toBeDefined();17 });18 it('should render child component', () => {19 const fixture = MockRender(ParentComponent);20 expect(ngMocks.find(ChildComponent)).toBeDefined();21 });22 it('should render child component', () => {23 const fixture = MockRender(ParentComponent);24 expect(ngMocks.find(ChildComponent)).toBeDefined();25 });26 it('should render child component', () => {27 const fixture = MockRender(ParentComponent);28 expect(ngMocks.find(ChildComponent)).toBeDefined();29 });30 it('should render child component', () => {31 const fixture = MockRender(ParentComponent);32 expect(ngMocks.find(ChildComponent)).toBeDefined();33 });34 it('should render child component', () => {35 const fixture = MockRender(ParentComponent);36 expect(ngMocks.find(ChildComponent)).toBeDefined();37 });38 it('should render child component', () => {39 const fixture = MockRender(ParentComponent);40 expect(ngMocks.find(ChildComponent)).toBeDefined();41 });42 it('should render child component', () => {43 const fixture = MockRender(ParentComponent);44 expect(ngMocks.find(ChildComponent)).toBeDefined();45 });46 it('should render child component', () => {47 const fixture = MockRender(ParentComponent);48 expect(ngMocks.find(ChildComponent)).toBeDefined();49 });50 it('should render child component', () => {51 const fixture = MockRender(ParentComponent);52 expect(ngMocks.find(ChildComponent)).toBeDefined();53 });54 it('should render child component', () => {55 const fixture = MockRender(ParentComponent);56 expect(ngMocks.find(ChildComponent
Using AI Code Generation
1import { currentParent } from 'ng-mocks';2import { Component } from '@angular/core';3import { TestBed } from '@angular/core/testing';4@Component({5})6class ParentComponent {}7@Component({8})9class ChildComponent {}10describe('currentParent', () => {11 let parent: ParentComponent;12 beforeEach(() => {13 TestBed.configureTestingModule({14 });15 const fixture = TestBed.createComponent(ParentComponent);16 parent = fixture.componentInstance;17 fixture.detectChanges();18 });19 it('should get current parent', () => {20 const child = currentParent(ChildComponent);21 expect(child).toBe(parent);22 });23});24import 'ng-mocks';25const config = (config) => {26 config.frameworks.push('ng-mocks');27 return config;28};29module.exports = (config) => {30 config = require('./karma.conf')(config);31 return config;32};33const config = (config) => {34 config.frameworks.push('ng-mocks');35 return config;36};37module.exports = (config) => {38 config = require('./karma.conf')(config);39 return config;40};41const config = (config) => {42 config.frameworks.push('ng-mocks');43 return config;44};45module.exports = (config) => {46 config = require('./karma.conf')(config);47 return config;48};49const config = (config) => {50 config.frameworks.push('ng-mocks');51 return config;52};53module.exports = (config) => {54 config = require('./karma.conf')(config);55 return config;56};57const config = (config) => {58 config.frameworks.push('ng-mocks');59 return config;60};61module.exports = (config) => {62 config = require('./karma.conf')(config
Using AI Code Generation
1import { currentParent } from 'ng-mocks';2@Component({3})4class ChildComponent {}5@Component({6})7class ParentComponent {8 constructor() {9 console.log('currentParent', currentParent());10 }11}12@NgModule({13 imports: [BrowserModule],14})15class AppModule {}16platformBrowserDynamic().bootstrapModule(AppModule);17import { currentParent } from 'ng-mocks';18@Component({19})20class ChildComponent {}21@Component({22})23class ParentComponent {24 constructor() {25 console.log('currentParent', currentParent());26 }27}28@NgModule({29 imports: [BrowserModule],30})31class AppModule {}32platformBrowserDynamic().bootstrapModule(AppModule);33import { TestBed } from '@angular/core/testing';34import { AppComponent } from './app.component';35describe('AppComponent', () => {36 beforeEach(async () => {37 await TestBed.configureTestingModule({38 }).compileComponents();39 });40 it('should create the app', () => {41 const fixture = TestBed.createComponent(AppComponent);42 const app = fixture.componentInstance;43 expect(app).toBeTruthy();44 });45 it(`should have as title 'app'`, () => {46 const fixture = TestBed.createComponent(AppComponent);47 const app = fixture.componentInstance;48 expect(app.title).toEqual('app');49 });50 it('should render title', () => {51 const fixture = TestBed.createComponent(AppComponent);52 fixture.detectChanges();53 const compiled = fixture.nativeElement;54 expect(compiled.querySelector('.content span').textContent).toContain('app app is running!');55 });56});57import { TestBed } from '@angular/core/testing';58import { AppComponent } from './app.component';59describe('AppComponent', () => {60 beforeEach(async () => {61 await TestBed.configureTestingModule({62 }).compileComponents();63 });64 it('should create
Using AI Code Generation
1import { currentParent } from 'ng-mocks';2@Component({3})4export class ParentComponent {5 public ngAfterViewInit(): void {6 const child = currentParent(ChildComponent);7 console.log(child);8 }9}10@Component({11})12export class ChildComponent {}13import { MockBuilder, MockRender } from 'ng-mocks';14import { ChildComponent, ParentComponent } from './test';15describe('ParentComponent', () => {16 beforeEach(() => MockBuilder(ParentComponent));17 it('should get child component', () => {18 const fixture = MockRender(ParentComponent);19 fixture.detectChanges();20 expect(currentParent(ChildComponent)).toBeTruthy();21 });22});
Using AI Code Generation
1import { TestBed } from '@angular/core/testing';2import { Component, ViewChild, ElementRef } from '@angular/core';3import { MockBuilder, MockRender, MockInstance, MockedComponentFixture } from 'ng-mocks';4@Component({5})6class ChildComponent {}7@Component({8})9class ParentComponent {10 @ViewChild(ChildComponent, { static: true })11 public child: ChildComponent;12 public get childElement(): ElementRef {13 return this.child.elementRef;14 }15}16describe('ParentComponent', () => {17 let fixture: MockedComponentFixture<ParentComponent>;18 let component: ParentComponent;19 beforeEach(() => MockBuilder(ParentComponent).keep(ChildComponent));20 beforeEach(() => {21 fixture = MockRender(ParentComponent);22 component = fixture.point.componentInstance;23 });24 it('should create', () => {25 expect(component).toBeTruthy();26 });27 it('should have child', () => {28 expect(component.child).toBeTruthy();29 });30 it('should have childElement', () => {31 expect(component.childElement).toBeTruthy();32 });33});34import 'zone.js/dist/zone-testing';35import { getTestBed } from '@angular/core/testing';36import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';37declare const require: any;38getTestBed().initTestEnvironment(39 platformBrowserDynamicTesting()40);41const context = require.context('./', true, /\.spec\.ts$/);42context.keys().map(context);43{44 "compilerOptions": {45 "paths": {46 },
Using AI Code Generation
1describe('Unit: Testing Controllers', function() {2 var scope, ctrl, $httpBackend;3 beforeEach(module('myApp'));4 beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {5 $httpBackend = _$httpBackend_;6 respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);7 scope = $rootScope.$new();8 ctrl = $controller('PhoneListCtrl', {$scope: scope});9 }));10 it('should create "phones" model with 2 phones fetched from xhr', function() {11 expect(scope.phones).toBeUndefined();12 $httpBackend.flush();13 expect(scope.phones).toEqual([{name: 'Nexus S'},14 {name: 'Motorola DROID'}]);15 });16 it('should set the default value of orderProp model', function() {17 expect(scope.orderProp).toBe('age');18 });19});20describe('Unit: Testing Controllers', function() {21 var scope, ctrl, $httpBackend;22 beforeEach(module('myApp'));23 beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {24 $httpBackend = _$httpBackend_;25 respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);26 scope = $rootScope.$new();27 ctrl = $controller('PhoneListCtrl', {$scope: scope});28 }));29 it('should create "phones" model with 2 phones fetched from xhr', function() {30 expect(scope.phones).toBeUndefined();31 $httpBackend.flush();32 expect(scope.phones).toEqual([{name: 'Nexus S'},33 {name: 'Motorola DROID'}]);34 });35 it('should set the default value of orderProp model', function() {36 expect(scope.orderProp).toBe('age');37 });38});39 <span>{{phone.name}}</span>40 <p>{{phone.snippet}}</p>
Using AI Code Generation
1import { Component, Input } from '@angular/core';2import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';3@Component({4 template: `{{name}}`,5})6class ParentComponent {7 @Input() public name = 'parent';8}9@Component({10 template: `{{name}}`,11})12class ChildComponent {13 @Input() public name = 'child';14}15describe('currentParent', () => {16 beforeEach(() => MockBuilder(ParentComponent, ChildComponent));17 it('should get the parent component of the current component', () => {18 const fixture = MockRender(`19 `);20 const child = ngMocks.find(ChildComponent);21 const parent = ngMocks.currentParent(child);22 expect(parent).toBe(ngMocks.find(ParentComponent));23 });24});25import { Component, Input } from '@angular/core';26import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';27@Component({28 template: `{{name}}`,29})30class ParentComponent {31 @Input() public name = 'parent';32}33@Component({34 template: `{{name}}`,35})36class ChildComponent {37 @Input() public name = 'child';38}39describe('currentParent', () => {40 beforeEach(() => MockBuilder(ParentComponent, ChildComponent));41 it('should get the parent component of the current component', () => {42 const fixture = MockRender(`43 `);44 const child = ngMocks.find(ChildComponent);45 const parent = ngMocks.currentParent(child);46 expect(parent).toBe(ngMocks.find(ParentComponent));47 });48});
Using AI Code Generation
1describe('TestComponent', () => {2 let component: TestComponent;3 let fixture: ComponentFixture<TestComponent>;4 let parentComponent: ParentComponent;5 beforeEach(async(() => {6 TestBed.configureTestingModule({7 })8 .compileComponents();9 }));10 beforeEach(() => {11 fixture = TestBed.createComponent(TestComponent);12 component = fixture.componentInstance;13 parentComponent = fixture.debugElement.query(By.directive(ParentComponent)).componentInstance;14 fixture.detectChanges();15 });16 it('should create', () => {17 expect(component).toBeTruthy();18 });19 it('should set the input value of child component', () => {20 parentComponent.setValue('Hello World');21 expect(component.childComponent.inputValue).toBe('Hello World');22 });23 it('should call the method of child component', () => {24 parentComponent.callMethod();25 expect(component.childComponent.methodCalled).toBeTruthy();26 });27});28import { Component, OnInit, Input } from '@angular/core';29@Component({30})31export class ChildComponent implements OnInit {32 @Input() inputValue: string;33 methodCalled: boolean = false;34 constructor() { }35 ngOnInit() {36 }37 method() {38 this.methodCalled = true;39 }40}41import { Component, OnInit, ViewChild } from '@angular/core';42import { ChildComponent } from '../child/child.component';43@Component({44})45export class ParentComponent implements OnInit {46 @ViewChild(ChildComponent) childComponent: ChildComponent;47 constructor() { }48 ngOnInit() {49 }50 setValue(value: string) {51 this.childComponent.inputValue = value;52 }53 callMethod() {54 this.childComponent.method();55 }56}57import { Component, OnInit, ViewChild } from '@
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!!