improve: filter as a plugin (#4255)

This commit is contained in:
David Vujic
2018-02-28 00:44:30 +01:00
committed by kyle
parent 6f7a4c3097
commit e6722d87aa
6 changed files with 62 additions and 6 deletions

View File

@@ -21,7 +21,8 @@ export default class Operations extends React.Component {
layoutActions: PropTypes.object.isRequired,
authActions: PropTypes.object.isRequired,
authSelectors: PropTypes.object.isRequired,
getConfigs: PropTypes.func.isRequired
getConfigs: PropTypes.func.isRequired,
fn: PropTypes.func.isRequired
};
render() {
@@ -30,7 +31,8 @@ export default class Operations extends React.Component {
getComponent,
layoutSelectors,
layoutActions,
getConfigs
getConfigs,
fn
} = this.props
let taggedOps = specSelectors.taggedOperations()
@@ -52,9 +54,7 @@ export default class Operations extends React.Component {
if (filter) {
if (filter !== true) {
taggedOps = taggedOps.filter((tagObj, tag) => {
return tag.indexOf(filter) !== -1
})
taggedOps = fn.opsFilter(taggedOps, filter)
}
}

View File

@@ -0,0 +1,9 @@
import opsFilter from "./opsFilter"
export default function() {
return {
fn: {
opsFilter
}
}
}

View File

@@ -0,0 +1,3 @@
export default function(taggedOps, phrase) {
return taggedOps.filter((tagObj, tag) => tag.indexOf(phrase) !== -1)
}

View File

@@ -12,6 +12,7 @@ import SplitPaneModePlugin from "core/plugins/split-pane-mode"
import downloadUrlPlugin from "core/plugins/download-url"
import configsPlugin from "core/plugins/configs"
import deepLinkingPlugin from "core/plugins/deep-linking"
import filter from "core/plugins/filter"
import OperationContainer from "core/containers/OperationContainer"
@@ -152,6 +153,7 @@ export default function() {
ast,
SplitPaneModePlugin,
downloadUrlPlugin,
deepLinkingPlugin
deepLinkingPlugin,
filter
]
}

View File

@@ -0,0 +1,25 @@
import { Map } from "immutable"
import opsFilter from "corePlugins/filter/opsFilter"
import expect from "expect"
describe("opsFilter", function() {
const taggedOps = Map([["pet"], ["store"], ["user"]])
it("should filter taggedOps by tag name", function () {
const filtered = opsFilter(taggedOps, "sto")
expect(filtered.size).toEqual(1)
})
it("should return all taggedOps when search phrase is empty", function () {
const filtered = opsFilter(taggedOps, "")
expect(filtered.size).toEqual(taggedOps.size)
})
it("should return empty result when there is no match", function () {
const filtered = opsFilter(taggedOps, "NoMatch")
expect(filtered.size).toEqual(0)
})
})

View File

@@ -5,6 +5,7 @@ import System from "core/system"
import { fromJS } from "immutable"
import { render } from "enzyme"
import ViewPlugin from "core/plugins/view/index.js"
import filterPlugin from "core/plugins/filter/index.js"
import { connect, Provider } from "react-redux"
describe("bound system", function(){
@@ -264,6 +265,22 @@ describe("bound system", function(){
})
describe("fn", function() {
it("should return helper functions", function () {
// Given
const system = new System({
plugins: [
filterPlugin
]
})
// When
const fn = system.getSystem().fn.opsFilter
expect(typeof fn).toEqual("function")
})
})
describe("selectors", function(){
it("should have the first arg be the nested state, and all other args to follow", function(){