Questions tagged unit-testing

Explore the latest questions and answers asked by our top developers.

How to mock / spyOn constants when they are outside the spectator.component

I need to decrease the CHARACTER_LIMIT value for the test because I don’t see any point in creating test data that will have more than 5,000 characters long… const CHARACTER_LIMIT = 5000; const NOT_ALLOWED_SIGN = ‘|’; @Component({ selector: ‘app-example’, templateUrl: ‘./example.component.html’, styleUrls: [‘./example.scss’], changeDetection: ChangeDetectionStrategy.OnPush }) export class ExampleComponent { … Source: Angular Questions

How to write Unit test case for below function

Below code is working but code is not written as it is supposed to do. Because function is returning something but I as per my unit test function is returning value TS Code: checkSelectionFromDeletable() { return this.numbersFacade.selectedRows$.pipe( map(selectedRowCapsule => selectedRowCapsule?.selectedRows.find(x => x.owner || x.mainNumberFlag) ) ); } spec.ts it(‘should test checkSelectionFromDeletable’, () => { const […]

By anchal gaur
Categorized as angular, jasmine, rxjs, typescript, unit-testing Tagged , , , ,

NG0100: ExpressionChangedAfterItHasBeenCheckedError in Angular Unit test case

I am getting the below error when running a unit test case, these unit test cases are were passed when using Angular 8, after recent Angular 12 upgrade I am getting the below error. NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: ‘true’. Current value: ‘false’.. Find more at I tried […]

Angular Jasmine Unit Testing: Mocking the results of a HTTP Request that is called in a service called from a component

Hi folks: I am attempting to write some unit tests for a change to a component where we change the order in which objects are returned from a service. I’m really struggling with how to mock the http request. These tests are to be written in karma/jasmine for angular plans-component.ts // this method is called […]

How to identify which modules , dependencies add to testbed in angular unit test

Currently im working on angular unit test with jasmine framework, I have few questions related testing. 1..Im bit confused with which modules need to add dependencies, providers and imports section in testbed. My constructor function as below in component.ts constructor( private employeeService: EmployeeService, private formBuilder: FormBuilder, private router: Router, private activatedRoute: ActivatedRoute, private customerService: CustomerService, […]

How can i test activatedRoute with query params

i need to test this component which uses activatedRoute with queryparams method. i am new in testing and all i know is that i have to include activateRoute in the providers of the test, thanks in advance for your help export class PasswordResetComponent implements OnInit { oobCode: string = ”; constructor(private route: ActivatedRoute) { } […]

How to write angular unit test for function and switch case

extractValue(exemptionLevel: string, exemptionValue: any): string { switch(exemptionLevel) { case "C": return exemptionValue.isoCountryCode.toString(); case "R": return exemptionValue.districtNumber.toString(); case "D": return exemptionValue.divisionCode.toString(); case "T": return exemptionValue.terminalNumber.toString(); } } i tried below it(‘expected value extractValue() c’, () => { component.extractValue(‘C’,’isoCountry’); expect(‘C’).toEqual(‘isoCountry’) // expect(component.extractValue(expectedLevel[0], exemptionValue.isoCountryCode)).toHaveBeenCalled(); }); it showing error TypeError: Cannot read property ‘toString’ of undefined Source: Angular Questions

By Angular doubt
Categorized as angular, unit-testing Tagged ,

How to get toHaveBeenCalledTimes(9) with a nested function in jasmine?

I’m trying to make a unit test to check if a function is called a maximum of 9 times, the following code is an approximation of the real code: function method1( event ) { method2(event); } function method3( event ) { input.value += event.key; } function method2(event) { if (input.length < 9) method3(event); } As […]

How to unit test capacitor plugin function calls (Ionic 5)

I have a function that specifically uses a plugin called @capacitor/keyboard, so I want to know if it is possible to test the call to some function of the plugin. The problem I have is that the plugin does not work for the web and it is throwing me that error. My function is this: […]

angular Unit test cdkDragEntered with jasmine

I have implemented drag and drop components with angular and angular material in HTML <div class="sortable-list-view-container" cdkDropListGroup> <div [ngClass]="itemClass" *ngFor="let item of items; let i = index" cdkDropList [cdkDropListData]="i"> <div cdkDrag [cdkDragData]="i" (cdkDragEntered)="dragEntered($event)" class="sortable-list-view-box" > <ng-container *ngTemplateOutlet="itemTemplate; context: { $implicit: item }"></ng-container> </div> </div> </div> for the component itself import { CdkDragEnter, moveItemInArray } from […]

1 2 3 100

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us