Update tests

This commit is contained in:
Alexandre Bodin 2023-09-21 18:42:19 +02:00
parent c9516d4bf5
commit c4573af2eb
2 changed files with 15 additions and 13 deletions

View File

@ -1,7 +1,7 @@
import { Database } from '../index'; import { Database, DatabaseConfig } from '../index';
jest.mock('../connection', () => jest.mock('../connection', () => ({
jest.fn(() => { createConnection: jest.fn(() => {
const trx = { const trx = {
commit: jest.fn(), commit: jest.fn(),
rollback: jest.fn(), rollback: jest.fn(),
@ -10,8 +10,8 @@ jest.mock('../connection', () =>
...trx, ...trx,
transaction: jest.fn(async () => trx), transaction: jest.fn(async () => trx),
}; };
}) }),
); }));
jest.mock('../dialects', () => ({ jest.mock('../dialects', () => ({
getDialect: jest.fn(() => ({ getDialect: jest.fn(() => ({
@ -24,10 +24,13 @@ jest.mock('../migrations', () => ({
createMigrationsProvider: jest.fn(), createMigrationsProvider: jest.fn(),
})); }));
const config = { const config: DatabaseConfig = {
models: [ models: [
{ {
uid: 'test',
singularName: 'test',
tableName: 'strapi_core_store_settings', tableName: 'strapi_core_store_settings',
attributes: {},
}, },
], ],
connection: { connection: {
@ -40,14 +43,11 @@ const config = {
host: 'localhost', host: 'localhost',
}, },
}, },
settings: {},
}; };
describe('Database', () => { describe('Database', () => {
describe('constructor', () => { describe('constructor', () => {
it('should throw an error if no config is provided', async () => {
expect(async () => Database.init()).rejects.toThrowError();
});
it('it should intialize if config is provided', async () => { it('it should intialize if config is provided', async () => {
expect(() => Database.init(config)).toBeDefined(); expect(() => Database.init(config)).toBeDefined();
}); });
@ -63,7 +63,7 @@ describe('Database', () => {
const db = await Database.init(config); const db = await Database.init(config);
const result = await db.transaction(async () => 'test'); const result = await db.transaction(async () => 'test');
expect(result).toBe('test'); expect(result).toBe('test');
expect(db.connection.commit).toHaveBeenCalledTimes(1); expect((db.connection as any).commit).toHaveBeenCalledTimes(1);
}); });
it('rollback is called incase of error', async () => { it('rollback is called incase of error', async () => {
@ -75,7 +75,7 @@ describe('Database', () => {
} catch { } catch {
/* ignore */ /* ignore */
} }
expect(db.connection.rollback).toHaveBeenCalledTimes(1); expect((db.connection as any).rollback).toHaveBeenCalledTimes(1);
}); });
it('should throw error', async () => { it('should throw error', async () => {

View File

@ -1,4 +1,5 @@
import { createLifecyclesProvider } from '../lifecycles'; import { createLifecyclesProvider } from '../lifecycles';
import type { Database } from '..';
describe('LifecycleProvider', () => { describe('LifecycleProvider', () => {
describe('run', () => { describe('run', () => {
@ -11,7 +12,8 @@ describe('LifecycleProvider', () => {
metadata: { metadata: {
get: dbMetadataGetStub, get: dbMetadataGetStub,
}, },
}; } as any as Database;
provider = createLifecyclesProvider(db); provider = createLifecyclesProvider(db);
provider.clear(); provider.clear();
}); });