Questions tagged unit-testing

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

Unit test a return methods output value

I have a method that a string enters, its transforms then returns a new string value. I have tried numerous ways to get it to test correctly, as you can see below there are a few lines commented out… spec.ts const string = "testing%20this.jpg"; //input const sanitisedString = "testing this"; //expected output it(‘should sanitiseString’, () […]

Inconsistency jasmine test with tick and auditTime

After check several posts I can’t find a solution to the issue I’m faced of. I have a form which is saved each 2 seconds in database if a value changes. So in the app.ts looks like this: ngAfterViewInit() { this.form.valueChanges .pipe( auditTime(2000), distinctUntilChanged(…), concatMap(() => { return this.saveform(); }) ) .subscribe(…); And to test […]

By sha
Published
Categorized as angular, jasmine, karma-jasmine, unit-testing Tagged , , ,

Angular 11 – Test case writing – Jasmine – deprecation issue

I’m trying to create my very first test case using Jasmine and its my first attempt in writing test cases(actually just started learning). I’m currently referring This site to write test case Here I’m facing two issues. The signature ‘(token: any, notFoundValue?: any): any’ of ‘TestBed.get’ is deprecated.ts(6387) testing.d.ts(382, 9): The declaration was marked as […]

Handle set time out in unit testing angular

I have function which handling setTimeOut. I have seen the answer from stackoverflow, but it still throw error in karma. I have function like this in component.ts doEditName() { if (this.nameConfig.canEdit === true) { this.nameConfig.isEdit = true; setTimeout(() => { document.getElementById(‘name-editor’).focus(); }, 50); } } and in its component.spec.ts, I write like this: it(‘doEditTitle function’, […]

By justTrustMe
Published
Categorized as angular, jasmine, jestjs, unit-testing Tagged , , ,

How can I unit test if certain website has been opened using Karma – Jasmine?

I have a list of components, some of them come with the property "factor" and others comes with the property "quickpay". I created a couple getters for them: get hasFactor(): boolean { return this.load.factor; } get hasQuickpay(): boolean { return this.load.quickpay; } and then created a function that among other things opens a certain URL […]

How to unit test window.location.reload in angular 12 unit testing?

In my angular project, I have a function reloadPage(): void { window.location.reload(); } So when ever I need to reload the page, I used to call this function. Now I am trying to add the unit testing for this function but it is not working. it("reloadPage: should be validated", () => { spyOn(window.location, ‘reload’).and.callFake(() => […]

testing an angular service without an application

I have only one .ts file (not angular project) containing a single service it is just one file, not a complete angular project, and it is even not an angular library (doesn’t contain angular.json at all) I can import this service to any angular application and it works fine. I want to add a test […]

How to mock Cropperjs functions in angular 12 unit test?

I am using Cropperjs for my Angular 12 project. I have added the Cropper in declarations. But it is giving the error Error: Can’t resolve all parameters for Cropper: (?). How can I mock the functions of Cropperjs inside angular? Is there any way.. If so please guide me. Thanks in advance Source: Angular Questions

Angular Unit Testing for HostListener (Window Resize)

I have a component file that has below code @HostListener(‘window:resize’, [‘$event’]) onResize(event) { this.width = event.target.innerWidth; } I want to test it. I have tried with the below code in my spec file. window.dispatchEvent(new Event("resize"); expect(component.onResize).toHaveBeenCalled(); but it is not working. Any help will be appreciated. Source: Angular Questions

Unit testing Router events angular

I have something like this ngOnInit(): void { this.router.events.forEach((event) => { if (event instanceof NavigationEnd) { if (this.route.root.firstChild?.snapshot.data.title) { this.showHeader = this.route.root.firstChild?.snapshot.data.header; this.titleService.setTitle( this.route.root.firstChild?.snapshot.data.title ); } } }); } In my app component, i need to make unit test for this, I dont know where I am wrong this is my spec app.component.spec.ts import { […]

By Miomir Dancevic
Published
Categorized as angular, typescript, unit-testing Tagged , ,
1 2 3 82

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
faq