test: add unit tests for DAOs
This commit is contained in:
76
src/app/dao/EstablishmentDAO.spec.ts
Normal file
76
src/app/dao/EstablishmentDAO.spec.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { EstablishmentDAO } from './EstablishmentDAO';
|
||||
import { Sqlite } from '../services/sqlite';
|
||||
import { Establishment } from '../models/Establishment';
|
||||
import { Chain } from '../models/Chain';
|
||||
import { EstablishmentQueryResult } from '../types/sqlite.type';
|
||||
|
||||
describe('EstablishmentDAO', () => {
|
||||
let service: EstablishmentDAO;
|
||||
let sqlite: Partial<Sqlite>;
|
||||
let ESTABLISHMENT_MOCK: Establishment;
|
||||
let QUERY_RESULT_MOCK: EstablishmentQueryResult;
|
||||
|
||||
beforeEach(() => {
|
||||
QUERY_RESULT_MOCK = {
|
||||
address: 'mock street',
|
||||
image: 'mock.jpg',
|
||||
name: 'mock',
|
||||
chain_id: 1,
|
||||
id: 1,
|
||||
};
|
||||
ESTABLISHMENT_MOCK = new Establishment(
|
||||
new Chain(QUERY_RESULT_MOCK.name, QUERY_RESULT_MOCK.image, QUERY_RESULT_MOCK.chain_id),
|
||||
QUERY_RESULT_MOCK.address,
|
||||
QUERY_RESULT_MOCK.id,
|
||||
);
|
||||
|
||||
sqlite = {
|
||||
executeQuery: vi.fn().mockResolvedValue({ rows: [QUERY_RESULT_MOCK] }),
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [{ provide: Sqlite, useValue: sqlite }],
|
||||
});
|
||||
|
||||
service = TestBed.inject(EstablishmentDAO);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should receive mapped results on findAll', async () => {
|
||||
const result = await service.findAll();
|
||||
if (result.length > 0) {
|
||||
expect(result[0]).toEqual(ESTABLISHMENT_MOCK);
|
||||
} else {
|
||||
test.fails('Expected mock response to contain at least one value');
|
||||
}
|
||||
});
|
||||
|
||||
it('should receive mapped results on findBy', async () => {
|
||||
const result = await service.findBy({ id: 1 });
|
||||
if (result.length > 0) {
|
||||
expect(result[0]).toEqual(ESTABLISHMENT_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 establishment ( address, chain_id ) VALUES ( ?, ? );';
|
||||
const expectedParams = [ESTABLISHMENT_MOCK.address, ESTABLISHMENT_MOCK.chain.id];
|
||||
ESTABLISHMENT_MOCK.id = undefined;
|
||||
await service.insert(ESTABLISHMENT_MOCK);
|
||||
expect(sqlite.executeQuery).toHaveBeenCalledExactlyOnceWith(expectedSql, expectedParams);
|
||||
});
|
||||
|
||||
it('should throw if toDB is called with a chain that has no id', async () => {
|
||||
sqlite.executeQuery = vi.fn().mockResolvedValue({ rows: [] });
|
||||
ESTABLISHMENT_MOCK.id = undefined;
|
||||
ESTABLISHMENT_MOCK.chain.id = undefined;
|
||||
expect(service.insert(ESTABLISHMENT_MOCK)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user