<commit_before>describe("ConfirmableView", function() {
  var view, link

  beforeEach(function() {
    loadFixtures('confirmable_view.html')

    link = $('a#confirmable')
    view = new ConfirmableView({ el: link })
  });

  afterEach(function () {
    $('.modal, .modal-backdrop').remove()
  })

  describe("when it is clicked", function() {
    it("renders a modal", function() {
      spyOn(view.modal, "render")

      link.click()

      expect(view.modal.render).toHaveBeenCalled()
    });

    describe("when the modal is confirmed", function() {
      var request

      beforeEach(function() {
        jasmine.Ajax.useMock()
      });

      it("triggers a confirmable:confirm event on the link", function() {
        var confirmed = false
        link.on('confirmable:confirm', function () {
          confirmed = true
        })

        link.click()

        waitsFor(function () {
          return $('#confirmable-modal').is(':visible')
        }, 500)

        runs(function () {
          $('#confirmable-modal').find('[data-confirmable-confirm]').click()

          expect(confirmed).toBeTruthy()
        })
      });

      it("sends an ajax DELETE request with the link href as URL", function() {
        link.click()

        waitsFor(function () {
          return $('#confirmable-modal').is(':visible')
        }, 500)

        runs(function () {
          $('#confirmable-modal').find('[data-confirmable-confirm]').click()

          request = mostRecentAjaxRequest();
          request.response({status: 200, responseText: ''})

          expect(request.url).toBe(link.attr('href'))
          expect(request.method).toBe('DELETE')
        })
      });

      it("triggers a confirmable:confirmed event on the link", function() {
        var confirmed = false
        link.on('confirmable:confirmed', function (event, data) { confirmed = data })

        link.click()

        waitsFor(function () {
          return $('#confirmable-modal').is(':visible')
        }, 500)

        runs(function () {
          $('#confirmable-modal').find('[data-confirmable-confirm]').click()

          var data = { some: 'data' }

          request = mostRecentAjaxRequest();
          request.response({status: 200, responseText: JSON.stringify(data)})

          expect(confirmed.some).toBe(data.some)
        })
      });

      it("sends a ajax request with the given method on the link", function() {
        link = $('a#confirmable-post')
        view = new ConfirmableView({ el: link })

        link.click()

        waitsFor(function () {
          return $('#confirmable-modal').is(':visible')
        }, 500)

        runs(function () {
          $('#confirmable-modal').find('[data-confirmable-confirm]').click()

          request = mostRecentAjaxRequest();
          request.response({status: 200, responseText: ''})

          expect(request.url).toBe(link.attr('href'))
          expect(request.method).toBe('POST')
        })
      });
    });

    describe("with custom labels", function() {
      var labels

      beforeEach(function() {
        link = $('a#confirmable-custom-labels')<commit_msg>Adding specs for when an error happen while confirming in ConfirmableView<commit_after>describe("ConfirmableView", function() {
  var view, link

  beforeEach(function() {
    loadFixtures('confirmable_view.html')

    link = $('a#confirmable')
    view = new ConfirmableView({ el: link })
  });

  afterEach(function () {
    $('.modal, .modal-backdrop').remove()
  })

  describe("when it is clicked", function() {
    it("renders a modal", function() {
      spyOn(view.modal, "render")

      link.click()

      expect(view.modal.render).toHaveBeenCalled()
    });

    describe("when the modal is confirmed", function() {
      var request

      beforeEach(function() {
        jasmine.Ajax.useMock()
      });

      it("triggers a confirmable:confirm event on the link", function() {
        var confirmed = false
        link.on('confirmable:confirm', function () {
          confirmed = true
        })

        link.click()

        waitsFor(function () {
          return $('#confirmable-modal').is(':visible')
        }, 500)

        runs(function () {
          $('#confirmable-modal').find('[data-confirmable-confirm]').click()

          expect(confirmed).toBeTruthy()
        })
      });

      it("sends an ajax DELETE request with the link href as URL", function() {
        link.click()

        waitsFor(function () {
          return $('#confirmable-modal').is(':visible')
        }, 500)

        runs(function () {
          $('#confirmable-modal').find('[data-confirmable-confirm]').click()

          request = mostRecentAjaxRequest();
          request.response({status: 200, responseText: ''})

          expect(request.url).toBe(link.attr('href'))
          expect(request.method).toBe('DELETE')
        })
      });

      describe("with a different method on the link", function() {
        it("sends a ajax request with the given method on the link", function() {
          link = $('a#confirmable-post')
          view = new ConfirmableView({ el: link })

          link.click()

          waitsFor(function () {
            return $('#confirmable-modal').is(':visible')
          }, 500)

          runs(function () {
            $('#confirmable-modal').find('[data-confirmable-confirm]').click()

            request = mostRecentAjaxRequest();
            request.response({status: 200, responseText: ''})

            expect(request.url).toBe(link.attr('href'))
            expect(request.method).toBe('POST')
          })
        });
      });

      describe("after the ajax call returns", function() {
        describe("successfully", function() {
          it("triggers a confirmable:confirmed event on the link", function() {
            var confirmed = false
            link.on('confirmable:confirmed', function (event, data) { confirmed = data })

            link.click()

            waitsFor(function () {
              return $('#confirmable-modal').is(':visible')
            }, 500)

            runs(function () {
              $('#confirmable-modal').find('[data-confirmable-confirm]').click()

              var data = { some: 'data' }

              request = mostRecentAjaxRequest();
              request.response({status: 200, responseText: JSON.stringify(data)})

              expect(confirmed.some).toBe(data.some)
            })
          });
        });

        describe("with an error", function() {
          it("does triggers a confirmable:error event on the link", function() {
            var error = false
            link.on('confirmable:error', function (event, jqXHR) { error = $.parseJSON(jqXHR.responseText) })

            link.click()

            waitsFor(function () {
              return $('#confirmable-modal').is(':visible')
            }, 500)

            runs(function () {
              $('#confirmable-modal').find('[data-confirmable-confirm]').click()

              var data = { some: 'data' }

              request = mostRecentAjaxRequest();
              request.response({status: 500, responseText: JSON.stringify(data)})

              expect(error.some).toBe(data.some)
            })
          });
        });
      });
    });

    describe("with custom labels", function() {
      var labels

      beforeEach(function() {
        link = $('a#confirmable-custom-labels')
{'fraction_non_alphanumeric': 0.15544741822114086, 'fraction_numerical': 0.006488240064882401, 'mean_word_length': 2.2002595155709344, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3818361', 'n_tokens_mistral': 2097, 'n_tokens_neox': 1952, 'n_words': 397}