Skip to content

Commit

Permalink
test(helpers): re-organize tests to reflect code structure
Browse files Browse the repository at this point in the history
Refs #2717
  • Loading branch information
char0n committed Jan 11, 2023
1 parent 3bf7c1d commit f212615
Show file tree
Hide file tree
Showing 5 changed files with 705 additions and 698 deletions.
110 changes: 110 additions & 0 deletions test/helpers/get-operation-raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import getOperationRaw from '../../src/helpers/get-operation-raw.js';

describe('helpers', () => {
describe('getOperationRaw', () => {
test('should return the operation object, given an explicit operationId', () => {
// Given
const spec = {
paths: {
'/one': {
get: { operationId: 'op1' },
},
},
};

// When
const op = getOperationRaw(spec, 'op1');

// Then
expect(op).toMatchObject({
operation: spec.paths['/one'].get,
pathName: '/one',
method: 'GET',
});
});

test('should return the operation object, given an explicit operationId with special characters', () => {
// Given
const spec = {
paths: {
'/one': {
get: { operationId: 'A.Very_Special-operationID!' },
},
},
};

// When
const op = getOperationRaw(spec, 'A.Very_Special-operationID!');

// Then
expect(op).toMatchObject({
operation: spec.paths['/one'].get,
pathName: '/one',
method: 'GET',
});
});

test('should return null, given an explicit operationId that does not exist', () => {
// Given
const spec = {
paths: {
'/one': {
get: { operationId: 'getOne' },
},
},
};

// When
const op = getOperationRaw(spec, 'ThisOperationIdDoesNotExist');

// Then
expect(op).toEqual(null);
});

test('should return the operationObj, given a generated operationId', () => {
// Given`
const spec = {
paths: {
'/two': {
get: {
description: 'an operation',
},
},
},
};

// When
const op = getOperationRaw(spec, 'get_two');

// Then
expect(op).toMatchObject({
operation: spec.paths['/two'].get,
pathName: '/two',
method: 'GET',
});
});

test('should return the operationObj, given a generated legacy operationId', () => {
// Given`
const spec = {
paths: {
'/two': {
get: {
description: 'an operation',
},
},
},
};

// When
const op = getOperationRaw(spec, 'get-/two');

// Then
expect(op).toMatchObject({
operation: spec.paths['/two'].get,
pathName: '/two',
method: 'GET',
});
});
});
});
29 changes: 29 additions & 0 deletions test/helpers/id-from-path-method/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import idFromPathMethod from '../../../src/helpers/id-from-path-method/index.js';

describe('helpers', () => {
describe('idFromPathMethod', () => {
test('should return get_one as an operationId', () => {
const id = idFromPathMethod('/one', 'get');

expect(id).toEqual('get_one');
});

test('should return get_one as an operationId', () => {
const id = idFromPathMethod('/one', 'get');

expect(id).toEqual('get_one');
});

test('should handle strange paths/methods correctly when in v2 mode', () => {
const fn = (path, method) =>
idFromPathMethod(path, method, {
v2OperationIdCompatibilityMode: true,
});

// https://github.com/swagger-api/swagger-js/issues/1269#issue-309070070
expect(fn('/foo/{bar}/baz', 'get')).toEqual('get_foo_bar_baz');
expect(fn('/one/{foo}/{bar}', 'get')).toEqual('get_one_foo_bar');
expect(fn('/one/{bar}/-----{baz}', 'get')).toEqual('get_one_bar_baz');
});
});
});
Loading

0 comments on commit f212615

Please sign in to comment.