1
0
Files
yel-dspace-angular/src/app/notifications/qa/topics/quality-assurance-topics.component.spec.ts
Yury Bondarenko 762e4616cc Manual fix: sync removed imports between tests and components
The automatic migration made it so HTML always uses the `Themed*` component, and it must therefore be imported in all standalone components that use it.
Afterwards, many unit tests fail because the removed imports no longer match up (can't inject `ThemeService`).

While we could try to support this as part of the automatic migration, there are too many edge cases for this to be consistent.
2024-03-28 18:52:06 +01:00

183 lines
6.3 KiB
TypeScript

/* eslint-disable no-empty, @typescript-eslint/no-empty-function */
import { CommonModule } from '@angular/common';
import {
Component,
NO_ERRORS_SCHEMA,
} from '@angular/core';
import {
ComponentFixture,
inject,
TestBed,
waitForAsync,
} from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';
import { TranslateModule } from '@ngx-translate/core';
import { cold } from 'jasmine-marbles';
import { of as observableOf } from 'rxjs';
import { ItemDataService } from 'src/app/core/data/item-data.service';
import { PaginationService } from '../../../core/pagination/pagination.service';
import { AlertComponent } from '../../../shared/alert/alert.component';
import { ThemedLoadingComponent } from '../../../shared/loading/themed-loading.component';
import {
getMockNotificationsStateService,
qualityAssuranceTopicObjectMoreAbstract,
qualityAssuranceTopicObjectMorePid,
} from '../../../shared/mocks/notifications.mock';
import { PaginationComponent } from '../../../shared/pagination/pagination.component';
import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub';
import { createTestComponent } from '../../../shared/testing/utils.test';
import { NotificationsStateService } from '../../notifications-state.service';
import { QualityAssuranceTopicsComponent } from './quality-assurance-topics.component';
describe('QualityAssuranceTopicsComponent test suite', () => {
let fixture: ComponentFixture<QualityAssuranceTopicsComponent>;
let comp: QualityAssuranceTopicsComponent;
let compAsAny: any;
const mockNotificationsStateService = getMockNotificationsStateService();
const activatedRouteParams = {
qualityAssuranceTopicsParams: {
currentPage: 0,
pageSize: 5,
},
};
const paginationService = new PaginationServiceStub();
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
CommonModule,
TranslateModule.forRoot(),
QualityAssuranceTopicsComponent,
TestComponent,
],
providers: [
{ provide: NotificationsStateService, useValue: mockNotificationsStateService },
{ provide: ActivatedRoute, useValue: { data: observableOf(activatedRouteParams), snapshot: {
params: {
sourceId: 'openaire',
targetId: null,
},
} } },
{ provide: PaginationService, useValue: paginationService },
{ provide: ItemDataService, useValue: {} },
QualityAssuranceTopicsComponent,
],
schemas: [NO_ERRORS_SCHEMA],
})
.overrideComponent(QualityAssuranceTopicsComponent, {
remove: {
imports: [
AlertComponent,
ThemedLoadingComponent,
PaginationComponent,
],
},
})
.compileComponents().then(() => {
mockNotificationsStateService.getQualityAssuranceTopics.and.returnValue(observableOf([
qualityAssuranceTopicObjectMorePid,
qualityAssuranceTopicObjectMoreAbstract,
]));
mockNotificationsStateService.getQualityAssuranceTopicsTotalPages.and.returnValue(observableOf(1));
mockNotificationsStateService.getQualityAssuranceTopicsCurrentPage.and.returnValue(observableOf(0));
mockNotificationsStateService.getQualityAssuranceTopicsTotals.and.returnValue(observableOf(2));
mockNotificationsStateService.isQualityAssuranceTopicsLoaded.and.returnValue(observableOf(true));
mockNotificationsStateService.isQualityAssuranceTopicsLoading.and.returnValue(observableOf(false));
mockNotificationsStateService.isQualityAssuranceTopicsProcessing.and.returnValue(observableOf(false));
});
}));
// First test to check the correct component creation
describe('', () => {
let testComp: TestComponent;
let testFixture: ComponentFixture<TestComponent>;
// synchronous beforeEach
beforeEach(() => {
const html = `
<ds-quality-assurance-topic></ds-quality-assurance-topic>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
afterEach(() => {
testFixture.destroy();
});
it('should create QualityAssuranceTopicsComponent', inject([QualityAssuranceTopicsComponent], (app: QualityAssuranceTopicsComponent) => {
expect(app).toBeDefined();
}));
});
describe('Main tests running with two topics', () => {
beforeEach(() => {
fixture = TestBed.createComponent(QualityAssuranceTopicsComponent);
comp = fixture.componentInstance;
compAsAny = comp;
});
afterEach(() => {
fixture.destroy();
comp = null;
compAsAny = null;
});
it(('Should init component properly'), () => {
comp.ngOnInit();
fixture.detectChanges();
expect(comp.topics$).toBeObservable(cold('(a|)', {
a: [
qualityAssuranceTopicObjectMorePid,
qualityAssuranceTopicObjectMoreAbstract,
],
}));
expect(comp.totalElements$).toBeObservable(cold('(a|)', {
a: 2,
}));
});
it(('Should set data properly after the view init'), () => {
spyOn(compAsAny, 'getQualityAssuranceTopics');
comp.ngAfterViewInit();
fixture.detectChanges();
expect(compAsAny.getQualityAssuranceTopics).toHaveBeenCalled();
});
it(('isTopicsLoading should return FALSE'), () => {
expect(comp.isTopicsLoading()).toBeObservable(cold('(a|)', {
a: false,
}));
});
it(('isTopicsProcessing should return FALSE'), () => {
expect(comp.isTopicsProcessing()).toBeObservable(cold('(a|)', {
a: false,
}));
});
it(('getQualityAssuranceTopics should call the service to dispatch a STATE change'), () => {
comp.ngOnInit();
fixture.detectChanges();
compAsAny.notificationsStateService.dispatchRetrieveQualityAssuranceTopics(comp.paginationConfig.pageSize, comp.paginationConfig.currentPage).and.callThrough();
expect(compAsAny.notificationsStateService.dispatchRetrieveQualityAssuranceTopics).toHaveBeenCalledWith(comp.paginationConfig.pageSize, comp.paginationConfig.currentPage);
});
});
});
// declare a test component
@Component({
selector: 'ds-test-cmp',
template: ``,
standalone: true,
imports: [CommonModule],
})
class TestComponent {
}