Я пишу angular тест, который сравнивает длину двух строковых массивов на основе условия. Я получаю ошибку:
Ошибка: ожидается, что 0 будет равно 6, даже если я выполняю условие.
Если вы видите в моем тестовом коде, я инициализирую idDocument объект.
Я пытаюсь проверить этот код
acceptedFileTypes = this.idDocument === undefined ? [] : AcceptedFileTypes;
TestComponent
describe('IdUploadComponent', () => {
let component: IdUploadComponent;
let fixture: ComponentFixture<IdUploadComponent>;
let injector: TestBed;
let messageService: MessageService;
let messageServiceSpy: jasmine.Spy;
const event = { file: {name : 'Test'} };
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [IbaModule, NgxsModule.forRoot([]), HttpClientModule, HttpClientTestingModule],
providers: [
{ provide: AppConfigService, useClass: MockAppConfigService },
{ provide: MessageService, useClass: MockMessageService },
{ provide: AuthenticationService, useClass: MockAuthenticationService }
]
}).compileComponents();
injector = getTestBed();
messageService = injector.get(MessageService);
messageServiceSpy = spyOn(messageService, 'add').and.callThrough();
}));
beforeEach(() => {
fixture = TestBed.createComponent(IdUploadComponent);
component = fixture.componentInstance;
component.fileUpload = () => undefined;
component.idDocument = new IdDocument();
fixture.detectChanges();
});
it('calls messageService on upload success', () => {
component.fileUploaded(event);
expect(messageServiceSpy).toHaveBeenCalled();
});
fit('sets the fileupload to be able to upload specific files types', () => {
const expectedResult = [ '.pdf','.heic','.jpeg','.jpg','.png','.webp'];
component.idDocument = new IdDocument({birthCountry: 23, hasDocument: true, birthCountryRequired: true, issuedCountry: 23, required: true, uploaded: true});
fixture.detectChanges();
expect(component.acceptedFileTypes.length).toEqual(expectedResult.length);
});
});
Компонент
export class IdUploadComponent implements OnInit {
value: any[] = [];
uploadUrl: string;
componentReady = false;
public uploading: boolean;
@Input() documentType = '';
@Input() section: string;
@Input() header: string;
@Input() userId: string;
@Input() idDocument: IdDocument;
@Output() fileUploadEvent = new EventEmitter<string>();
@Input() fileUpload: Function = noop;
authHeader = {
authorization: undefined,
document: undefined
};
acceptedFileTypes = this.idDocument === undefined ? [] : AcceptedFileTypes;
accept = this.idDocument === undefined ? '' : AcceptedFileTypes.join(',');
readonly maxFileSize = 10485760;
constructor(
private messageService: MessageService,
private authenticationService: AuthenticationService,
private appConfigService: AppConfigService
) {}
ngOnInit() {
this.authHeader.document = this.documentType;
this.uploadUrl = this.appConfigService.appSettings.apiServer.identification + this.section
+ '/'
+ this.userId
+ '/UploadIdentification';
this.authenticationService.getAuthenticationToken().then(r => {
this.authHeader.authorization = 'Bearer ' + r;
this.componentReady = true;
});
}
}
Может кто-нибудь сказать мне, в чем проблема ??