pubnub-functions-mock
Mocks PubNub Functions modules for unit testing
Last updated a year ago by ajb413 .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install pubnub-functions-mock 
SYNC missed versions from official npm registry.

PubNub Functions Mock

Build Status Coverage Status Known Vulnerabilities

Unit test PubNub Functions event handlers on your local machine

Currently supported modules for mock (docs here):

Any module can be overridden using overrideDefaultModules within a single test body. The module or modules will only be overridden in that single test block.

endpoint.overrideDefaultModules({
    "xhr" : () => {
        return Promise.resolve(200);
    }
});

To override a default module in all tests, pass the module object when the Event Handler is initialized.

endpoint = Mock('./myEndpointEventHandler.js', { 
    "xhr" : () => {
        return Promise.resolve(200);
    }
});

Mock the KVStore for a test

endpoint.mockKVStoreData({"key":"value"});

Mock the KVStore counters for a test

endpoint.mockKVStoreCounters({"key":123});

Example PubNub Function Endpoint unit test with Mocha and Chai

// myTest.js
const assert = require('chai').assert;
const Mock = require('pubnub-functions-mock');

const endpointRequestObject = {
    "body": "{}",
    "message": {},
    "method": null,
    "params": {}
};

const endpointResponseObject = {
    "headers": {},
    "status": 200,
    "send": function ( body ) {
        return new Promise( (resolve, reject) => {
            resolve({
                "body": body || "",
                "status": this.status
            });
        });
    }
};

describe('#endpoint', () => {
    let endpoint;

    beforeEach(() => {
        endpoint = Mock('./myEndpointEventHandler.js');
    });

    it('creates endpoint event handler of type Function', (done) => {
        assert.isFunction(endpoint, 'was successfully created');
        done();
    });

    it('returns "Hello World!"', (done) => {
        
        let request = Object.assign({}, endpointRequestObject);
        let response = Object.assign({}, endpointResponseObject);

        let correctResult = {
            "body": "Hello World!",
            "status": 200 
        };

        endpoint(request, response).then((testResult) => {

            assert.equal(testResult.status, correctResult.status, 'status');
            assert.equal(testResult.body, correctResult.body, 'response body');

            done();
        });
    });

    it('returns a kvstore value', (done) => {
        
        let request = Object.assign({}, endpointRequestObject);
        let response = Object.assign({}, endpointResponseObject);

        request.getKvValue = true;

        let preExistingValue = { "key" : "value" };

        let correctResult = {
            "body": preExistingValue.key,
            "status": 200 
        };

        // Mock the pre-existing KVStore value for this test only
        endpoint.mockKVStoreData(preExistingValue);

        endpoint(request, response).then((testResult) => {

            assert.equal(testResult.status, correctResult.status, 'status');
            assert.equal(testResult.body, correctResult.body, 'response body');

            done();
        });
    });
});

The above test would be run on myEndpointEventHandler.js using mocha myTest

// myEndpointEventHandler.js
export default (request, response) => {
    const pubnub = require('pubnub');
    const kvstore = require('kvstore');

    if (request.getKvValue) {
        return kvstore.get('key').then((value) => {
            response.status = 200;
            return response.send(value);
        });
    }

    response.status = 200;
    return response.send("Hello World!");
};

Current Tags

  • 0.0.17                                ...           latest (a year ago)

17 Versions

  • 0.0.17                                ...           a year ago
  • 0.0.16                                ...           2 years ago
  • 0.0.15                                ...           2 years ago
  • 0.0.14                                ...           3 years ago
  • 0.0.13                                ...           3 years ago
  • 0.0.12                                ...           3 years ago
  • 0.0.11                                ...           3 years ago
  • 0.0.10                                ...           3 years ago
  • 0.0.9                                ...           3 years ago
  • 0.0.8                                ...           3 years ago
  • 0.0.7                                ...           3 years ago
  • 0.0.6                                ...           3 years ago
  • 0.0.5                                ...           3 years ago
  • 0.0.4                                ...           3 years ago
  • 0.0.3                                ...           3 years ago
  • 0.0.2                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 6
Last Day 0
Last Week 0
Last Month 24
Dependencies (7)
Dev Dependencies (11)

Copyright 2014 - 2016 © taobao.org |