101 lines
2.9 KiB
TypeScript
101 lines
2.9 KiB
TypeScript
/* tslint:disable max-line-length */
|
|
import { vitest } from 'vitest';
|
|
import { type MountingOptions, shallowMount } from '@vue/test-utils';
|
|
import sinon, { type SinonStubbedInstance } from 'sinon';
|
|
|
|
import Transaction from './transaction.vue';
|
|
import TransactionService from './transaction.service';
|
|
import AlertService from '@/shared/alert/alert.service';
|
|
|
|
type TransactionComponentType = InstanceType<typeof Transaction>;
|
|
|
|
const bModalStub = {
|
|
render: () => {},
|
|
methods: {
|
|
hide: () => {},
|
|
show: () => {},
|
|
},
|
|
};
|
|
|
|
describe('Component Tests', () => {
|
|
let alertService: AlertService;
|
|
|
|
describe('Transaction Management Component', () => {
|
|
let transactionServiceStub: SinonStubbedInstance<TransactionService>;
|
|
let mountOptions: MountingOptions<TransactionComponentType>['global'];
|
|
|
|
beforeEach(() => {
|
|
transactionServiceStub = sinon.createStubInstance<TransactionService>(TransactionService);
|
|
transactionServiceStub.retrieve.resolves({ headers: {} });
|
|
|
|
alertService = new AlertService({
|
|
bvToast: {
|
|
toast: vitest.fn(),
|
|
} as any,
|
|
});
|
|
|
|
mountOptions = {
|
|
stubs: {
|
|
bModal: bModalStub as any,
|
|
'font-awesome-icon': true,
|
|
'b-badge': true,
|
|
'b-button': true,
|
|
'router-link': true,
|
|
},
|
|
directives: {
|
|
'b-modal': {},
|
|
},
|
|
provide: {
|
|
alertService,
|
|
transactionService: () => transactionServiceStub,
|
|
},
|
|
};
|
|
});
|
|
|
|
describe('Mount', () => {
|
|
it('Should call load all on init', async () => {
|
|
// GIVEN
|
|
transactionServiceStub.retrieve.resolves({ headers: {}, data: [{ id: 123 }] });
|
|
|
|
// WHEN
|
|
const wrapper = shallowMount(Transaction, { global: mountOptions });
|
|
const comp = wrapper.vm;
|
|
await comp.$nextTick();
|
|
|
|
// THEN
|
|
expect(transactionServiceStub.retrieve.calledOnce).toBeTruthy();
|
|
expect(comp.transactions[0]).toEqual(expect.objectContaining({ id: 123 }));
|
|
});
|
|
});
|
|
describe('Handles', () => {
|
|
let comp: TransactionComponentType;
|
|
|
|
beforeEach(async () => {
|
|
const wrapper = shallowMount(Transaction, { global: mountOptions });
|
|
comp = wrapper.vm;
|
|
await comp.$nextTick();
|
|
transactionServiceStub.retrieve.reset();
|
|
transactionServiceStub.retrieve.resolves({ headers: {}, data: [] });
|
|
});
|
|
|
|
it('Should call delete service on confirmDelete', async () => {
|
|
// GIVEN
|
|
transactionServiceStub.delete.resolves({});
|
|
|
|
// WHEN
|
|
comp.prepareRemove({ id: 123 });
|
|
|
|
comp.removeTransaction();
|
|
await comp.$nextTick(); // clear components
|
|
|
|
// THEN
|
|
expect(transactionServiceStub.delete.called).toBeTruthy();
|
|
|
|
// THEN
|
|
await comp.$nextTick(); // handle component clear watch
|
|
expect(transactionServiceStub.retrieve.callCount).toEqual(1);
|
|
});
|
|
});
|
|
});
|
|
});
|