forked from cdmbase/graphql-nats-subscriptions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
asynciterator still shows value instead of event
- Loading branch information
Showing
6 changed files
with
155 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { NatsPubSub } from '../nats-pubsub'; | ||
import { logger } from './logger'; | ||
import { isAsyncIterable } from 'iterall'; | ||
import 'jest'; | ||
const customLogger = logger.child({ className: '---------------async-iterator.test------------' }); | ||
describe('AsyncIterator', () => { | ||
it('should expose valid asyncIterator for a specific event', () => { | ||
const eventName = 'test1'; | ||
const ps = new NatsPubSub({ logger }); | ||
const iterator = ps.asyncIterator(eventName); | ||
expect(iterator).toBeDefined(); | ||
expect(isAsyncIterable(iterator)).toBeTruthy(); | ||
}); | ||
|
||
it('should trigger event on asyncIterator when published', done => { | ||
const eventName = 'test2'; | ||
const ps = new NatsPubSub({ logger }); | ||
const iterator = ps.asyncIterator(eventName); | ||
|
||
iterator.next().then(result => { | ||
customLogger.trace('result: (%j)', result); | ||
expect(result).toBeDefined(); | ||
expect(result.value).toBeDefined(); | ||
expect(result.done).toBeDefined(); | ||
done(); | ||
}); | ||
ps.publish(eventName, { test: true }); | ||
}); | ||
|
||
it('should not trigger event on asyncIterator when publishing other event', () => { | ||
const eventName = 'test3'; | ||
const ps = new NatsPubSub({ logger }); | ||
const iterator = ps.asyncIterator('test'); | ||
const spy = jest.fn(); | ||
iterator.next().then(spy); | ||
ps.publish(eventName, { test: true }); | ||
expect(spy).not.toBeCalled(); | ||
}); | ||
|
||
it('register to multiple events', done => { | ||
const eventName = 'test4'; | ||
const ps = new NatsPubSub({ logger }); | ||
const iterator = ps.asyncIterator(['test', 'test2']); | ||
const spy = jest.fn(); | ||
|
||
iterator.next().then(() => { | ||
spy(); | ||
expect(spy).toHaveBeenCalled(); | ||
done(); | ||
}); | ||
ps.publish(eventName, { test: true }); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { NatsPubSub } from '../nats-pubsub'; | ||
import { logger } from './logger'; | ||
|
||
|
||
describe('PubSub', function () { | ||
it('can subscribe and is called when events happen', function (done) { | ||
const ps = new NatsPubSub({ logger }); | ||
ps.subscribe('a', payload => { | ||
expect(payload).toBe('test'); | ||
done(); | ||
}).then(() => { | ||
const succeed = ps.publish('a', 'test'); | ||
expect(succeed).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
it('can unsubscribe', function (done) { | ||
const ps = new NatsPubSub({ logger }); | ||
ps.subscribe('a', payload => { | ||
expect(payload).toBe('test'); | ||
}).then(subId => { | ||
ps.unsubscribe(subId); | ||
const succeed = ps.publish('a', 'test'); | ||
expect(succeed).toBeTruthy(); // True because publish success is not | ||
// indicated by trigger having subscriptions | ||
done(); // works because pubsub is synchronous | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters