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);
    });
});
@@ -617,18 +617,19 @@
 incident
+s
 ;%0A
-
     %0A   
@@ -645,24 +645,407 @@
 unction() %7B%0A
+        mockery.registerMock('../models/incident', function () %7B%0A            var model = function () %7B%7D;%0A            model.prototype = %7B%0A                save: function () %7B %0A                    console.warn('Should be overriden with sinon.mock');%0A                    return Promise.resolve();%0A                %7D%0A            %7D;%0A            %0A            return model;%0A        %7D());%0A    
     // mock 
@@ -1063,16 +1063,16 @@
 eporter%0A
-
         
@@ -1216,32 +1216,33 @@
         incident
+s
  = require('./in
@@ -1440,24 +1440,25 @@
 eof(incident
+s
 .POST)).equa
@@ -1584,55 +1584,8 @@
 ');%0A
-        var incident = require('./incidents');%0A
     
@@ -1854,24 +1854,16 @@
      %7D;%0A
-        
 %0A       
@@ -1914,18 +1914,8 @@
 rns(
-null); // 
 Prom
@@ -1941,16 +1941,17 @@
 ncident)
+)
 %0A       
@@ -2276,32 +2276,32 @@
 spy();%0A        %0A
-
         incident
@@ -2300,16 +2300,17 @@
 incident
+s
 .POST(re

Apply sinon with mockery to hijack module loading