diff --git a/src/core/components/operations.jsx b/src/core/components/operations.jsx index 32b15437..70a5538f 100644 --- a/src/core/components/operations.jsx +++ b/src/core/components/operations.jsx @@ -2,6 +2,13 @@ import React from "react" import PropTypes from "prop-types" import { createDeepLinkPath, sanitizeUrl } from "core/utils" +const SWAGGER2_OPERATION_METHODS = [ + "get", "put", "post", "delete", "options", "head", "patch" +] + +const OAS3_OPERATION_METHODS = SWAGGER2_OPERATION_METHODS.concat(["trace"]) + + export default class Operations extends React.Component { static propTypes = { @@ -113,6 +120,18 @@ export default class Operations extends React.Component { const path = op.get("path") const method = op.get("method") + // FIXME: (someday) this logic should probably be in a selector, + // but doing so would require further opening up + // selectors to the plugin system, to allow for dynamic + // overriding of low-level selectors that other selectors + // rely on. --KS, 12/17 + const validMethods = specSelectors.isOAS3() ? + OAS3_OPERATION_METHODS : SWAGGER2_OPERATION_METHODS + + if(validMethods.indexOf(method) === -1) { + return null + } + return { return state || Map() } @@ -97,9 +95,6 @@ export const operations = createSelector( return {} } path.forEach((operation, method) => { - if(OPERATION_METHODS.indexOf(method) === -1) { - return - } list = list.push(fromJS({ path: pathName, method, diff --git a/test/components/operations.js b/test/components/operations.js new file mode 100644 index 00000000..8a6a7506 --- /dev/null +++ b/test/components/operations.js @@ -0,0 +1,123 @@ +/* eslint-env mocha */ +import React from "react" +import expect, { createSpy } from "expect" +import { render } from "enzyme" +import { fromJS } from "immutable" +import Operations from "components/operations" +import {Collapse} from "components/layout-utils" + +const components = { + Collapse, + OperationContainer: ({ path, method }) => +} + +describe("", function(){ + it("should render a Swagger2 `get` method, but not a `trace` or `foo` method", function(){ + + let props = { + fn: {}, + specActions: {}, + layoutActions: {}, + getComponent: (name)=> { + return components[name] || null + }, + getConfigs: () => { + return {} + }, + specSelectors: { + isOAS3() { return false }, + taggedOperations() { + return fromJS({ + "default": { + "operations": [ + { + "path": "/pets/{id}", + "method": "get" + }, + { + "path": "/pets/{id}", + "method": "trace" + }, + { + "path": "/pets/{id}", + "method": "foo" + }, + ] + } + }) + }, + }, + layoutSelectors: { + currentFilter() { + return null + }, + isShown() { + return true + }, + show() { + return true + } + } + } + + let wrapper = render() + + expect(wrapper.find("span.mocked-op").length).toEqual(1) + expect(wrapper.find("span.mocked-op").eq(0).attr("id")).toEqual("/pets/{id}-get") + }) + + it("should render an OAS3 `get` and `trace` method, but not a `foo` method", function(){ + + let props = { + fn: {}, + specActions: {}, + layoutActions: {}, + getComponent: (name)=> { + return components[name] || null + }, + getConfigs: () => { + return {} + }, + specSelectors: { + isOAS3() { return true }, + taggedOperations() { + return fromJS({ + "default": { + "operations": [ + { + "path": "/pets/{id}", + "method": "get" + }, + { + "path": "/pets/{id}", + "method": "trace" + }, + { + "path": "/pets/{id}", + "method": "foo" + }, + ] + } + }) + }, + }, + layoutSelectors: { + currentFilter() { + return null + }, + isShown() { + return true + }, + show() { + return true + } + } + } + + let wrapper = render() + + expect(wrapper.find("span.mocked-op").length).toEqual(2) + expect(wrapper.find("span.mocked-op").eq(0).attr("id")).toEqual("/pets/{id}-get") + expect(wrapper.find("span.mocked-op").eq(1).attr("id")).toEqual("/pets/{id}-trace") + }) +})