├── .gitignore ├── CHANGELOG.md ├── README.md ├── bower.json ├── dist └── pusher-test-stub.js ├── examples ├── common │ └── styles.css ├── qunit │ └── index.html └── selenium │ ├── chat-message-added-test.test │ └── index.html ├── gulpfile.js ├── package.json ├── src ├── Channel.js ├── Connection.js ├── PresenceChannel.js ├── PrivateChannel.js ├── PusherDependencies.js └── PusherTestStub.js └── test ├── acceptance.js └── runner.html /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | node_modules 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | ## 2.0.0 4 | 5 | * **REMOVED** `dispatch` no longer available. Please use `emit` 6 | * **REMOVED** `Pusher.emit` has been removed. Please use `Pusher.singleton.trigger` 7 | * **ADDED** `Pusher.singleton` access last created `Pusher` instance 8 | * **ADDED** `Pusher.singleton.trigger(channelName, eventName, eventData)` to trigger 9 | and event with payload on a given channel 10 | 11 | ## 1.0.0 12 | 13 | Please see the [Pusher Test Stub blog post](http://blog.pusher.com/2011/9/2/testing-your-integration-with-the-pusher-javascript-library) for more information. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pusher Test Stub 2 | 3 | Stubs out the `Pusher` object with a fake object that can be manipulated for 4 | testing purposes. 5 | 6 | __This does NOT work with PusherJS 3.1 yet. We're working on it.__ 7 | 8 | ## Quick Start 9 | 10 | Install Pusher and the test stub. 11 | 12 | ``` 13 | $ bower install pusher 14 | $ bower install pusher-test-stub 15 | ``` 16 | 17 | When in test mode inject the test stub and execute tests. 18 | 19 | ```html 20 | 21 | 22 | 23 | 24 | 25 | 39 | 51 | 52 | 53 | ``` 54 | 55 | ## Installation 56 | 57 | ``` 58 | bower install pusher-test-stub 59 | ``` 60 | 61 | Include the `dist/pusher-test-stub.js` file after `pusher.js` script. 62 | 63 | ```html 64 | 65 | 66 | ``` 67 | 68 | The `Pusher` definition will then be overwritten by the `PusherTestStub` 69 | definition. 70 | 71 | ## Supported API 72 | 73 | The public API offers everything that pusher-js supports. 74 | 75 | ### EventsDispacher 76 | 77 | The `EventsDispacher` is used by `Pusher`, `Connection`, `Channel`, 78 | `PrivateChannel` and `PresenceChannel` (all channel types). The functions on 79 | include: 80 | 81 | * `obj.bind( eventName, callback )` - bind to all events with an event name: 82 | * `obj.unbind( eventName[, callback] )` - Unbind to all events with a given name 83 | * `obj.bind_all( callback )` - Bind to all absolutely all events. *I'd be interested if anybody uses this* 84 | * `obj.unbind_all( callback )` - Unbind the all callback handler. *I'd be interested if anybody uses this* 85 | 86 | ### Pusher 87 | 88 | Static variables: 89 | 90 | * `Pusher.instances` - Array of instances of `Pusher` objects instances 91 | 92 | Constructor: 93 | 94 | * `new Pusher( key[, options] )` - Constructor 95 | 96 | Functions: 97 | 98 | * `pusher.subscribe( channelName )` 99 | * `pusher.unsubscribe( channelName )` 100 | * `pusher.channel( channelName )` - get a channel by name 101 | * `pusher.allChannels()` 102 | * `pusher.connect()` 103 | * `pusher.disconnect()` 104 | 105 | Ones I'm hoping we can ignore: 106 | 107 | * `pusher.bind( eventName, callback )` - Bind to all events with an event name: 108 | * `pusher.bind_all( callback )` - Bind to all absolutely all events 109 | 110 | Properties: 111 | 112 | * `pusher.key` - The current app key: 113 | 114 | ### Pusher.Connection 115 | 116 | Inherits from `EventsDispacher`. 117 | 118 | * `connection.state` - Connection state 119 | 120 | ### Public Channel 121 | 122 | 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. 123 | 124 | However, there are a few properties: 125 | 126 | * `channel.subscribed` - Subscription state 127 | * `channel.name` - Channel name 128 | 129 | ### Private Channel 130 | 131 | Inherits from the public channel and as such inherits `EventsDispacher`. 132 | 133 | * `channel.trigger( eventName, data )` - the ability to trigger client events on the channel. 134 | 135 | ### Presence Channel 136 | 137 | Inherits from the private channel and as such inherits `EventsDispacher` and the private channel properties. Also offers access to some presence state variables. 138 | 139 | * `channel.members` - Members on the channel. An instance of `Pusher.Members` 140 | 141 | ### Members 142 | 143 | Functions: 144 | 145 | * `members.get( memberId )` - get a member with a given ID 146 | * `members.each( callback )` - used to iterate members 147 | 148 | Properties: 149 | 150 | * `members.count` - the number of members on the channel 151 | * `members.me` - a reference to the current user on the channel 152 | 153 | ### Member 154 | 155 | Not represented as an object anywhere, but the structure is: 156 | 157 | * `member.id` - a unique user id 158 | * `member.info` - application specific user information 159 | 160 | ## Helpers 161 | 162 | ### `Pusher.singleton` 163 | 164 | Reference the last instance of `Pusher` that was created. Since you generally only 165 | want to create a single `Pusher` instance this can come in handy. 166 | 167 | ### `Pusher.singleton.trigger(channelName, eventName, eventData)` 168 | 169 | Trigger an event on the given channel. 170 | 171 | ```js 172 | Pusher.singleton.trigger('channel', 'my-event', {}); 173 | ``` 174 | 175 | ## Development 176 | 177 | ``` 178 | $ npm install 179 | ``` 180 | 181 | ### Build 182 | 183 | The build process uses [gulp](http://gulpjs.com/) and 184 | [webpack](http://webpack.github.io/).. 185 | 186 | ``` 187 | $ gulp build 188 | ``` 189 | 190 | A new build will be created in `dist/pusher-test-stub.js`. 191 | 192 | ### Tests 193 | 194 | Tests are defined in `test/acceptance.js` and run via `test/runner.html`. 195 | 196 | *Note: `Pusher` assumes it's running in a browser and has a hard dependency 197 | on it at the moment. So the Pusher Test Stub is also restricted by that 198 | dependency* 199 | 200 | To run the tests: 201 | 202 | ``` 203 | $ gulp serve 204 | ``` 205 | 206 | Navigate to http://localhost:8000/test/runner.html 207 | 208 | ## CHANGELOG 209 | 210 | See [CHANGELOG.md](CHANGELOG.md). 211 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pusher-test-stub", 3 | "version": "2.0.1", 4 | "homepage": "https://github.com/leggetter/pusher-test-stub", 5 | "authors": [ 6 | "leggetter " 7 | ], 8 | "description": "Overrides the Pusher JavaScript library allowing functionality to be controlled and thus your applications integration with the Pusher library tested.", 9 | "main": "dist/pusher-test-stub.js", 10 | "keywords": [ 11 | "pusher", 12 | "test" 13 | ], 14 | "license": "MIT", 15 | "ignore": [ 16 | "**/.*", 17 | "src", 18 | "gulpfile.js", 19 | "package.json", 20 | "node_modules", 21 | "bower_components", 22 | "test", 23 | "tests" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /dist/pusher-test-stub.js: -------------------------------------------------------------------------------- 1 | var PusherTestStub = 2 | /******/ (function(modules) { // webpackBootstrap 3 | /******/ // The module cache 4 | /******/ var installedModules = {}; 5 | 6 | /******/ // The require function 7 | /******/ function __webpack_require__(moduleId) { 8 | 9 | /******/ // Check if module is in cache 10 | /******/ if(installedModules[moduleId]) 11 | /******/ return installedModules[moduleId].exports; 12 | 13 | /******/ // Create a new module (and put it into the cache) 14 | /******/ var module = installedModules[moduleId] = { 15 | /******/ exports: {}, 16 | /******/ id: moduleId, 17 | /******/ loaded: false 18 | /******/ }; 19 | 20 | /******/ // Execute the module function 21 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); 22 | 23 | /******/ // Flag the module as loaded 24 | /******/ module.loaded = true; 25 | 26 | /******/ // Return the exports of the module 27 | /******/ return module.exports; 28 | /******/ } 29 | 30 | 31 | /******/ // expose the modules object (__webpack_modules__) 32 | /******/ __webpack_require__.m = modules; 33 | 34 | /******/ // expose the module cache 35 | /******/ __webpack_require__.c = installedModules; 36 | 37 | /******/ // __webpack_public_path__ 38 | /******/ __webpack_require__.p = ""; 39 | 40 | /******/ // Load entry module and return exports 41 | /******/ return __webpack_require__(0); 42 | /******/ }) 43 | /************************************************************************/ 44 | /******/ ([ 45 | /* 0 */ 46 | /***/ function(module, exports, __webpack_require__) { 47 | 48 | var deps = __webpack_require__(1); 49 | var Connection = __webpack_require__(2); 50 | var Channel = __webpack_require__(3); 51 | var PrivateChannel = __webpack_require__(4); 52 | var PresenceChannel = __webpack_require__(5); 53 | 54 | /** 55 | * Create a new instance of the test stub. 56 | * 57 | * @param {String} key the Pusher application key 58 | * @param {Object} options additional config options 59 | */ 60 | function PusherTestStub(key, options) { 61 | this.key = key; 62 | this.connection = new Connection(); 63 | 64 | this._channels = {}; 65 | 66 | PusherTestStub.singleton = this; 67 | PusherTestStub.instances.push(this); 68 | } 69 | deps.Util.extend(PusherTestStub.prototype, deps.EventDispatcher.prototype); 70 | 71 | PusherTestStub.IS_STUB = true; 72 | PusherTestStub.singleton = null; 73 | PusherTestStub.instances = []; 74 | PusherTestStub.Util = deps.Util; 75 | 76 | PusherTestStub.prototype.subscribe = function(channelName, options) { 77 | var channel = this._channels[ channelName ]; 78 | 79 | if(channel === undefined) { 80 | channel = this._channelFactory(channelName, options); 81 | this._channels[ channelName ] = channel; 82 | } 83 | 84 | return channel; 85 | }; 86 | 87 | /** @private **/ 88 | PusherTestStub.ready = function() {}; 89 | 90 | /** @private **/ 91 | PusherTestStub.prototype._channelFactory = function(channelName, options) { 92 | var channel = null; 93 | 94 | if(channelName.indexOf('private-') == 0) { 95 | channel = new PrivateChannel(name); 96 | } 97 | else if(channelName.indexOf('presence-') === 0) { 98 | channel = new PresenceChannel(channelName); 99 | } 100 | else { 101 | channel = new Channel(channelName); 102 | } 103 | 104 | return channel; 105 | }; 106 | 107 | PusherTestStub.prototype.unsubscribe = function(channelName) { 108 | delete this._channels[channelName]; 109 | }; 110 | 111 | PusherTestStub.prototype.channel = function(channelName) { 112 | return this._channels[channelName] 113 | }; 114 | 115 | PusherTestStub.prototype.allChannels = function() { 116 | var channels = []; 117 | for(var name in this._channels) { 118 | channels.push(name); 119 | } 120 | return channels; 121 | }; 122 | 123 | PusherTestStub.prototype.connect = function() { 124 | 125 | }; 126 | 127 | PusherTestStub.prototype.disconnect = function() { 128 | 129 | }; 130 | 131 | /** 132 | * Helper function for triggering events on channels. 133 | * 134 | * @param {String} channelName the name of the channel to trigger an event on 135 | * @param {String} eventName the event to be triggered on the channel 136 | * @param {Object} eventData the event data payload to be associated with the event 137 | */ 138 | PusherTestStub.prototype.trigger = function(channelName, eventName, eventData) { 139 | this.channel(channelName).emit(eventName, eventData); 140 | }; 141 | 142 | window.Pusher = PusherTestStub; 143 | 144 | module.exports = PusherTestStub; 145 | 146 | 147 | /***/ }, 148 | /* 1 */ 149 | /***/ function(module, exports) { 150 | 151 | var PusherDefinition = window.Pusher; 152 | 153 | module.exports = { 154 | Pusher: PusherDefinition, 155 | EventDispatcher: PusherDefinition.EventsDispatcher, 156 | Util: PusherDefinition.Util, 157 | Members: PusherDefinition.Members 158 | }; 159 | 160 | 161 | /***/ }, 162 | /* 2 */ 163 | /***/ function(module, exports, __webpack_require__) { 164 | 165 | var deps = __webpack_require__(1); 166 | 167 | function Connection() { 168 | deps.EventDispatcher.call(this, function(event, data) { 169 | console.log('No callbacks on connection for ' + event); 170 | }); 171 | 172 | this.state = 'disconnected'; 173 | } 174 | deps.Util.extend(Connection.prototype, deps.EventDispatcher.prototype); 175 | 176 | module.exports = Connection; 177 | 178 | 179 | /***/ }, 180 | /* 3 */ 181 | /***/ function(module, exports, __webpack_require__) { 182 | 183 | var deps = __webpack_require__(1); 184 | 185 | function Channel(name) { 186 | deps.EventDispatcher.call(this, function(event, data) { 187 | console.log('No callbacks on ' + name + ' for ' + event); 188 | }); 189 | 190 | this.name = name; 191 | this.subscribed = false; 192 | } 193 | deps.Util.extend(Channel.prototype, deps.EventDispatcher.prototype); 194 | 195 | module.exports = Channel; 196 | 197 | 198 | /***/ }, 199 | /* 4 */ 200 | /***/ function(module, exports, __webpack_require__) { 201 | 202 | var deps = __webpack_require__(1); 203 | var Channel = __webpack_require__(3); 204 | 205 | function PrivateChannel(name) { 206 | Channel.call(this, name); 207 | } 208 | deps.Util.extend(PrivateChannel.prototype, Channel.prototype); 209 | 210 | PrivateChannel.prototype.trigger = function(eventName, eventData) { 211 | 212 | }; 213 | 214 | module.exports = PrivateChannel; 215 | 216 | 217 | /***/ }, 218 | /* 5 */ 219 | /***/ function(module, exports, __webpack_require__) { 220 | 221 | var deps = __webpack_require__(1); 222 | var PrivateChannel = __webpack_require__(4); 223 | 224 | function PresenceChannel(name) { 225 | PrivateChannel.call(this, name); 226 | 227 | this.members = new deps.Members(); 228 | } 229 | deps.Util.extend(PresenceChannel.prototype, PrivateChannel.prototype); 230 | 231 | module.exports = PresenceChannel; 232 | 233 | 234 | /***/ } 235 | /******/ ]); -------------------------------------------------------------------------------- /examples/common/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; 3 | color: #363C4F; 4 | } 5 | 6 | #my_application { 7 | clear: both; 8 | overflow: auto; 9 | } 10 | 11 | #chat_entry, #chat_messages { 12 | float: left; 13 | width: 200px; 14 | margin-bottom: 2em; 15 | margin-right: 2em; 16 | background-color: #F8F8F8; 17 | border: 1px #CCC solid; 18 | padding: 1em; 19 | -moz-border-radius: 5px; 20 | -webkit-border-radius: 5px; 21 | border-radius: 5px; 22 | } 23 | 24 | #chat_message { 25 | clear: right; 26 | height: 300px; 27 | overflow: auto; 28 | } -------------------------------------------------------------------------------- /examples/qunit/index.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 |

My Chat Application

12 | 13 |
14 | Status: 15 |
16 | 17 |
18 | Your ID:
19 | 20 | 21 |
22 | 23 |
24 |
25 |
26 | 27 | 28 | 29 | 30 | 31 | 32 | 56 | 57 |
58 | 59 | 60 | 61 | 62 | 63 | 116 | 117 |

QUnit example

118 |

119 |
120 |

121 |
    122 |
    test markup, will be hidden
    123 |
    124 | 125 | 126 | -------------------------------------------------------------------------------- /examples/selenium/chat-message-added-test.test: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | New Test 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
    Trigger Pusher Chat message updates UI
    openindex.html
    runScriptPusher.singleton.trigger("presence-chat-channel", "chat-message-received", {from: "Phil", text: "Selenium"});
    assertText//span[@class='text']Selenium
    30 | 31 | 32 | -------------------------------------------------------------------------------- /examples/selenium/index.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
    10 | 11 |

    My Chat Application

    12 | 13 |
    14 | Status: 15 |
    16 | 17 |
    18 | Your ID:
    19 | 20 | 21 |
    22 | 23 |
    24 |
    25 |
    26 | 27 | 28 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var webpack = require('gulp-webpack'); 3 | var server = require('gulp-server-livereload'); 4 | 5 | var dir = __dirname; 6 | 7 | gulp.task('build', function() { 8 | return gulp.src(dir + '/src/PusherTestStub.js') 9 | .pipe(webpack({ 10 | output: { 11 | library: 'PusherTestStub', 12 | filename: 'pusher-test-stub.js' 13 | } 14 | })) 15 | .pipe(gulp.dest(dir + '/dist/')); 16 | }); 17 | 18 | gulp.task('serve', ['build'], function() { 19 | gulp.src(dir) 20 | .pipe(server({ 21 | livereload: true, 22 | directoryListing: true, 23 | open: true 24 | })); 25 | }); 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pusher-test-stub", 3 | "version": "2.0.1", 4 | "description": "The Pusher Test Stub provide a way of testing your client-side integration with the Pusher JavaScript library and lets you trigger events to ensure that your application behaves in the way you expect it to.", 5 | "main": "index.js", 6 | "directories": { 7 | "example": "examples" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/leggetter/pusher-test-stub.git" 15 | }, 16 | "keywords": [ 17 | "pusher", 18 | "test", 19 | "stub" 20 | ], 21 | "author": "\"phil@leggetter.co.uk (http://www.leggetter.co.uk)", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/leggetter/pusher-test-stub/issues" 25 | }, 26 | "homepage": "https://github.com/leggetter/pusher-test-stub", 27 | "devDependencies": { 28 | "expect.js": "^0.3.1", 29 | "gulp": "^3.8.10", 30 | "gulp-server-livereload": "^1.2.1", 31 | "gulp-webpack": "^1.3.1", 32 | "mocha": "^2.2.1" 33 | }, 34 | "dependencies": {} 35 | } 36 | -------------------------------------------------------------------------------- /src/Channel.js: -------------------------------------------------------------------------------- 1 | var deps = require('./PusherDependencies'); 2 | 3 | function Channel(name) { 4 | deps.EventDispatcher.call(this, function(event, data) { 5 | console.log('No callbacks on ' + name + ' for ' + event); 6 | }); 7 | 8 | this.name = name; 9 | this.subscribed = false; 10 | } 11 | deps.Util.extend(Channel.prototype, deps.EventDispatcher.prototype); 12 | 13 | module.exports = Channel; 14 | -------------------------------------------------------------------------------- /src/Connection.js: -------------------------------------------------------------------------------- 1 | var deps = require('./PusherDependencies'); 2 | 3 | function Connection() { 4 | deps.EventDispatcher.call(this, function(event, data) { 5 | console.log('No callbacks on connection for ' + event); 6 | }); 7 | 8 | this.state = 'disconnected'; 9 | } 10 | deps.Util.extend(Connection.prototype, deps.EventDispatcher.prototype); 11 | 12 | module.exports = Connection; 13 | -------------------------------------------------------------------------------- /src/PresenceChannel.js: -------------------------------------------------------------------------------- 1 | var deps = require('./PusherDependencies'); 2 | var PrivateChannel = require('./PrivateChannel'); 3 | 4 | function PresenceChannel(name) { 5 | PrivateChannel.call(this, name); 6 | 7 | this.members = new deps.Members(); 8 | } 9 | deps.Util.extend(PresenceChannel.prototype, PrivateChannel.prototype); 10 | 11 | module.exports = PresenceChannel; 12 | -------------------------------------------------------------------------------- /src/PrivateChannel.js: -------------------------------------------------------------------------------- 1 | var deps = require('./PusherDependencies'); 2 | var Channel = require('./Channel'); 3 | 4 | function PrivateChannel(name) { 5 | Channel.call(this, name); 6 | } 7 | deps.Util.extend(PrivateChannel.prototype, Channel.prototype); 8 | 9 | PrivateChannel.prototype.trigger = function(eventName, eventData) { 10 | 11 | }; 12 | 13 | module.exports = PrivateChannel; 14 | -------------------------------------------------------------------------------- /src/PusherDependencies.js: -------------------------------------------------------------------------------- 1 | var PusherDefinition = window.Pusher; 2 | 3 | module.exports = { 4 | Pusher: PusherDefinition, 5 | EventDispatcher: PusherDefinition.EventsDispatcher, 6 | Util: PusherDefinition.Util, 7 | Members: PusherDefinition.Members 8 | }; 9 | -------------------------------------------------------------------------------- /src/PusherTestStub.js: -------------------------------------------------------------------------------- 1 | var deps = require('./PusherDependencies'); 2 | var Connection = require('./Connection'); 3 | var Channel = require('./Channel'); 4 | var PrivateChannel = require('./PrivateChannel'); 5 | var PresenceChannel = require('./PresenceChannel'); 6 | 7 | /** 8 | * Create a new instance of the test stub. 9 | * 10 | * @param {String} key the Pusher application key 11 | * @param {Object} options additional config options 12 | */ 13 | function PusherTestStub(key, options) { 14 | this.key = key; 15 | this.connection = new Connection(); 16 | 17 | this._channels = {}; 18 | 19 | PusherTestStub.singleton = this; 20 | PusherTestStub.instances.push(this); 21 | } 22 | deps.Util.extend(PusherTestStub.prototype, deps.EventDispatcher.prototype); 23 | 24 | PusherTestStub.IS_STUB = true; 25 | PusherTestStub.singleton = null; 26 | PusherTestStub.instances = []; 27 | PusherTestStub.Util = deps.Util; 28 | 29 | PusherTestStub.prototype.subscribe = function(channelName, options) { 30 | var channel = this._channels[ channelName ]; 31 | 32 | if(channel === undefined) { 33 | channel = this._channelFactory(channelName, options); 34 | this._channels[ channelName ] = channel; 35 | } 36 | 37 | return channel; 38 | }; 39 | 40 | /** @private **/ 41 | PusherTestStub.ready = function() {}; 42 | 43 | /** @private **/ 44 | PusherTestStub.prototype._channelFactory = function(channelName, options) { 45 | var channel = null; 46 | 47 | if(channelName.indexOf('private-') == 0) { 48 | channel = new PrivateChannel(name); 49 | } 50 | else if(channelName.indexOf('presence-') === 0) { 51 | channel = new PresenceChannel(channelName); 52 | } 53 | else { 54 | channel = new Channel(channelName); 55 | } 56 | 57 | return channel; 58 | }; 59 | 60 | PusherTestStub.prototype.unsubscribe = function(channelName) { 61 | delete this._channels[channelName]; 62 | }; 63 | 64 | PusherTestStub.prototype.channel = function(channelName) { 65 | return this._channels[channelName] 66 | }; 67 | 68 | PusherTestStub.prototype.allChannels = function() { 69 | var channels = []; 70 | for(var name in this._channels) { 71 | channels.push(name); 72 | } 73 | return channels; 74 | }; 75 | 76 | PusherTestStub.prototype.connect = function() { 77 | 78 | }; 79 | 80 | PusherTestStub.prototype.disconnect = function() { 81 | 82 | }; 83 | 84 | /** 85 | * Helper function for triggering events on channels. 86 | * 87 | * @param {String} channelName the name of the channel to trigger an event on 88 | * @param {String} eventName the event to be triggered on the channel 89 | * @param {Object} eventData the event data payload to be associated with the event 90 | */ 91 | PusherTestStub.prototype.trigger = function(channelName, eventName, eventData) { 92 | this.channel(channelName).emit(eventName, eventData); 93 | }; 94 | 95 | window.Pusher = PusherTestStub; 96 | 97 | module.exports = PusherTestStub; 98 | -------------------------------------------------------------------------------- /test/acceptance.js: -------------------------------------------------------------------------------- 1 | var requireDefined = (typeof( require ) !== 'undefined'); 2 | var expect = (requireDefined? require('expect.js') : window.expect); 3 | var PusherTestStub = window.PusherTestStub; 4 | var Pusher = window.Pusher 5 | 6 | describe('Pusher', function(){ 7 | 8 | it('should have been overridden by the test stub', function() { 9 | expect(Pusher).to.be(PusherTestStub) 10 | }) 11 | 12 | describe('#constructor()', function(){ 13 | it('should create a new test pusher instance', function(){ 14 | var pusher = new Pusher(); 15 | expect(pusher).to.be.ok(); 16 | }); 17 | }); 18 | 19 | describe('#subscribe()', function(){ 20 | it('should return a channel object', function(){ 21 | var pusher = new Pusher(); 22 | var channel = pusher.subscribe('channel'); 23 | expect(channel).to.be.ok(); 24 | }); 25 | 26 | it('should return a channel object with a name property', function(){ 27 | var pusher = new Pusher(); 28 | var channel = pusher.subscribe('channel'); 29 | expect(channel.name).to.be('channel'); 30 | }); 31 | }); 32 | 33 | describe('#channel()', function(){ 34 | it('should return the expected channel object', function(){ 35 | var pusher = new Pusher(); 36 | var subChannel = pusher.subscribe('channel'); 37 | var fetchedChannel = pusher.channel('channel'); 38 | 39 | expect(subChannel).to.be(fetchedChannel); 40 | }); 41 | 42 | it('should not return an unsubscribed channel', function() { 43 | var pusher = new Pusher(); 44 | var subChannel = pusher.subscribe('channel'); 45 | var fetchedChannel = pusher.channel('channel'); 46 | 47 | expect(subChannel).to.be(fetchedChannel); 48 | 49 | pusher.unsubscribe('channel'); 50 | 51 | fetchedChannel = pusher.channel('channel'); 52 | expect(fetchedChannel).to.be(undefined); 53 | }); 54 | }); 55 | 56 | describe('#allChannels()', function(){ 57 | it('should return and Array of channel names', function(){ 58 | var pusher = new Pusher(); 59 | pusher.subscribe('channel'); 60 | pusher.subscribe('channel2'); 61 | var channels = pusher.allChannels(); 62 | 63 | expect(channels.length).to.be(2); 64 | }); 65 | }); 66 | 67 | describe('pusher:subscription_succeeded', function() { 68 | it('should be possible to emit the succeeded event on a channel', function() { 69 | var pusher = new Pusher(); 70 | var channel = pusher.subscribe('channel'); 71 | 72 | var succeeded = false; 73 | channel.bind('pusher:subscription_succeeded', function() { 74 | succeeded = true; 75 | }); 76 | 77 | channel.emit('pusher:subscription_succeeded', {}); 78 | 79 | expect(succeeded).to.be(true); 80 | }); 81 | }); 82 | 83 | }); 84 | -------------------------------------------------------------------------------- /test/runner.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Mocha 5 | 6 | 7 | 8 | 9 | 10 |
    11 | 12 | 13 | 14 | 15 | 16 | 17 | 24 | 25 | 26 | --------------------------------------------------------------------------------