Угловая 2-модульная тестовая функция для ngonInit () - PullRequest
0 голосов
/ 09 июля 2019

У меня есть ионное приложение, мне нужно написать модульный тест для этой страницы.Я новичок в Жасмин.как написать контрольный пример для следующих методов

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';

  import { MarketingPreference } from '../../models/marketing-preference';
  import { PreferencesService } from  '../../providers/authentication/preferences.service';
  import { CommonConstants } from '../../providers/common.constants';
  import { UtilService } from './../../providers/util.service';

@Component({
selector: 'marketing-preferences-content',
templateUrl: 'marketing-preferences-content.html'
})
export class MarketingPreferencesContent implements OnInit {
listPreferences: MarketingPreference[];

@Input() loginEnrollmentFlow: boolean;
@Output() preferencesSubmittedSuccess: EventEmitter<any> = new EventEmitter();
@Output() preferencesSubmittedError: EventEmitter<any> = new EventEmitter();

constructor(private preferencesService: PreferencesService, private utilService: UtilService) {}

ngOnInit(): void {
    this.preferencesService
        .getPreferences('marketing')
        .then(({ preferences }) => {
            this.listPreferences = preferences;
        })
        .catch(() => {
            this.utilService.showAlert('', CommonConstants.ErrorMessages.SERVER_DOWN, CommonConstants.Buttons.OK);
        });
}

checkBoxListener(e: HTMLInputElement, index: number): void {
    this.listPreferences[index].election = e.checked ? CommonConstants.Application_Variables.YES : CommonConstants.Application_Variables.NO;
}

updatePreferences(): void {
    this.preferencesService
        .postPreferences({ preferences: this.listPreferences })
        .then(() => {
            this.preferencesSubmittedSuccess.emit(this.listPreferences);
        })
        .catch(error => {
            this.preferencesSubmittedError.emit(error);
        });
}
 }

Мои спецификации.ts

import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { UtilService } from '../../providers/util.service';
import { MarketingPreferencesContent } from './marketing-preferences-content';
 import { PreferencesService } from 
   '../../providers/authentication/preferences.service';
import { } 'jasmine';
import { CommonConstants } from '../../providers/common.constants';
import { MarketingPreference } from '../../models/marketing-preference';

describe('MarketingPreferencesContent', () => {
let fixture: ComponentFixture<MarketingPreferencesContent>;
let component: MarketingPreferencesContent;
let mockUtilService: UtilService;
let mockPreferencesService: PreferencesService;

beforeEach(async(() => {
    mockUtilService = jasmine.createSpyObj('utils', ['showAlert']);
    mockPreferencesService = jasmine.createSpyObj('preferencesService', ['getPreferences', 'postPreferences']);

    TestBed.configureTestingModule({
        declarations: [MarketingPreferencesContent],
        schemas: [CUSTOM_ELEMENTS_SCHEMA],
        providers: [
            { provide: UtilService, useValue: mockUtilService },
            { provide: PreferencesService, useValue: mockPreferencesService }
        ]
    });

    fixture = TestBed.createComponent(MarketingPreferencesContent);
    component = fixture.componentInstance;
}));

it('should create the component instance', async(() => {
    expect(component).toBeDefined();
}));

});
...