RuleProviderSpec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import testRulesModule from './rules';
  6. import sayNoRulesModule from './say-no-rules';
  7. import priorityRulesModule from './priority-rules';
  8. import rulesModule from 'lib/features/rules';
  9. import modelingModule from 'lib/features/modeling';
  10. import RuleProvider from 'lib/features/rules/RuleProvider';
  11. import inherits from 'inherits';
  12. describe('features/rules - RuleProvider', function() {
  13. describe('basics', function() {
  14. beforeEach(bootstrapDiagram({ modules: [ rulesModule ] }));
  15. it('should work standalone', inject(function(injector, rules) {
  16. // assume
  17. expect(injector.get('commandStack', false)).not.to.exist;
  18. // when
  19. var allowed = rules.allowed('foo', {});
  20. // then
  21. expect(allowed).to.be.true;
  22. }));
  23. });
  24. describe('inheritance', function() {
  25. it('should allow sub-classing', function() {
  26. // given
  27. var CustomRules = function(eventBus) {
  28. RuleProvider.call(this, eventBus);
  29. };
  30. inherits(CustomRules, RuleProvider);
  31. // when
  32. var customRules = new CustomRules(null);
  33. // then
  34. expect(customRules).to.exist;
  35. });
  36. it('should initialize rule via RuleProvider#init', function() {
  37. // given
  38. var CustomRules = function(eventBus) {
  39. RuleProvider.call(this, eventBus);
  40. };
  41. inherits(CustomRules, RuleProvider);
  42. CustomRules.prototype.init = function() {
  43. this._initialized = true;
  44. };
  45. // when
  46. var customRules = new CustomRules(null);
  47. // then
  48. expect(customRules._initialized).to.be.true;
  49. });
  50. });
  51. describe('rule execution', function() {
  52. beforeEach(bootstrapDiagram({ modules: [ testRulesModule, modelingModule ] }));
  53. var resizableShape, nonResizableShape, ignoreResizeShape, unspecifiedShape, customResizableShape;
  54. beforeEach(inject(function(canvas, elementFactory, elementRegistry) {
  55. resizableShape = canvas.addShape(elementFactory.createShape({
  56. id: 's1',
  57. resizable: true, // checked by our rules
  58. x: 100, y: 100, width: 100, height: 100
  59. }));
  60. nonResizableShape = canvas.addShape(elementFactory.createShape({
  61. id: 's2',
  62. resizable: false, // checked by our rules
  63. x: 200, y: 100, width: 100, height: 100
  64. }));
  65. ignoreResizeShape = canvas.addShape(elementFactory.createShape({
  66. id: 's3',
  67. ignoreResize: true, // checked by our rules
  68. x: 300, y: 100, width: 100, height: 100
  69. }));
  70. unspecifiedShape = canvas.addShape(elementFactory.createShape({
  71. id: 's4',
  72. x: 400, y: 100, width: 100, height: 100
  73. }));
  74. customResizableShape = canvas.addShape(elementFactory.createShape({
  75. id: 's5',
  76. x: 400, y: 100, width: 100, height: 100,
  77. resizable: 'maybe'
  78. }));
  79. }));
  80. it('should correctly interpret allow', inject(function(rules) {
  81. // when
  82. var result = rules.allowed('shape.resize', { shape: resizableShape });
  83. // then
  84. expect(result).to.be.true;
  85. }));
  86. it('should correctly interpret reject', inject(function(rules) {
  87. // when
  88. var result = rules.allowed('shape.resize', { shape: nonResizableShape });
  89. // then
  90. expect(result).to.be.false;
  91. }));
  92. it('should correctly interpret ignore', inject(function(rules) {
  93. // when
  94. var result = rules.allowed('shape.resize', { shape: ignoreResizeShape });
  95. // then
  96. expect(result).to.be.null;
  97. }));
  98. it('should correctly interpret undefined', inject(function(rules) {
  99. // when
  100. var result = rules.allowed('shape.resize', { shape: unspecifiedShape });
  101. // then
  102. expect(result).to.be.true;
  103. }));
  104. it('should correctly interpret custom object', inject(function(rules) {
  105. // when
  106. var result = rules.allowed('shape.resize', { shape: customResizableShape });
  107. // then
  108. expect(result).to.eql('maybe');
  109. }));
  110. describe('unspecified handler', function() {
  111. it('should return false', inject(function(rules) {
  112. // when
  113. var result = rules.allowed('unregistered.action', { shape: resizableShape });
  114. // then
  115. expect(result).to.be.false;
  116. }));
  117. });
  118. });
  119. describe('rule module overrides', function() {
  120. beforeEach(bootstrapDiagram({ modules: [ sayNoRulesModule, testRulesModule, modelingModule ] }));
  121. var shape;
  122. beforeEach(inject(function(canvas) {
  123. shape = canvas.addShape({
  124. id: 's1',
  125. x: 100, y: 100, width: 100, height: 100
  126. });
  127. }));
  128. it('should reject in first initialized module', inject(function(rules) {
  129. // when
  130. var result = rules.allowed('shape.resize', { shape: shape });
  131. // then
  132. expect(result).to.be.false;
  133. }));
  134. });
  135. describe('rule priorities', function() {
  136. beforeEach(bootstrapDiagram({ modules: [ priorityRulesModule, modelingModule ] }));
  137. var normalShape, alwaysResizableShape, neverResizableShape;
  138. beforeEach(inject(function(canvas) {
  139. normalShape = canvas.addShape({
  140. id: 'shape',
  141. x: 100, y: 100, width: 100, height: 100,
  142. resizable: true
  143. });
  144. alwaysResizableShape = canvas.addShape({
  145. id: 'always-resizable',
  146. x: 100, y: 100, width: 100, height: 100
  147. });
  148. neverResizableShape = canvas.addShape({
  149. id: 'never-resizable',
  150. x: 100, y: 100, width: 100, height: 100
  151. });
  152. }));
  153. it('should pass through priority rule', inject(function(rules) {
  154. // when
  155. var result = rules.allowed('shape.resize', { shape: normalShape });
  156. // then
  157. expect(result).to.be.true;
  158. }));
  159. it('should allow in priority rule', inject(function(rules) {
  160. // when
  161. var result = rules.allowed('shape.resize', { shape: alwaysResizableShape });
  162. // then
  163. expect(result).to.be.true;
  164. }));
  165. it('should reject in priority rule', inject(function(rules) {
  166. // when
  167. var result = rules.allowed('shape.resize', { shape: neverResizableShape });
  168. // then
  169. expect(result).to.be.false;
  170. }));
  171. });
  172. });