<commit_before>/**
 * Created by zarges on 16/02/16.
 */
describe('mwBackbone nested models', function () {

  var NestedModel = window.mwUI.Backbone.NestedModel;

  beforeAll(function(){
    mwUI.Backbone.use$http = false;
  });

  afterAll(function(){
    mwUI.Backbone.use$http = true;
  });

  describe('nested model as attribute', function () {
    var NestedModelAttr, ModelWithNestedModel;
    beforeEach(function () {
      NestedModelAttr = window.Backbone.Model.extend({
        defaults: {
          name: 'Nested Model Attr'
        }
      });
      ModelWithNestedModel = NestedModel.extend({
        url: 'withNestedModel',
        nested: function () {
          return {
            nestedModelAttr: NestedModelAttr
          };
        }
      });
    });

    it('initializes a model with a nested model as attribute', function () {
      var modelWithNestedModel = new ModelWithNestedModel();

      expect(modelWithNestedModel.get('nestedModelAttr') instanceof NestedModelAttr).toBeTruthy();
    });

    it('initializes a model with a nested model as attribute and default options', function () {
      var modelWithNestedModel = new (ModelWithNestedModel.extend({
        defaults: function () {
          return {
            name: 'Test'
          };
        }
      }))();

      expect(modelWithNestedModel.get('nestedModelAttr') instanceof NestedModelAttr).toBeTruthy();
    });

    it('initializes a model with a nested model when attributes are passed into constructor', function () {
      var modelWithNestedModel = new ModelWithNestedModel({
        name: 'Test',
        nestedModelAttr: {
          name: 'Test'
        }
      });

      expect(modelWithNestedModel.get('nestedModelAttr') instanceof NestedModelAttr).toBeTruthy();
    });

  });

  describe('nested collection as attribute', function () {
    var NestedCollectionAttr, ModelWithNestedCollection;
    beforeEach(function () {
      NestedCollectionAttr = window.Backbone.Collection.extend({});
      ModelWithNestedCollection = NestedModel.extend({
        url: 'withNestedCollection',
        nested: function () {
          return {
            nestedCollectionAttr: NestedCollectionAttr
          };
        }
      });
    });

    it('initializes be possible to define a model with a nested collection as attribute', function () {
      var modelWithNestedCollectionModel = new ModelWithNestedCollection();

      expect(modelWithNestedCollectionModel.get('nestedCollectionAttr') instanceof NestedCollectionAttr).toBeTruthy();
    });

    it('initializes a nested collection as attribute and default options', function () {
      var modelWithNestedCollectionModel = new (ModelWithNestedCollection.extend({
        defaults: function () {
          return {
            name: 'Test'
          };
        }
      }))();

      expect(modelWithNestedCollectionModel.get('nestedCollectionAttr') instanceof NestedCollectionAttr).toBeTruthy();
      expect(modelWithNestedCollectionModel.get('name')).toEqual('Test');
    });

    it('initializes a model with a nested collection when attributes are passed into constructor', function () {
      var modelWithNestedModel = new ModelWithNestedCollection({
        name: 'Test',
        nestedCollectionAttr: [
          {
            id: 1,
            name: 'Test'
          },
          {
            id: 2,
            name: '<NAME>'
          }
        ]
      });

      expect(modelWithNestedModel.get('nestedCollectionAttr') instanceof NestedCollectionAttr).toBeTruthy();
      expect(modelWithNestedModel.get('nestedCollectionAttr').first().get('id')).toBe(1);
      expect(modelWithNestedModel.get('nestedCollectionAttr').last().get('name')).toEqual('Test 2');
    });

  });

  describe('nested model that is created by a collection', function () {
    var NestedModelAttr, ModelWithNestedModel, CollectionWithNestedModels;
    beforeEach(function () {
      NestedModelAttr = window.Backbone.Model.extend({
        defaults: {
          name: 'Nested Model Attr'
        }
      });

      ModelWithNestedModel = NestedModel.extend({
        defaults: function () {
          return {
            name: 'Test'
          };
        },
        nested: function () {
          return {
            nestedModelAttr: NestedModelAttr<commit_msg>adds test cases to verify that clear method does not destroy reference to nested models/collections<commit_after>/**
 * Created by zarges on 16/02/16.
 */
describe('mwBackbone nested models', function () {

  var NestedModel = window.mwUI.Backbone.NestedModel;

  beforeAll(function () {
    mwUI.Backbone.use$http = false;
  });

  afterAll(function () {
    mwUI.Backbone.use$http = true;
  });

  describe('nested model as attribute', function () {
    var NestedModelAttr, ModelWithNestedModel;
    beforeEach(function () {
      NestedModelAttr = window.Backbone.Model.extend({
        defaults: {
          name: 'Nested Model Attr'
        }
      });
      ModelWithNestedModel = NestedModel.extend({
        url: 'withNestedModel',
        nested: function () {
          return {
            nestedModelAttr: NestedModelAttr
          };
        }
      });
    });

    it('initializes a model with a nested model as attribute', function () {
      var modelWithNestedModel = new ModelWithNestedModel();

      expect(modelWithNestedModel.get('nestedModelAttr') instanceof NestedModelAttr).toBeTruthy();
    });

    it('initializes a model with a nested model as attribute and default options', function () {
      var modelWithNestedModel = new (ModelWithNestedModel.extend({
        defaults: function () {
          return {
            name: 'Test'
          };
        }
      }))();

      expect(modelWithNestedModel.get('nestedModelAttr') instanceof NestedModelAttr).toBeTruthy();
    });

    it('initializes a model with a nested model when attributes are passed into constructor', function () {
      var modelWithNestedModel = new ModelWithNestedModel({
        name: 'Test',
        nestedModelAttr: {
          name: 'Test'
        }
      });

      expect(modelWithNestedModel.get('nestedModelAttr') instanceof NestedModelAttr).toBeTruthy();
    });

    it('clears the model and its nested model', function () {
      var modelWithNestedModel = new ModelWithNestedModel();
      modelWithNestedModel.set('name', 'xxx');
      modelWithNestedModel.get('nestedModelAttr').set('name','xxx');

      modelWithNestedModel.clear();

      expect(modelWithNestedModel.get('name')).toBeUndefined();
      expect(modelWithNestedModel.get('nestedModelAttr').get('name')).toBeUndefined();
    });

    it('keeps the reference to nested model on clear', function () {
      var modelWithNestedModel = new ModelWithNestedModel(),
        referenceToNestedModel = modelWithNestedModel.get('nestedModelAttr');

      modelWithNestedModel.clear();

      expect(modelWithNestedModel.get('nestedModelAttr')).toBe(referenceToNestedModel);
    });

  });

  describe('nested collection as attribute', function () {
    var NestedCollectionAttr, ModelWithNestedCollection;
    beforeEach(function () {
      NestedCollectionAttr = window.Backbone.Collection.extend({});
      ModelWithNestedCollection = NestedModel.extend({
        url: 'withNestedCollection',
        nested: function () {
          return {
            nestedCollectionAttr: NestedCollectionAttr
          };
        }
      });
    });

    it('initializes be possible to define a model with a nested collection as attribute', function () {
      var modelWithNestedCollectionModel = new ModelWithNestedCollection();

      expect(modelWithNestedCollectionModel.get('nestedCollectionAttr') instanceof NestedCollectionAttr).toBeTruthy();
    });

    it('initializes a nested collection as attribute and default options', function () {
      var modelWithNestedCollectionModel = new (ModelWithNestedCollection.extend({
        defaults: function () {
          return {
            name: 'Test'
          };
        }
      }))();

      expect(modelWithNestedCollectionModel.get('nestedCollectionAttr') instanceof NestedCollectionAttr).toBeTruthy();
      expect(modelWithNestedCollectionModel.get('name')).toEqual('Test');
    });

    it('initializes a model with a nested collection when attributes are passed into constructor', function () {
      var modelWithNestedModel = new ModelWithNestedCollection({
        name: 'Test',
        nestedCollectionAttr: [
          {
            id: 1,
            name: 'Test'
          },
          {
            id: 2,
            name: '<NAME>'
          }
        ]
      });

      expect(modelWithNestedModel.get('nestedCollectionAttr') instanceof NestedCollectionAttr).toBeTruthy();
      expect(modelWithNestedModel.get('nestedCollectionAttr').first().get('id')).toBe(1);
      expect(modelWithNestedModel.get('nestedCollectionAttr').last().get('name')).toEqual('Test 2');
    });

    it('clears the model and its nested collection', function () {
      var modelWithNestedCollection = new ModelWithNestedCollection();
      modelWithNestedCollection.set('name', 'xxx');
      modelWithNestedCollection.get('nestedCollectionAttr').add({'id':1});

      modelWithNestedCollection.clear();

      expect(modelWithNestedCollection.get('name')).toBeUndefined();
      expect(modelWithNestedCollection.get('nestedCollectionAttr').length).toBe(0);
    });

    it('keeps the reference to nested collection on clear', function () {
      var modelWithNestedCollection = new ModelWithNestedCollection(),
        referenceToNestedCollection = modelWithNestedCollection.get('nestedCollectionAttr');

      modelWithNestedCollection.clear();

      expect(modelWithNestedCollection.get('nestedCollectionAttr')).toBe(referenceToNestedCollection);
    });

  });

  describe('nested model that is created by a collection', function () {
    var NestedModelAttr, ModelWithNestedModel, CollectionWithNestedModels;
    beforeEach(function () {
      NestedModelAttr = window.Backbone.Model.extend({
        defaults: {
          name: 'Nested Model Attr'
        }
      });

      ModelWithNestedModel = NestedModel.extend({
        defaults: function () {
          return {
            name: 'Test'
          };
        },
        nested: function () {
          return {
            nestedModelAttr: NestedModelAttr
{'fraction_non_alphanumeric': 0.11229325853564175, 'fraction_numerical': 0.0021278653641551406, 'mean_word_length': 2.8755622188905545, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4218193', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2678, 'n_words': 571}