Skip to content

Commit

Permalink
Create ServersService test
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed May 11, 2024
1 parent f96cd09 commit bbab852
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/routes/server.$serverId.shlink-api.$method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check warning on line 6 in app/routes/server.$serverId.shlink-api.$method.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/server.$serverId.shlink-api.$method.ts#L5-L6

Added lines #L5 - L6 were not covered by tests

type Callback = (...args: unknown[]) => unknown;

Expand All @@ -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),
) {

Check warning on line 26 in app/routes/server.$serverId.shlink-api.$method.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/server.$serverId.shlink-api.$method.ts#L22-L26

Added lines #L22 - L26 were not covered by tests
try {
Expand Down
2 changes: 1 addition & 1 deletion app/servers/ServersService.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Server> {
Expand Down
33 changes: 33 additions & 0 deletions test/servers/ServersService.server.test.ts
Original file line number Diff line number Diff line change
@@ -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<EntityManager>({ 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<Server>({});
findOneBy.mockResolvedValue(server);

const result = await service.getByPublicId('123');

expect(result).toEqual(server);
});
});
});

0 comments on commit bbab852

Please sign in to comment.