Skip to content

Commit

Permalink
chore: add test cases for utility to improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayteki95 committed Feb 18, 2025
1 parent ccb101a commit a6d6527
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/v0/util/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
convertToUuid,
} = require('./index');
const exp = require('constants');
const { ERROR_MESSAGES } = require('./constant');

// Names of the utility functions to test
const functionNames = [
Expand Down Expand Up @@ -1073,3 +1074,63 @@ describe('', () => {
expect(res).toBe(1003);
});
});

describe('getBodyFromV2SpecPayload', () => {
it('should successfully parse valid JSON body', () => {
const input = {
request: {
body: '{"key": "value", "number": 123}',
},
};
const expected = {
key: 'value',
number: 123,
};
expect(utilities.getBodyFromV2SpecPayload(input)).toEqual(expected);
});

it('should throw TransformationError for malformed JSON', () => {
const input = {
request: {
body: '{invalid json}',
},
};
expect(() => utilities.getBodyFromV2SpecPayload(input)).toThrow(
ERROR_MESSAGES.MALFORMED_JSON_IN_REQUEST_BODY,
);
});

it('should throw TransformationError when request body is missing', () => {
const input = {
request: {},
};
expect(() => utilities.getBodyFromV2SpecPayload(input)).toThrow(
ERROR_MESSAGES.REQUEST_BODY_NOT_PRESENT_IN_V2_SPEC_PAYLOAD,
);
});

it('should throw TransformationError when request is missing', () => {
const input = {};
expect(() => utilities.getBodyFromV2SpecPayload(input)).toThrow(
ERROR_MESSAGES.REQUEST_BODY_NOT_PRESENT_IN_V2_SPEC_PAYLOAD,
);
});

it('should parse empty JSON object', () => {
const input = {
request: {
body: '{}',
},
};
expect(utilities.getBodyFromV2SpecPayload(input)).toEqual({});
});

it('should parse JSON array', () => {
const input = {
request: {
body: '[1,2,3]',
},
};
expect(utilities.getBodyFromV2SpecPayload(input)).toEqual([1, 2, 3]);
});
});

0 comments on commit a6d6527

Please sign in to comment.