Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperKoza343 committed Jan 21, 2025
1 parent 315a132 commit 312bb08
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/client-linkedin/__tests__/get-random-integer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, it, expect } from 'vitest';
import { getRandomInteger } from '../src/helpers/get-random-integer';

describe('getRandomInteger', () => {
it('should return a number between min and max inclusive', () => {
const min = 1;
const max = 10;
const result = getRandomInteger(min, max);

expect(result).toBeGreaterThanOrEqual(min);
expect(result).toBeLessThanOrEqual(max);
expect(Number.isInteger(result)).toBe(true);
});

it('should work with same min and max values', () => {
const result = getRandomInteger(5, 5);
expect(result).toBe(5);
});

it('should throw error when min is greater than max', () => {
expect(() => getRandomInteger(10, 1)).toThrow('Min value cannot be greater than max value');
});

it('should throw error for NaN values', () => {
expect(() => getRandomInteger(NaN, 10)).toThrow('Invalid range: min and max must be valid numbers');
expect(() => getRandomInteger(1, NaN)).toThrow('Invalid range: min and max must be valid numbers');
expect(() => getRandomInteger(NaN, NaN)).toThrow('Invalid range: min and max must be valid numbers');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, it, expect } from 'vitest';
import { AxiosError } from 'axios';
import { prepareAxiosErrorMessage } from '../src/helpers/prepare-axios-error-message';

describe('prepareAxiosErrorMessage', () => {
it('should format error with all fields', () => {
const mockError = new AxiosError(
'Network Error',
'ERR_NETWORK',
undefined,
undefined,
{
status: 404,
data: { error: 'Not Found' },
} as any
);

const result = prepareAxiosErrorMessage(mockError);
const parsed = JSON.parse(result);

expect(parsed).toEqual({
message: 'Network Error',
status: 404,
data: { error: 'Not Found' },
code: 'ERR_NETWORK'
});
});

it('should handle error without response data', () => {
const mockError = new AxiosError(
'Timeout Error',
'ECONNABORTED'
);

const result = prepareAxiosErrorMessage(mockError);
const parsed = JSON.parse(result);

expect(parsed).toEqual({
message: 'Timeout Error',
status: undefined,
data: undefined,
code: 'ECONNABORTED'
});
});
});

0 comments on commit 312bb08

Please sign in to comment.