How to call $(document).ready(function() {…}); from another file JS
Asked Answered
C

1

1

I would like to know how to call the function below without refactoring from another js file.

$(document).ready(function() {

  check();

  function check () {
    setTimeout(function(){
      location.reload(true);
    }, 10000);
  }

});

I saw a question exist for this issue very old one but I cannot understand how to use the answer for my function. StackOverflow answer from another question

I would like to see an example with my function and the proposed solution from the link.

My example which does not work correctly:

//= require rspec_helper
//= require background_index
//= require sinon



describe("Background Index, timeout after 10s", function() {
  // Tweak for testing
  var doc_ready = $.readyList[0]();

  it ("Reload the location", function(){
    clock = sinon.useFakeTimers();

    var check = doc_ready.check();
    var set_timeout = doc_ready.setTimeout();
    /*var stub_check = sinon.stub(check, "check");
    var stub_timeout = sinon.stub(timeout, "setTimeout");*/


    timedOut = false;



    setTimeout(function () {
      timedOut = true;
    }, 1000);


    timedOut.should.be.false;
    clock.tick(10010);
    timedOut.should.be.true;

    clock.restore();
  });

});
Cosmorama answered 21/8, 2018 at 11:10 Comment(9)
This works just fine, you just have to make sure that you load in jQuery before the other JS file.Mantooth
You can't call that, it isn't a function. $(document).ready(...) is a function call.Taryntaryne
So I need to refactor? But the answer I post is saying is possible. Can you please explain to me to understand.Cosmorama
@Cosmorama — I don't understand what you are trying to achieve.Taryntaryne
@Taryntaryne what I need is to access this function for test it without refactoring as my CTO forbidden me any change in source code also after I told him that in this way is not testable and he 40yrs more experience of me so I have this situationCosmorama
Then the question you probably should be asking is "How do I mock the things I need to mock in order to prove some specific thing?"Taryntaryne
Maybe but still need to access it for be able to mockCosmorama
@Cosmorama - This might be an opportunity to show initiative by giving your CTO a working refactoring for approval. It seems to me that such a refactoring would be minor.Win
@Cosmorama - How many calls to document.ready do you think exist in your application because the order will matter.Win
A
0

This is re-written from the answer you pasted.

$(document).ready(check);

  function check () {
    setTimeout(function(){
      location.reload(true);
    }, 10000);
  }   

// In the test file
TestFile.prototype.testDocumentReadyContents = function () {
  check();
}

A better way would be to include all the JS-files in your HTML with <script src="./path-to-file"> and then just call the functions in the order you want them to be called.

Abercrombie answered 21/8, 2018 at 11:20 Comment(9)
Ok and in the other answer is saying the Hack part to use only TestFile.prototype.testDocumentReadyContents = function () { $.readyList[0](); } If you checked is separate from the other part is that what I do not understand.Cosmorama
What I understood from other answer to use $.readyList[0](); to access it from my JS file where I'm testingCosmorama
@ChaosPandion: can you take a lookCosmorama
@Cosmorama He posts 2 different ways where the one I copied is "the good way". I think you should go for that one. Otherwise just copy the "hacky" part and use it instead of the last 3 lines in my answer.Abercrombie
I cannot refactor the code as I'm forbidden to do so. I cannot understand how with that $.readyList[0]() I could test that JS. I'm missing that part at all.Cosmorama
What is your code supposed to do? It looks like it should refresh the page after 10 secs once the document had loaded? What's the point in that?Abercrombie
The point is that I have a page in my RoR application which shows Background jobs and the reload happening to refresh and show the added new background jobs so that the point of itCosmorama
Aren't you able to add the scripts with "require" in the manifest? What you're asking is a very uncommon practice and with good reason.Abercrombie
I added an example of what I'm trying to doCosmorama

© 2022 - 2024 — McMap. All rights reserved.