Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine: how to check that array contains an object?

Tags:

jasmine

This check used to pass:

expect(array).toContain(value)

Array:

[
  {"_t":"user","id":1073970419,"email":"[email protected]","name":"Spectator"},
  {"_t":"user","id":4464992042,"email":"[email protected]","name":"Collaborator"},
  {"_t":"user","id":1978569710,"email":"[email protected]","name":"Manage"}
]

Value:

{"_t":"user","id":1978569710,"email":"[email protected]","name":"Manage"}

But no longer passes. Whats the new way to write the same test?

like image 583
hakunin Avatar asked Sep 09 '16 12:09

hakunin


People also ask

How do I check if an object contains an array?

isArray() method is used to check if an object is an array. The Array. isArray() method returns true if an object is an array, otherwise returns false .

How do I test an array in Jasmine?

Array ChecksarrayContaining method to check the content of the array. For example, we can write: describe("jasmine. arrayContaining", function () { let foo; beforeEach(function () { foo = [1, 2, 3]; }); it("matches arrays with some of the values", function () { expect(foo).

How to check if an array contains an object or not?

There are various methods to check an array includes an object or not. Using includes () Method: If array contains an object/element can be determined by using includes () method. This method returns true if the array contains the object/element else return false.

How to check if every element of the first array exists?

In this post, we will look at different ways to check if every element of the first array exists in the second array. We can use the Array.prototype.every () method (which was introduced in ES5) to check whether all elements in the array pass the test implemented by the provided function.

How does the array find method work?

The function we passed to the Array.find method gets called with each array element, until it returns a truthy value or iterates over all array elements.

What is the difference between array find and array findindex?

The Array.findIndex method is very similar to the Array.find method, but returns the index of the element that satisfies the conditional check, and not the element itself. The Array.findIndex method calls its callback function with each element in the array until a truthy value is returned or the values in the array are exhausted.


3 Answers

The syntax you need is:

const obj = {"_t":"user","id":1978569710,"email":"[email protected]","name":"Manage"};

expect(array).toContain(jasmine.objectContaining(obj));

See fiddle: https://jsfiddle.net/bblackwo/4o5u5Lmo/16/

like image 103
BBlackwo Avatar answered Jan 03 '23 02:01

BBlackwo


It won't contain that object (remember, two objects with the same properties are not the same object for the purposes of equality), so toContain will never pass.

You need to use another test, like toEqual or (if you only want to check for a subset of properties), toEqual combined with jasmine.objectContaining.

Here's the toEqual example from the Jasmine documentation on that page:

describe("The 'toEqual' matcher", function() {

  it("works for simple literals and variables", function() {
    var a = 12;
    expect(a).toEqual(12);
  });

  it("should work for objects", function() {
    var foo = {
      a: 12,
      b: 34
    };
    var bar = {
      a: 12,
      b: 34
    };
    expect(foo).toEqual(bar);
  });
});

Note now foo equals bar.

Here's their example using jasmine.objectContaining:

describe("jasmine.objectContaining", function() {
  var foo;

  beforeEach(function() {
    foo = {
      a: 1,
      b: 2,
      bar: "baz"
    };
  });

  it("matches objects with the expect key/value pairs", function() {
    expect(foo).toEqual(jasmine.objectContaining({
      bar: "baz"
    }));
    expect(foo).not.toEqual(jasmine.objectContaining({
      c: 37
    }));
  });

  // ...
});

Note how the object with several properties matches the partial object supplied to jasmine.objectContaining.

like image 44
T.J. Crowder Avatar answered Jan 03 '23 02:01

T.J. Crowder


@T.J.Crowder already explained precisely the problem. Just to help you a tad more, if you want to adapt your example, you'd need something like this:

var userA = {"_t":"user","id":1978569710,"email":"[email protected]","name":"Manage"}

array =
[
  {"_t":"user","id":1073970419,"email":"[email protected]","name":"Spectator"},
  {"_t":"user","id":4464992042,"email":"[email protected]","name":"Collaborator"},
  userA
]

expect(array).toContain(userA);
like image 29
Pedro Vaz Avatar answered Jan 03 '23 04:01

Pedro Vaz