<commit_before>/* global app, chai, describe, it, sinon, beforeEach, afterEach,
   ChatApp, $ */

/* jshint expr:true */
var expect = chai.expect;

describe('TextChatView', function() {
  "use strict";
  var chatApp, media, sandbox, call;

  beforeEach(function() {
    $('body').append([
      '<div id="textchat">',
      '  <ul></ul>',
      '  <form><input name="message"></form>',
      '</div>'
    ].join(''));
    sandbox = sinon.sandbox.create();
    sandbox.stub(navigator, "mozGetUserMedia");
    sandbox.stub(window, "mozRTCPeerConnection").returns({
      createDataChannel: function() {}
    });

    // This stops us changing the document's title unnecessarily
    sandbox.stub(app.views.ChatView.prototype, "initialize");

    media = new app.models.WebRTCCall();
    call = new app.models.Call({}, {media: media});
    chatApp = new ChatApp();
    app.data.user.set("nick", "niko");
  });

  afterEach(function() {
    $('#textchat').remove();
    sandbox.restore();
    media = null;
  });

  it("should be empty by default", function() {
    var view = new app.views.TextChatView({
      call: call,
      collection: new app.models.TextChat()
    });
    expect(view.collection).to.have.length.of(0);
    view.render();
    expect(view.$('ul').html()).to.equal('');
  });

  it("should update rendering when its collection is updated", function() {
    var view = new app.views.TextChatView({
      call: call,
      collection: new app.models.TextChat([
        {nick: "niko", message: "plop"},
        {nick: "jb", message: "hello"}
      ])
    });
    expect(view.collection).to.have.length.of(2);

    // check rendered view
    view.render();
    expect(view.$('li')).to.have.length.of(2);

    // add a new message to the conversation
    view.collection.add({nick: "niko", message: "how is it going?"});

    expect(view.collection).to.have.length.of(3);
    expect(view.$('li')).to.have.length.of(3);
  });

  it("should allow the caller to send a first message", function(done) {
    var textChat = chatApp.textChatView.collection;
    app.port.trigger("talkilla.call-start", {caller: "niko", callee: "jb"});
    expect(textChat).to.have.length.of(0);

    textChat.once("add", function(entry) {
      expect(entry).to.be.an.instanceOf(app.models.TextChatEntry);
      expect(entry.get("nick")).to.equal("niko");
      expect(entry.get("message")).to.equal("plop");
      done();
    });

    $('#textchat [name="message"]').val("plop");
    $("#textchat form").trigger("submit");
  });

  describe("Change events", function() {
    var textChatView;

    beforeEach(function() {
      sandbox.stub(call, "on");

      textChatView = new app.views.TextChatView({
        call: call,
        collection: new app.models.TextChat()
      });
    });

    it("should attach to change:state events on the call model", function() {
      sinon.assert.calledOnce(call.on);
      sinon.assert.calledWith(call.on, 'change:state');
    });

    it("should show the element when change:state goes to the pending state",
      function() {
        call.on.args[0][1]("pending");

        expect(textChatView.$el.is(":visible")).to.be.equal(true);
      });

    it("should hide the element when change:state goes to the terminated " +
      "state", function() {
        call.on.args[0][1]("terminated");

        expect(textChatView.$el.is(":visible")).to.be.equal(false);
      });
  });
});
<commit_msg>fixed text chat tests were playing actual sounds<commit_after>/* global app, chai, describe, it, sinon, beforeEach, afterEach,
   ChatApp, $ */

/* jshint expr:true */
var expect = chai.expect;

describe('TextChatView', function() {
  "use strict";
  var chatApp, sandbox, call;

  beforeEach(function() {
    $('body').append([
      '<div id="textchat">',
      '  <ul></ul>',
      '  <form><input name="message"></form>',
      '</div>'
    ].join(''));
    sandbox = sinon.sandbox.create();
    sandbox.stub(navigator, "mozGetUserMedia");
    sandbox.stub(window, "mozRTCPeerConnection").returns({
      createDataChannel: function() {}
    });
    sandbox.stub(window, "Audio").returns({
      play: sinon.spy(),
      pause: sinon.spy()
    });

    // This stops us changing the document's title unnecessarily
    sandbox.stub(app.views.ChatView.prototype, "initialize");

    call = new app.models.Call({}, {media: new app.models.WebRTCCall()});
    chatApp = new ChatApp();
    app.data.user.set("nick", "niko");
  });

  afterEach(function() {
    $('#textchat').remove();
    sandbox.restore();
  });

  it("should be empty by default", function() {
    var view = new app.views.TextChatView({
      call: call,
      collection: new app.models.TextChat()
    });
    expect(view.collection).to.have.length.of(0);
    view.render();
    expect(view.$('ul').html()).to.equal('');
  });

  it("should update rendering when its collection is updated", function() {
    var view = new app.views.TextChatView({
      call: call,
      collection: new app.models.TextChat([
        {nick: "niko", message: "plop"},
        {nick: "jb", message: "hello"}
      ])
    });
    expect(view.collection).to.have.length.of(2);

    // check rendered view
    view.render();
    expect(view.$('li')).to.have.length.of(2);

    // add a new message to the conversation
    view.collection.add({nick: "niko", message: "how is it going?"});

    expect(view.collection).to.have.length.of(3);
    expect(view.$('li')).to.have.length.of(3);
  });

  it("should allow the caller to send a first message", function(done) {
    var textChat = chatApp.textChatView.collection;
    app.port.trigger("talkilla.call-start", {caller: "niko", callee: "jb"});
    expect(textChat).to.have.length.of(0);

    textChat.once("add", function(entry) {
      expect(entry).to.be.an.instanceOf(app.models.TextChatEntry);
      expect(entry.get("nick")).to.equal("niko");
      expect(entry.get("message")).to.equal("plop");
      done();
    });

    $('#textchat [name="message"]').val("plop");
    $("#textchat form").trigger("submit");
  });

  describe("Change events", function() {
    var textChatView;

    beforeEach(function() {
      sandbox.stub(call, "on");

      textChatView = new app.views.TextChatView({
        call: call,
        collection: new app.models.TextChat()
      });
    });

    it("should attach to change:state events on the call model", function() {
      sinon.assert.calledOnce(call.on);
      sinon.assert.calledWith(call.on, 'change:state');
    });

    it("should show the element when change:state goes to the pending state",
      function() {
        call.on.args[0][1]("pending");

        expect(textChatView.$el.is(":visible")).to.be.equal(true);
      });

    it("should hide the element when change:state goes to the terminated " +
      "state", function() {
        call.on.args[0][1]("terminated");

        expect(textChatView.$el.is(":visible")).to.be.equal(false);
      });
  });
});

{'fraction_non_alphanumeric': 0.1875810401959372, 'fraction_numerical': 0.0028814291888776835, 'mean_word_length': 3.1223277909738716, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 17, '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': '2751925', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2220, 'n_words': 450}