I have created a function that compares two arrays and makes sure they are not equal to each other. I run the function twice but I must admit it is a very poor design, however, it was the only way I could get it to work. Is there an easier way to accomplish the same thing? Maybe deepcloning with lodash? Here is my go at it.
let callCount = 0;
let values = [];
let values2 = [];
const getData = () => {
const chart = [{ //dynamic: changes the id numbers on second call
items: [{
id: 1,
name: 'foo'
}, {
id: 2,
name: 'bar'
}, {
id: 3,
name: 'bar'
}, {
id: 4,
name: 'bar'
}]
}];
if (callCount < 1) { //if and for block cannot be extracted for various reasons
for (let i = 0; i < 5; i++) {
const arr = chart[i].items[0];
arr.forEach((element) => {
values.push(element.id);
});
}
callCount++;
return values;
}
if (callCount >= 1) {
for (let i = 0; i < 5; i++) {
const arr = chart[i].items[0];
arr.forEach((element) => {
values2.push(element.id);
});
}
expect(values).to.not.eq(values2);
}
};
it ('Comparing two arrays', function () {
getData()
// logic that will change effectively change some of the values of `chart`
getData()
// is successful but wildly inefficient
}
Is there an easier way using Javascript to do the same thing without counting the number of times the function is called and running the same thing again?
Return the array each time and compare at the end of the sequence,
const getData = () => {
const chart = [{ //dynamic: changes the id numbers on second call
items: [{
id: 1,
name: 'foo'
}, {
id: 2,
name: 'bar'
}, {
id: 3,
name: 'bar'
}, {
id: 4,
name: 'bar'
}]
}];
const arr = chart[i].items[0].map(element => element.id)
return arr;
};
it ('Comparing two arrays', function () {
const values1 = getData()
// logic that will change effectively change some of the values of `chart`
const values2 = getData()
expect(values1).to.not.eq(values2);
}
I am wondering how chart gets changed - is there some cy.get() commands involved?
If so you will have to return the result of them and nest the results
it ('Comparing two arrays', function () {
getData().then(values1 => {
// logic that will change effectively change some of the values of `chart`
getData().then(const values2 => {
expect(values1).to.not.eq(values2);
})
})
}