var cdb = require('cartodb-deep-insights.js');
var WidgetDefinitionModel = require('../../../../javascripts/cartodb3/data-models/widget-definition-model');
var LayerDefinitionModel = require('../../../../javascripts/cartodb3/data-models/layer-definition-model');

describe('widget-defintion-model', function () {
  beforeEach(function () {
    this.layerModel = new cdb.core.Model();
    this.layerDefModel = new LayerDefinitionModel(null, {
      layerModel: this.layerModel
    });
    this.layerDefModel.url = function () {
      return '/layers/l-123';
    };
    this.dashboardWidgetsService = jasmine.createSpyObj('dashboard.widgets', [
      'createFormulaModel',
      'createCategoryModel'
    ]);
    this.widgetDefModel = new WidgetDefinitionModel({
      id: 'w-456',
      title: 'some title'
    }, {
      layerDefinitionModel: this.layerDefModel,
      dashboardWidgetsService: this.dashboardWidgetsService
    });
  });

  it('should have a url pointing under layers API endpoint', function () {
    expect(this.widgetDefModel.url()).toEqual('/layers/l-123/widgets/w-456');

    // when no id:
    this.widgetDefModel.set('id', null);
    expect(this.widgetDefModel.url()).toEqual('/layers/l-123/widgets');
  });

  describe('when it defines a formula widget', function () {
    beforeEach(function () {
      this.widgetDefModel.set({
        type: 'formula',
        options: {
          column: 'col',
          operation: 'avg'
        }
      });
    });

    describe('when is saved', function () {
      beforeEach(function () {
        this.dashboardWidgetsService.createFormulaModel.and.returnValue({});
        this.widgetDefModel.trigger('sync');
      });

      it('should create the widget model', function () {
        expect(this.dashboardWidgetsService.createFormulaModel).toHaveBeenCalled();
        var args = this.dashboardWidgetsService.createFormulaModel.calls.argsFor(0);
        expect(args[0]).toEqual(jasmine.objectContaining({ title: 'some title' }));
        expect(args[0]).toEqual(jasmine.objectContaining({ column: 'col' }));
        expect(args[0]).toEqual(jasmine.objectContaining({ operation: 'avg' }));
        expect(args[1]).toBe(this.layerModel);
        expect(this.widgetDefModel._widgetModel).toBeDefined();
      });
    });
  });
});

@@ -346,40 +346,434 @@
 his.
-layerModel = new cdb.core.Model(
+el = document.createElement('div');%0A    this.el.id = 'wdmtmp';%0A    document.body.appendChild(this.el);%0A    var dashboard = cdb.deepInsights.createDashboard('#wdmtmp', %7B%0A      user: %7B%7D,%0A      datasource: %7B%0A        maps_api_template: 'asd',%0A        user_name: 'pepe'%0A      %7D,%0A      layers: %5B%7B%0A        id: 'l-1',%0A        type: 'CartoDB'%0A      %7D%5D,%0A      widgets: %5B%5D%0A    %7D);%0A    this.layerModel = dashboard.vis.map.layers.get('l-1'
 );%0A 
@@ -821,20 +821,39 @@
 onModel(
-null
+%7B%0A      id: 'l-1'%0A    %7D
 , %7B%0A    
@@ -959,14 +959,19 @@
 ers/
-l-123'
+' + this.id
 ;%0A  
@@ -975,16 +975,17 @@
 %0A    %7D;%0A
+%0A
     this
@@ -1015,114 +1015,25 @@
 e = 
-jasmine.createSpyObj('dashboard.widgets', %5B%0A      'createFormulaModel',%0A      'createCategoryModel'%0A    %5D)
+dashboard.widgets
 ;%0A  
@@ -1261,16 +1261,89 @@
 %0A  %7D);%0A%0A
+  afterEach(function () %7B%0A    document.body.removeChild(this.el);%0A  %7D);%0A%0A
   it('sh
@@ -1459,34 +1459,32 @@
 ual('/layers/l-1
-23
 /widgets/w-456')
@@ -1608,10 +1608,8 @@
 /l-1
-23
 /wid
@@ -1949,24 +1949,30 @@
 ) %7B%0A        
+spyOn(
 this.dashboa
@@ -1979,33 +1979,35 @@
 rdWidgetsService
-.
+, '
 createFormulaMod
@@ -2012,27 +2012,27 @@
 odel
+')
 .and.
-returnValue(%7B%7D
+callThrough(
 );%0A 

Make widget model creation to be integration test