-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
export const FILTERS_PIPELINE = {}; | ||
const PRE_AJAX_REQUEST = "PRE_AJAX_REQUEST"; | ||
|
||
export const Hooks = { | ||
PRE_AJAX_REQUEST: PRE_AJAX_REQUEST | ||
}; | ||
|
||
export default class PipelineStore { | ||
|
||
applyFilter(filterName, filterParameters) { | ||
if (FILTERS_PIPELINE[filterName]) { | ||
for (let filter of FILTERS_PIPELINE[filterName]) { | ||
filter(filterParameters); | ||
} | ||
} | ||
return filterParameters; | ||
}; | ||
|
||
register(filterName, callback) { | ||
|
||
if (!FILTERS_PIPELINE[filterName]) { | ||
FILTERS_PIPELINE[filterName] = []; | ||
} | ||
|
||
let callBacklist = FILTERS_PIPELINE[filterName]; | ||
if (callBacklist.indexOf(callback) === -1) { | ||
FILTERS_PIPELINE[filterName].push(callback); | ||
} | ||
}; | ||
|
||
deregister(filterName, callback) { | ||
var callbckPosition = FILTERS_PIPELINE[filterName].indexOf(callback); | ||
if (callbckPosition !== -1) { | ||
FILTERS_PIPELINE[filterName].splice(callbckPosition, 1); | ||
} | ||
}; | ||
|
||
getPipeline() { | ||
return FILTERS_PIPELINE; | ||
}; | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import {expect} from "chai"; | ||
import PipelineStore, {Hooks} from "../../js/plugin/sdk/stores/PipelineStore" | ||
import {FILTERS_PIPELINE} from "../../js/plugin/sdk/stores/PipelineStore" | ||
|
||
describe("Pipeline tests", function () { | ||
|
||
beforeEach(function (){ | ||
for (let a of Object.keys(FILTERS_PIPELINE)){ | ||
delete FILTERS_PIPELINE[a]; | ||
} | ||
}); | ||
|
||
it("should add a new function to the pipeline", function () { | ||
var filter = function() {}; | ||
|
||
let pipeline = new PipelineStore(); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.be.equal(undefined); | ||
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(1); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST][0]).to.be.equal(filter); | ||
}); | ||
|
||
it("should add multiple functions to the pipeline", function () { | ||
var filter = function() {}; | ||
var filter2 = function() {}; | ||
|
||
let pipeline = new PipelineStore(); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.be.equal(undefined); | ||
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter); | ||
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter2); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(2); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST][0]).to.be.equal(filter); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST][1]).to.be.equal(filter2); | ||
}); | ||
|
||
it("should remove a function from the pipeline", function () { | ||
var filter = function() {}; | ||
|
||
let pipeline = new PipelineStore(); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.be.equal(undefined); | ||
|
||
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(1); | ||
|
||
pipeline.deregister(Hooks.PRE_AJAX_REQUEST, filter); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(0); | ||
}); | ||
|
||
it("should not error when removing the same function twice", function () { | ||
var filter = function() {}; | ||
|
||
let pipeline = new PipelineStore(); | ||
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter); | ||
|
||
pipeline.deregister(Hooks.PRE_AJAX_REQUEST, filter); | ||
pipeline.deregister(Hooks.PRE_AJAX_REQUEST, filter); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(0); | ||
}); | ||
|
||
it("should not add the same function twice", function () { | ||
var filter = function() {}; | ||
|
||
let pipeline = new PipelineStore(); | ||
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter); | ||
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(1); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST][0]).to.be.equal(filter); | ||
}); | ||
|
||
it("should call all functions of the chosen pipeline", function () { | ||
var filter_one = function(data) { | ||
data.value = data.value + "_filter_one"; | ||
}; | ||
var filter_two = function(data) { | ||
data.value = data.value + "_filter_two"; | ||
}; | ||
|
||
let pipeline = new PipelineStore(); | ||
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter_one); | ||
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter_two); | ||
let filtered_data = pipeline.applyFilter(Hooks.PRE_AJAX_REQUEST, {value: "initial"}); | ||
expect(filtered_data.value).to.be.equal("initial_filter_one_filter_two"); | ||
}); | ||
|
||
it("should be able to run an empty pipeline", function () { | ||
let pipeline = new PipelineStore(); | ||
let filtered_data = pipeline.applyFilter(Hooks.PRE_AJAX_REQUEST, {value: "initial"}); | ||
expect(filtered_data.value).to.be.equal("initial"); | ||
}); | ||
|
||
it ("should share the same filters between multiple pipeline instances", function (){ | ||
let filter = function (){}; | ||
|
||
let pipeline_one = new PipelineStore(); | ||
let pipeline_two = new PipelineStore(); | ||
pipeline_one.register(Hooks.PRE_AJAX_REQUEST, filter); | ||
pipeline_two.register(Hooks.PRE_AJAX_REQUEST, filter); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(1); | ||
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST][0]).to.be.equal(filter); | ||
}); | ||
|
||
}); |