123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- import {
- bootstrapDiagram,
- inject
- } from 'test/TestHelper';
- import inherits from 'inherits';
- import cmdModule from 'lib/command';
- import CommandInterceptor from 'lib/command/CommandInterceptor';
- // example commands
- function TracableCommand() {
- this.execute = function(ctx) { };
- this.revert = function(ctx) { };
- }
- function SimpleCommand() {
- TracableCommand.call(this);
- }
- function ComplexCommand(commandStack) {
- TracableCommand.call(this);
- this.preExecute = function(ctx) {
- commandStack.execute('pre-command', { element: ctx.element });
- };
- this.postExecute = function(ctx) {
- commandStack.execute('post-command', { element: ctx.element });
- };
- }
- function PreCommand() {
- TracableCommand.call(this);
- }
- function PostCommand() {
- TracableCommand.call(this);
- }
- /**
- * A command interceptor used for testing
- */
- function TestInterceptor(eventBus) {
- CommandInterceptor.call(this, eventBus);
- }
- TestInterceptor.$inject = [ 'eventBus' ];
- inherits(TestInterceptor, CommandInterceptor);
- describe('command/CommandInterceptor', function() {
- beforeEach(bootstrapDiagram({ modules: [ cmdModule ] }));
- var element, context;
- beforeEach(inject(function(commandStack) {
- element = { trace: [] };
- context = {
- element: element
- };
- commandStack.registerHandler('simple-command', SimpleCommand);
- commandStack.registerHandler('complex-command', ComplexCommand);
- commandStack.registerHandler('pre-command', PreCommand);
- commandStack.registerHandler('post-command', PostCommand);
- }));
- function traceCommand(e) {
- expect(e).to.exist;
- e.context.element.trace.push(e.command);
- }
- function traceUnwrappedCommand(context, command, event) {
- expect(context).to.exist;
- expect(command).to.exist;
- expect(event).to.exist;
- context.element.trace.push(command);
- }
- describe('should hook into commands', function() {
- describe('#preExecute', function() {
- it('should register global', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.preExecute(traceCommand);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command',
- 'pre-command'
- ]);
- }));
- it('should register global / unwrap', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.preExecute(traceUnwrappedCommand, true);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command',
- 'pre-command'
- ]);
- }));
- it('should register scoped', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.preExecute('simple-command', traceCommand);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- it('should register scoped / unwrap', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.preExecute('simple-command', traceUnwrappedCommand, true);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- });
- describe('#preExecuted', function() {
- it('should register scoped', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.preExecuted('simple-command', traceCommand);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- });
- describe('#postExecute', function() {
- it('should register scoped', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.postExecute('simple-command', traceCommand);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- });
- describe('#postExecuted', function() {
- it('should register scoped', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.postExecuted('simple-command', traceCommand);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- });
- describe('#canExecute', function() {
- it('should register scoped', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.canExecute('simple-command', traceCommand);
- // when
- commandStack.canExecute('simple-command', context);
- commandStack.canExecute('pre-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- });
- describe('#execute', function() {
- it('should register scoped', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.execute('simple-command', traceCommand);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- });
- describe('#executed', function() {
- it('should register scoped', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.executed('simple-command', traceCommand);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- });
- describe('#revert', function() {
- it('should register scoped', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.executed('simple-command', traceCommand);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- commandStack.undo();
- commandStack.undo();
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- });
- describe('#reverted', function() {
- it('should register scoped', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.executed('simple-command', traceCommand);
- // when
- commandStack.execute('simple-command', context);
- commandStack.execute('pre-command', context);
- commandStack.undo();
- commandStack.undo();
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- });
- describe('#on', function() {
- it('should register global preExecute', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.on('preExecute', traceCommand);
- // when
- commandStack.execute('simple-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- it('should register global preExecute / unwrap', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.on('preExecute', traceUnwrappedCommand, true);
- // when
- commandStack.execute('simple-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command'
- ]);
- }));
- });
- });
- describe('priorities', function() {
- function trace(suffix) {
- return function(e) {
- e.context.element.trace.push(e.command + '-' + suffix);
- };
- }
- describe('via #preExecute', function() {
- it('should register globally', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.preExecute(500, trace('low'));
- interceptor.preExecute(trace('default'));
- interceptor.preExecute(1600, trace('high'));
- // when
- commandStack.execute('simple-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command-high',
- 'simple-command-default',
- 'simple-command-low'
- ]);
- }));
- it('should register locally', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.preExecute('simple-command', 500, trace('low'));
- interceptor.preExecute('simple-command', trace('default'));
- interceptor.preExecute('simple-command', 1600, trace('high'));
- // when
- commandStack.execute('simple-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command-high',
- 'simple-command-default',
- 'simple-command-low'
- ]);
- }));
- });
- describe('via #on', function() {
- it('should register', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.on('preExecute', 500, trace('global-low'));
- interceptor.on([ 'simple-command.preExecute' ], trace('local-default'));
- interceptor.on('simple-command', 'preExecute', 1600, trace('local-high'));
- // when
- commandStack.execute('simple-command', context);
- // then
- expect(element.trace).to.eql([
- 'simple-command-local-high',
- 'simple-command-local-default',
- 'simple-command-global-low'
- ]);
- }));
- });
- describe('mixed', function() {
- it('should register mixed', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- interceptor.on([ 'simple-command.preExecute', 'preExecute'], 500, trace('raw-multiple-low'));
- interceptor.preExecute([ 'simple-command' ], 500, trace('local-multiple-low'));
- interceptor.preExecute(500, trace('global-low'));
- interceptor.preExecute('simple-command', trace('default'));
- interceptor.preExecute(1500, trace('global-high'));
- interceptor.preExecute('simple-command', 1600, trace('local-high'));
- // when
- commandStack.execute('simple-command', context);
- // then
- expect(element.trace).to.eql([
- // local listeners are invoked first
- 'simple-command-local-high',
- 'simple-command-default',
- 'simple-command-raw-multiple-low',
- 'simple-command-local-multiple-low',
- // global listeners are invoked after local ones
- 'simple-command-global-high',
- 'simple-command-raw-multiple-low',
- 'simple-command-global-low'
- ]);
- }));
- });
- });
- describe('context', function() {
- function Dog() {
- this.barks = [];
- }
- Dog.prototype.bark = function() {
- this.barks.push('WOOF WOOF');
- };
- beforeEach(inject(function(commandStack) {
- commandStack.registerHandler('bark', ComplexCommand);
- }));
- it('should pass context -> WITHOUT unwrap', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- // given
- Dog.prototype.bindListener = function() {
- interceptor.execute('bark', function(event) {
- return this.bark();
- }, this);
- };
- var bobby = new Dog();
- bobby.bindListener();
- // when
- commandStack.execute('bark', context);
- // then
- expect(bobby.barks).to.have.length(1);
- }));
- it('should pass context -> WITH unwrap', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- // given
- Dog.prototype.bindListener = function() {
- interceptor.execute('bark', function(event) {
- return this.bark();
- }, true, this);
- };
- var bobby = new Dog();
- bobby.bindListener();
- // when
- commandStack.execute('bark', context);
- // then
- expect(bobby.barks).to.have.length(1);
- }));
- it('should pass context -> WITH everything', inject(function(commandStack, eventBus) {
- var interceptor = new TestInterceptor(eventBus);
- // given
- Dog.prototype.bindListener = function() {
- interceptor.execute('bark', 1000, function(event) {
- return this.bark();
- }, false, this);
- };
- var bobby = new Dog();
- bobby.bindListener();
- // when
- commandStack.execute('bark', context);
- // then
- expect(bobby.barks).to.have.length(1);
- }));
- });
- });
|