11import { PubSub } from "./pubsub.js" ;
22import { createInjector } from "../../core/di/injector.js" ;
33import { Angular } from "../../angular.js" ;
4+ import { wait } from "../../shared/utils.js" ;
45
56describe ( "PubSubProvider" , ( ) => {
67 it ( "should be injectable" , ( ) => {
@@ -9,7 +10,6 @@ describe("PubSubProvider", () => {
910 const $injector = createInjector ( [ "test" ] ) ;
1011 expect ( $injector . has ( "$eventBus" ) ) . toBeTrue ( ) ;
1112 expect ( $injector . get ( "$eventBus" ) instanceof PubSub ) . toBeTrue ( ) ;
12- expect ( $injector . get ( "$eventBus" ) . async_ ) . toBeTrue ( ) ;
1313 } ) ;
1414} ) ;
1515
@@ -117,18 +117,20 @@ describe("PubSub", function () {
117117 expect ( pubsub . getCount ( "X" ) ) . toBe ( 0 ) ;
118118 } ) ;
119119
120- it ( "should subscribe once correctly" , function ( ) {
120+ it ( "should subscribe once correctly" , async function ( ) {
121121 let called ;
122122 let context ;
123123
124124 called = false ;
125125 pubsub . subscribeOnce ( "someTopic" , ( ) => {
126126 called = true ;
127127 } ) ;
128+ await wait ( ) ;
128129 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 1 ) ;
129130 expect ( called ) . toBe ( false ) ;
130131
131132 pubsub . publish ( "someTopic" ) ;
133+ await wait ( ) ;
132134 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 0 ) ;
133135 expect ( called ) . toBe ( true ) ;
134136
@@ -140,10 +142,12 @@ describe("PubSub", function () {
140142 } ,
141143 context ,
142144 ) ;
145+ await wait ( ) ;
143146 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 1 ) ;
144147 expect ( context . called ) . toBe ( false ) ;
145148
146149 pubsub . publish ( "someTopic" ) ;
150+ await wait ( ) ;
147151 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 0 ) ;
148152 expect ( context . called ) . toBe ( true ) ;
149153
@@ -156,11 +160,13 @@ describe("PubSub", function () {
156160 } ,
157161 context ,
158162 ) ;
163+ await wait ( ) ;
159164 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 1 ) ;
160165 expect ( context . called ) . toBe ( false ) ;
161166 expect ( context . value ) . toBe ( 0 ) ;
162167
163168 pubsub . publish ( "someTopic" , 17 ) ;
169+ await wait ( ) ;
164170 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 0 ) ;
165171 expect ( context . called ) . toBe ( true ) ;
166172 expect ( context . value ) . toBe ( 17 ) ;
@@ -227,7 +233,7 @@ describe("PubSub", function () {
227233 } , 0 ) ;
228234 } ) ;
229235
230- it ( "should subscribe once with bound function correctly" , function ( ) {
236+ it ( "should subscribe once with bound function correctly" , async function ( ) {
231237 const context = { called : false , value : 0 } ;
232238
233239 function subscriber ( value ) {
@@ -236,17 +242,21 @@ describe("PubSub", function () {
236242 }
237243
238244 pubsub . subscribeOnce ( "someTopic" , subscriber . bind ( context ) ) ;
245+ await wait ( ) ;
246+
239247 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 1 ) ;
240248 expect ( context . called ) . toBe ( false ) ;
241249 expect ( context . value ) . toBe ( 0 ) ;
242250
243251 pubsub . publish ( "someTopic" , 17 ) ;
252+ await wait ( ) ;
253+
244254 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 0 ) ;
245255 expect ( context . called ) . toBe ( true ) ;
246256 expect ( context . value ) . toBe ( 17 ) ;
247257 } ) ;
248258
249- it ( "should subscribe once with partial function correctly" , function ( ) {
259+ it ( "should subscribe once with partial function correctly" , async function ( ) {
250260 let called = false ;
251261 let value = 0 ;
252262
@@ -256,17 +266,21 @@ describe("PubSub", function () {
256266 }
257267
258268 pubsub . subscribeOnce ( "someTopic" , subscriber . bind ( null , true ) ) ;
269+ await wait ( ) ;
270+
259271 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 1 ) ;
260272 expect ( called ) . toBe ( false ) ;
261273 expect ( value ) . toBe ( 0 ) ;
262274
263275 pubsub . publish ( "someTopic" , 17 ) ;
276+ await wait ( ) ;
277+
264278 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 0 ) ;
265279 expect ( called ) . toBe ( true ) ;
266280 expect ( value ) . toBe ( 17 ) ;
267281 } ) ;
268282
269- it ( "should handle self resubscribe correctly" , function ( ) {
283+ it ( "should handle self resubscribe correctly" , async function ( ) {
270284 let value = null ;
271285
272286 function resubscribe ( iteration , newValue ) {
@@ -275,18 +289,26 @@ describe("PubSub", function () {
275289 }
276290
277291 pubsub . subscribeOnce ( "someTopic" , resubscribe . bind ( null , 0 ) ) ;
292+ await wait ( ) ;
293+
278294 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 1 ) ;
279295 expect ( value ) . toBeNull ( ) ;
280296
281297 pubsub . publish ( "someTopic" , "foo" ) ;
298+ await wait ( ) ;
299+
282300 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 1 ) ;
283301 expect ( value ) . toBe ( "foo:0" ) ;
284302
285303 pubsub . publish ( "someTopic" , "bar" ) ;
304+ await wait ( ) ;
305+
286306 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 1 ) ;
287307 expect ( value ) . toBe ( "bar:1" ) ;
288308
289309 pubsub . publish ( "someTopic" , "baz" ) ;
310+ await wait ( ) ;
311+
290312 expect ( pubsub . getCount ( "someTopic" ) ) . toBe ( 1 ) ;
291313 expect ( value ) . toBe ( "baz:2" ) ;
292314 } ) ;
@@ -352,50 +374,57 @@ describe("PubSub", function () {
352374 expect ( record . y ) . toBe ( "y" ) ;
353375 }
354376
355- it ( "should call subscribed functions on publish" , function ( ) {
377+ it ( "should call subscribed functions on publish" , async function ( ) {
356378 pubsub . subscribe ( SOME_TOPIC , foo ) ;
357379 pubsub . subscribe ( SOME_TOPIC , bar , context ) ;
358380
359381 expect ( pubsub . publish ( SOME_TOPIC , { x : "x" , y : "y" } ) ) . toBe ( true ) ;
382+ await wait ( ) ;
360383 expect ( fooCalled ) . toBe ( true , "foo() must have been called" ) ;
361384 expect ( barCalled ) . toBe ( true , "bar() must have been called" ) ;
362385 } ) ;
363386
364- it ( "should not call unsubscribed functions on publish" , function ( ) {
387+ it ( "should not call unsubscribed functions on publish" , async function ( ) {
365388 pubsub . subscribe ( SOME_TOPIC , foo ) ;
366389 pubsub . subscribe ( SOME_TOPIC , bar , context ) ;
367390
368391 pubsub . publish ( SOME_TOPIC , { x : "x" , y : "y" } ) ;
392+ await wait ( ) ;
393+ expect ( fooCalled ) . toBe ( true , "foo() must have been called" ) ;
394+ expect ( barCalled ) . toBe ( true , "bar() must have been called" ) ;
369395 fooCalled = false ;
370396 barCalled = false ;
371397 expect ( pubsub . unsubscribe ( SOME_TOPIC , foo ) ) . toBe ( true ) ;
372-
373398 expect ( pubsub . publish ( SOME_TOPIC , { x : "x" , y : "y" } ) ) . toBe ( true ) ;
399+
400+ await wait ( ) ;
374401 expect ( fooCalled ) . toBe ( false , "foo() must not have been called" ) ;
375402 expect ( barCalled ) . toBe ( true , "bar() must have been called" ) ;
376403 } ) ;
377404
378- it ( "should only call functions subscribed to the correct topic" , function ( ) {
405+ it ( "should only call functions subscribed to the correct topic" , async function ( ) {
379406 pubsub . subscribe ( SOME_TOPIC , bar , context ) ;
380407 pubsub . subscribe ( "differentTopic" , foo ) ;
381408
382409 pubsub . publish ( SOME_TOPIC , { x : "x" , y : "y" } ) ;
383410 fooCalled = false ;
384411 barCalled = false ;
385412
413+ await wait ( ) ;
386414 expect ( pubsub . publish ( SOME_TOPIC , { x : "x" , y : "y" } ) ) . toBe ( true ) ;
387415 expect ( fooCalled ) . toBe ( false , "foo() must not have been called" ) ;
388416 expect ( barCalled ) . toBe ( true , "bar() must have been called" ) ;
389417 } ) ;
390418
391- it ( "should trigger functions if not arguments are provided" , function ( ) {
419+ it ( "should trigger functions if not arguments are provided" , async function ( ) {
392420 let called = false ;
393421 pubsub . subscribe ( SOME_TOPIC , ( ) => {
394422 called = true ;
395423 0 ;
396424 } ) ;
397425
398426 pubsub . publish ( SOME_TOPIC ) ;
427+ await wait ( ) ;
399428
400429 expect ( pubsub . publish ( SOME_TOPIC ) ) . toBe ( true ) ;
401430 expect ( called ) . toBeTrue ( ) ;
0 commit comments