test: add unit tests for DAOs
This commit is contained in:
95
src/app/dao/PurchaseDAO.spec.ts
Normal file
95
src/app/dao/PurchaseDAO.spec.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { PurchaseDAO } from './PurchaseDAO';
|
||||
import { Sqlite } from '../services/sqlite';
|
||||
import { Purchase } from '../models/Purchase';
|
||||
import { Establishment } from '../models/Establishment';
|
||||
import { Chain } from '../models/Chain';
|
||||
import { Product } from '../models/Product';
|
||||
import { PurchaseQueryResult } from '../types/sqlite.type';
|
||||
|
||||
describe('PurchaseDAO', () => {
|
||||
let service: PurchaseDAO;
|
||||
let sqlite: Partial<Sqlite>;
|
||||
|
||||
let PURCHASE_MOCK: Purchase;
|
||||
let PURCHASE_QUERY_RESULT_MOCK: PurchaseQueryResult;
|
||||
|
||||
beforeEach(() => {
|
||||
PURCHASE_MOCK = new Purchase(
|
||||
new Establishment(new Chain('mock', 'mock.jpg', 1), 'mock street', 1),
|
||||
new Product('1212112', 'mock', 'mock.png', 1),
|
||||
1245.55,
|
||||
3,
|
||||
Date.now(),
|
||||
1,
|
||||
);
|
||||
PURCHASE_QUERY_RESULT_MOCK = {
|
||||
address: PURCHASE_MOCK.establishment.address,
|
||||
barcode: PURCHASE_MOCK.product.barcode,
|
||||
chain_id: PURCHASE_MOCK.establishment.chain.id!,
|
||||
chain_image: PURCHASE_MOCK.establishment.chain.image,
|
||||
chain_name: PURCHASE_MOCK.establishment.chain.name,
|
||||
date: PURCHASE_MOCK.date,
|
||||
establishment_id: PURCHASE_MOCK.establishment.id!,
|
||||
id: PURCHASE_MOCK.id!,
|
||||
price: PURCHASE_MOCK.price,
|
||||
product_id: PURCHASE_MOCK.product.id!,
|
||||
product_image: PURCHASE_MOCK.product.image,
|
||||
product_name: PURCHASE_MOCK.product.name,
|
||||
quantity: PURCHASE_MOCK.quantity,
|
||||
};
|
||||
|
||||
sqlite = {
|
||||
executeQuery: vi.fn().mockResolvedValue({ rows: [PURCHASE_QUERY_RESULT_MOCK] }),
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [{ provide: Sqlite, useValue: sqlite }],
|
||||
});
|
||||
|
||||
service = TestBed.inject(PurchaseDAO);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(PURCHASE_MOCK).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should receive mapped results on findAll', async () => {
|
||||
const result = await service.findAll();
|
||||
if (result.length > 0) {
|
||||
expect(result[0]).toEqual(PURCHASE_MOCK);
|
||||
} else {
|
||||
test.fails('Expected mock response to contain at least one value');
|
||||
}
|
||||
});
|
||||
|
||||
it('should call executeQuery with object fields on insert', async () => {
|
||||
sqlite.executeQuery = vi.fn().mockResolvedValue({ rows: [] });
|
||||
const expectedSql =
|
||||
'INSERT INTO purchase ( establishment_id, product_id, date, price, quantity ) VALUES ( ?, ?, ?, ?, ? );';
|
||||
const expectedParams: any[] = [
|
||||
PURCHASE_MOCK.establishment.id,
|
||||
PURCHASE_MOCK.product.id,
|
||||
PURCHASE_MOCK.date,
|
||||
PURCHASE_MOCK.price * 100,
|
||||
PURCHASE_MOCK.quantity,
|
||||
];
|
||||
PURCHASE_MOCK.id = undefined;
|
||||
await service.insert(PURCHASE_MOCK);
|
||||
expect(sqlite.executeQuery).toHaveBeenCalledExactlyOnceWith(expectedSql, expectedParams);
|
||||
});
|
||||
|
||||
describe('toDB', () => {
|
||||
it('should throw if toDB is called with a establishmen that has no id', async () => {
|
||||
sqlite.executeQuery = vi.fn().mockResolvedValue({ rows: [] });
|
||||
PURCHASE_MOCK.establishment.id = undefined;
|
||||
expect(service.insert(PURCHASE_MOCK)).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('should throw if toDB is called with a product that has no id', async () => {
|
||||
sqlite.executeQuery = vi.fn().mockResolvedValue({ rows: [] });
|
||||
PURCHASE_MOCK.product.id = undefined;
|
||||
expect(service.insert(PURCHASE_MOCK)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user