Best JavaScript code snippet using ng-mocks
contextualLifeCycleRule.js
Source:contextualLifeCycleRule.js
1"use strict";2var __extends = (this && this.__extends) || (function () {3 var extendStatics = Object.setPrototypeOf ||4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };6 return function (d, b) {7 extendStatics(d, b);8 function __() { this.constructor = d; }9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());10 };11})();12Object.defineProperty(exports, "__esModule", { value: true });13var sprintf_js_1 = require("sprintf-js");14var Lint = require("tslint");15var ngWalker_1 = require("./angular/ngWalker");16var Rule = (function (_super) {17 __extends(Rule, _super);18 function Rule() {19 return _super !== null && _super.apply(this, arguments) || this;20 }21 Rule.prototype.apply = function (sourceFile) {22 return this.applyWithWalker(new ClassMetadataWalker(sourceFile, this.getOptions()));23 };24 Rule.metadata = {25 description: 'Ensure that classes use allowed life cycle method in its body.',26 options: null,27 optionsDescription: 'Not configurable.',28 rationale: 'Some life cycle methods can only be used in certain class types.For example, ngOnInit() hook method should not be used in an @Injectable class.',29 ruleName: 'contextual-life-cycle',30 type: 'functionality',31 typescriptOnly: true32 };33 Rule.FAILURE_STRING = 'In the class "%s" which have the "%s" decorator, the "%s" hook method is not allowed. Please, drop it.';34 return Rule;35}(Lint.Rules.AbstractRule));36exports.Rule = Rule;37var ClassMetadataWalker = (function (_super) {38 __extends(ClassMetadataWalker, _super);39 function ClassMetadataWalker() {40 var _this = _super !== null && _super.apply(this, arguments) || this;41 _this.isInjectable = false;42 _this.isComponent = false;43 _this.isDirective = false;44 _this.isPipe = false;45 return _this;46 }47 ClassMetadataWalker.prototype.visitMethodDeclaration = function (method) {48 var methodName = method.name.getText();49 if (methodName === 'ngOnInit') {50 if (this.isInjectable) {51 this.generateFailure(method, this.className, '@Injectable', 'ngOnInit()');52 }53 else if (this.isPipe) {54 this.generateFailure(method, this.className, '@Pipe', 'ngOnInit()');55 }56 }57 if (methodName === 'ngOnChanges') {58 if (this.isInjectable) {59 this.generateFailure(method, this.className, '@Injectable', 'ngOnChanges()');60 }61 else if (this.isPipe) {62 this.generateFailure(method, this.className, '@Pipe', 'ngOnChanges()');63 }64 }65 if (methodName === 'ngDoCheck') {66 if (this.isInjectable) {67 this.generateFailure(method, this.className, '@Injectable', 'ngDoCheck()');68 }69 else if (this.isPipe) {70 this.generateFailure(method, this.className, '@Pipe', 'ngDoCheck()');71 }72 }73 if (methodName === 'ngAfterContentInit') {74 if (this.isInjectable) {75 this.generateFailure(method, this.className, '@Injectable', 'ngAfterContentInit()');76 }77 else if (this.isPipe) {78 this.generateFailure(method, this.className, '@Pipe', 'ngAfterContentInit()');79 }80 }81 if (methodName === 'ngAfterContentChecked') {82 if (this.isInjectable) {83 this.generateFailure(method, this.className, '@Injectable', 'ngAfterContentChecked()');84 }85 else if (this.isPipe) {86 this.generateFailure(method, this.className, '@Pipe', 'ngAfterContentChecked()');87 }88 }89 if (methodName === 'ngAfterViewInit') {90 if (this.isInjectable) {91 this.generateFailure(method, this.className, '@Injectable', 'ngAfterViewInit()');92 }93 else if (this.isPipe) {94 this.generateFailure(method, this.className, '@Pipe', 'ngAfterViewInit()');95 }96 }97 if (methodName === 'ngAfterViewChecked') {98 if (this.isInjectable) {99 this.generateFailure(method, this.className, '@Injectable', 'ngAfterViewChecked()');100 }101 else if (this.isPipe) {102 this.generateFailure(method, this.className, '@Pipe', 'ngAfterViewChecked()');103 }104 }105 _super.prototype.visitMethodDeclaration.call(this, method);106 };107 ClassMetadataWalker.prototype.visitNgInjectable = function (controller, decorator) {108 this.className = controller.name.text;109 this.isInjectable = true;110 this.isComponent = false;111 this.isDirective = false;112 this.isPipe = false;113 _super.prototype.visitNgInjectable.call(this, controller, decorator);114 };115 ClassMetadataWalker.prototype.visitNgComponent = function (metadata) {116 this.className = metadata.controller.name.text;117 this.isComponent = true;118 this.isInjectable = false;119 this.isDirective = false;120 this.isPipe = false;121 _super.prototype.visitNgComponent.call(this, metadata);122 };123 ClassMetadataWalker.prototype.visitNgDirective = function (metadata) {124 this.className = metadata.controller.name.text;125 this.isDirective = true;126 this.isInjectable = false;127 this.isComponent = false;128 this.isPipe = false;129 _super.prototype.visitNgDirective.call(this, metadata);130 };131 ClassMetadataWalker.prototype.visitNgPipe = function (controller, decorator) {132 this.className = controller.name.text;133 this.isPipe = true;134 this.isInjectable = false;135 this.isComponent = false;136 this.isDirective = false;137 _super.prototype.visitNgPipe.call(this, controller, decorator);138 };139 ClassMetadataWalker.prototype.generateFailure = function (method) {140 var failureConfig = [];141 for (var _i = 1; _i < arguments.length; _i++) {142 failureConfig[_i - 1] = arguments[_i];143 }144 this.addFailureAtNode(method, sprintf_js_1.vsprintf(Rule.FAILURE_STRING, failureConfig));145 };146 return ClassMetadataWalker;147}(ngWalker_1.NgWalker));...
decoratorNotAllowedRule.js
Source:decoratorNotAllowedRule.js
1"use strict";2var __extends = (this && this.__extends) || (function () {3 var extendStatics = Object.setPrototypeOf ||4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };6 return function (d, b) {7 extendStatics(d, b);8 function __() { this.constructor = d; }9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());10 };11})();12Object.defineProperty(exports, "__esModule", { value: true });13var sprintf_js_1 = require("sprintf-js");14var Lint = require("tslint");15var ngWalker_1 = require("./angular/ngWalker");16var Rule = (function (_super) {17 __extends(Rule, _super);18 function Rule() {19 return _super !== null && _super.apply(this, arguments) || this;20 }21 Rule.prototype.apply = function (sourceFile) {22 return this.applyWithWalker(new ClassMetadataWalker(sourceFile, this.getOptions()));23 };24 Rule.metadata = {25 description: 'Ensure that classes use allowed decorator in its body.',26 options: null,27 optionsDescription: 'Not configurable.',28 rationale: 'Some decorators can only be used in certain class types. For example, an @Input should not be used in an @Injectable class.',29 ruleName: 'decorator-not-allowed',30 type: 'functionality',31 typescriptOnly: true32 };33 Rule.FAILURE_STRING = 'In the class "%s" which have the "%s" decorator, the "%s" decorator is not allowed. Please, drop it.';34 return Rule;35}(Lint.Rules.AbstractRule));36exports.Rule = Rule;37var ClassMetadataWalker = (function (_super) {38 __extends(ClassMetadataWalker, _super);39 function ClassMetadataWalker() {40 var _this = _super !== null && _super.apply(this, arguments) || this;41 _this.isInjectable = false;42 return _this;43 }44 ClassMetadataWalker.prototype.visitNgInjectable = function (classDeclaration, decorator) {45 this.className = classDeclaration.name.text;46 this.isInjectable = true;47 _super.prototype.visitNgInjectable.call(this, classDeclaration, decorator);48 };49 ClassMetadataWalker.prototype.visitNgDirective = function (metadata) {50 this.isInjectable = false;51 _super.prototype.visitNgDirective.call(this, metadata);52 };53 ClassMetadataWalker.prototype.visitNgPipe = function (controller, decorator) {54 this.isInjectable = false;55 _super.prototype.visitNgPipe.call(this, controller, decorator);56 };57 ClassMetadataWalker.prototype.visitNgComponent = function (metadata) {58 this.isInjectable = false;59 _super.prototype.visitNgComponent.call(this, metadata);60 };61 ClassMetadataWalker.prototype.visitNgInput = function (property, input, args) {62 if (this.isInjectable) {63 this.generateFailure(property, this.className, '@Injectable', '@Input');64 }65 _super.prototype.visitNgInput.call(this, property, input, args);66 };67 ClassMetadataWalker.prototype.visitNgOutput = function (property, input, args) {68 if (this.isInjectable) {69 this.generateFailure(property, this.className, '@Injectable', '@Output');70 }71 _super.prototype.visitNgInput.call(this, property, input, args);72 };73 ClassMetadataWalker.prototype.visitNgHostBinding = function (property, decorator, args) {74 if (this.isInjectable) {75 this.generateFailure(property, this.className, '@Injectable', '@HostBinding');76 }77 _super.prototype.visitNgHostBinding.call(this, property, decorator, args);78 };79 ClassMetadataWalker.prototype.visitNgHostListener = function (method, decorator, args) {80 if (this.isInjectable) {81 this.generateFailure(method, this.className, '@Injectable', '@HostListener');82 }83 _super.prototype.visitNgHostListener.call(this, method, decorator, args);84 };85 ClassMetadataWalker.prototype.visitNgContentChild = function (property, input, args) {86 if (this.isInjectable) {87 this.generateFailure(property, this.className, '@Injectable', '@ContentChild');88 }89 _super.prototype.visitNgContentChild.call(this, property, input, args);90 };91 ClassMetadataWalker.prototype.visitNgContentChildren = function (property, input, args) {92 if (this.isInjectable) {93 this.generateFailure(property, this.className, '@Injectable', '@ContentChildren');94 }95 _super.prototype.visitNgContentChildren.call(this, property, input, args);96 };97 ClassMetadataWalker.prototype.visitNgViewChild = function (property, input, args) {98 if (this.isInjectable) {99 this.generateFailure(property, this.className, '@Injectable', '@ViewChild');100 }101 _super.prototype.visitNgViewChild.call(this, property, input, args);102 };103 ClassMetadataWalker.prototype.visitNgViewChildren = function (property, input, args) {104 if (this.isInjectable) {105 this.generateFailure(property, this.className, '@Injectable', '@ViewChildren');106 }107 _super.prototype.visitNgViewChildren.call(this, property, input, args);108 };109 ClassMetadataWalker.prototype.generateFailure = function (property) {110 var failureConfig = [];111 for (var _i = 1; _i < arguments.length; _i++) {112 failureConfig[_i - 1] = arguments[_i];113 }114 this.addFailureAtNode(property, sprintf_js_1.vsprintf(Rule.FAILURE_STRING, failureConfig));115 };116 return ClassMetadataWalker;117}(ngWalker_1.NgWalker));...
Using AI Code Generation
1describe('test', () => {2 it('should be injectable', () => {3 expect(ngMocks.isInjectable(TestService)).toBeTruthy();4 });5});6@Injectable()7export class TestService {8 constructor() {}9}
Using AI Code Generation
1const isInjectable = ngMocks.isInjectable;2isInjectable(service: Type): boolean3const isInjectable = ngMocks.isInjectable;4class Service {5 constructor() {}6}7const isServiceInjectable = isInjectable(Service);8const isInjectable = ngMocks.isInjectable;9class Service {10 constructor() {}11}12const isServiceInjectable = isInjectable(Service);13const isInjectable = ngMocks.isInjectable;14class Service {15 constructor() {}16}17const isServiceInjectable = isInjectable(Service);18const isInjectable = ngMocks.isInjectable;19class Service {20 constructor() {}21}
Using AI Code Generation
1import { isInjectable } from 'ng-mocks';2import { Injectable } from '@angular/core';3@Injectable()4export class TestService {5 constructor() { }6}7describe('TestService', () => {8 it('should be created', () => {9 expect(isInjectable(TestService)).toBeTruthy();10 });11});12import { TestBed } from '@angular/core/testing';13import { TestService } from './test.service';14describe('TestService', () => {15 beforeEach(() => TestBed.configureTestingModule({}));16 it('should be created', () => {17 const service: TestService = TestBed.get(TestService);18 expect(service).toBeTruthy();19 });20});21import { TestBed } from '@angular/core/testing';22import { TestService } from './test.service';23describe('TestService', () => {24 beforeEach(() => TestBed.configureTestingModule({}));25 it('should be created', () => {26 const service: TestService = TestBed.get(TestService);27 expect(service).toBeTruthy();28 });29});30import { TestBed } from '@angular/core/testing';31import { TestService } from './test.service';32describe('TestService', () => {33 beforeEach(() => TestBed.configureTestingModule({}));34 it('should be created', () => {35 const service: TestService = TestBed.get(TestService);36 expect(service).toBeTruthy();37 });38});
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!!