<commit_before>var expect            = require('expect.js');
var _                 = require('lodash');
var sampleData        = require('./lib/sampleData');
var fruits            = sampleData.fruits;
var fns               = sampleData.fns;
var helpers           = require('./lib/helpers');
var setupModel        = helpers.getModelSetup(fns, '*', ['name', 'color']);
var EventListenerData = helpers.EventListenerData;

describe('Base Events', function () {
  describe('Adding item to empty collection', function () {
    it('Triggers "change" ONLY on collection for filtered item', function () {
      var listenerData = new EventListenerData();
      var model = setupModel();
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));
      model.add('fruits', {name: 'grapefruit', color: 'orange', amount: 10, id: 'grapefruitId'});
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.grapefruitId',
          eventEmitted: 'change',
          args: {
            name: 'grapefruit', 
            color: 'orange',
            amount: 10,
            id: 'grapefruitId'
          }
        }
      ]);  
    });

    it('Triggers "change" on collection AND view for non-filtered item', function () {
      var listenerData = new EventListenerData();
      var model = setupModel();
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));
      model.add('fruits', {name: 'banana', color: 'yellow', amount: 15, id: 'bananaId'});
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.bananaId',
          eventEmitted: 'change',
          args: {
            name: 'banana', 
            color: 'yellow',
            amount: 15,
            id: 'bananaId' 
          }
        }, 
        {
          path: 'filteredFruits.banana*yellow',
          eventEmitted: 'change',
          args: {
            name: 'banana', 
            color: 'yellow',
            amount: 15,
            id: 'bananaId' 
          }
        }
      ]);
    });
  });

  describe('Adding item to non-empty collection', function () {
    it('Triggers "change" ONLY on collection for filtered item', function () {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));
      model.add('fruits', {name: 'grapefruit', color: 'orange', amount: 10, id: 'grapefruitId'});
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.grapefruitId',
          eventEmitted: 'change',
          args: {
            name: 'grapefruit', 
            color: 'orange',
            amount: 10,
            id: 'grapefruitId' 
          }
        }
      ]); 
    });

    it('Triggers "change" on collection AND view for non-filtered item', function () {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));
      model.add('fruits', {name: 'mango', color: 'yellow', amount: 15, id: 'mangoId'});
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.mangoId',
          eventEmitted: 'change',
          args: {
            name: 'mango', 
            color: 'yellow',
            amount: 15,
            id: 'mangoId'
          }
        }, 
        {
          path: 'filteredFruits.mango*yellow',
          eventEmitted: 'change',
          args: {
            name: 'mango', 
            color: 'yellow',
            amount: 15,
            id: 'mangoId'
          }
        }
      ]);
    });
  });

  describe('Removing item from collection', function() {
    it('Triggers "change" ONLY on collection for filtered item', function() {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));       
      model.del('fruits.appleId');
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.appleId',
          eventEmitted: 'change',
          args: undefined
        }
      ]);
    });

    // 'change' is triggered thrice (view first, then collection and then view again). It should ony be twice?
    it('Triggers "change" on collection AND view for non-filtered item', function() {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));     
      model.del('fruits.bananaId');
      expect(listenerData.eventData).to.eql([{
          path: 'filteredFruits.banana*yellow',
          eventEmitted: 'change',
          args: undefined
        },
        {
          path: 'fruits.bananaId',
          eventEmitted: 'change',
          args: undefined
        }
      ]);
    });
  });

  // Does NOT trigger change on the view at all. Updated item is NOT included in the view as it should be.  
  // What should the order of the events be (i.e. first view and then collection OR vice versa)? 
  describe('Updating item in collection', function() {   
    it.skip('Triggers "change" on collection AND view as the update causes emit now', function() {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));
      model.set('fruits.appleId.color', 'yellow');
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.appleId.color',
          eventEmitted: 'change',
          args: 'yellow'
        },
        {
          path: 'filteredFruits.apple*yellow',
          eventEmitted: 'change',
          args: 'yellow'
        }
      ]);
    });

    // 'change' is fired correctly for both view and collection, but the view is NOT updated 
    // and still contains the updated item together with the originally emitted key. Does this
    // mean that still another event is required to update it?
    // What should the order of the events be (i.e. first view and then collection OR vice versa)? 
    it('Triggers "change" on collection AND view as the update no longer causes emit', function() {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));  
      model.set('fruits.bananaId.color', 'green');
      expect(listenerData.eventData).to.eql([{
          path: 'filteredFruits.banana*yellow',
          eventEmitted: 'change',
          args: undefined
        },
        {
          path: 'fruits.bananaId.color',
          eventEmitted: 'change',
          args: 'green'
        }
      ]);
    });
  });
});

describe('Base Events: Multilevel Keys', function() {<commit_msg>Fix test with wrong expectations<commit_after>var expect            = require('expect.js');
var _                 = require('lodash');
var sampleData        = require('./lib/sampleData');
var fruits            = sampleData.fruits;
var fns               = sampleData.fns;
var helpers           = require('./lib/helpers');
var setupModel        = helpers.getModelSetup(fns, '*', ['name', 'color']);
var EventListenerData = helpers.EventListenerData;

describe('Base Events', function () {
  describe('Adding item to empty collection', function () {
    it('Triggers "change" ONLY on collection for filtered item', function () {
      var listenerData = new EventListenerData();
      var model = setupModel();
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));
      model.add('fruits', {name: 'grapefruit', color: 'orange', amount: 10, id: 'grapefruitId'});
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.grapefruitId',
          eventEmitted: 'change',
          args: {
            name: 'grapefruit', 
            color: 'orange',
            amount: 10,
            id: 'grapefruitId'
          }
        }
      ]);  
    });

    it('Triggers "change" on collection AND view for non-filtered item', function () {
      var listenerData = new EventListenerData();
      var model = setupModel();
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));
      model.add('fruits', {name: 'banana', color: 'yellow', amount: 15, id: 'bananaId'});
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.bananaId',
          eventEmitted: 'change',
          args: {
            name: 'banana', 
            color: 'yellow',
            amount: 15,
            id: 'bananaId' 
          }
        }, 
        {
          path: 'filteredFruits.banana*yellow',
          eventEmitted: 'change',
          args: {
            name: 'banana', 
            color: 'yellow',
            amount: 15,
            id: 'bananaId' 
          }
        }
      ]);
    });
  });

  describe('Adding item to non-empty collection', function () {
    it('Triggers "change" ONLY on collection for filtered item', function () {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));
      model.add('fruits', {name: 'grapefruit', color: 'orange', amount: 10, id: 'grapefruitId'});
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.grapefruitId',
          eventEmitted: 'change',
          args: {
            name: 'grapefruit', 
            color: 'orange',
            amount: 10,
            id: 'grapefruitId' 
          }
        }
      ]); 
    });

    it('Triggers "change" on collection AND view for non-filtered item', function () {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));
      model.add('fruits', {name: 'mango', color: 'yellow', amount: 15, id: 'mangoId'});
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.mangoId',
          eventEmitted: 'change',
          args: {
            name: 'mango', 
            color: 'yellow',
            amount: 15,
            id: 'mangoId'
          }
        }, 
        {
          path: 'filteredFruits.mango*yellow',
          eventEmitted: 'change',
          args: {
            name: 'mango', 
            color: 'yellow',
            amount: 15,
            id: 'mangoId'
          }
        }
      ]);
    });
  });

  describe('Removing item from collection', function() {
    it('Triggers "change" ONLY on collection for filtered item', function() {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));       
      model.del('fruits.appleId');
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.appleId',
          eventEmitted: 'change',
          args: undefined
        }
      ]);
    });

    // 'change' is triggered thrice (view first, then collection and then view again). It should ony be twice?
    it('Triggers "change" on collection AND view for non-filtered item', function() {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));     
      model.del('fruits.bananaId');
      expect(listenerData.eventData).to.eql([{
          path: 'filteredFruits.banana*yellow',
          eventEmitted: 'change',
          args: undefined
        },
        {
          path: 'fruits.bananaId',
          eventEmitted: 'change',
          args: undefined
        }
      ]);
    });
  });

  // Does NOT trigger change on the view at all. Updated item is NOT included in the view as it should be.  
  // What should the order of the events be (i.e. first view and then collection OR vice versa)? 
  describe('Updating item in collection', function() {   
    it('Triggers "change" on collection AND view as the update causes emit now', function() {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));
      model.set('fruits.appleId.color', 'yellow');
      expect(listenerData.eventData).to.eql([{
          path: 'fruits.appleId.color',
          eventEmitted: 'change',
          args: 'yellow'
        },
        {
          path: 'filteredFruits.apple*yellow',
          eventEmitted: 'change',
          args: {
            name: 'apple',
            color: 'yellow',
            amount: 5,
            id: 'appleId'
          }
        }
      ]);
    });

    // 'change' is fired correctly for both view and collection, but the view is NOT updated 
    // and still contains the updated item together with the originally emitted key. Does this
    // mean that still another event is required to update it?
    // What should the order of the events be (i.e. first view and then collection OR vice versa)? 
    it('Triggers "change" on collection AND view as the update no longer causes emit', function() {
      var listenerData = new EventListenerData();
      var model = setupModel({fruits: fruits});
      var view = model.at('fruits').view('yellowFruitsWithPath');
      view.ref('_page.filteredFruits');
      model.on('all', '**', listenerData.collectListenerData.bind(listenerData));  
      model.set('fruits.bananaId.color', 'green');
      expect(listenerData.eventData).to.eql([{
          path: 'filteredFruits.banana*yellow',
          eventEmitted: 'change',
          args: undefined
        },
        {
          path: 'fruits.bananaId.color',
          eventEmitted: 'change',
          args: 'green'
        }
      ]);
    });
  });
});

describe('Base Events: Multilevel Keys', function() {
{'fraction_non_alphanumeric': 0.14095950477173072, 'fraction_numerical': 0.0026437967500644827, 'mean_word_length': 2.337421992683452, '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': '4252409', 'n_tokens_mistral': 4661, 'n_tokens_neox': 4461, 'n_words': 1071}