Guest User

Untitled

a guest
Jun 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. public class EventRuleExamples
  2. {
  3. [Rule]
  4. public var expectEvent:EventRule = new EventRule();
  5.  
  6. [Rule]
  7. public var expectOrderedEvent:EventRule = new EventRule(/*ordered = */ true);
  8.  
  9. public var dispatcher:IEventDispatcher;
  10.  
  11. [Before]
  12. public function setup():void
  13. {
  14. dispatcher = new EventDispatcher();
  15. }
  16.  
  17. [Test]
  18. public function exampleExpectEvent():void
  19. {
  20. expectEvent.from(dispatcher).withType(Event.COMPLETE).once();
  21. }
  22.  
  23. [Test]
  24. public function exampleExpectEventWithProperties():void
  25. {
  26. expectEvent.from(dispatcher).withType(EntityEvent.CREATE)
  27. .instanceOf(EntityEvent)
  28. .hasProperties({ entity: instanceOf(Entity) })
  29. .once();
  30.  
  31. setTimeout(function():void {
  32. dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
  33. }, 50);
  34. }
  35.  
  36. [Test]
  37. public function exampleExpectEventWithTimeout():void
  38. {
  39. expectEvent.from(dispatcher).withType(EntityEvent.CREATE)
  40. .instanceOf(EntityEvent).once()
  41. .timeout(500, function():void {
  42. // do timeout assertions
  43. });
  44.  
  45. setTimeout(function():void {
  46. dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
  47. }, 100);
  48. }
  49.  
  50. [Test]
  51. public function exampleExpectEventWithProceed():void
  52. {
  53. expectEvent.from(dispatcher).withType(EntityEvent.CREATE)
  54. .instanceOf(EntityEvent).once()
  55. .proceedOnEvent();
  56.  
  57. setTimeout(function():void {
  58. dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
  59. }, 100);
  60. }
  61.  
  62. [Test]
  63. public function exampleExpectEventWithEventListener():void
  64. {
  65. expectEvent.from(dispatcher).withType(EntityEvent.CREATE).once()
  66. .calls(function(event:Event):void {
  67. // do event assertions
  68. });
  69.  
  70. setTimeout(function():void {
  71. dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
  72. }, 100);
  73. }
  74.  
  75. [Test]
  76. public function exampleExpectEventWithSuccess():void
  77. {
  78. expectEvent.from(dispatcher).withType(EntityEvent.CREATE).once()
  79. .onEventProceed()
  80. .onEventWait()
  81. .onEventCall();
  82.  
  83. .then.proceed()
  84. .then.wait(500)
  85. .then.calls(function():void {
  86. // do next step
  87. });
  88.  
  89. expectEvent.from(dispatcher).withType(EntityEvent.CREATE).once()
  90. .calls(expectEvent.proceed);
  91.  
  92. expectEvent.from(dispatcher).withType(EntityEvent.CREATE).once()
  93. .proceed();
  94.  
  95. setTimeout(function():void {
  96. dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
  97. }, 100);
  98.  
  99. }
  100.  
  101. [Test]
  102. public function exampleExpectEventWithWaits():void
  103. {
  104. expectEvent.from(dispatcher).withType(EntityEvent.CREATE).once()
  105. .waitFor(EntityEvent.CREATE_SUCESS));
  106.  
  107. expectEvent.from(dispatcher).withType(EntityEvent.CREATE_SUCESS).once()
  108. .proceed()
  109.  
  110. dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
  111. }
  112.  
  113. [Test]
  114. public function expectEventWithSequence():void
  115. {
  116. expectOrderedEvent.from(dispatcher).withType("create");
  117. expectOrderedEvent.from(dispatcher).withType("createSuccess");
  118. expectOrderedEvent.from(dispatcher).withType("destroy");
  119. expectOrderedEvent.from(dispatcher).withType("destroySuccess").proceed();
  120. }
  121.  
  122. [Test]
  123. public function expectsManyEvents():void
  124. {
  125. var collection:ArrayCollection = new ArrayCollection();
  126.  
  127. expectEvent.from(collection).withType(CollectionEvent.COLLECTION_CHANGE)
  128. .hasProperties({ kind: CollectionEventKind.ADD }).atLeast(3)
  129. .proceed();
  130.  
  131. collection.addItem({ id: 1 });
  132. collection.addItem({ id: 2 });
  133. collection.addItem({ id: 3 });
  134.  
  135. // what happens? success or failure?
  136. collection.addItem({ id: 4 });
  137. }
  138. }
Add Comment
Please, Sign In to add comment