JavaScript Unit testing logic inside promise callback unit testing logic apps,unit testing logic,unit testing business

I have an ES6 / Aurelia app that I am using jasmine to test. The method I am trying to test looks something like this:

update() {
    let vm = this;
    vm.getData()
        .then((response) => {
            vm.processData(response);
        });
}

Where this.getData is a function that returns a promise.

My spec file looks something like this:

describe('my service update function', () => {
    it('it will call the other functions', () => { 
        myService = new MyService();
        spyOn(myService, 'getData').and.callFake(function() {
            return new Promise((resolve) => { resolve(); });
        });
        spyOn(myService, 'processData').and.callFake(function() { return; });
        myService.update();

        // this one passes
        expect(myService.getData).toHaveBeenCalled();

        // this one fails
        expect(myService.processData).toHaveBeenCalled();
    });
});

I understand why this fails - promises are asynchronous and it hasn't been resolved by the time it hits the expect.

How can I push the promises to resolve from my test so that I can test the code inside the call back?

jsfiddle of failed test: http://jsfiddle.net/yammerade/2aap5u37/6/

Answer:1

I got a workaround running by returning an object that behaves like a promise instead of an actual promise

describe('my service update function', () => {
    it('it will call the other functions', () => { 
        myService = new MyService();
        spyOn(myService, 'getData').and.returnValue({
            then(callback) {
                callback();
            }
        });
        spyOn(myService, 'processData').and.callFake(function() { return; });
        myService.update();

        // this one passes
        expect(myService.getData).toHaveBeenCalled();

        // this one fails
        expect(myService.processData).toHaveBeenCalled();
    });
});

Fiddle here: http://jsfiddle.net/yammerade/9rLrzszm/2/

Is there anything wrong with doing it this way?

Answer:2

Imagine an object like this: var values = { "2": 1, "53": 2, "56": 4, "57": 9, "61": 2, "62": 16, "63": 2, "398": 24, ... } My goal is, to find the 10 object keys,...

Imagine an object like this: var values = { "2": 1, "53": 2, "56": 4, "57": 9, "61": 2, "62": 16, "63": 2, "398": 24, ... } My goal is, to find the 10 object keys,...

  1. object keys with spaces javascript
  2. object keys with condition
  3. object.keys with map
  4. object.keys with index
  5. object.keys with value
  6. object.keys with filter
  7. object.keys with foreach
  8. object with keys
  9. object with keys javascript
  10. object with keys typescript
  11. object with keys react
  12. object with keys to array
  13. object with keys react native
  14. object with keys c#
  15. object keys and values javascript
  16. object keys foreach
  17. object.keys for loop
  18. object keys and values js
  19. object.keys on enum
  20. object.keys for json

I work entering data online to a form that I would like to change the tabbing order but have had no luck using Greasemonkey and Scriptish. I am able to remove tabindexes of -1 to make fields ...

I work entering data online to a form that I would like to change the tabbing order but have had no luck using Greasemonkey and Scriptish. I am able to remove tabindexes of -1 to make fields ...

  1. setting tabindex using css
  2. setting tabindex in javascript
  3. setting tabindex in html
  4. setting tabindex in css
  5. setting tabindex in jquery
  6. setting tabindex jquery

I am using Express.js server. With cookie-parser I have opened this endpoint app.get("/s", (req,res) => { res.cookie("bsaSession", req.session.id) res.send("set cookie ok") }) When I ...

I am using Express.js server. With cookie-parser I have opened this endpoint app.get("/s", (req,res) => { res.cookie("bsaSession", req.session.id) res.send("set cookie ok") }) When I ...

I'm trying to draw a map with d3.js. I already did it a first time and it worked, but now I try to split my code to make it easier to read. When i split it i get NaN instead of position in my path. ...

I'm trying to draw a map with d3.js. I already did it a first time and it worked, but now I try to split my code to make it easier to read. When i split it i get NaN instead of position in my path. ...

  1. d3js path data
  2. d3js path d
  3. d3js path drag
  4. d3js draw path