<commit_before>/* jshint expr: true */
var
  mdescribe = require('mocha-repeat'),
  expect    = require('chai').expect,
  semver    = require('semver'),
  proxy     = require('proxyquire');

var versions = {
  '0.6.0':   require('../vendor/ractive/ractive-edge.js'),
  '0.5.8':   require('../vendor/ractive/ractive-0.5.8.js'),
  '0.5.5':   require('../vendor/ractive/ractive-0.5.5.js'),
  '0.5.0':   require('../vendor/ractive/ractive-0.5.0.js'),
};

mdescribe("Ractive adaptor", versions, function (Ractive, version) {

  var child, parent, adapt, subchild, user;

  // Load dependencies
  before(function () {
    proxy('../index', { ractive: Ractive });
    adapt = Ractive.adaptors.Ractive;
    Ractive.defaults.adapt = ['Ractive'];
  });

  // Makes tests faster
  before(function () {
    adapt.maxKeyLength = 64;
  });

  /*
   * simple cases
   */

  describe('simple', function () {
    this.timeout(250);

    beforeEach(function () {
      child  = new Ractive();
      parent = new Ractive();
    });

    it(".get picks up the child's value", function () {
      child.set('one', 1);
      parent.set('child', child);

      expect(parent.get('child.one')).eql(1);
    });

    forVersion('> 0.5.0', function () {
      it(".reset on child gets picked up", function () {
        parent.set('child', child);
        child.reset({ ten: 10, eleven: 11 });

        expect(parent.get('child.ten')).eql(10);
        expect(parent.get('child.eleven')).eql(11);
      });
    });

    it('propagates changes from child to parent', function () {
      child.set('two', 1);
      parent.set('child', child);
      child.set('two', 2);

      expect(parent.get('child.two')).eql(2);
    });

    it('fires the "wrap" event', function (next) {
      child.on('wrap', function (_parent, key) {
        expect(key).eql('child');
        next();
      });

      parent.set('child', child);
    });

    it('fires the "unwrap" event after reset', function (next) {
      child.on('unwrap', function (_parent, key) {
        expect(key).eql('child');
        next();
      });

      parent.set('child', child);
      parent.set('child', undefined);
    });

    // ensure that you can observe changes in the child via the parent.
    it('can observe changes of the child', function (next) {
      var runs = 0;
      parent.set('child', child);

      parent.observe('child.three', function (val) {
        runs++;
        if (runs === 1) {
          expect(val).be.undefined;
        }
        else if (runs === 2) {
          expect(val).eql(3);
          next();
        }
      });

      child.set('three', 3);
    });

    it('updates the parent HTML when the child updates', function () {
      parent = new Ractive({
        template: "{{#child}}value={{enabled}}{{/child}}"
      });

      parent.set('child', child);

      child.set('enabled', 1);
      expect(parent.toHTML()).eql('value=1');

      child.set('enabled', 2);
      expect(parent.toHTML()).eql('value=2');
    });

    it('works bidirectionally by propagating parent changes to child', function () {
      parent.set('child', child);
      parent.set('child.enabled', 2);

      expect(child.get('enabled')).eql(2);
    });

    it('works bidirectionally with html', function () {
      child = new Ractive({
        template: "value={{enabled}}"
      });

      expect(child.toHTML()).eql('value=');

      parent.set('child', child);
      expect(child.toHTML()).eql('value=');

      parent.set('child.enabled', 2);
      expect(child.toHTML()).eql('value=2');
    });

    it('handles being reset to an object', function () {
      parent.set('child', child);
      parent.set('child', { enabled: 300 });

      expect(child.get('enabled')).eql(300);
    });

    it('handles being reset to undefined', function () {
      parent.set('child', child);
      parent.set('child', undefined);
    });
  });

  /*
   * deep nesting
   */

  describe('using with deeply-nested cases', function () {
    beforeEach(function () {
      subchild = new Ractive({ data: { name: "subchild" }});
      child    = new Ractive({ data: { name: "child" }});
      parent   = new Ractive({ data: { name: "parent" }});
    });

    it('catches circular dependencies', function () {
      expect(function () {
        parent.set('child', child);
        child.set('subchild', subchild);
        subchild.set('parent', parent);
      }).throw(/circular/);
    });

    describe('organized linearly', function () {
      beforeEach(function () {
        parent.set('child', child);<commit_msg>Add more tests<commit_after>/* jshint expr: true */
var
  mdescribe = require('mocha-repeat'),
  expect    = require('chai').expect,
  semver    = require('semver'),
  proxy     = require('proxyquire');

var versions = {
  '0.6.0':   require('../vendor/ractive/ractive-edge.js'),
  '0.5.8':   require('../vendor/ractive/ractive-0.5.8.js'),
  '0.5.5':   require('../vendor/ractive/ractive-0.5.5.js'),
  '0.5.0':   require('../vendor/ractive/ractive-0.5.0.js'),
};

mdescribe("Ractive adaptor", versions, function (Ractive, version) {

  var child, parent, adapt, subchild, user;

  // Load dependencies
  before(function () {
    proxy('../index', { ractive: Ractive });
    adapt = Ractive.adaptors.Ractive;
    Ractive.defaults.adapt = ['Ractive'];
  });

  // Makes tests faster
  before(function () {
    adapt.maxKeyLength = 64;
  });

  /*
   * simple cases
   */

  describe('simple', function () {
    this.timeout(250);

    beforeEach(function () {
      child  = new Ractive();
      parent = new Ractive();
    });

    it(".get picks up the child's value", function () {
      child.set('one', 1);
      parent.set('child', child);

      expect(parent.get('child.one')).eql(1);
    });

    forVersion('> 0.5.0', function () {
      it(".reset on child gets picked up", function () {
        parent.set('child', child);
        child.reset({ ten: 10, eleven: 11 });

        expect(parent.get('child.ten')).eql(10);
        expect(parent.get('child.eleven')).eql(11);
      });
    });

    it('propagates changes from child to parent', function () {
      child.set('two', 1);
      parent.set('child', child);
      child.set('two', 2);

      expect(parent.get('child.two')).eql(2);
    });

    it('fires the "wrap" event', function (next) {
      child.on('wrap', function (_parent, key) {
        expect(key).eql('child');
        next();
      });

      parent.set('child', child);
    });

    it('fires the "unwrap" event after reset', function (next) {
      child.on('unwrap', function (_parent, key) {
        expect(key).eql('child');
        next();
      });

      parent.set('child', child);
      parent.set('child', undefined);
    });

    // ensure that you can observe changes in the child via the parent.
    it('can observe changes of the child', function (next) {
      var runs = 0;
      parent.set('child', child);

      parent.observe('child.three', function (val) {
        runs++;
        if (runs === 1) {
          expect(val).be.undefined;
        }
        else if (runs === 2) {
          expect(val).eql(3);
          next();
        }
      });

      child.set('three', 3);
    });

    it('updates the parent HTML when the child updates', function () {
      parent = new Ractive({
        template: "{{#child}}value={{enabled}}{{/child}}"
      });

      parent.set('child', child);

      child.set('enabled', 1);
      expect(parent.toHTML()).eql('value=1');

      child.set('enabled', 2);
      expect(parent.toHTML()).eql('value=2');
    });

    it('works bidirectionally by propagating parent changes to child', function () {
      parent.set('child', child);
      parent.set('child.enabled', 2);

      expect(child.get('enabled')).eql(2);
    });

    it('works bidirectionally with html', function () {
      child = new Ractive({
        template: "value={{enabled}}"
      });

      expect(child.toHTML()).eql('value=');

      parent.set('child', child);
      expect(child.toHTML()).eql('value=');

      parent.set('child.enabled', 2);
      expect(child.toHTML()).eql('value=2');
    });

    it('handles being reset to an object', function () {
      parent.set('child', child);
      parent.set('child', { enabled: 300 });

      expect(child.get('enabled')).eql(300);
    });

    it('handles being reset to undefined', function () {
      parent.set('child', child);
      parent.set('child', undefined);
    });

    it('doesnt bleed to instance properties', function () {
      var original = child.template;

      parent.set('child', child);
      parent.set('child.template', 'jade');
      expect(child.template).eql(original);
    });

    it('allows the name "data"', function () {
      parent.set('child', child);
      parent.set('child.data', 'this is data');
      expect(child.get('data')).eql('this is data');
    });

    // https://github.com/rstacruz/ractive-ractive/pull/2/files
    it('proxies to a childs data object, not the instance properties', function () {
      var template = child.template;

      parent.set('child', child);
      parent.set('child.data', 'datum');

      expect(child.get('data')).eql('datum');

      parent.set('child.template', 'templating');

      expect(child.template).equal(template);
      expect(child.get('template')).equal('templating');

      child.template = "<h1>Hello Test</h1>";
      expect(child.get('template')).equal('templating');
      expect(parent.get('child.template')).equal('templating');
    });
  });

  /*
   * deep nesting
   */

  describe('using with deeply-nested cases', function () {
    beforeEach(function () {
      subchild = new Ractive({ data: { name: "subchild" }});
      child    = new Ractive({ data: { name: "child" }});
      parent   = new Ractive({ data: { name: "parent" }});
    });

    it('catches circular dependencies', function () {
      expect(function () {
        parent.set('child', child);
        child.set('subchild', subchild);
        subchild.set('parent', parent);
      }).throw(/circular/);
    });

    describe('organized linearly', function () {
      beforeEach(function () {
        parent.set('child', child);
{'fraction_non_alphanumeric': 0.19335169158143195, 'fraction_numerical': 0.012293469708890637, 'mean_word_length': 2.5793734600492786, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 1, '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': '3771865', 'n_tokens_mistral': 3279, 'n_tokens_neox': 3120, 'n_words': 660}