<commit_before>import {assert} from 'chai';

import augment from '../../src/mdc-knockout-augment';

function koMock () {
  return {
    bindingHandlers: {},
    virtualElements: {
      allowedBindings: []
    },
    components: {
      register: function () {}
    }
  }
}

function expectBindings () {
  return [
    'mdc-bindings', 'mdc-attrs', 'mdc-parent-bindings', 'mdc-parent-attrs',
    'mdc-child', 'mdc-ripple', 'mdc-css', 'mdc-attr', 'mdc-instance'
  ];
}

suite('Augment');

test('augment exports registerBindings and registerComponent', () => {
  assert.equal(typeof augment.registerBindings, 'function');
  assert.equal(typeof augment.registerComponent, 'function');
});

test('registerBindings requires knockout as a parameter', () => {
  const ko = koMock();
  assert.throws(augment.registerBindings);
  assert.throws(() => augment.registerBindings({}));
  assert.doesNotThrow(() => augment.registerBindings(ko));
});

test('registerBindings sets a complete list of bindings', () => {
  const ko = koMock();
  augment.registerBindings(ko);

  const handlers = ko.bindingHandlers;
  const bindings = expectBindings();

  bindings.forEach(binding => {
    if (handlers[binding]) {
      delete handlers[binding];
    }
    else {
      handlers[binding] = 'is missing';
    }
  });
  assert.deepEqual(handlers, {});
});

test('registerBindings bounces multiple calls', () => {
  const ko = koMock();
  const handlers = ko.bindingHandlers;

  augment.registerBindings(ko);
  Object.keys(handlers).forEach(binding => handlers[binding] = 1);
  augment.registerBindings(ko);

  const nonOnes = [];
  Object.keys(handlers).forEach(binding => {
    if (handlers[binding] !== 1) {
      nonOnes.push(binding);
    }
  });

  assert.equal(nonOnes.length, 0);
});<commit_msg>test(augment): Test registerComponent<commit_after>import {assert} from 'chai';
import td from 'testdouble';
import bel from 'bel';
import augment from '../../src/mdc-knockout-augment';

import {utils} from 'knockout';

function koMock () {
  return {
    bindingHandlers: {},
    virtualElements: {
      allowedBindings: []
    },
    components: {
      register: function () {}
    },
    utils: utils
  }
}



function expectBindings () {
  return [
    'mdc-bindings', 'mdc-attrs', 'mdc-parent-bindings', 'mdc-parent-attrs',
    'mdc-child', 'mdc-ripple', 'mdc-css', 'mdc-attr', 'mdc-instance'
  ];
}

suite('Augment');

test('augment exports registerBindings and registerComponent', () => {
  assert.equal(typeof augment.registerBindings, 'function');
  assert.equal(typeof augment.registerComponent, 'function');
});

test('registerBindings requires knockout as a parameter', () => {
  const ko = koMock();
  assert.throws(augment.registerBindings);
  assert.throws(() => augment.registerBindings({}));
  assert.doesNotThrow(() => augment.registerBindings(ko));
});

test('registerBindings sets a complete list of bindings', () => {
  const ko = koMock();
  augment.registerBindings(ko);

  const handlers = ko.bindingHandlers;
  const bindings = expectBindings();

  bindings.forEach(binding => {
    if (handlers[binding]) {
      delete handlers[binding];
    }
    else {
      handlers[binding] = 'is missing';
    }
  });
  assert.deepEqual(handlers, {});
});

test('registerBindings bounces multiple calls', () => {
  const ko = koMock();
  const handlers = ko.bindingHandlers;

  augment.registerBindings(ko);
  Object.keys(handlers).forEach(binding => handlers[binding] = 1);
  augment.registerBindings(ko);

  const nonOnes = [];
  Object.keys(handlers).forEach(binding => {
    if (handlers[binding] !== 1) {
      nonOnes.push(binding);
    }
  });

  assert.equal(nonOnes.length, 0);
});

test('registerComponent requires knockout as the first parameter', () => {
  const ko = koMock();
  assert.throws(augment.registerComponent);
  assert.throws (() => augment.registerComponent({}));
  assert.doesNotThrow(() => augment.registerComponent(ko));
});

test('registerComponent registers a component with a name passed as the second argument', () => {
  const ko = koMock();
  const register = td.function();
  ko.components.register = register;

  augment.registerComponent(ko, 'component_name');
  td.verify(register('component_name', td.matchers.anything()));
});

test('registerComponent uses the third argument as the component template', () => {
  const ko = koMock();
  const register = td.function();
  ko.components.register = register;

  const templateText = 'template text';
  augment.registerComponent(ko, '', templateText);
  td.verify(register('', td.matchers.contains({template: templateText})));
});

test('registerComponent uses a createViewModel factory for the component', () => {
  const ko = koMock();
  const register = td.function();
  ko.components.register = register;

  augment.registerComponent(ko, '');
  td.verify(register('', td.matchers.argThat(obj => {
    if (obj !== null && typeof obj === 'object'){
      if ('viewModel' in obj) {
        if ('createViewModel' in obj.viewModel) {
          if (typeof obj.viewModel.createViewModel == 'function') {
            return true;
          }
        }
      }
    }
    return false;
  })));
});

test('createViewModel instantiates a viewmodel class passed as the fourth argument ' +
     'and returns the instance', () => {
  const ko = koMock();
  const ViewModelConstructor = td.function();

  ko.components.register = (name, obj) => {
    const element = bel`<div></div>`;
    const vm = obj.viewModel.createViewModel('params', {element: element});
    assert.instanceOf(vm, ViewModelConstructor);
  };

  augment.registerComponent(ko, '', '', ViewModelConstructor);
  td.verify(ViewModelConstructor(), {ignoreExtraArgs: true});
});

test('registerComponent processes attributes properly', () => {
  const ko = koMock();
  const ViewModelConstructor = td.function();
  const element = bel`
    <div id="el_1" attr1 attr2="something"
         class="many class names"
         params="param1: val1, param2: val2"
         data-bind="visible: true">
    </div>
  `;

  ko.components.register = (name, obj) => {
    obj.viewModel.createViewModel('paramsStub', {element: element});
  };

  augment.registerComponent(ko, '', '', ViewModelConstructor);
  assert.equal(element.attributes.length, 3);
  assert.equal(element.attributes.getNamedItem('class').value, 'many class names');
  assert.equal(element.attributes.getNamedItem('params').value, 'param1: val1, param2: val2');
  assert.equal(element.attributes.getNamedItem('data-bind').value, 'visible: true');
});

test('registerComponent passes right arguments to the viewmodel constructor', () => {
  const ko = koMock();
  const ViewModelConstructor = td.function();
  const element = bel`<div id="el_1" attr1 attr2="something"></div>`;

  ko.components.register = (name, obj) => {
    obj.viewModel.createViewModel('paramsStub', {element: element});
  };

  augment.registerComponent(ko, '', '', ViewModelConstructor,
                            'MDCComponentStub', 'MDCFoundationStub');
  td.verify(ViewModelConstructor(element,
                                'paramsStub',
                                {id: 'el_1', attr1: 'attr1', attr2: 'something'},
                                'MDCComponentStub', 'MDCFoundationStub'));
});

test('', () => {

});
{'fraction_non_alphanumeric': 0.1617768028516589, 'fraction_numerical': 0.0034274746366876885, 'mean_word_length': 3.866577718478986, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 41, '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': '1847807', 'n_tokens_mistral': 2220, 'n_tokens_neox': 2168, 'n_words': 462}