/*
  backgrid
  http://github.com/wyuenho/backgrid

  Copyright (c) 2013 Jimmy Yuen Ho Wong
  Licensed under the MIT @license.
*/
describe("A SelectRowCell", function () {

  var collection;
  var model;
  var cell;

  beforeEach(function () {
    model = new Backbone.Model();
    collection = new Backbone.Collection();
    collection.add(model);

    cell = new Backgrid.Extension.SelectRowCell({
      model: model,
      column: {
        name: "",
        cell: "select-row"
      }
    });

    cell.render();
  });

  describe("when used with Backbone.Collection", function () {

    it("renders a checkbox", function () {
      expect(cell.$el.find(":checkbox").length).toBe(1);
    });

    it("triggers a Backbone `backgrid:selected` event when the checkbox is checked", function () {
      var selectedTriggered = false;
      model.on("backgrid:selected", function () {
        selectedTriggered = true;
      });
      cell.$el.find(":checkbox").prop("checked", true).change();
      expect(selectedTriggered).toBe(true);
    });

    it("checks or unchecks its checkbox when the model triggers a Backbone `backgrid:select` event", function () {
      model.trigger("backgrid:select", model, true);
      expect(cell.$el.find(":checkbox").prop("checked")).toBe(true);
      model.trigger("backgrid:select", model, false);
      expect(cell.$el.find(":checkbox").prop("checked")).toBe(false);
    });

  });

});

describe("A SelectAllHeaderCell", function () {

  var collection;
  var cell;

  beforeEach(function () {
    collection = new Backbone.Collection([{id: 1}, {id: 2}]);
    cell = new Backgrid.Extension.SelectAllHeaderCell({
      collection: collection,
      column: {
        headerCell: "select-all",
        cell: "select-row",
        name: ""
      }
    });

    cell.render();
  });

  it("triggers a `backgrid:select` event on each model when its checkbox is checked", function () {
    var selectTriggerArgs = [];
    collection.on("backgrid:select", function () {
      selectTriggerArgs.push(Array.prototype.slice.apply(arguments));
    });

    cell.$el.find(":checkbox").prop("checked", true).change();
    expect(selectTriggerArgs.length).toBe(2);
    expect(selectTriggerArgs[0][0]).toBe(collection.at(0));
    expect(selectTriggerArgs[0][1]).toBe(true);
    expect(selectTriggerArgs[1][0]).toBe(collection.at(1));
    expect(selectTriggerArgs[1][1]).toBe(true);
  });

  it("unchecks itself when a model triggers a `backgrid:selected` event with a false value", function () {
    cell.$el.find(":checkbox").prop("checked", true).change();
    collection.at(0).trigger("backgrid:selected", collection.at(0), false);
    expect(cell.$el.find(":checkbox").prop("checked"), false);
  });

  it("will trigger a `backgrid:select` event on each previously selected model after a `backgrid:refresh` event", function () {
    var ids1 = '';
    collection.on("backgrid:select", function (model) {
      ids1 = ids1 + model.id;
      model.trigger("backgrid:selected", model, true);
    });
    cell.$el.find(":checkbox").prop("checked", true).change();
    collection.off("backgrid:select");

    var ids2 = '';
    collection.on("backgrid:select", function (model) {
      ids2 = ids2 + model.id;
    });
    Backbone.trigger("backgrid:refresh");
    collection.off("backgrid:select");

    expect(ids1).not.toBe('');
    expect(ids2).not.toBe('');
    expect(ids1).toBe(ids2);
  });

});

@@ -3407,20 +3407,899 @@
 e(ids2);%0A  %7D);%0A%0A%7D);%0A
+%0Adescribe(%22Grid#getSelectedModels%22, function () %7B%0A%0A  it(%22will be attached to Backgrid.Grid's prototype%22, function () %7B%0A    expect(typeof Backgrid.Grid.prototype.getSelectedModels).toBe(%22function%22);%0A  %7D);%0A%0A  it(%22will return a list of selected models%22, function () %7B%0A    var collection = new Backbone.Collection(%5B%7Bid: 1%7D, %7Bid: 2%7D%5D);%0A%0A    var grid = new Backgrid.Grid(%7B%0A      collection: collection,%0A      columns: %5B%7B%0A        name: %22%22,%0A        cell: %22select-row%22,%0A        headerCell: %22select-all%22%0A      %7D, %7B%0A        name: %22id%22,%0A        cell: %22integer%22%0A      %7D%5D%0A    %7D);%0A%0A    grid.render();%0A%0A    collection.each(function (model) %7B%0A      model.trigger(%22backgrid:selected%22, model, true);%0A    %7D);%0A%0A    var selectedModels = grid.getSelectedModels();%0A    expect(selectedModels.length).toBe(2);%0A    expect(selectedModels%5B0%5D.id).toBe(1);%0A    expect(selectedModels%5B1%5D.id).toBe(2);%0A  %7D);%0A%0A%7D);%0A

Add convenient method on grid to retrieve selected models