Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleythedeveloper committed Oct 28, 2024
1 parent 6fc3047 commit 9d6d775
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
26 changes: 23 additions & 3 deletions packages/vc-test-suite/__tests__/httpService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@ const mockedAxios = axios as jest.Mocked<typeof axios>;

describe('httpService', () => {
beforeEach(() => {
jest.clearAllMocks();

const mockRequest = jest.fn().mockResolvedValue({ data: 'mocked response' });

mockedAxios.create.mockReturnValue({
request: jest.fn().mockResolvedValue({ data: 'mocked response' }),
request: mockRequest,
} as any);
});

it('should accept a plain object for headers', async () => {
const result = await request({ headers: { test: 'test' } });
expect(result).toBeDefined();
const params = { headers: { test: 'test' } };
const result = await request(params);

expect(result).toEqual({ data: 'mocked response' });

const mockAxiosInstance = mockedAxios.create.mock.results[0].value;
expect(mockAxiosInstance.request).toHaveBeenCalledTimes(1);
expect(mockAxiosInstance.request).toHaveBeenCalledWith(params);
});

it('should throw an error if headers is not a plain object', async () => {
Expand All @@ -29,4 +38,15 @@ describe('httpService', () => {
'Headers specified in the config must be a plain object with string values.',
);
});

it('should work when no headers are provided in params', async () => {
const params = { url: 'test-url' };
const result = await request(params);

expect(result).toEqual({ data: 'mocked response' });

const mockAxiosInstance = mockedAxios.create.mock.results[0].value;
expect(mockAxiosInstance.request).toHaveBeenCalledTimes(1);
expect(mockAxiosInstance.request).toHaveBeenCalledWith(params);
});
});
13 changes: 8 additions & 5 deletions packages/vc-test-suite/httpService.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import axios, { AxiosRequestConfig } from 'axios';
import { isPlainObject, every, isString } from 'lodash';

const defaultHeaders = {
'Content-Type': 'application/json',
};

export const request = async (params: AxiosRequestConfig) => {
const instance = axios.create({
headers: defaultHeaders,
});

// Check if user defined headers is an object and contains only string values
if (params.headers) {
if (!isPlainObject(params.headers) || !every(params.headers, isString)) {
throw new Error('Headers specified in the config must be a plain object with string values.');
}
}

const instance = axios.create({
headers: {
...defaultHeaders,
...params.headers,
},
});

const response = await instance.request(params);
return response;
};
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ describe('RenderTemplate2024', function () {
},
);

// TODO check grammar
reportRow("should successfully render when 'mediaQuery' is not provided", config.implementationName, async () => {
reportRow('should render successfully when ‘mediaQuery’ is not provided', config.implementationName, async () => {
// Import the input data for the test from the specified JSON file.
const input = require('./input/no-mediaQuery-in-renderMethod-ok.json');

Expand Down

0 comments on commit 9d6d775

Please sign in to comment.