JSFiddle - React, Tailwind, and code Playground

by michaelschmid

HTML

Ajax Fake Responses with sinon

JavaScript

var server;

          describe("Catch ajax request and test Url", function () {
              
              before(function () { server = sinon.fakeServer.create(); });
              after(function () { server.restore(); });

              it("should be google", function () {

                  jQuery.ajax({
                      url: "www.google.de",
                      success: function (data) {
                          callback(null, data);
                      }
                  });
                  // test the issued url against it expectation
                  expect(server.requests[0].url).to.be("www.google.de");
              });
          });


          describe("Test how many ajax calls where issued", function () {

              var server;
              before(function () { server = sinon.fakeServer.create(); });
              after(function () { server.restore(); });

              // test code
              var testF = function () {

                  jQuery.ajax({ url: "www.google.de" });
                  jQuery.ajax({ url: "www.google.de" });
                  jQuery.ajax({ url: "www.google.de" });
                  jQuery.ajax({ url: "www.google.de" });
              };

              it("should called 4 times to google", function () {
                  testF();
                  // test the issued url against it expectation
                  expect(server.requests.length).to.be(4);
              });
          });


          describe("Test the Ajax request and provide a response", function () {

              before(function () { server = sinon.fakeServer.create(); });
              after(function () { server.restore(); });

              it("of $.ajax", function () {

                  var callback = sinon.spy();

                  $.support.cors = true;
                  jQuery.ajax({
                      url: "http://localhost:10001/api/sm5/report/testreportparameter",
                      success: function (data) {
              ...