Skip to content

Commit

Permalink
Merge pull request #867 from terrestris/find-one-by-name
Browse files Browse the repository at this point in the history
feat: extends the util by a find by name function
  • Loading branch information
FritzHoing authored Jan 16, 2025
2 parents 0cf15d0 + f5a1186 commit ff91112
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/service/ApplicationService/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,72 @@ import ApplicationService from '.';

describe('ApplicationService', () => {
let service: ApplicationService<Application>;
const mockApplication = { id: 123, name: 'TestApp' } as Application;

beforeEach(() => {
service = new ApplicationService<Application>();
global.fetch = jest.fn();
});

afterEach(() => {
jest.clearAllMocks();
});

it('should return the application when the fetch is successful', async () => {
(fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce(mockApplication),
});

const result = await service.findOneByName('TestApp');

expect(fetch).toHaveBeenCalledWith('/applications/findByName/TestApp', expect.objectContaining({
method: 'GET',
headers: expect.any(Object),
}));
expect(result).toEqual(mockApplication);
});

it('should throw an error when the response is not ok', async () => {
(fetch as jest.Mock).mockResolvedValueOnce({
ok: false,
status: 404,
});

await expect(service.findOneByName('NonExistentApp')).rejects.toThrow('HTTP error status: 404');
expect(fetch).toHaveBeenCalledWith('/applications/findByName/NonExistentApp', expect.objectContaining({
method: 'GET',
headers: expect.any(Object),
}));
});

it('should throw an error when fetch fails', async () => {
(fetch as jest.Mock).mockRejectedValueOnce(new Error('Network error'));

await expect(service.findOneByName('TestApp')).rejects.toThrow('Error while requesting a single entity: Error: Network error');
expect(fetch).toHaveBeenCalledWith('/applications/findByName/TestApp', expect.objectContaining({
method: 'GET',
headers: expect.any(Object),
}));
});

it('should return the application when the fetch is successful', async () => {
const mockApplication = { id: 123, name: 'TestApp' } as Application;

(fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce(mockApplication),
});

const result = await service.findOneByName('TestApp');

expect(fetch).toHaveBeenCalledWith('/applications/findByName/TestApp', expect.objectContaining({
method: 'GET',
headers: expect.any(Object),
}));

expect(result).toEqual(mockApplication);
expect(result.name).toBe('TestApp');
});

it('is defined', () => {
Expand Down
21 changes: 21 additions & 0 deletions src/service/ApplicationService/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Application from '../../model/Application';
import { getBearerTokenHeader } from '../../security/getBearerTokenHeader';
import GenericEntityService, { GenericEntityServiceOpts } from '../GenericEntityService';

export class ApplicationService<T extends Application> extends GenericEntityService<T> {
Expand All @@ -9,6 +10,26 @@ export class ApplicationService<T extends Application> extends GenericEntityServ
super(opts);
}

async findOneByName(name: string, fetchOpts?: RequestInit): Promise<T> {
try {
const response = await fetch(`${this.basePath}/findByName/${name}`, {
method: 'GET',
headers: {
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});

if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}

return await response.json();
} catch (error) {
throw new Error(`Error while requesting a single entity: ${error}`);
}
}

}

export default ApplicationService;

0 comments on commit ff91112

Please sign in to comment.