Best JavaScript code snippet using ng-mocks
resolveAssets.spec.ts
Source:resolveAssets.spec.ts
1import {2 createApp,3 nodeOps,4 resolveComponent,5 resolveDirective,6 Component,7 Directive,8 resolveDynamicComponent,9 h,10 serializeInner,11 createVNode,12 Comment,13 VNode14} from '@vue/runtime-test'15describe('resolveAssets', () => {16 test('should work', () => {17 const FooBar = () => null18 const BarBaz = { mounted: () => null }19 let component1: Component | string20 let component2: Component | string21 let component3: Component | string22 let component4: Component | string23 let directive1: Directive24 let directive2: Directive25 let directive3: Directive26 let directive4: Directive27 const Root = {28 components: {29 FooBar: FooBar30 },31 directives: {32 BarBaz: BarBaz33 },34 setup() {35 return () => {36 component1 = resolveComponent('FooBar')!37 directive1 = resolveDirective('BarBaz')!38 // camelize39 component2 = resolveComponent('Foo-bar')!40 directive2 = resolveDirective('Bar-baz')!41 // capitalize42 component3 = resolveComponent('fooBar')!43 directive3 = resolveDirective('barBaz')!44 // camelize and capitalize45 component4 = resolveComponent('foo-bar')!46 directive4 = resolveDirective('bar-baz')!47 }48 }49 }50 const app = createApp(Root)51 const root = nodeOps.createElement('div')52 app.mount(root)53 expect(component1!).toBe(FooBar)54 expect(component2!).toBe(FooBar)55 expect(component3!).toBe(FooBar)56 expect(component4!).toBe(FooBar)57 expect(directive1!).toBe(BarBaz)58 expect(directive2!).toBe(BarBaz)59 expect(directive3!).toBe(BarBaz)60 expect(directive4!).toBe(BarBaz)61 })62 test('maybeSelfReference', async () => {63 let component1: Component | string64 let component2: Component | string65 let component3: Component | string66 const Foo = () => null67 const Root = {68 name: 'Root',69 components: {70 Foo,71 Root: Foo72 },73 setup() {74 return () => {75 component1 = resolveComponent('Root', true)76 component2 = resolveComponent('Foo', true)77 component3 = resolveComponent('Bar', true)78 }79 }80 }81 const app = createApp(Root)82 const root = nodeOps.createElement('div')83 app.mount(root)84 expect(component1!).toBe(Root) // explicit self name reference85 expect(component2!).toBe(Foo) // successful resolve take higher priority86 expect(component3!).toBe(Root) // fallback when resolve fails87 })88 describe('warning', () => {89 test('used outside render() or setup()', () => {90 resolveComponent('foo')91 expect(92 'resolveComponent can only be used in render() or setup().'93 ).toHaveBeenWarned()94 resolveDirective('foo')95 expect(96 'resolveDirective can only be used in render() or setup().'97 ).toHaveBeenWarned()98 })99 test('not exist', () => {100 const Root = {101 setup() {102 resolveComponent('foo')103 resolveDirective('bar')104 return () => null105 }106 }107 const app = createApp(Root)108 const root = nodeOps.createElement('div')109 app.mount(root)110 expect('Failed to resolve component: foo').toHaveBeenWarned()111 expect('Failed to resolve directive: bar').toHaveBeenWarned()112 })113 test('resolve dynamic component', () => {114 const dynamicComponents = {115 foo: () => 'foo',116 bar: () => 'bar',117 baz: { render: () => 'baz' }118 }119 let foo, bar, baz // dynamic components120 let dynamicVNode: VNode121 const Child = {122 render(this: any) {123 return this.$slots.default()124 }125 }126 const Root = {127 components: { foo: dynamicComponents.foo },128 setup() {129 return () => {130 foo = resolveDynamicComponent('foo') // <component is="foo"/>131 bar = resolveDynamicComponent(dynamicComponents.bar) // <component :is="bar"/>, function132 dynamicVNode = createVNode(resolveDynamicComponent(null)) // <component :is="null"/>133 return h(Child, () => {134 // check inside child slots135 baz = resolveDynamicComponent(dynamicComponents.baz) // <component :is="baz"/>, object136 })137 }138 }139 }140 const app = createApp(Root)141 const root = nodeOps.createElement('div')142 app.mount(root)143 expect(foo).toBe(dynamicComponents.foo)144 expect(bar).toBe(dynamicComponents.bar)145 expect(baz).toBe(dynamicComponents.baz)146 // should allow explicit falsy type to remove the component147 expect(dynamicVNode!.type).toBe(Comment)148 })149 test('resolve dynamic component should fallback to plain element without warning', () => {150 const Root = {151 setup() {152 return () => {153 return createVNode(resolveDynamicComponent('div') as string, null, {154 default: () => 'hello'155 })156 }157 }158 }159 const app = createApp(Root)160 const root = nodeOps.createElement('div')161 app.mount(root)162 expect(serializeInner(root)).toBe('<div>hello</div>')163 })164 })165 test('resolving from mixins & extends', () => {166 const FooBar = () => null167 const BarBaz = { mounted: () => null }168 let component1: Component | string169 let component2: Component | string170 let component3: Component | string171 let component4: Component | string172 let directive1: Directive173 let directive2: Directive174 let directive3: Directive175 let directive4: Directive176 const Base = {177 components: {178 FooBar: FooBar179 }180 }181 const Mixin = {182 directives: {183 BarBaz: BarBaz184 }185 }186 const Root = {187 extends: Base,188 mixins: [Mixin],189 setup() {190 return () => {191 component1 = resolveComponent('FooBar')!192 directive1 = resolveDirective('BarBaz')!193 // camelize194 component2 = resolveComponent('Foo-bar')!195 directive2 = resolveDirective('Bar-baz')!196 // capitalize197 component3 = resolveComponent('fooBar')!198 directive3 = resolveDirective('barBaz')!199 // camelize and capitalize200 component4 = resolveComponent('foo-bar')!201 directive4 = resolveDirective('bar-baz')!202 }203 }204 }205 const app = createApp(Root)206 const root = nodeOps.createElement('div')207 app.mount(root)208 expect(component1!).toBe(FooBar)209 expect(component2!).toBe(FooBar)210 expect(component3!).toBe(FooBar)211 expect(component4!).toBe(FooBar)212 expect(directive1!).toBe(BarBaz)213 expect(directive2!).toBe(BarBaz)214 expect(directive3!).toBe(BarBaz)215 expect(directive4!).toBe(BarBaz)216 })...
processNgModuleDocs.spec.js
Source:processNgModuleDocs.spec.js
1const testPackage = require('../../helpers/test-package');2const Dgeni = require('dgeni');3describe('processNgModuleDocs processor', () => {4 let processor;5 let injector;6 beforeEach(() => {7 const dgeni = new Dgeni([testPackage('angular-api-package')]);8 injector = dgeni.configureInjector();9 processor = injector.get('processNgModuleDocs');10 });11 it('should be available on the injector', () => {12 expect(processor.$process).toBeDefined();13 });14 it('should run before the correct processor', () => {15 expect(processor.$runBefore).toEqual(['createSitemap']);16 });17 it('should run after the correct processor', () => {18 expect(processor.$runAfter).toEqual(['extractDecoratedClassesProcessor', 'computeIdsProcessor']);19 });20 it('should non-arrayNgModule options to arrays', () => {21 const docs = [{22 docType: 'ngmodule',23 ngmoduleOptions: {24 a: ['AAA'],25 b: 'BBB',26 c: 4227 }28 }];29 processor.$process(docs);30 expect(docs[0].ngmoduleOptions.a).toEqual(['AAA']);31 expect(docs[0].ngmoduleOptions.b).toEqual(['BBB']);32 expect(docs[0].ngmoduleOptions.c).toEqual([42]);33 });34 it('should link directive/pipe docs with their NgModule docs (sorted by id)', () => {35 const aliasMap = injector.get('aliasMap');36 const ngModule1 = { docType: 'ngmodule', id: 'NgModule1', aliases: ['NgModule1'], ngmoduleOptions: {}};37 const ngModule2 = { docType: 'ngmodule', id: 'NgModule2', aliases: ['NgModule2'], ngmoduleOptions: {}};38 const directive1 = { docType: 'directive', id: 'Directive1', ngModules: ['NgModule1']};39 const directive2 = { docType: 'directive', id: 'Directive2', ngModules: ['NgModule2']};40 const directive3 = { docType: 'directive', id: 'Directive3', ngModules: ['NgModule1', 'NgModule2']};41 const pipe1 = { docType: 'pipe', id: 'Pipe1', ngModules: ['NgModule1']};42 const pipe2 = { docType: 'pipe', id: 'Pipe2', ngModules: ['NgModule2']};43 const pipe3 = { docType: 'pipe', id: 'Pipe3', ngModules: ['NgModule1', 'NgModule2']};44 aliasMap.addDoc(ngModule1);45 aliasMap.addDoc(ngModule2);46 processor.$process([ngModule1, pipe2, directive2, directive3, pipe1, ngModule2, directive1, pipe3]);47 expect(ngModule1.directives).toEqual([directive1, directive3]);48 expect(ngModule1.pipes).toEqual([pipe1, pipe3]);49 expect(ngModule2.directives).toEqual([directive2, directive3]);50 expect(ngModule2.pipes).toEqual([pipe2, pipe3]);51 expect(directive1.ngModules).toEqual([ngModule1]);52 expect(directive2.ngModules).toEqual([ngModule2]);53 expect(directive3.ngModules).toEqual([ngModule1, ngModule2]);54 expect(pipe1.ngModules).toEqual([ngModule1]);55 expect(pipe2.ngModules).toEqual([ngModule2]);56 expect(pipe3.ngModules).toEqual([ngModule1, ngModule2]);57 });58 it('should error if a pipe/directive does not have a `@ngModule` tag', () => {59 const log = injector.get('log');60 expect(() => {61 processor.$process([{ docType: 'directive', id: 'Directive1' }]);62 }).toThrowError('Failed to process NgModule relationships.');63 expect(log.error).toHaveBeenCalledWith(64 '"Directive1" has no @ngModule tag. Docs of type "directive" must have this tag. - doc "Directive1" (directive) ');65 expect(() => {66 processor.$process([{ docType: 'pipe', id: 'Pipe1' }]);67 }).toThrowError('Failed to process NgModule relationships.');68 expect(log.error).toHaveBeenCalledWith(69 '"Pipe1" has no @ngModule tag. Docs of type "pipe" must have this tag. - doc "Pipe1" (pipe) ');70 });71 it('should error if a pipe/directive has an @ngModule tag that does not match an NgModule doc', () => {72 const log = injector.get('log');73 expect(() => {74 processor.$process([{ docType: 'directive', id: 'Directive1', ngModules: ['MissingNgModule'] }]);75 }).toThrowError('Failed to process NgModule relationships.');76 expect(log.error).toHaveBeenCalledWith(77 '"@ngModule MissingNgModule" does not match a public NgModule - doc "Directive1" (directive) ');78 expect(() => {79 processor.$process([{ docType: 'pipe', id: 'Pipe1', ngModules: ['MissingNgModule'] }]);80 }).toThrowError('Failed to process NgModule relationships.');81 expect(log.error).toHaveBeenCalledWith(82 '"@ngModule MissingNgModule" does not match a public NgModule - doc "Pipe1" (pipe) ');83 });84 it('should error if a pipe/directive has an @ngModule tag that matches more than one NgModule doc', () => {85 const aliasMap = injector.get('aliasMap');86 const log = injector.get('log');87 const ngModule1 = { docType: 'ngmodule', id: 'NgModule1', aliases: ['NgModuleAlias'], ngmoduleOptions: {}};88 const ngModule2 = { docType: 'ngmodule', id: 'NgModule2', aliases: ['NgModuleAlias'], ngmoduleOptions: {}};89 aliasMap.addDoc(ngModule1);90 aliasMap.addDoc(ngModule2);91 expect(() => {92 processor.$process([{ docType: 'directive', id: 'Directive1', ngModules: ['NgModuleAlias'] }]);93 }).toThrowError('Failed to process NgModule relationships.');94 expect(log.error).toHaveBeenCalledWith(95 '"@ngModule NgModuleAlias" is ambiguous. Matches: NgModule1, NgModule2 - doc "Directive1" (directive) ');96 expect(() => {97 processor.$process([{ docType: 'pipe', id: 'Pipe1', ngModules: ['NgModuleAlias'] }]);98 }).toThrowError('Failed to process NgModule relationships.');99 expect(log.error).toHaveBeenCalledWith(100 '"@ngModule NgModuleAlias" is ambiguous. Matches: NgModule1, NgModule2 - doc "Pipe1" (pipe) ');101 });...
Using AI Code Generation
1describe('directive3', () => {2 beforeEach(() => {3 TestBed.configureTestingModule({4 });5 });6 it('should create an instance', () => {7 const directive = Directive3();8 expect(directive).toBeTruthy();9 });10});11import { Directive } from '@angular/core';12@Directive({13})14export class Directive3 {}15import { Directive3 } from './directive3.directive';16describe('Directive3Directive', () => {17 it('should create an instance', () => {18 const directive = new Directive3();19 expect(directive).toBeTruthy();20 });21});22"paths": {23 }24"paths": {25 }26"paths": {27 }28"paths": {29 }30"paths": {31 }32"paths": {33 }34"paths": {35 }36"paths": {37 }
Using AI Code Generation
1import {Directive, Input} from '@angular/core';2import {ComponentFixture, TestBed} from '@angular/core/testing';3import {MockBuilder, MockRender, ngMocks} from 'ng-mocks';4@Component({5 <span>{{text}}</span>6})7class Directive3 {8 @Input() text: string;9}10describe('Directive3', () => {11 ngMocks.faster();12 beforeEach(() => MockBuilder(Directive3));13 it('should render text', () => {14 const fixture = MockRender(Directive3, {text: 'test'});15 const directive = ngMocks.findInstance(Directive3);16 expect(directive.text).toEqual('test');17 expect(directive).toBeTruthy();18 });19});20import {ComponentFixture, TestBed} from '@angular/core/testing';21import {MockBuilder, MockRender, ngMocks} from 'ng-mocks';22import {Directive3} from './test';23describe('Directive3', () => {24 ngMocks.faster();25 beforeEach(() => MockBuilder(Directive3));26 it('should render text', () => {27 const fixture = MockRender(Directive3, {text: 'test'});28 const directive = ngMocks.findInstance(Directive3);29 expect(directive.text).toEqual('test');30 expect(directive).toBeTruthy();31 });32});
Using AI Code Generation
1import { directive } from 'ng-mocks';2import { directive } from 'ng-mocks';3describe('Directive: Directive', () => {4 it('should create an instance', () => {5 const directive = directive(Directive);6 expect(directive).toBeTruthy();7 });8});
Using AI Code Generation
1import {directive3} from 'ng-mocks';2import {directive} from 'ng-mocks';3import {directive2} from 'ng-mocks';4import {directive4} from 'ng-mocks';5import {directive5} from 'ng-mocks';6import {directive6} from 'ng-mocks';7import {directive7} from 'ng-mocks';8import {directive8} from 'ng-mocks';9import {directive9} from 'ng-mocks';10import {directive10} from 'ng-mocks';11import {directive11} from 'ng-mocks';12import {directive12} from 'ng-mocks';13import {directive13} from 'ng-mocks';14import {directive14} from 'ng-mocks';15import {directive15} from 'ng-mocks';16import {directive16} from 'ng-mocks';17import {directive17} from 'ng-mocks';18import {directive18} from 'ng-mocks';19import {directive19} from 'ng-mocks';20import {directive20} from 'ng-mocks';21import {directive21} from 'ng-mocks';22import {directive22} from 'ng-mocks';23import {directive23} from 'ng-mocks';
Using AI Code Generation
1var directive3 = ngMocks.directive3('myDirective', {2});3var directive3 = ngMocks.directive3('myDirective', {4});5var directive3 = ngMocks.directive3('myDirective', {6});
Using AI Code Generation
1var mockModule = require('ng-mocks');2mockModule.directive3('myDirective', function () {3 return {4 };5});6describe('myDirective', function () {7 var $compile;8 var $rootScope;9 beforeEach(function () {10 mockModule('myApp');11 });12 beforeEach(inject(function (_$compile_, _$rootScope_) {13 $compile = _$compile_;14 $rootScope = _$rootScope_;15 }));16 it('Replaces the element with the appropriate content', function () {17 var element = $compile('<my-directive></my-directive>')($rootScope);18 $rootScope.$digest();19 expect(element.html()).toContain('My Directive');20 });21});
Using AI Code Generation
1describe('mockDirective', function() {2 beforeEach(function() {3 module('app');4 });5 it('should call directive3', function() {6 var directive3 = jasmine.createSpy('directive3');7 mockDirective('directive3', directive3);8 inject(function() {9 expect(directive3).toHaveBeenCalled();10 });11 });12});13angular.module('app', [])14 .directive('directive3', function() {15 return {16 };17 });18describe('mockDirective', function() {19 beforeEach(function() {20 module('app');21 });22 it('should call directive4', function() {23 var directive4 = jasmine.createSpy('directive4');24 mockDirective('directive4', directive4);25 inject(function() {26 expect(directive4).toHaveBeenCalled();27 });28 });29});30angular.module('app', [])31 .directive('directive4', function() {32 return {33 };34 });35describe('mockDirective', function() {36 beforeEach(function() {37 module('app');38 });39 it('should call directive5', function() {40 var directive5 = jasmine.createSpy('directive5');41 mockDirective('directive5', directive5);42 inject(function() {43 expect(directive5).toHaveBeenCalled();44 });45 });46});47angular.module('app', [])48 .directive('directive5', function() {49 return {50 };51 });52describe('mockDirective', function() {53 beforeEach(function() {54 module('app');55 });56 it('should call directive6', function() {57 var directive6 = jasmine.createSpy('directive6');58 mockDirective('directive6', directive6
Using AI Code Generation
1describe('directive3', () => {2 it('should render 3 span elements', () => {3 const fixture = MockRender(`4 `);5 const spans = directive3(SpanDirective, fixture.debugElement);6 expect(spans.length).toBe(3);7 });8});9import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';10@Directive({11})12export class SpanDirective {13 @Input() set appSpan(condition: boolean) {14 if (condition) {15 this.viewContainer.createEmbeddedView(this.templateRef);16 } else {17 this.viewContainer.clear();18 }19 }20 constructor(21 ) {}22}23import { Component } from '@angular/core';24@Component({25})26export class AppComponent {}27import { MockBuilder, MockRender } from 'ng-mocks';28import { AppComponent } from './app.component';29import { SpanDirective } from './span.directive';30describe('AppComponent', () => {31 beforeEach(() => MockBuilder(AppComponent, SpanDirective));32 it('should render 3 span elements', () => {33 const fixture = MockRender(AppComponent);34 const spans = fixture.debugElement.queryAll(By.directive(SpanDirective));35 expect(spans.length).toBe(3);36 });37});38import { MockBuilder, MockRender } from 'ng-mocks';39import { SpanDirective } from './span.directive';40describe('SpanDirective', () => {41 beforeEach(() => MockBuilder(SpanDirective));42 it('should render 3 span elements', () => {43 const fixture = MockRender(`44 `);45 const spans = fixture.debugElement.queryAll(By.directive(SpanDirective));46 expect(spans.length).toBe(3);47 });
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!!