Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
R11manish committed Sep 13, 2024
1 parent 33131c8 commit baa9d48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
14 changes: 6 additions & 8 deletions test/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { paths } from '../src/api';
import { GTWError } from '../src/helpers/custom-error';
import { Account } from '../src/modules/account/account';
import { MediaType } from 'openapi-typescript-helpers';

const mockClient = {
GET: jest.fn(),
};
import { mockClient, mockGet } from './stubs/common.stub';
import { routes } from '../src/common/routes';

describe('Account', () => {
let account: Account;
Expand All @@ -21,7 +19,7 @@ describe('Account', () => {
describe('getAccountInfo', () => {
it('should return account info when API call is successful', async () => {
const mockData: MyAccountResponse = { did: '123', username: 'Test User' };
mockClient.GET.mockResolvedValue({
mockGet.mockResolvedValue({
data: mockData,
response: {},
error: null,
Expand All @@ -30,20 +28,20 @@ describe('Account', () => {
const result = await account.getAccountInfo();

expect(result).toEqual(mockData);
expect(mockClient.GET).toHaveBeenCalledWith('/accounts/me');
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetMyAccount);
});

it('should throw GTWError when API call fails', async () => {
const mockResponse = { status: 401 } as Response;

mockClient.GET.mockResolvedValue({
mockGet.mockResolvedValue({
data: null,
response: mockResponse,
error: { error: 'Unauthorized' },
});

await expect(account.getAccountInfo()).rejects.toThrow(GTWError);
expect(mockClient.GET).toHaveBeenCalledWith('/accounts/me');
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetMyAccount);
});
});
});
32 changes: 12 additions & 20 deletions test/data-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import { ValidationService } from '../src/services/validator-service';
import { GTWError } from '../src/helpers/custom-error';
import { DataModel } from '../src/modules/data-model/data-model';
import { Config } from '../src/common/types';

const mockClient = {
GET: jest.fn(),
POST: jest.fn(),
PUT: jest.fn(),
};
import { mockClient, mockGet } from './stubs/common.stub';
import { routes } from '../src/common/routes';

const mockValidationService = {} as ValidationService;
const mockConfig = {} as Config;
Expand All @@ -17,11 +13,7 @@ describe('DataModel', () => {

beforeEach(() => {
jest.clearAllMocks();
dataModel = new DataModel(
mockClient as any,
mockValidationService,
mockConfig,
);
dataModel = new DataModel(mockClient, mockValidationService, mockConfig);
});

describe('getDataModels', () => {
Expand All @@ -32,7 +24,7 @@ describe('DataModel', () => {
page: 1,
pageSize: 10,
};
mockClient.GET.mockResolvedValue({
mockGet.mockResolvedValue({
data: mockResponse,
error: null,
response: {} as Response,
Expand All @@ -41,15 +33,15 @@ describe('DataModel', () => {
const result = await dataModel.getDataModels();

expect(result).toEqual(mockResponse);
expect(mockClient.GET).toHaveBeenCalledWith('/data-models', {
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetDataModels, {
params: { query: { page: 1, page_size: 10 } },
});
});

it('should throw GTWError on API error', async () => {
const mockError = { error: 'API Error' };
const mockResponse = { status: 400 } as Response;
mockClient.GET.mockResolvedValue({
mockGet.mockResolvedValue({
data: null,
error: mockError,
response: mockResponse,
Expand All @@ -70,7 +62,7 @@ describe('DataModel', () => {
describe('getDataModelById', () => {
it('should fetch a data model by id successfully', async () => {
const mockResponse = { id: 1, name: 'Model1' };
mockClient.GET.mockResolvedValue({
mockGet.mockResolvedValue({
data: mockResponse,
error: null,
response: {} as Response,
Expand All @@ -79,15 +71,15 @@ describe('DataModel', () => {
const result = await dataModel.getDataModelById(1);

expect(result).toEqual(mockResponse);
expect(mockClient.GET).toHaveBeenCalledWith('/data-models/{id}', {
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetDataModelByID, {
params: { path: { id: 1 } },
});
});

it('should throw GTWError on API error', async () => {
const mockError = { error: 'Not Found' };
const mockResponse = { status: 404 } as Response;
mockClient.GET.mockResolvedValue({
mockGet.mockResolvedValue({
data: null,
error: mockError,
response: mockResponse,
Expand All @@ -113,7 +105,7 @@ describe('DataModel', () => {
page: 1,
pageSize: 10,
};
mockClient.GET.mockResolvedValue({
mockGet.mockResolvedValue({
data: mockResponse,
error: null,
response: {} as Response,
Expand All @@ -122,15 +114,15 @@ describe('DataModel', () => {
const result = await dataModel.getMyDataModels();

expect(result).toEqual(mockResponse);
expect(mockClient.GET).toHaveBeenCalledWith('/data-models/me', {
expect(mockClient.GET).toHaveBeenCalledWith(routes.GetDataModelsByUser, {
params: { query: { page: 1, page_size: 10 } },
});
});

it('should throw GTWError on API error', async () => {
const mockError = { error: 'Unauthorized' };
const mockResponse = { status: 401 } as Response;
mockClient.GET.mockResolvedValue({
mockGet.mockResolvedValue({
data: null,
error: mockError,
response: mockResponse,
Expand Down

0 comments on commit baa9d48

Please sign in to comment.