Untitled

mail@pastecode.io avatar
unknown
javascript
a month ago
1.2 kB
7
Indexable
Never
/**
 * arrayIncludesObjectOnPath - Check if from a list of requests, the value of a given jsonPath is qual to an expected item
 * @param {requestsList} Array
 * @param {expectedItem} JSON
 * @param {jsonPath} String
 */
export function arrayIncludesObjectOnPath(requestsList, expectedItem, jsonPath) {
  let valid = false;
  cy.task("log", `expectedItem: ${JSON.stringify(expectedItem)}`);
  cy.task("log", `jsonPath: ${jsonPath}`);
  cy.task("log", `Number of requests: ${requestsList.length}`);
  let i = 1;
  // eslint-disable-next-line guard-for-in
  for (const entry of requestsList) {
    const jsonRequestBody = entry.request.body;
    cy.task("log", `#${i} jsonRequestBody: ${JSON.stringify(jsonRequestBody)}`);
    const objectValueOnPath = JSONPath({ path: jsonPath, json: jsonRequestBody });
    valid = isEqual(objectValueOnPath[0], expectedItem);
    cy.task("log", `#${i} objectValueOnPath: ${JSON.stringify(objectValueOnPath)}`);
    if (valid === false) {
      const result = compare(objectValueOnPath[0], expectedItem);
      cy.task("log", `#${i} Diff: ${JSON.stringify(result)}`);
    } else {
      cy.task("log", `#${i} Found a match on request`);
      return valid;
    }
    i++;
  }
  return valid;
}
Leave a Comment