Stubs out the Pusher
object with a fake object that can be manipulated for
testing purposes.
This does NOT work with PusherJS 3.1 yet. We're working on it.
Install Pusher and the test stub.
$ bower install pusher
$ bower install pusher-test-stub
When in test mode inject the test stub and execute tests.
<html>
<body>
<script src="./bower_components/pusher/dist/pusher.min.js"></script>
<!-- ONLY include in test mode -->
<script src="./bower_components/pusher-test-stub/dist/pusher-test-stub.js"></script>
<script>
var MyApp = {
messages: [],
init: function() {
var pusher = new Pusher('key');
var channel = pusher.subscribe('messages');
channel.bind('new-message', function(message) {
this.messages.push(message);
}.bind(this));
}
};
MyApp.init();
</script>
<script>
// Define your tests - probably in a separate file that injected in test mode
describe('Message Collection', function() {
it('should be updated with a new message when new-message is triggered', function() {
var channel = Pusher.singleton.channel('messages');
channel.emit('new-message', { text: 'test stub message'});
expect(MyApp.messages.length).to.be(1);
expect(MyApp.messages[0].text).to.be('test stub message');
});
});
</script>
</body>
</html>
bower install pusher-test-stub
Include the dist/pusher-test-stub.js
file after pusher.js
script.
<script src="//js.pusher.com/3.0/pusher.min.js"></script>
<script src="bower_components/dist/pusher-test-stub.js"></script>
The Pusher
definition will then be overwritten by the PusherTestStub
definition.
The public API offers everything that pusher-js supports.
The EventsDispacher
is used by Pusher
, Connection
, Channel
,
PrivateChannel
and PresenceChannel
(all channel types). The functions on
include:
obj.bind( eventName, callback )
- bind to all events with an event name:obj.unbind( eventName[, callback] )
- Unbind to all events with a given nameobj.bind_all( callback )
- Bind to all absolutely all events. I'd be interested if anybody uses thisobj.unbind_all( callback )
- Unbind the all callback handler. I'd be interested if anybody uses this
Static variables:
Pusher.instances
- Array of instances ofPusher
objects instances
Constructor:
new Pusher( key[, options] )
- Constructor
Functions:
pusher.subscribe( channelName )
pusher.unsubscribe( channelName )
pusher.channel( channelName )
- get a channel by namepusher.allChannels()
pusher.connect()
pusher.disconnect()
Ones I'm hoping we can ignore:
pusher.bind( eventName, callback )
- Bind to all events with an event name:pusher.bind_all( callback )
- Bind to all absolutely all events
Properties:
pusher.key
- The current app key:
Inherits from EventsDispacher
.
connection.state
- Connection state
The basic channel type which inherits from EventsDispacher
. It doesn't offer any other functions other than those exposed by the object it inherits from.
However, there are a few properties:
channel.subscribed
- Subscription statechannel.name
- Channel name
Inherits from the public channel and as such inherits EventsDispacher
.
channel.trigger( eventName, data )
- the ability to trigger client events on the channel.
Inherits from the private channel and as such inherits EventsDispacher
and the private channel properties. Also offers access to some presence state variables.
channel.members
- Members on the channel. An instance ofPusher.Members
Functions:
members.get( memberId )
- get a member with a given IDmembers.each( callback )
- used to iterate members
Properties:
members.count
- the number of members on the channelmembers.me
- a reference to the current user on the channel
Not represented as an object anywhere, but the structure is:
member.id
- a unique user idmember.info
- application specific user information
Reference the last instance of Pusher
that was created. Since you generally only
want to create a single Pusher
instance this can come in handy.
Trigger an event on the given channel.
Pusher.singleton.trigger('channel', 'my-event', {});
$ npm install
The build process uses gulp and webpack..
$ gulp build
A new build will be created in dist/pusher-test-stub.js
.
Tests are defined in test/acceptance.js
and run via test/runner.html
.
Note: Pusher
assumes it's running in a browser and has a hard dependency
on it at the moment. So the Pusher Test Stub is also restricted by that
dependency
To run the tests:
$ gulp serve
Navigate to http://localhost:8000/test/runner.html
See CHANGELOG.md.