From eb97b91d7ea54bacf507b665d9db13c7ee154775 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Wed, 6 Dec 2017 13:34:33 -0800 Subject: [PATCH 1/3] Add failing tests --- test/components/operations.js | 120 ++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 test/components/operations.js diff --git a/test/components/operations.js b/test/components/operations.js new file mode 100644 index 00000000..7609b985 --- /dev/null +++ b/test/components/operations.js @@ -0,0 +1,120 @@ +/* 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.only("", function(){ + it("should render a Swagger2 `get` method, but not a `trace` or `foo` method", function(){ + + let props = { + 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(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") + }) + + it("should render an OAS3 `get` and `trace` method, but not a `foo` method", function(){ + + let props = { + 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") + }) +}) From 328f02c4637d473269023c604e7cb8256cbd6c41 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Wed, 6 Dec 2017 13:52:06 -0800 Subject: [PATCH 2/3] Move method filtering logic to Operations component --- src/core/components/operations.jsx | 14 ++++++++++++++ src/core/plugins/spec/selectors.js | 5 ----- test/components/operations.js | 13 ++++++++----- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/core/components/operations.jsx b/src/core/components/operations.jsx index 32b15437..f1174e32 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,13 @@ export default class Operations extends React.Component { const path = op.get("path") const method = op.get("method") + 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 index 7609b985..8a6a7506 100644 --- a/test/components/operations.js +++ b/test/components/operations.js @@ -11,12 +11,14 @@ const components = { OperationContainer: ({ path, method }) => } -describe.only("", function(){ +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: () => { @@ -60,16 +62,17 @@ describe.only("", function(){ let wrapper = render() - expect(wrapper.find("span.mocked-op").length).toEqual(2) + expect(wrapper.find("span.mocked-op").length).toEqual(1) 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") }) 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: () => { From a7e71d81172da87f4d4da88b5632670146a9f2d1 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Thu, 7 Dec 2017 13:40:20 -0800 Subject: [PATCH 3/3] Add FIXME --- src/core/components/operations.jsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core/components/operations.jsx b/src/core/components/operations.jsx index f1174e32..70a5538f 100644 --- a/src/core/components/operations.jsx +++ b/src/core/components/operations.jsx @@ -120,6 +120,11 @@ 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