Skip to content

Commit

Permalink
Update test to get over 80% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sasha-dresden committed Dec 5, 2024
1 parent 2dcd227 commit c6e8476
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { provideMockStore } from '@ngrx/store/testing';
import { testMockStore } from 'app/shared/utils/unit-test.utils';
import { Form3X } from 'app/shared/models/form-3x.model';
Expand All @@ -18,43 +17,34 @@ import { of } from 'rxjs';
import { ReportLevelMemoComponent } from './report-level-memo.component';
import { Report } from 'app/shared/models/report.model';
import { SubscriptionFormControl } from 'app/shared/utils/subscription-form-control';
import { provideHttpClient } from '@angular/common/http';

describe('ReportLevelMemoComponent', () => {
let component: ReportLevelMemoComponent;
let fixture: ComponentFixture<ReportLevelMemoComponent>;
let testMemoTextService: MemoTextService;
let testMessageService: MessageService;
let testRouter: Router;
const f3x: Form3X = Form3X.fromJSON({
id: '999',
coverage_from_date: '2022-05-25',
form_type: 'F3XN',
report_code: 'Q1',
});

let mockRouter: jasmine.SpyObj<Router>;

beforeEach(async () => {
mockRouter = jasmine.createSpyObj('Router', ['navigateByUrl']);

await TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
SharedModule,
CardModule,
ToastModule,
ReactiveFormsModule,
ButtonModule,
InputTextareaModule,
RouterTestingModule.withRoutes([]),
],
imports: [SharedModule, CardModule, ToastModule, ReactiveFormsModule, ButtonModule, InputTextareaModule],
declarations: [ReportLevelMemoComponent],
providers: [
provideHttpClient(),
provideHttpClientTesting(),
MessageService,
MemoTextService,
FormBuilder,
provideMockStore(testMockStore),
{ provide: Router, useValue: mockRouter },
{
provide: ActivatedRoute,
useValue: {
data: of({
report: f3x,
getNextUrl: (report?: Report) => `/reports/f3x/submit/step1/${report?.id}`,
}),
},
Expand All @@ -64,7 +54,6 @@ describe('ReportLevelMemoComponent', () => {
});

beforeEach(() => {
testRouter = TestBed.inject(Router);
testMemoTextService = TestBed.inject(MemoTextService);
testMessageService = TestBed.inject(MessageService);
fixture = TestBed.createComponent(ReportLevelMemoComponent);
Expand Down Expand Up @@ -95,12 +84,11 @@ describe('ReportLevelMemoComponent', () => {
life: 3000,
};
const testMemoTextServiceSpy = spyOn(testMemoTextService, 'update').and.returnValue(of(new MemoText()));
const navigateSpy = spyOn(testRouter, 'navigateByUrl');
const testMessageServiceSpy = spyOn(testMessageService, 'add');
component.assignedMemoText.id = '1';
component.save();
expect(testMemoTextServiceSpy).toHaveBeenCalledTimes(1);
expect(navigateSpy).toHaveBeenCalledWith('/reports/f3x/submit/step1/999');
expect(mockRouter.navigateByUrl).toHaveBeenCalledWith('/reports/f3x/submit/step1/999');
expect(testMessageServiceSpy).toHaveBeenCalledOnceWith(expectedMessage);
});

Expand All @@ -112,12 +100,11 @@ describe('ReportLevelMemoComponent', () => {
life: 3000,
};
const testMemoTextServiceSpy = spyOn(testMemoTextService, 'create').and.returnValue(of(new MemoText()));
const navigateSpy = spyOn(testRouter, 'navigateByUrl');
const testMessageServiceSpy = spyOn(testMessageService, 'add');
component.assignedMemoText.id = undefined;
component.save();
expect(testMemoTextServiceSpy).toHaveBeenCalledTimes(1);
expect(navigateSpy).toHaveBeenCalledWith('/reports/f3x/submit/step1/999');
expect(mockRouter.navigateByUrl).toHaveBeenCalledWith('/reports/f3x/submit/step1/999');
expect(testMessageServiceSpy).toHaveBeenCalledOnceWith(expectedMessage);
});
});
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { FormBuilder, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { ApiService, QueryParams } from 'app/shared/services/api.service';
import { ReportService } from 'app/shared/services/report.service';
import { Confirmation, ConfirmationService, MessageService, SharedModule } from 'primeng/api';
import { Observable, of } from 'rxjs';
import { SubmitReportStep2Component } from './submit-report-step2.component';
import { provideMockStore } from '@ngrx/store/testing';
import { Form3X } from 'app/shared/models/form-3x.model';
import { ApiService } from 'app/shared/services/api.service';
import { ReportService } from 'app/shared/services/report.service';
import { testMockStore } from 'app/shared/utils/unit-test.utils';
import { Confirmation, ConfirmationService, MessageService, SharedModule } from 'primeng/api';
import { CheckboxModule } from 'primeng/checkbox';
import { DividerModule } from 'primeng/divider';
import { RadioButtonModule } from 'primeng/radiobutton';
import { of } from 'rxjs';
import { ReportsModule } from '../reports.module';
import { SubmitReportStep2Component } from './submit-report-step2.component';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { HttpResponse, provideHttpClient } from '@angular/common/http';
import { Report } from 'app/shared/models/report.model';

describe('SubmitReportStep2Component', () => {
let component: SubmitReportStep2Component;
let fixture: ComponentFixture<SubmitReportStep2Component>;
let reportService: ReportService;
let apiService: ApiService;
let testConfirmationService: ConfirmationService;
let confirmSpy: jasmine.Spy;
let mockRouter: jasmine.SpyObj<Router>;
let mockMessageService: jasmine.SpyObj<MessageService>;

beforeEach(async () => {
mockRouter = jasmine.createSpyObj('Router', ['navigateByUrl']);
mockMessageService = jasmine.createSpyObj('MessageService', ['add']);

await TestBed.configureTestingModule({
declarations: [SubmitReportStep2Component],
imports: [
FormsModule,
ReactiveFormsModule,
RouterTestingModule.withRoutes([]),
HttpClientTestingModule,
DividerModule,
CheckboxModule,
RadioButtonModule,
SharedModule,
ReportsModule,
],
declarations: [SubmitReportStep2Component],
providers: [
provideHttpClient(),
provideHttpClientTesting(),
FormBuilder,
ReportService,
MessageService,
{ provide: Router, useValue: mockRouter },
provideMockStore(testMockStore),
{ provide: MessageService, useValue: mockMessageService },
ConfirmationService,
ReportService,
ApiService,
provideMockStore(testMockStore),
ReportService,
{
provide: ActivatedRoute,
useValue: {
Expand All @@ -59,43 +61,111 @@ describe('SubmitReportStep2Component', () => {
},
],
}).compileComponents();
});

beforeEach(() => {
reportService = TestBed.inject(ReportService);
apiService = TestBed.inject(ApiService);
fixture = TestBed.createComponent(SubmitReportStep2Component);
component = fixture.componentInstance;

testConfirmationService = TestBed.inject(ConfirmationService);
component = fixture.componentInstance;
confirmSpy = spyOn(testConfirmationService, 'confirm');
confirmSpy.and.callFake((confirmation: Confirmation) => {
if (confirmation.accept) return confirmation?.accept();
});
spyOn(reportService, 'get').and.returnValue(of(Form3X.fromJSON({ id: '999' })));
fixture.detectChanges();
});

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

it('should save and submti', async () => {
component.form.patchValue({
treasurer_name_1: 'name1',
treasurer_name_2: 'name2',
treasurer_name_middle: 'm',
treasurer_name_prefix: 'pre.',
treasurer_name_suffix: 'suf',
filingPassword: '12345aA!',
userCertified: true,
});
const updateSpy = spyOn(reportService, 'update').and.returnValue(of(Form3X.fromJSON({ id: '999' })));
const submtiSpy = spyOn(apiService, 'post').and.returnValue(of());
it('should initialize form with proper controls', () => {
expect(component.form.contains('treasurer_first_name')).toBeTrue();
expect(component.form.contains('filingPassword')).toBeTrue();
expect(component.form.controls['filingPassword'].hasValidator(Validators.required)).toBeFalse();
});

it('should add backdoor_code control when backdoorYesNo is true', () => {
component.form.get('backdoorYesNo')?.setValue(true);
expect(component.form.contains('backdoor_code')).toBeTrue();
});

it('should remove backdoor_code control when backdoorYesNo is false', () => {
component.form.get('backdoorYesNo')?.setValue(true);
component.form.get('backdoorYesNo')?.setValue(false);
expect(component.form.contains('backdoor_code')).toBeFalse();
});

it('should not submit when form is invalid', async () => {
spyOn(component, 'saveAndSubmit');
component.form.patchValue({ filingPassword: '', userCertified: false });
component.submitClicked();
expect(component.saveAndSubmit).not.toHaveBeenCalled();
});

describe('saveAndSubmit', () => {
let confirmSpy: jasmine.Spy<(confirmation: Confirmation) => ConfirmationService>;
let saveTreasurerSpy: jasmine.Spy<() => Promise<Report | undefined>>;
let submitSpy: jasmine.Spy<() => Promise<boolean>>;
let reportSpy: jasmine.Spy<(report: Report, fieldsToValidate?: string[]) => Observable<Report>>;
let apiSpy: jasmine.Spy<{
<T>(endpoint: string, payload: unknown, queryParams?: QueryParams): Observable<T>;
<T>(
endpoint: string,
payload: unknown,
queryParams?: QueryParams,
allowedErrorCodes?: number[],
): Observable<HttpResponse<T>>;
}>;
const mockPassword = '12345aA!';

beforeEach(() => {
confirmSpy = spyOn(component.confirmationService, 'confirm').and.callFake((confirmation: Confirmation) => {
if (confirmation.accept) confirmation.accept();
return component.confirmationService;
});
saveTreasurerSpy = spyOn(component, 'saveTreasurerName').and.callThrough();
submitSpy = spyOn(component, 'submitReport').and.callThrough();
reportSpy = spyOn(component.reportService, 'update').and.returnValue(of(new Form3X()));
apiSpy = spyOn(component.apiService, 'post').and.returnValue(of(new HttpResponse()));

component.form.patchValue({
treasurer_name_1: 'name1',
treasurer_name_2: 'name2',
treasurer_name_middle: 'm',
treasurer_name_prefix: 'pre.',
treasurer_name_suffix: 'suf',
filingPassword: mockPassword,
userCertified: true,
});
});

it('should call saveAndSubmit when form is valid', fakeAsync(async () => {
if (component.report) component.report.id = '999';
const saveSepy = spyOn(component, 'saveAndSubmit').and.callThrough();

component.submitClicked();
tick(100);
expect(component.form.invalid).toBeFalse();
expect(confirmSpy).toHaveBeenCalled();
expect(saveSepy).toHaveBeenCalled();
expect(saveTreasurerSpy).toHaveBeenCalled();
expect(reportSpy).toHaveBeenCalled();
expect(submitSpy).toHaveBeenCalled();
expect(apiSpy).toHaveBeenCalledWith('/web-services/submit-to-fec/', {
report_id: '999',
password: mockPassword,
backdoor_code: undefined,
});
expect(mockRouter.navigateByUrl).toHaveBeenCalledWith('');
expect(mockRouter.navigateByUrl).toHaveBeenCalledWith('/reports/f3x/submit/status/999/');
}));

it('should redirect to reports if no report id', fakeAsync(async () => {
if (component.report) component.report.id = undefined;

component.submitClicked();
tick(100);

expect(updateSpy).toHaveBeenCalled();
expect(submtiSpy).toHaveBeenCalled();
expect(apiSpy).toHaveBeenCalledWith('/web-services/submit-to-fec/', {
report_id: undefined,
password: mockPassword,
backdoor_code: undefined,
});
expect(mockRouter.navigateByUrl).toHaveBeenCalledWith('');
expect(mockRouter.navigateByUrl).toHaveBeenCalledWith('/reports/');
}));
});
});
Loading

0 comments on commit c6e8476

Please sign in to comment.