123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- import {
- bootstrapDiagram,
- inject
- } from 'test/TestHelper';
- import testRulesModule from './rules';
- import sayNoRulesModule from './say-no-rules';
- import priorityRulesModule from './priority-rules';
- import rulesModule from 'lib/features/rules';
- import modelingModule from 'lib/features/modeling';
- import RuleProvider from 'lib/features/rules/RuleProvider';
- import inherits from 'inherits';
- describe('features/rules - RuleProvider', function() {
- describe('basics', function() {
- beforeEach(bootstrapDiagram({ modules: [ rulesModule ] }));
- it('should work standalone', inject(function(injector, rules) {
- // assume
- expect(injector.get('commandStack', false)).not.to.exist;
- // when
- var allowed = rules.allowed('foo', {});
- // then
- expect(allowed).to.be.true;
- }));
- });
- describe('inheritance', function() {
- it('should allow sub-classing', function() {
- // given
- var CustomRules = function(eventBus) {
- RuleProvider.call(this, eventBus);
- };
- inherits(CustomRules, RuleProvider);
- // when
- var customRules = new CustomRules(null);
- // then
- expect(customRules).to.exist;
- });
- it('should initialize rule via RuleProvider#init', function() {
- // given
- var CustomRules = function(eventBus) {
- RuleProvider.call(this, eventBus);
- };
- inherits(CustomRules, RuleProvider);
- CustomRules.prototype.init = function() {
- this._initialized = true;
- };
- // when
- var customRules = new CustomRules(null);
- // then
- expect(customRules._initialized).to.be.true;
- });
- });
- describe('rule execution', function() {
- beforeEach(bootstrapDiagram({ modules: [ testRulesModule, modelingModule ] }));
- var resizableShape, nonResizableShape, ignoreResizeShape, unspecifiedShape, customResizableShape;
- beforeEach(inject(function(canvas, elementFactory, elementRegistry) {
- resizableShape = canvas.addShape(elementFactory.createShape({
- id: 's1',
- resizable: true, // checked by our rules
- x: 100, y: 100, width: 100, height: 100
- }));
- nonResizableShape = canvas.addShape(elementFactory.createShape({
- id: 's2',
- resizable: false, // checked by our rules
- x: 200, y: 100, width: 100, height: 100
- }));
- ignoreResizeShape = canvas.addShape(elementFactory.createShape({
- id: 's3',
- ignoreResize: true, // checked by our rules
- x: 300, y: 100, width: 100, height: 100
- }));
- unspecifiedShape = canvas.addShape(elementFactory.createShape({
- id: 's4',
- x: 400, y: 100, width: 100, height: 100
- }));
- customResizableShape = canvas.addShape(elementFactory.createShape({
- id: 's5',
- x: 400, y: 100, width: 100, height: 100,
- resizable: 'maybe'
- }));
- }));
- it('should correctly interpret allow', inject(function(rules) {
- // when
- var result = rules.allowed('shape.resize', { shape: resizableShape });
- // then
- expect(result).to.be.true;
- }));
- it('should correctly interpret reject', inject(function(rules) {
- // when
- var result = rules.allowed('shape.resize', { shape: nonResizableShape });
- // then
- expect(result).to.be.false;
- }));
- it('should correctly interpret ignore', inject(function(rules) {
- // when
- var result = rules.allowed('shape.resize', { shape: ignoreResizeShape });
- // then
- expect(result).to.be.null;
- }));
- it('should correctly interpret undefined', inject(function(rules) {
- // when
- var result = rules.allowed('shape.resize', { shape: unspecifiedShape });
- // then
- expect(result).to.be.true;
- }));
- it('should correctly interpret custom object', inject(function(rules) {
- // when
- var result = rules.allowed('shape.resize', { shape: customResizableShape });
- // then
- expect(result).to.eql('maybe');
- }));
- describe('unspecified handler', function() {
- it('should return false', inject(function(rules) {
- // when
- var result = rules.allowed('unregistered.action', { shape: resizableShape });
- // then
- expect(result).to.be.false;
- }));
- });
- });
- describe('rule module overrides', function() {
- beforeEach(bootstrapDiagram({ modules: [ sayNoRulesModule, testRulesModule, modelingModule ] }));
- var shape;
- beforeEach(inject(function(canvas) {
- shape = canvas.addShape({
- id: 's1',
- x: 100, y: 100, width: 100, height: 100
- });
- }));
- it('should reject in first initialized module', inject(function(rules) {
- // when
- var result = rules.allowed('shape.resize', { shape: shape });
- // then
- expect(result).to.be.false;
- }));
- });
- describe('rule priorities', function() {
- beforeEach(bootstrapDiagram({ modules: [ priorityRulesModule, modelingModule ] }));
- var normalShape, alwaysResizableShape, neverResizableShape;
- beforeEach(inject(function(canvas) {
- normalShape = canvas.addShape({
- id: 'shape',
- x: 100, y: 100, width: 100, height: 100,
- resizable: true
- });
- alwaysResizableShape = canvas.addShape({
- id: 'always-resizable',
- x: 100, y: 100, width: 100, height: 100
- });
- neverResizableShape = canvas.addShape({
- id: 'never-resizable',
- x: 100, y: 100, width: 100, height: 100
- });
- }));
- it('should pass through priority rule', inject(function(rules) {
- // when
- var result = rules.allowed('shape.resize', { shape: normalShape });
- // then
- expect(result).to.be.true;
- }));
- it('should allow in priority rule', inject(function(rules) {
- // when
- var result = rules.allowed('shape.resize', { shape: alwaysResizableShape });
- // then
- expect(result).to.be.true;
- }));
- it('should reject in priority rule', inject(function(rules) {
- // when
- var result = rules.allowed('shape.resize', { shape: neverResizableShape });
- // then
- expect(result).to.be.false;
- }));
- });
- });
|