FRONTEND-231: claims tests (#430)

This commit is contained in:
Aleksandra Usacheva 2021-04-14 15:01:35 +03:00 committed by GitHub
parent 402bb03ef2
commit a9a2c1c544
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 476 additions and 2 deletions

View File

@ -0,0 +1,112 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FlexLayoutModule } from '@angular/flex-layout';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { TranslocoTestingModule } from '@ngneat/transloco';
import { Claim } from '@dsh/api-codegen/claim-management';
import { EmptySearchResultModule } from '@dsh/components/empty-search-result';
import { SpinnerModule } from '@dsh/components/indicators';
import { AccordionModule, CardModule, ExpandPanelModule } from '@dsh/components/layout';
import { ShowMorePanelModule } from '@dsh/components/show-more-panel';
import { generateMockClaim } from '../tests/generate-mock-claim';
import { ClaimsListComponent } from './claims-list.component';
@Component({
selector: 'dsh-claim-row-header',
template: '',
})
class MockRowHeaderComponent {}
@Component({
selector: 'dsh-claim-row',
template: '',
})
class MockRowComponent {
@Input() claim: Claim;
@Output() goToClaimDetails: EventEmitter<number> = new EventEmitter();
}
describe('ClaimsListComponent', () => {
let component: ClaimsListComponent;
let fixture: ComponentFixture<ClaimsListComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
FlexLayoutModule,
SpinnerModule,
EmptySearchResultModule,
AccordionModule,
CardModule,
ShowMorePanelModule,
ExpandPanelModule,
NoopAnimationsModule,
HttpClientTestingModule,
TranslocoTestingModule.withLangs(
{
ru: {
emptySearchResult: 'Данные за указанный период отсутствуют',
},
},
{
availableLangs: ['ru'],
defaultLang: 'ru',
}
),
],
declarations: [ClaimsListComponent, MockRowHeaderComponent, MockRowComponent],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ClaimsListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('isEmptyList', () => {
it('should be false if list was not provided', () => {
expect(component.isEmptyList).toBe(false);
});
it('should be true if list is empty', () => {
component.claimList = [];
fixture.detectChanges();
expect(component.isEmptyList).toBe(true);
});
it('should be false if list contains at least one element', () => {
component.claimList = new Array(1).fill(generateMockClaim(1));
fixture.detectChanges();
expect(component.isEmptyList).toBe(false);
component.claimList = new Array(15).fill(generateMockClaim(1));
fixture.detectChanges();
expect(component.isEmptyList).toBe(false);
});
});
describe('showMoreElements', () => {
it('should emit output event showMore', () => {
const spyOnShowMore = spyOn(component.showMore, 'emit');
component.showMoreElements();
expect(spyOnShowMore).toHaveBeenCalledTimes(1);
});
});
});

View File

@ -0,0 +1,46 @@
import { ChangeDetectionStrategy } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslocoTestingModule } from '@ngneat/transloco';
import { RowModule } from '@dsh/components/layout';
import * as ru from '../../../../../../assets/i18n/ru.json';
import { ClaimRowHeaderComponent } from './claim-row-header.component';
const translationConfig = {
ru,
};
describe('ClaimRowHeaderComponent', () => {
let fixture: ComponentFixture<ClaimRowHeaderComponent>;
let component: ClaimRowHeaderComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RowModule,
TranslocoTestingModule.withLangs(translationConfig, {
availableLangs: ['ru'],
defaultLang: 'ru',
}),
],
declarations: [ClaimRowHeaderComponent],
})
.overrideComponent(ClaimRowHeaderComponent, {
set: {
changeDetection: ChangeDetectionStrategy.Default,
},
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ClaimRowHeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,77 @@
import { ChangeDetectionStrategy } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { TranslocoTestingModule } from '@ngneat/transloco';
import * as moment from 'moment';
import { ClaimStatusColorPipe } from '@dsh/app/shared/pipes/api-model-types/claim-status-color.pipe';
import { StatusModule } from '@dsh/components/indicators';
import { RowModule } from '@dsh/components/layout';
import * as ru from '../../../../../../assets/i18n/ru.json';
import { generateMockClaim } from '../../../tests/generate-mock-claim';
import { ClaimRowComponent } from './claim-row.component';
const translationConfig = {
ru,
};
describe('ClaimRowComponent', () => {
let fixture: ComponentFixture<ClaimRowComponent>;
let component: ClaimRowComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RowModule,
TranslocoTestingModule.withLangs(translationConfig, {
availableLangs: ['ru'],
defaultLang: 'ru',
}),
StatusModule,
],
declarations: [ClaimRowComponent, ClaimStatusColorPipe],
})
.overrideComponent(ClaimRowComponent, {
set: {
changeDetection: ChangeDetectionStrategy.Default,
},
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ClaimRowComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('template', () => {
it('should show loading value if claim was not provided', () => {
const labels = fixture.debugElement.queryAll(By.css('dsh-row dsh-row-label'));
expect(labels.length).toBe(1);
expect(labels[0].nativeElement.textContent.trim()).toBe('Loading ...');
});
it('should show row component if claim was provided', () => {
const claim = generateMockClaim();
const { createdAt } = claim;
component.claim = claim;
fixture.detectChanges();
const labels = fixture.debugElement.queryAll(By.css('dsh-row dsh-row-label'));
expect(labels[0].nativeElement.textContent.trim()).toBe('1');
expect(labels[1].nativeElement.textContent.trim()).toBe('claimStatus.pending');
expect(labels[2].nativeElement.children[0].textContent.trim()).toBe(
moment(createdAt).format('DD MMMM YYYY, HH:mm')
);
});
});
});

View File

@ -1,6 +1,6 @@
import { StatusModificationUnit } from '@dsh/api-codegen/claim-management/swagger-codegen';
export interface ClaimsSearchFiltersSearchParams {
claimID: number;
claimStatuses: StatusModificationUnit.StatusEnum[];
claimID?: number;
claimStatuses?: StatusModificationUnit.StatusEnum[];
}

View File

@ -0,0 +1,174 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FlexLayoutModule } from '@angular/flex-layout';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { Router } from '@angular/router';
import { of } from 'rxjs';
import { deepEqual, instance, mock, verify, when } from 'ts-mockito';
import { Claim } from '@dsh/api-codegen/claim-management';
import { getTranslocoModule } from '@dsh/app/shared/tests/get-transloco-module';
import { LastUpdatedModule } from '@dsh/components/indicators/last-updated/last-updated.module';
import { ClaimsSearchFiltersStore } from './claims-search-filters-store.service';
import { ClaimsSearchFiltersSearchParams } from './claims-search-filters/claims-search-filters-search-params';
import { ClaimsComponent } from './claims.component';
import { FetchClaimsService } from './services/fetch-claims/fetch-claims.service';
import { generateMockClaim } from './tests/generate-mock-claim';
@Component({
selector: 'dsh-claims-search-filters',
template: '',
})
class MockClaimsSearchFiltersComponent {
@Input() initParams: ClaimsSearchFiltersSearchParams;
@Output()
searchParamsChanges = new EventEmitter<ClaimsSearchFiltersSearchParams>();
}
@Component({
selector: 'dsh-claims-list',
template: '',
})
class MockClaimsListComponent {
@Input() claimList: Claim[];
@Input() lastUpdated: string;
@Input() isLoading: boolean;
@Input() hasMore: boolean;
@Input() expandedId: number;
@Output() refresh = new EventEmitter<void>();
@Output() showMore = new EventEmitter<void>();
@Output() goToClaimDetails: EventEmitter<number> = new EventEmitter();
}
describe('ClaimsComponent', () => {
let component: ClaimsComponent;
let fixture: ComponentFixture<ClaimsComponent>;
let mockFetchClaimsService: FetchClaimsService;
let mockRouter: Router;
let mockMockClaimsSearchFiltersStore: ClaimsSearchFiltersStore;
beforeEach(() => {
mockRouter = mock(Router);
mockFetchClaimsService = mock(FetchClaimsService);
mockMockClaimsSearchFiltersStore = mock(ClaimsSearchFiltersStore);
});
beforeEach(() => {
when(mockFetchClaimsService.searchResult$).thenReturn(of([generateMockClaim(1), generateMockClaim(2)]));
when(mockFetchClaimsService.isLoading$).thenReturn(of(false));
when(mockFetchClaimsService.hasMore$).thenReturn(of(false));
when(mockFetchClaimsService.lastUpdated$).thenReturn(of());
});
async function configureTestingModule() {
await TestBed.configureTestingModule({
imports: [
getTranslocoModule(),
NoopAnimationsModule,
LastUpdatedModule,
FlexLayoutModule,
HttpClientTestingModule,
],
declarations: [ClaimsComponent, MockClaimsSearchFiltersComponent, MockClaimsListComponent],
providers: [
{
provide: FetchClaimsService,
useFactory: () => instance(mockFetchClaimsService),
},
{
provide: Router,
useFactory: () => instance(mockRouter),
},
{
provide: ClaimsSearchFiltersStore,
useValue: instance(mockMockClaimsSearchFiltersStore),
},
],
})
.overrideComponent(ClaimsComponent, {
set: {
providers: [],
},
})
.compileComponents();
}
async function createComponent() {
await configureTestingModule();
fixture = TestBed.createComponent(ClaimsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}
beforeEach(() => {
when(mockMockClaimsSearchFiltersStore.data$).thenReturn(of());
});
describe('creation', () => {
beforeEach(async () => {
await createComponent();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
describe('refreshList', () => {
beforeEach(async () => {
await createComponent();
});
it('should call refresh data', () => {
when(mockFetchClaimsService.refresh()).thenReturn();
component.refresh();
verify(mockFetchClaimsService.refresh()).once();
expect().nothing();
});
});
describe('requestNextPage', () => {
beforeEach(async () => {
await createComponent();
});
it('should call fetch more data', () => {
when(mockFetchClaimsService.fetchMore()).thenReturn();
component.fetchMore();
verify(mockFetchClaimsService.fetchMore()).once();
expect().nothing();
});
});
describe('filtersChanged', () => {
beforeEach(async () => {
await createComponent();
});
it('should request list using filters data', () => {
const filtersData = {
claimID: 1,
};
component.search(filtersData);
verify(
mockFetchClaimsService.search(
deepEqual({
claimID: filtersData.claimID,
})
)
);
expect().nothing();
});
});
});

View File

@ -0,0 +1,49 @@
import { TestBed } from '@angular/core/testing';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { TranslocoTestingModule } from '@ngneat/transloco';
import * as ru from '../../../assets/i18n/ru.json';
import { ClaimsService } from '../../api/claims';
import { FetchClaimsService } from './services/fetch-claims/fetch-claims.service';
class MockApiClaimsService {}
const translationConfig = {
ru,
};
describe('FetchClaimsService', () => {
let service: FetchClaimsService;
let apiClaimsService: MockApiClaimsService;
beforeEach(() => {
apiClaimsService = new MockApiClaimsService();
});
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
MatSnackBarModule,
TranslocoTestingModule.withLangs(translationConfig, {
availableLangs: ['ru'],
defaultLang: 'ru',
}),
],
providers: [
FetchClaimsService,
{
provide: ClaimsService,
useValue: apiClaimsService,
},
],
});
});
beforeEach(() => {
service = TestBed.inject(FetchClaimsService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,16 @@
import { Claim, StatusModificationUnit } from '../../../api-codegen/claim-management/swagger-codegen';
export const generateMockClaim = (
id: number = 1,
status: StatusModificationUnit.StatusEnum | string = StatusModificationUnit.StatusEnum.Pending,
revision: number = 1,
createdAt: Date = new Date(),
updatedAt: Date = new Date()
): Claim => ({
id,
status,
changeset: [],
revision,
createdAt,
updatedAt,
});