Palaniichuk Dmytro
Palaniichuk Dmytro

Reputation: 3173

Compare two array of object to its value, unit test

Suppose I have two similar array, how can I write unittest to check if these array has the same values. And these values are equal. I use mocha and enzyme for unit testing. What is the best way to compare this value?

const devices = [{
        deviceType: 'Tag',
        deviceId: 1,
        name: 'Tag For Sending and alarm',
        version: '',
        location: 'Room1',
        lastAliveMessage: '',
        deviceStatus: 'Active',
        actions: 'offline',

    },{
        deviceType: 'Tag1',
        deviceId: 2,
        name: 'Tag For Sending and alarm2 ',
        version: '',
        location: 'Room2',
        lastAliveMessage: '',
        deviceStatus: 'Active',
        actions: 'offline',
    }]

const devices2 = [{
        deviceType: 'Tag',
        deviceId: 1,
        name: 'Tag For Sending and alarm',
        version: '',
        location: 'Room1',
        lastAliveMessage: '',
        deviceStatus: 'Active',
        actions: 'offline',

    },{
        deviceType: 'Tag1',
        deviceId: 2,
        name: 'Tag For Sending and alarm2 ',
        version: '',
        location: 'Room2',
        lastAliveMessage: '',
        deviceStatus: 'Active',
        actions: 'offline',
    }]

 data.forEach(item => {
                devices.forEach(device =>{
item.deviceId.should.be.equal(device.deviceId))
                })
            })

Upvotes: 0

Views: 2361

Answers (2)

Einar Ólafsson
Einar Ólafsson

Reputation: 3177

If you are using Chai you can use deep.equal check if the arrays of the objects are the same. It will work even if the order of the keys in the object are not the same between the two.

expect(devices).to.deep.equal(devices2)

Upvotes: 1

Palaniichuk Dmytro
Palaniichuk Dmytro

Reputation: 3173

devices.forEach((device, index) => {  
     data[index].deviceType.props.children.should.be.equal(device.deviceType)
     data[index].deviceId.props.children.should.be.equal(device.deviceId)
        })

Upvotes: 0

Related Questions