<commit_before>require('es6-promise').polyfill();

var expect = require('expect.js');
var sinon = require('sinon');
var mockery = require('mockery');

describe('Incident controller: GET', function () {
    var incident = require('./incidents');
    
    it('has GET handler', function () {
        expect(typeof(incident.GET)).equal('function');
    });
    
    it('resonds to /', function () {
        var req = res = {};
        res.send = sinon.spy();
        
        incident.GET(req, res);
        
        expect(res.send.calledOnce).to.equal(true);
    });
});

describe('Incident controller: POST', function () {
    var incident;
    
    before(function() {
    // mock the error reporter
        mockery.enable({
          warnOnReplace: false,
          warnOnUnregistered: false,
          useCleanCache: true
        });
        
        incident = require('./incidents');
    });
    
    after(function() {
        // disable mock after tests complete
        mockery.disable();
    });
    
    it('has POST handler', function () {
        expect(typeof(incident.POST)).equal('function');
    });
    
    it('resonds to /', function (done) {
        var Incident = require('../models/incident');
        var incident = require('./incidents');
        
        var mockedIncident = {
            __v: 0,
            _id: 'abc123',
            type: 'accident',
            location: {
                latlng: [1,2],
                spec: {
                    placeid: '123'
                }
            }
        };
        
        sinon.stub(Incident.prototype, 'save').returns(null); // Promise.resolve(mockedIncident)
        
        var req = {
            body: {
                type: 'accident',
                location: {
                    latlng: [1,2],
                    spec: {
                        placeid: '123'
                    }
                }
            }
        };
        var res = {};
        res.send = sinon.spy();
        
        incident.POST(req, res);
        
        setTimeout(function() {
            expect(Incident.prototype.save.calledOnce).to.equal(true);
            // TODO Fix the assertion
            expect(res.send.calledOnce).to.equal(true);
            
            done();
        }, 0);
    });
});<commit_msg>Apply sinon with mockery to hijack module loading<commit_after>require('es6-promise').polyfill();

var expect = require('expect.js');
var sinon = require('sinon');
var mockery = require('mockery');

describe('Incident controller: GET', function () {
    var incident = require('./incidents');
    
    it('has GET handler', function () {
        expect(typeof(incident.GET)).equal('function');
    });
    
    it('resonds to /', function () {
        var req = res = {};
        res.send = sinon.spy();
        
        incident.GET(req, res);
        
        expect(res.send.calledOnce).to.equal(true);
    });
});

describe('Incident controller: POST', function () {
    var incidents;
    
    before(function() {
        mockery.registerMock('../models/incident', function () {
            var model = function () {};
            model.prototype = {
                save: function () { 
                    console.warn('Should be overriden with sinon.mock');
                    return Promise.resolve();
                }
            };
            
            return model;
        }());
        // mock the error reporter
        mockery.enable({
          warnOnReplace: false,
          warnOnUnregistered: false,
          useCleanCache: true
        });
        
        incidents = require('./incidents');
    });
    
    after(function() {
        // disable mock after tests complete
        mockery.disable();
    });
    
    it('has POST handler', function () {
        expect(typeof(incidents.POST)).equal('function');
    });
    
    it('resonds to /', function (done) {
        var Incident = require('../models/incident');
        
        var mockedIncident = {
            __v: 0,
            _id: 'abc123',
            type: 'accident',
            location: {
                latlng: [1,2],
                spec: {
                    placeid: '123'
                }
            }
        };

        sinon.stub(Incident.prototype, 'save').returns(Promise.resolve(mockedIncident))
        
        var req = {
            body: {
                type: 'accident',
                location: {
                    latlng: [1,2],
                    spec: {
                        placeid: '123'
                    }
                }
            }
        };
        var res = {};
        res.send = sinon.spy();
        
        incidents.POST(req, res);
        
        setTimeout(function() {
            expect(Incident.prototype.save.calledOnce).to.equal(true);
            // TODO Fix the assertion
            expect(res.send.calledOnce).to.equal(true);
            
            done();
        }, 0);
    });
});
{'fraction_non_alphanumeric': 0.13866990095007076, 'fraction_numerical': 0.006468566808166565, 'mean_word_length': 1.3788461538461538, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3074056', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1323, 'n_words': 256}