Angular unit testing prime ng calendar

Issue Inside a unit test, when I have a regular element I can set its value, trigger a change event and verify the form value matches like so: const hostElement = fixture.nativeElement; const userIdSelect: HTMLInputElement = hostElement.querySelector(‘select[formcontrolname=”userId”]’); userIdSelect.value = ‘myUser’;

Continue reading

How to test controller separately?

Issue I have a controller for changing some DOM from different places. The controller is: angular.module(‘myModule’).controller(‘myController’, myController); function myController() { this.addSomeClass = function() { $(‘#idOfSomeElement’).addClass(‘someClass’); }; } And It is used like this. Inside different components and html. <div id=”idOfSomeElement”></div>

Continue reading

unit testing of many simple *ngIf

Issue I have simply component @Component({ selector: ‘app-certificate-panel’, templateUrl: ‘./certificate.component.html’ }) export class CertificateComponent { @Input() public certificate: Certificate; } with template <h3 translate>General</h3> <x-form-row label=”{{‘Version’ | translate}}”> {{ certificate.version }} </x-form-row> <x-form-row label=”{{‘Serial Number’ | translate}}”> {{ certificate.serialNumber }}

Continue reading

How do I provide ControlContainer inside an Angular component's unit test?

Issue As per the title, how can I provide ControlContainer inside an Angular unit test? The following is the error produced: NullInjectorError: StaticInjectorError(DynamicTestModule)[ChildFormComponent -> ControlContainer]: StaticInjectorError(Platform: core)[ChildFormComponent -> ControlContainer]: NullInjectorError: No provider for ControlContainer! My attempt to provide ControlContainer: beforeEach(()

Continue reading

Angular: Testing Http POST method

Issue I have a simple service method that does an http post call to add a todoList: add(event: any): Observable<TodoList> { let todoListToAdd: TodoList = { name: event.target.value, listItems: []}; return this.http.post<TodoList>("https://localhost:44305/todolist", todoListToAdd); } I want to unit test this

Continue reading

Angular how to test service methods?

Issue I’m trying to learn how to write tests. I don’t understand how to mock and test methods from services. My current service file is the following: import {Injectable} from ‘@angular/core’; import {HttpClient, HttpErrorResponse} from ‘@angular/common/http’; import {Observable, throwError} from

Continue reading

How do I mock $window injected manually in provider private function?

Issue I have the following provider: angular.module(‘MyApp’).provider(‘MyDevice’, function () { var ngInjector = angular.injector([‘ng’]), $window = ngInjector.get(‘$window’); function isMobileDevice () { return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/) .test($window.navigator.userAgent || $window.navigator.vendor || $window.opera); } this.$get = function () { return { isDesktop: function ()

Continue reading

How to unit test (using Jasmine) a function in a controller which calls a factory service which returns a promise

Issue In the below SampleController, how do I unit test that postAttributes function calls sampleService.updateMethod. I’m having trouble since the updateMethod returns promise. angular.module(‘sampleModule’) .controller(‘SampleController’, SampleController); SampleController.$inject =[‘sampleService’]; function SampleController(sampleService){ this.postAttributes = function() { sampleService.updateMethod(number,attributes) .then(function(response){ //do something on successful

Continue reading

How to test $scope in Jasmine test?

Issue I trying to write unit tests for Angularjs with Jasmine. Here is my controller: function HomeController($scope, fav, news, materials) { console.log(‘home controller’); $scope.testMe = true; } module.controller(‘HomeController’, HomeController); And tests describe(‘Home controller tests’, function() { var $rootScope, $scope, controller;

Continue reading

Unit test controller

Issue I make an ionic app and it finish but when i start to add tests to it I face a problem with $resources ,in this case I have this Controller : .controller(‘newAccountCtrl’, function($scope, $window, $rootScope, API, $ionicPopup, $state) {

Continue reading

Unit test $mdDialog angular material

Issue I called one $mdDialog inside a function. I want to unit-test $mdDialog ok and cancel cases. The below is my controller code (app.controller.js). (function () { ‘use strict’; app.controller(‘AppCtrl’, AppCtrl); AppCtrl.$inject = [‘$scope’, ‘$mdDialog’]; function AppCtrl($scope, $mdDialog) { $scope.saveEntry

Continue reading

I have a problem with testing adding user with password encoder

Issue I have a problem when testing a method with using passwordencoder: Cannot invoke "org.springframework.security.crypto.password.PasswordEncoder.encode(java.lang.CharSequence)" because the return value of "com.store.restAPI.user.UserConfig.passwordEncoder()" is null` Thats my test class method: @ExtendWith(MockitoExtension.class) class UserServiceTest { private UserService underTest; @Mock private UserRepository userRepository; @Mock

Continue reading

Re-direct requests to SideEffect Utility Classes

Issue for a spring boot application that needs to be tested below is my query. @CustomLog @RestController @RequestMapping("/my_path") public class MyController { @GetMapping(path = "**", produces = {MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<JsonNode> fetchData(HttpServletRequest request){ … some code…..which also calls external apis….. }

Continue reading

Why do Angular unit tests load $route?

Issue After updating to Angular 1.5.3 (from 1.4.9) all my unit tests have started failing, where they worked before. The error is as follows: Error: [$injector:unpr] Unknown provider: AuthenticationHttpInterceptorProvider <- AuthenticationHttpInterceptor <- $http <- $templateRequest <- $route It is expected

Continue reading

can i set a base url for expect()

Issue My restangular call has a baseUrl set in a config file to http://localhost:3000/. So a call like Restangular.all(“awards”).customPOST(award) Calls at baseUrl+”awards” Now when I write a test for this, i have to write: httpBackend.expectPOST(“http://localhost:3000/awards”) But later if this baseUrl

Continue reading

Angular httpBackend crashing browser

Issue When using Jasmine and Angular (1.4.7) with Restangular (1.4.0), httpBackend and angular-mocks (1.4.7), Chrome and PhantomJS both crash when encountering the following line: httpBackend.whenGET(‘/something’).respond(200); If I get rid of it entirely, as below, I get the following error: //httpBackend.whenGET(‘/something’).respond(200);

Continue reading

Mock Activated Route With Params in Angular

Issue Question How to mock ActivatedRoutes in my unit tests? Service: constructor( private route: ActivatedRoute, private knowledgeModelService: KnowledgeModelService ) { route.params.pipe(map(p => p.modelId)).subscribe((modelId) => { this.modelId = modelId; }); } My unit test: With mock class class ActivatedRouteMock { //

Continue reading