<commit_before>var map = Ember.EnumerableUtils.map;

var dispatcher, wrapper, select;

module("Ember.Select", {
  setup: function() {
    dispatcher = Ember.EventDispatcher.create();
    dispatcher.setup();
    wrapper = Ember.ContainerView.create();
    select = Ember.Select.create();
    wrapper.get('childViews').pushObject(select);
  },

  teardown: function() {
    Ember.run(function() {
      dispatcher.destroy();
      select.destroy();
      wrapper.destroy();
    });
  }
});

function setAndFlush(view, key, value) {
  Ember.run(function() {
    Ember.set(view, key, value);
  });
}

function append() {
  Ember.run(function() {
    wrapper.appendTo('#qunit-fixture');
  });
}

function selectedOptions() {
  var rv = [];
  for(var i=0, len = select.get('content.length'); i < len; ++i) {
    rv.push(select.get('childViews.' + i + '.childViews.0.selected'));
  }
  return rv;
}

test("has 'ember-view' and 'ember-select' CSS classes", function() {
  deepEqual(select.get('classNames'), ['ember-view', 'ember-select']);
});

test("should render", function() {
  append();

  ok(select.$().length, "Select renders");
});

test("should begin disabled if the disabled attribute is true", function() {
  select.set('disabled', true);
  append();

  ok(select.$().is(":disabled"));
});

test("should become disabled if the disabled attribute is changed", function() {
  append();
  ok(select.$().is(":not(:disabled)"));

  Ember.run(function() { select.set('disabled', true); });
  ok(select.$().is(":disabled"));

  Ember.run(function() { select.set('disabled', false); });
  ok(select.$().is(":not(:disabled)"));
});

test("can have options", function() {
  select.set('content', Ember.A([1, 2, 3]));

  append();

  equal(select.$('option').length, 3, "Should have three options");
  equal(select.$().text(), "123", "Options should have content");
});


test("select tabindex is updated when setting tabindex property of view", function() {
  select.set('tabindex', '4');
  append();

  equal(select.$().attr('tabindex'), "4", "renders select with the tabindex");

  select.set('tabindex', '1');

  equal(select.$().attr('tabindex'), "1", "updates select after tabindex changes");
});

test("can specify the property path for an option's label and value", function() {
  select.set('content', Ember.A([
    { id: 1, firstName: 'Yehuda' },
    { id: 2, firstName: 'Tom' }
  ]));

  select.set('optionLabelPath', 'content.firstName');
  select.set('optionValuePath', 'content.id');

  append();

  equal(select.$('option').length, 2, "Should have two options");
  equal(select.$().text(), "YehudaTom", "Options should have content");
  deepEqual(map(select.$('option').toArray(), function(el) { return Ember.$(el).attr('value'); }), ["1", "2"], "Options should have values");
});

test("can retrieve the current selected option when multiple=false", function() {
  var yehuda = { id: 1, firstName: 'Yehuda' },
      tom = { id: 2, firstName: 'Tom' };
  select.set('content', Ember.A([yehuda, tom]));

  append();

  equal(select.get('selection'), yehuda, "By default, the first option is selected");

  select.$()[0].selectedIndex = 1; // select Tom
  select.$().trigger('change');

  equal(select.get('selection'), tom, "On change, the new option should be selected");
});

test("can retrieve the current selected options when multiple=true", function() {
  var yehuda = { id: 1, firstName: 'Yehuda' },
      tom = { id: 2, firstName: 'Tom' },
      david = { id: 3, firstName: 'David' },
      brennain = { id: 4, firstName: 'Brennain' };
  select.set('content', Ember.A([yehuda, tom, david, brennain]));
  select.set('multiple', true);
  select.set('optionLabelPath', 'content.firstName');

  append();

  deepEqual(select.get('selection'), [], "By default, nothing is selected");

  select.$('option').each(function() {
    if (this.value === 'Tom' || this.value === 'David') {
      this.selected = true;
    }
  });

  select.$().trigger('change');<commit_msg>More explicit value setting for Select tests<commit_after>var map = Ember.EnumerableUtils.map;

var dispatcher, wrapper, select;

module("Ember.Select", {
  setup: function() {
    dispatcher = Ember.EventDispatcher.create();
    dispatcher.setup();
    wrapper = Ember.ContainerView.create();
    select = Ember.Select.create();
    wrapper.get('childViews').pushObject(select);
  },

  teardown: function() {
    Ember.run(function() {
      dispatcher.destroy();
      select.destroy();
      wrapper.destroy();
    });
  }
});

function setAndFlush(view, key, value) {
  Ember.run(function() {
    Ember.set(view, key, value);
  });
}

function append() {
  Ember.run(function() {
    wrapper.appendTo('#qunit-fixture');
  });
}

function selectedOptions() {
  var rv = [];
  for(var i=0, len = select.get('content.length'); i < len; ++i) {
    rv.push(select.get('childViews.' + i + '.childViews.0.selected'));
  }
  return rv;
}

test("has 'ember-view' and 'ember-select' CSS classes", function() {
  deepEqual(select.get('classNames'), ['ember-view', 'ember-select']);
});

test("should render", function() {
  append();

  ok(select.$().length, "Select renders");
});

test("should begin disabled if the disabled attribute is true", function() {
  select.set('disabled', true);
  append();

  ok(select.$().is(":disabled"));
});

test("should become disabled if the disabled attribute is changed", function() {
  append();
  ok(select.$().is(":not(:disabled)"));

  Ember.run(function() { select.set('disabled', true); });
  ok(select.$().is(":disabled"));

  Ember.run(function() { select.set('disabled', false); });
  ok(select.$().is(":not(:disabled)"));
});

test("can have options", function() {
  select.set('content', Ember.A([1, 2, 3]));

  append();

  equal(select.$('option').length, 3, "Should have three options");
  equal(select.$().text(), "123", "Options should have content");
});


test("select tabindex is updated when setting tabindex property of view", function() {
  select.set('tabindex', '4');
  append();

  equal(select.$().attr('tabindex'), "4", "renders select with the tabindex");

  select.set('tabindex', '1');

  equal(select.$().attr('tabindex'), "1", "updates select after tabindex changes");
});

test("can specify the property path for an option's label and value", function() {
  select.set('content', Ember.A([
    { id: 1, firstName: 'Yehuda' },
    { id: 2, firstName: 'Tom' }
  ]));

  select.set('optionLabelPath', 'content.firstName');
  select.set('optionValuePath', 'content.id');

  append();

  equal(select.$('option').length, 2, "Should have two options");
  equal(select.$().text(), "YehudaTom", "Options should have content");
  deepEqual(map(select.$('option').toArray(), function(el) { return Ember.$(el).attr('value'); }), ["1", "2"], "Options should have values");
});

test("can retrieve the current selected option when multiple=false", function() {
  var yehuda = { id: 1, firstName: 'Yehuda' },
      tom = { id: 2, firstName: 'Tom' };
  select.set('content', Ember.A([yehuda, tom]));

  append();

  equal(select.get('selection'), yehuda, "By default, the first option is selected");

  select.$()[0].selectedIndex = 1; // select Tom
  select.$().trigger('change');

  equal(select.get('selection'), tom, "On change, the new option should be selected");
});

test("can retrieve the current selected options when multiple=true", function() {
  var yehuda = { id: 1, firstName: 'Yehuda' },
      tom = { id: 2, firstName: 'Tom' },
      david = { id: 3, firstName: 'David' },
      brennain = { id: 4, firstName: 'Brennain' };
  select.set('content', Ember.A([yehuda, tom, david, brennain]));
  select.set('multiple', true);
  select.set('optionLabelPath', 'content.firstName');
  select.set('optionValuePath', 'content.firstName');

  append();

  deepEqual(select.get('selection'), [], "By default, nothing is selected");

  select.$('option').each(function() {
    if (this.value === 'Tom' || this.value === 'David') {
      this.selected = true;
    }
  });

  select.$().trigger('change');
{'fraction_non_alphanumeric': 0.20784069695084006, 'fraction_numerical': 0.006471686372121966, 'mean_word_length': 4.353764157228515, 'pattern_counts': {'":': 8, '<': 5, '<?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': '4149925', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2540, 'n_words': 619}