mardi 21 août 2018

Why won't this XMLhttpRequest fire?

MCVE is at this github repo.

I'm working on a javascript application that reads a manifest file when loaded. I'd like to be able to test that the function works. I've got this test function:

it('We can get a manifest file', function(doneFn) {
        manifest=""
        console.log(window.location.pathname);
        console.log("1")
        load_manifest("/Users/joepublic/MCVE/manifest.json");

        console.log("2")
        console.log(manifest.length)
        setTimeout(expect(manifest.length).not.toBeLessThan(1),4000);
        console.log(manifest);
        doneFn();
        });         
    });

and the function I'm testing looks like this:

function load_manifest(filename){
    //go and get a manifest. We're starting with the one in the root of the template for now.
      var req = new XMLHttpRequest();
          console.log("a"); 
      req.open("GET", filename);
      req.send(null);
    req.onreadystatechange = function() {

        if (req.readyState == 4 && req.status == 200) {
           console.log("hello"); 
           console.log(req.responseText); 
           manifest= JSON.parse(req.responseText);
        } else {
            console.log("Problem reading file");
        }
    };
          console.log("b"); 

} 

The output from karma is:

Chrome 68.0.3440 (Mac OS X 10.13.4): Executed 1 of 1 (1 FAILED) ERROR (0.036 secs / 0.01 secs)
21 08 2018 12:27:18.738:INFO [watcher]: Changed file "/Users/joepublic/MCVE/read_data_test.js".
Chrome 68.0.3440 (Mac OS X 10.13.4): Executed 0 of 1 SUCCESS (0 secs / 0 secs)
LOG: '/context.html'
LOG: '1'
LOG: 'a'
LOG: 'b'
LOG: '2'
LOG: 0
LOG: ''
Chrome 68.0.3440 (Mac OS X 10.13.4) Displaying OBF on the index page Test Name property We can get a manifest file FAILED
    Expected 0 not to be less than 1.
        at <Jasmine>
        at UserContext.<anonymous> (read_data_test.js:30:43)
        at <Jasmine>
LOG: 'Problem reading file'
LOG: 'Problem reading file'
LOG: 'Problem reading file'
Chrome 68.0.3440 (Mac OS X 10.13.4): Executed 1 of 1 (1 FAILED) ERROR (0.044 secs / 0.009 secs)

So the onreadystatechange clearly isn't firing. What's going on?

Aucun commentaire:

Enregistrer un commentaire