Skip to content

Commit

Permalink
feat(context-pad): allow to remove and update entries
Browse files Browse the repository at this point in the history
  • Loading branch information
barmac authored and fake-join[bot] committed Nov 11, 2019
1 parent c189016 commit ffa0c6b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
12 changes: 8 additions & 4 deletions lib/features/context-pad/ContextPad.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,15 @@ ContextPad.prototype.getEntries = function(element) {
// loop through all providers and their entries.
// group entries by id so that overriding an entry is possible
forEach(this._providers, function(provider) {
var e = provider.getContextPadEntries(element);
var entriesOrUpdater = provider.getContextPadEntries(element);

forEach(e, function(entry, id) {
entries[id] = entry;
});
if (isFunction(entriesOrUpdater)) {
entries = entriesOrUpdater(entries);
} else {
forEach(entriesOrUpdater, function(entry, id) {
entries[id] = entry;
});
}
});

return entries;
Expand Down
74 changes: 72 additions & 2 deletions test/spec/features/context-pad/ContextPadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
inject
} from 'test/TestHelper';

import { assign } from 'min-dash';

import {
query as domQuery,
queryAll as domQueryAll,
Expand Down Expand Up @@ -55,9 +57,9 @@ describe('features/context-pad', function() {
beforeEach(bootstrapDiagram({ modules: [ contextPadModule, initPadModule ] }));


function Provider(entries) {
function Provider(entriesOrUpdater) {
this.getContextPadEntries = function(element) {
return entries || {};
return entriesOrUpdater || {};
};
}

Expand Down Expand Up @@ -95,6 +97,74 @@ describe('features/context-pad', function() {
}));


describe('with updater', function() {

it('should allow to add entries', inject(function(contextPad) {

// given
function updater(entries) {
return assign(entries, { entryB: {} });
}

var plainProvider = new Provider({ entryA: { action: function() {} } }),
updatingProvider = new Provider(updater);

contextPad.registerProvider(plainProvider);
contextPad.registerProvider(updatingProvider);

// when
var entries = contextPad.getEntries();

// then
expect(entries.entryA).to.exist;
expect(entries.entryB).to.exist;
}));


it('should allow to update entries', inject(function(contextPad) {

// given
function updater(entries) {
return assign(entries, { entryA: { alt: 'text' } });
}

var plainProvider = new Provider({ entryA: { action: function() {} } }),
updatingProvider = new Provider(updater);

contextPad.registerProvider(plainProvider);
contextPad.registerProvider(updatingProvider);

// when
var entries = contextPad.getEntries();

// then
expect(entries.entryA).to.exist;
expect(entries.entryA).to.have.property('alt');
}));


it('should allow to remove entries', inject(function(contextPad) {

// given
function updater(entries) {
return {};
}

var plainProvider = new Provider({ entryA: { action: function() {} } }),
updatingProvider = new Provider(updater);

contextPad.registerProvider(plainProvider);
contextPad.registerProvider(updatingProvider);

// when
var entries = contextPad.getEntries();

// then
expect(entries.entryA).to.not.exist;
}));
});


describe('entry className', function() {

function testClassName(options) {
Expand Down

0 comments on commit ffa0c6b

Please sign in to comment.