npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-socket.io-mock

v1.2.0

Published

Drop in Mock replacement for angular-socket-io

Downloads

24,236

Readme

angular-socket.io-mock Build Status

Mock Socket.io bindings for AngularJS, useful for testing as a drop in replacement for https://github.com/btford/angular-socket-io which allows testing against a fake server.

Usage

Standard usage

  • Make sure AngularJS is loaded prior to inclusion.

karma.conf.js

module.exports = function(config) {
  config.set({
    files: [
      {pattern: "bower_components/angular-socket.io-mock.js", included: false}
    ]
  })
}

With RequireJS

In all our projects we utilize RequireJS to use this test library with RequireJS is very simple. Just replace the entry that points to the standard angular-socket-io with this library. Below is an example.

main.js

require.config({
  baseUrl: "/base/js",
  shim: {
    "socketio": {exports: "io"},
    "angular": {exports: "angular"},
    "ng-socket-io": ["angular","socketio"],
    "ng-mocks": ["angular"]
  },
  paths: {
    "angular": "/base/bower_components/angular/angular",
    "socketio": "/base/bower_components/socket.io-client/dist/socket.io",
    "ng-socket-io": "/base/bower_components/angular-socket.io-mock/angular-socket.io-mock",
    "ng-mocks": "/base/bower_components/angular-mocks/angular-mocks"
  },
  callback: window.__karma__.start
})

Your bower.json should look something like this.

bower.json

{
  "name": "my-app",
  "version": "0.0.1",
  "dependencies": {
    "angular": "latest",
    "angular-socket-io": "latest"
  },
  "devDependencies": {
    "socket.io-client": "latest",
    "angular-mocks": "latest",
    "angular-socket.io-mock": "latest"
  }
}

Example Test

This example uses Mocha Test as the testing framework and ChaiJS for the assertion library. It also uses RequireJS to load the environment.

test.js

define(["chai","angular","angular-mocks","angular-socket-io"],function(chai){
  describe("Test Suite",function(){
    beforeEach(module("app"))
    it("should be using the mock ng-socket.io",inject(function($rootScope,$controller){
      var expect = chai.expect
        , scope = $rootScope.$new()
        , ctrl = $controller("MyController",{$scope: scope})
      expect(scope.save).to.be.a("function")
      expect(scope.items).to.be.a("array")
      expect(scope.items.length).to.equal(3)
    }))
  })
})

MyController.js

define(["angular","ng-socket-io"],function(ng){
  var app = ng.module("app",["btford.socket-io"])
  app.controller("MyController",["$scope","socket",function($scope,socket){
    //load items now
    $scope.items = []
    socket.emit("loadItems",{},function(res){
      $scope.items = res.res
    })
    //setup functions
    $scope.save = function(){}
  }])
})

Example test using Jasmine

Test.js

describe('Controller: socketController', function () {

    // load the controller's module
    beforeEach(module('app'));

    var socketController,
      notify,
      scope;

    // Initialize the controller and a mock scope
    beforeEach(inject(function ($controller, $rootScope, _notify_) {
      scope = $rootScope.$new();
      notify = _notify_;
      socketController = $controller('socketController', {
        $scope: scope
      });

    }));

    it('The scope.items should change somehow', function() {
      expect(scope.items.length).toEqual(0);
      notify.receive('loadItems',{res: ['1','2','3']} )
      expect(scope.items.length).toEqual(3);
    });

  });
'use strict';

angular.module('app')
  .controller('socketController', function ($scope, notify) {
    // As first step to unit-test the socket, what we are
    // going to do it is to try mocking an error, and changing
    // a variable.
    // This I hope it's easy enough to get started. It is not easy.


    $scope.items = []
    notify.on("loadItems", function(res){
      $scope.items = res.res
    });
  });

Todo

  • Usage with sinon.js for spies
  • Mocking responses

Note Please see https://github.com/btford/angular-socket-io for library usage.

Changelog

1.2.0

  • Merge pull reuqest #10 adding a prefix and forward option
  • Update to latest dependencies
  • Publish on NPM

1.1.1

  • Add forward support

1.1.0

  • .receive will acknowledge any .emit events with a callback as the last parameter

1.0.0

  • Upgraded to work with latest angular.
  • Use 0.1.0 with older versions of angularjs.
  • Add once support

0.1.0

  • Fixed issue interface with the latest angular package.

0.0.1

  • Initial Release