Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Added unit test cases for dismiss dialog #3176

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,100 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { IonicModule, PopoverController } from '@ionic/angular';

import { DismissDialogComponent } from './dismiss-dialog.component';
import { FormButtonValidationDirective } from 'src/app/shared/directive/form-button-validation.directive';
import { MatIconModule } from '@angular/material/icon';
import { MatIconTestingModule } from '@angular/material/icon/testing';
import { FormsModule } from '@angular/forms';
import { of, throwError } from 'rxjs';
import { click, getElementBySelector, getTextContent } from 'src/app/core/dom-helpers';

describe('DismissDialogComponent', () => {
let component: DismissDialogComponent;
let fixture: ComponentFixture<DismissDialogComponent>;
let popoverController: jasmine.SpyObj<PopoverController>;

const dismissMethod = () => of(true);
const errMethod = () => throwError(() => new Error('error'));

beforeEach(waitForAsync(() => {
const popoverControllerSpy = jasmine.createSpyObj('PopoverController', ['dismiss']);
TestBed.configureTestingModule({
declarations: [DismissDialogComponent],
imports: [IonicModule.forRoot()],
declarations: [DismissDialogComponent, FormButtonValidationDirective],
imports: [IonicModule.forRoot(), FormsModule, MatIconTestingModule, MatIconModule],
providers: [
{
provide: PopoverController,
useValue: popoverControllerSpy,
},
],
}).compileComponents();

fixture = TestBed.createComponent(DismissDialogComponent);
component = fixture.componentInstance;
popoverController = TestBed.inject(PopoverController) as jasmine.SpyObj<PopoverController>;
fixture.detectChanges();
}));

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

it('should display header and CTA text correctly', () => {
fixture.detectChanges();

expect(getTextContent(getElementBySelector(fixture, '.dismiss-dialog--header'))).toEqual(
'Dismiss duplicate expenses'
);
expect(getTextContent(getElementBySelector(fixture, '.dismiss-dialog--dismiss'))).toEqual('Yes, Dismiss');
});

it('cancel(): should cancel the CTA', () => {
popoverController.dismiss.and.callThrough();
component.dismissCallInProgress = false;
fixture.detectChanges();

component.cancel();
expect(popoverController.dismiss).toHaveBeenCalledTimes(1);
});

describe('dismiss():', () => {
it('should dismiss item', (done) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should dismiss expense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep updated

popoverController.dismiss.and.callThrough();
component.dismissCallInProgress = false;
component.dismissMethod = dismissMethod;
fixture.detectChanges();

component.dismiss();
expect(popoverController.dismiss).toHaveBeenCalledWith({ status: 'success' });
done();
});

it('should not dismiss item if dismiss method throws error', (done) => {
popoverController.dismiss.and.callThrough();
component.dismissCallInProgress = false;
component.dismissMethod = errMethod;
fixture.detectChanges();

component.dismiss();
expect(popoverController.dismiss).toHaveBeenCalledWith({ status: 'error' });
done();
});
});

it('should call cancel() if button is clicked', () => {
const cancelFn = spyOn(component, 'cancel');

const cancelButton = getElementBySelector(fixture, '.dismiss-dialog--cancel') as HTMLElement;
click(cancelButton);
expect(cancelFn).toHaveBeenCalledTimes(1);
});

it('should call dismiss() if card is clicked', () => {
const dismissFn = spyOn(component, 'dismiss');

const dismissCard = getElementBySelector(fixture, '.dismiss-dialog--dismiss') as HTMLElement;
click(dismissCard);
expect(dismissFn).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { ExpensesService } from 'src/app/core/services/platform/v1/spender/expen
import { cloneDeep } from 'lodash';
import { OrgSettingsService } from 'src/app/core/services/org-settings.service';
import { expenseDuplicateSet } from 'src/app/core/mock-data/platform/v1/expense-duplicate-sets.data';
import { PopoverController } from '@ionic/angular';
import { DismissDialogComponent } from '../dashboard/tasks/dismiss-dialog/dismiss-dialog.component';

describe('PotentialDuplicatesPage', () => {
let component: PotentialDuplicatesPage;
Expand All @@ -23,6 +25,7 @@ describe('PotentialDuplicatesPage', () => {
let matSnackBar: jasmine.SpyObj<MatSnackBar>;
let trackingService: jasmine.SpyObj<TrackingService>;
let expensesService: jasmine.SpyObj<ExpensesService>;
let popoverController: jasmine.SpyObj<PopoverController>;

beforeEach(waitForAsync(() => {
const routerSpy = jasmine.createSpyObj('Router', ['navigate']);
Expand All @@ -39,6 +42,7 @@ describe('PotentialDuplicatesPage', () => {
'dismissDuplicates',
]);
const orgSettingsServiceSpy = jasmine.createSpyObj('OrgSettingsService', ['get']);
const popoverControllerSpy = jasmine.createSpyObj('PopoverController', ['create']);

TestBed.configureTestingModule({
declarations: [PotentialDuplicatesPage],
Expand Down Expand Up @@ -68,6 +72,10 @@ describe('PotentialDuplicatesPage', () => {
provide: OrgSettingsService,
useValue: orgSettingsServiceSpy,
},
{
provide: PopoverController,
useValue: popoverControllerSpy,
},
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents();
Expand All @@ -81,6 +89,7 @@ describe('PotentialDuplicatesPage', () => {
matSnackBar = TestBed.inject(MatSnackBar) as jasmine.SpyObj<MatSnackBar>;
trackingService = TestBed.inject(TrackingService) as jasmine.SpyObj<TrackingService>;
expensesService = TestBed.inject(ExpensesService) as jasmine.SpyObj<ExpensesService>;
popoverController = TestBed.inject(PopoverController) as jasmine.SpyObj<PopoverController>;

component.loadData$ = new BehaviorSubject<void>(null);
component.duplicateSets$ = of([]);
Expand Down Expand Up @@ -166,38 +175,69 @@ describe('PotentialDuplicatesPage', () => {
expensesService.dismissDuplicates.and.returnValue(of(null));
});

it('should dismiss a duplicate expense', () => {
component.dismiss(apiExpenses1[0]);
it('should dismiss a duplicate expense and show the dialog', async () => {
const popoverResponse = { data: { status: 'success' } };
const popoverSpy = jasmine.createSpyObj('Popover', ['present', 'onDidDismiss']);
popoverSpy.onDidDismiss.and.resolveTo(popoverResponse);
popoverController.create.and.resolveTo(popoverSpy);

await component.dismiss(apiExpenses1[0]);

expect(popoverController.create).toHaveBeenCalledWith({
component: DismissDialogComponent,
cssClass: 'dismiss-dialog',
backdropDismiss: false,
componentProps: {
dismissMethod: jasmine.any(Function),
},
});

expect(popoverSpy.present).toHaveBeenCalledTimes(1);
expect(popoverSpy.onDidDismiss).toHaveBeenCalledTimes(1);

expect(component.duplicateExpenses[0].length).toEqual(1);
expect(expensesService.dismissDuplicates).toHaveBeenCalledOnceWith(
['txcSFe6efB6R', 'txDDLtRaflUW'],
['txDDLtRaflUW']
);
expect(component.showDismissedSuccessToast).toHaveBeenCalledTimes(1);
if (popoverResponse.data?.status === 'success') {
expect(component.duplicateExpenses[0].length).toEqual(1);
expect(component.showDismissedSuccessToast).toHaveBeenCalledTimes(1);
}
});
});

describe('dismissAll(): ', () => {
it('should dismiss all transactions', () => {
it('should dismiss all transactions and show the dialog', async () => {
component.duplicateSetData = [['tx5fBcPBAxLv'], ['tx5fBcPBAxLv', 'tx3nHShG60zq']];
component.selectedSet = 1;
const popoverResponse = { data: { status: 'success' } };

expensesService.dismissDuplicates.and.returnValue(of(null));
spyOn(component, 'showDismissedSuccessToast');
spyOn(component.loadData$, 'next');
expensesService.getExpenses.and.returnValue(of([apiExpenses1[0], expenseData]));
component.duplicateSets$ = of([[apiExpenses1[0]], [expenseData]]);

component.dismissAll();
const popoverSpy = jasmine.createSpyObj('Popover', ['present', 'onDidDismiss']);
popoverSpy.onDidDismiss.and.resolveTo(popoverResponse);
popoverController.create.and.resolveTo(popoverSpy);

await component.dismissAll();

expect(popoverController.create).toHaveBeenCalledWith({
component: DismissDialogComponent,
cssClass: 'dismiss-dialog',
backdropDismiss: false,
componentProps: {
dismissMethod: jasmine.any(Function),
},
});

expect(popoverSpy.present).toHaveBeenCalledTimes(1);
expect(popoverSpy.onDidDismiss).toHaveBeenCalledTimes(1);

expect(expensesService.dismissDuplicates).toHaveBeenCalledOnceWith(
['tx5fBcPBAxLv', 'tx3nHShG60zq'],
['tx5fBcPBAxLv', 'tx3nHShG60zq']
);
expect(component.selectedSet).toEqual(0);
expect(trackingService.dismissedDuplicateSet).toHaveBeenCalledTimes(1);
expect(component.showDismissedSuccessToast).toHaveBeenCalledTimes(1);
expect(component.loadData$.next).toHaveBeenCalledTimes(1);
if (popoverResponse.data?.status === 'success') {
expect(component.selectedSet).toEqual(0);
expect(trackingService.dismissedDuplicateSet).toHaveBeenCalledTimes(1);
expect(component.showDismissedSuccessToast).toHaveBeenCalledTimes(1);
expect(component.loadData$.next).toHaveBeenCalledTimes(1);
}
});
});

Expand Down
Loading