Reputation: 519
I send data to server and match returned data with sent data. If use expect.arrayContaining(array) to compare options and nested variants it swears on ids and fields that add db. How compare objects with arrays which contain arrays of objects ?
Data to send:
{
"name": "red dress",
"options": Array [
Object {
"name": "size",
"variants": Array [
Object {
"name": "M",
},
Object {
"name": "L",
},
Object {
"name": "S",
},
],
},
],
}
Returned data:
{
"id": "dc67efd8-dcc4-43df-a8eb-9d95ea641749",
"name": "red dress",
"options": Array [
Object {
"id": 1,
"name": "size",
"productId": "dc67efd8-dcc4-43df-a8eb-9d95ea641749",
"variants": Array [
Object {
"id": 1,
"name": "M",
"optionId": 1,
},
Object {
"id": 5,
"name": "S",
"optionId": 1,
},
Object {
"id": 6,
"name": "L",
"optionId": 1,
},
],
},
],
}
Test:
expect(body.data).toMatchObject(productData)
Upvotes: 6
Views: 4812
Reputation: 11067
use a map
to filter only the key you want to check
something like:
expect(yourResponseObject.options.variants.map(e=>({e.name}))).to.include(["blue","red", ... ]);
https://www.chaijs.com/api/bdd/
Upvotes: 0
Reputation: 310
Maybe you could use a forEach
loop to check the relevant data? I presume from your sent data that what matters is the provided options and variants are included in the response.
it('has provided options', () => {
sent.options.forEach(o => {
expect(received.options).toContainEqual(
expect.objectContaining({ name: o.name })
)
})
})
Similarly for the variants:
it('has provided variants', () => {
sent.options[0].variants.forEach(v => {
expect(received.options[0].variants).toContainEqual(
expect.objectContaining(v)
)
})
})
Upvotes: 1