How to unit test with Jasmine on multiple chained functions with returns?Ask Questions

 Posted on 07/14/2018

I have the following function:

/**
* filters array down to the given allowed keys
* @param {Object} data
* @param {String[]} allowedKeys
*/
$scope.filterData = function(data, allowedKeys) {
    return Object.keys(data)
        .filter(function(key) {
            return allowedKeys.includes(key);
        })
        .reduce(function(obj, key) {
            obj[key] = data[key];
            return obj;
        }, {});
};

that I want to create a unit test for and so far I have the following:

describe('$scope.filterData', function() { 
        //params
        var data = {
            key1: "value1",
            key2: "value2",
            key3: "value3"
        }
        var allowedKeys = ["key1", "key2"];
        //mockobject
        var $Object = jasmine.createSpyObj('Object', ['keys', 'filter', 'reduce']);

it
('should func', function() {
$Object
.keys.and.returnValue($Object); $Object.filter.and.returnValue($Object); $Object.reduce.and.returnValue($Object);
$scope
.filterData(data, allowedKeys); expect(Object.filter).toHaveBeenCalled(); }); });

The issue that I am having, is that I am getting the following error:

TypeError: undefined is not a constructor (evaluating 'allowedKeys.includes(key)')

I do not understand, how to fix that error?

Share On: facebook gplus twitter
profile
Asked by ketan Linbachiya on 07/14/2018 Score: 269 points
Add Comment:

Comments

0 Answers

0 Corrected Answers

Post Your Answers

Existing Members

Sign in to your account
Email Address
Password
New Member?
Sign up and complete profile
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.