Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jQuery.ajax.restore(); doesn't work #2001

Closed
76784 opened this issue Mar 31, 2019 · 8 comments
Closed

jQuery.ajax.restore(); doesn't work #2001

76784 opened this issue Mar 31, 2019 · 8 comments

Comments

@76784
Copy link
Contributor

76784 commented Mar 31, 2019

On this documentaion page (see "Testing Ajax"), the following example is provided:

The following function triggers network activity:

function getTodos(listId, callback) {
    jQuery.ajax({
        url: '/todo/' + listId + '/items',
        success: function (data) {
            // Node-style CPS: callback(err, data)
            callback(null, data);
        }
    });
}

To test this function without triggering network activity we could replace jQuery.ajax

after(function () {
    // When the test either fails or passes, restore the original
    // jQuery ajax function (Sinon.JS also provides tools to help
    // test frameworks automate clean-up like this)
    jQuery.ajax.restore();
});
it('makes a GET request for todo items', function () {
    sinon.replace(jQuery, 'ajax', sinon.fake());
    getTodos(42, sinon.fake());

    assert(jQuery.ajax.calledWithMatch({ url: '/todo/42/items' }));
});

When testing this using the following setup, jQuery.ajax.restore(); returns "TypeError: jQuery.ajax.restore is not a function". Here is the setup:
package.json

  "devDependencies": {
    "browserify": "^16.2.3",
    "jquery": "^3.3.1",
    "mocha": "^6.0.2",
    "sinon": "^7.3.0"
  }

testrunner.html

<body>
	<div id="mocha"></div>
	<script src="node_modules/mocha/mocha.js"></script>
	<script src="node_modules/sinon/pkg/sinon.js"></script>
	<script>mocha.setup('bdd')</script>
	<script src="bundle.js"></script>
	<script>
		mocha.run();
	</script>
</body>

test.js

var assert = require('assert'); 
var jQuery = require('jquery');

function getTodos(listId, callback) {
  jQuery.ajax({
      url: '/todo/' + listId + '/items',
      success: function (data) {
          // Node-style CPS: callback(err, data)
          callback(null, data);
      }
  });
}
describe('getTodos()', function() {
  //https://sinonjs.org/#get-started
  //Testing Ajax
  after(function () {
      jQuery.ajax.restore(); // returns "TypeError: jQuery.ajax.restore is not a function"
  });
  it('makes a GET request for todo items', function () {
      sinon.replace(jQuery, 'ajax', sinon.fake());
      getTodos(42, sinon.fake());

      assert(jQuery.ajax.calledWithMatch({ url: '/todo/42/items' }));
  });
});

run: browserify test.js --outfile bundle.js

@76784
Copy link
Contributor Author

76784 commented Mar 31, 2019

I just saw on another thread that sinon.restore() is deprecated. I'll edit the question to remove that.

@76784 76784 changed the title jQuery.ajax.restore(); doesn't work, but sinon.restore(); does jQuery.ajax.restore(); doesn't work Mar 31, 2019
@76784
Copy link
Contributor Author

76784 commented Apr 1, 2019

I just saw on another thread that sinon.restore() is deprecated. I'll edit the question to remove that.

So actually, I'm not sure where I saw the thread that said this, and I may be wrong, because I see sinon.restore() in the documentation. Whatever the case, I'll leave sinon.restore() to the side for the moment.

@mantoni
Copy link
Member

mantoni commented Apr 1, 2019

To clarify: sinon.restore() used to have a different meaning, was deprecated and then removed. Since v5, the sinon object is a default sandbox instance, which comes with a restore function. Confusing history, I know.

@fatso83
Copy link
Contributor

fatso83 commented Apr 1, 2019

Hi, this is a bug in the documentation. It has happened when someone updated the examples from using sinon.stub() to using the new fakes concept. Fakes do not have a restore method, so you need to restore using the sandbox, which means instead of ajax.restore(), it should be sinon.restore().

Would you like to do the update of the docs?

@76784
Copy link
Contributor Author

76784 commented Apr 2, 2019

Sure, I can update it. I just tried pushing but get a 403 error. Is there a way I can get permission? Do you need a feature branch for your pull requests?

@mroderick
Copy link
Member

Sure, I can update it. I just tried pushing but get a 403 error. Is there a way I can get permission? Do you need a feature branch for your pull requests?

You'll need to fork the repository, and make the pull request from your fork

@fatso83
Copy link
Contributor

fatso83 commented Apr 2, 2019

@mantoni
Copy link
Member

mantoni commented Apr 3, 2019

Addressed by #2004.

@mantoni mantoni closed this as completed Apr 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants