diff --git a/app/routes/server.$serverId.shlink-api.$method.ts b/app/routes/server.$serverId.shlink-api.$method.ts index cb00ae9..0cf8cb1 100644 --- a/app/routes/server.$serverId.shlink-api.$method.ts +++ b/app/routes/server.$serverId.shlink-api.$method.ts @@ -3,7 +3,7 @@ import { json } from '@remix-run/node'; import { ShlinkApiClient } from '@shlinkio/shlink-js-sdk'; import { NodeHttpClient } from '@shlinkio/shlink-js-sdk/node'; import type { Server } from '../entities/Server'; -import { ServersServiceServer } from '../servers/ServersService.server'; +import { ServersService } from '../servers/ServersService.server'; type Callback = (...args: unknown[]) => unknown; @@ -21,7 +21,7 @@ function argsAreValidForAction(args: any[], callback: Callback): args is Paramet export async function action( { params, request }: ActionFunctionArgs, - serversService = new ServersServiceServer(), + serversService = new ServersService(), createApiClient = (server: Server) => new ShlinkApiClient(new NodeHttpClient(), server), ) { try { diff --git a/app/servers/ServersService.server.ts b/app/servers/ServersService.server.ts index 828653b..77d9ddb 100644 --- a/app/servers/ServersService.server.ts +++ b/app/servers/ServersService.server.ts @@ -3,7 +3,7 @@ import { appDataSource } from '../db/data-source.server'; import type { Server } from '../entities/Server'; import { ServerEntity } from '../entities/Server'; -export class ServersServiceServer { +export class ServersService { constructor(private readonly em: EntityManager = appDataSource.manager) {} public async getByPublicId(publicId: string): Promise { diff --git a/test/servers/ServersService.server.test.ts b/test/servers/ServersService.server.test.ts new file mode 100644 index 0000000..25ea47d --- /dev/null +++ b/test/servers/ServersService.server.test.ts @@ -0,0 +1,33 @@ +import { fromPartial } from '@total-typescript/shoehorn'; +import type { EntityManager } from 'typeorm'; +import type { Server } from '../../app/entities/Server'; +import { ServersService } from '../../app/servers/ServersService.server'; + +describe('ServersService', () => { + const findOneBy = vi.fn(); + let em: EntityManager; + let service: ServersService; + + beforeEach(() => { + em = fromPartial({ findOneBy }); + service = new ServersService(em); + vi.clearAllMocks(); + }); + + describe('getByPublicId', () => { + it('throws error if server is not found', async () => { + await expect(() => service.getByPublicId('123')).rejects.toEqual( + new Error('Server with public ID 123 not found'), + ); + }); + + it('returns server when found', async () => { + const server = fromPartial({}); + findOneBy.mockResolvedValue(server); + + const result = await service.getByPublicId('123'); + + expect(result).toEqual(server); + }); + }); +});