angular2-testing

Issue with jasmine expectations not being tested/evaluated because of simulated mock error

安稳与你 提交于 2019-12-12 03:56:48
问题 I am trying to unit test a angular 2 component and simulate an error (400/bad request) from the http backend. Here is my code: describe('Component: UserAccountActivationComponent', () => { let fixture: ComponentFixture<UserAccountActivationComponent>; let userAccountActivationComponent: UserAccountActivationComponent; const observableMock = Observable.of('Some Observable'); beforeEach(async(() => { TestBed.configureTestingModule({ providers: [ MockBackend, BaseRequestOptions, { provide: Http,

Angular2 unit testing a component that uses MdlSnackbarService

你。 提交于 2019-12-12 03:46:44
问题 I've got the following component: import { Component, OnInit } from '@angular/core'; import {FormBuilder, FormGroup, FormControl, Validators} from "@angular/forms"; import {ValidationService} from "../../services/validation.service"; import {Router} from "@angular/router"; import {UsersService} from "../../services/users.service"; import {MdlSnackbarService} from "angular2-mdl"; @Component({ selector: 'app-signup', templateUrl: 'signup.component.html', styleUrls: ['signup.component.css'],

How to actually trigger a click event and check the outcome in Angular2

喜欢而已 提交于 2019-12-11 16:43:05
问题 I want to actually check, that the selected attribute has been rendered with a true instead of an undefined. My working (manually tested) Component functionality: export class PlayerSelectorComponent implements OnInit { players: any = [] ... toggleSelectPlayer( player: any ) { if (player.selected) { player.selected = false } else { player.selected = true } } } Here is the template: <ul class="list-group"> <player-row *ngFor="let player of players" [player]="player" [selected]="player.selected

angular 2 MockBackend test http timeout?

久未见 提交于 2019-12-11 06:42:46
问题 Is there a way to test http timeout behaviour of a service? I'm using MockBackend even when setting job's timeout to 0 no logging of 'TIMEOUT' present. export class MyHttpService { private sendGetRequest (job: HttpJob): Observable<any> { return this.http.get(job.getUrl(), this.options) .share() .timeout(job.getTimeout(), () => this.requestTimeout(job)) .catch((err) => this.errorHandler(err, job)); }; private requestTimeout(job: HttpJob): Error { job.errorCode = ErrorCode.TIMEOUT; console.log(

Test an async PipeTransform

左心房为你撑大大i 提交于 2019-12-11 04:38:58
问题 Context I have a basic PipeTransform , expect the fact that it is async. Why? because I have my own i18n service (because of parsing, pluralization and other constraints, I did my own) and it returns a Promise<string> : @Pipe({ name: "i18n", pure: false }) export class I18nPipe implements PipeTransform { private done = false; constructor(private i18n:I18n) { } value:string; transform(value:string, args:I18nPipeArgs):string { if(this.done){ return this.value; } if (args.plural) { this.i18n

Write angular2 tests and changing the mock return values - make it DRY?

五迷三道 提交于 2019-12-10 03:09:04
问题 I'm writing some tests for a service and I'm altering the response from mock functions to test various cases. At the moment, every time I want to change the response of a mock, I need to reset the TestBed and configure the testing module again, injecting my new Mocks as dependencies. I feel like there must be a DRYer way to write this spec, but I can't figure it out. Does anyone have any ideas? (I understand that I could write tests for this service as a standard ES6 class, but I get the same

How do you inject an angular2 service into a unit test? (RC3)

六眼飞鱼酱① 提交于 2019-12-09 18:30:14
问题 I am using RC3 . I am implementing the new Angular2 router as documented here: https://angular.io/docs/ts/latest/guide/router.html Everything works fine but I am having problem in unit testing. Specifically, I cannot inject Angular2 services into my unit tests. My relevant component code is: import {Component} from '@angular/core'; import {ActivatedRoute} from '@angular/router'; @Component({ templateUrl: ... styleUrls: ... }) export class Route1DetailComponent { constructor(private route

Karma cannot load Angular 2 Component

不想你离开。 提交于 2019-12-08 18:19:29
My Karma cannont load angular 2 component. Drop this error: [web-server] :404 : /base/src/client/app/container/container.component i do everything with angular 2 testing tutorial (i only modified path in karma) So problem must be in path. but i don't have any idea how to fix it. My Path: src | |client | | |container | | | |container.component.ts | | | |container.component.spec.ts |karma.conf.js |karma-test-shim.js container.component.ts import { Component, OnInit } from '@angular/core'; import { UserService } from '../user/user.service'; import { User } from '../user/user'; //import {

Angular2 unit testing - why nativeElement has empty CSSStyleDeclaration

泪湿孤枕 提交于 2019-12-07 13:48:28
问题 I'm trying to test a simple header component that has a button and when being focused - opens a dropdown using just css visibility property. This is the html: <nav class="navigation"> <button type="button"> <span>click here</span> </button> <ul> <li> Text </li> <li> Text </li> <li> Text </li> </ul> </nav> This is the scss style: button { span { margin-right: 10px; } &:focus { + ul { visibility: visible; } } } ul { position: absolute; list-style: none; z-index: 1000; visibility: hidden;

Angular 2 Custom validation unit testing

余生颓废 提交于 2019-12-07 06:42:55
问题 i'm writing custom angular(Angular 2.0.0) validation, following this guide https://angular.io/docs/ts/latest/cookbook/form-validation.html#!#custom-validation . @Directive({ selector: '[ngModel][emailValidator]', providers: [{provide: NG_VALIDATORS, useExisting: EmailValidatorDirective, multi: true}] }) export class EmailValidatorDirective implements Validator Now i'm trying to add unit test to my custom validation directive. beforeEach(() => { fixture = TestBed.createComponent(EmailComponent