From 31563b24d69f31f0fb9afb7d7a6ef6baa5901a53 Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Fri, 13 Sep 2024 22:41:07 +0200 Subject: [PATCH] refactor: remove lodash altogether --- package.json | 1 - src/to-have-form-values.js | 5 ++--- src/to-have-value.js | 5 ++--- src/utils.js | 17 ++++++++++++----- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index a9ddf16..f826ef8 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,6 @@ "chalk": "^3.0.0", "css.escape": "^1.5.1", "dom-accessibility-api": "^0.6.3", - "lodash": "^4.17.21", "redent": "^3.0.0" }, "devDependencies": { diff --git a/src/to-have-form-values.js b/src/to-have-form-values.js index cd479d6..28e80e6 100644 --- a/src/to-have-form-values.js +++ b/src/to-have-form-values.js @@ -1,9 +1,8 @@ -import isEqualWith from 'lodash/isEqualWith.js' import escape from 'css.escape' import { checkHtmlElement, getSingleElementValue, - compareArraysAsSet, + compare, } from './utils' // Returns the combined value of several elements that have the same name @@ -69,7 +68,7 @@ export function toHaveFormValues(formElement, expectedValues) { const formValues = getAllFormValues(formElement) return { pass: Object.entries(expectedValues).every(([name, expectedValue]) => - isEqualWith(formValues[name], expectedValue, compareArraysAsSet), + compare(formValues[name], expectedValue), ), message: () => { const to = this.isNot ? 'not to' : 'to' diff --git a/src/to-have-value.js b/src/to-have-value.js index 37cca52..ff07118 100644 --- a/src/to-have-value.js +++ b/src/to-have-value.js @@ -1,9 +1,8 @@ -import isEqualWith from 'lodash/isEqualWith.js' import { checkHtmlElement, getMessage, getSingleElementValue, - compareArraysAsSet, + compare, } from './utils' export function toHaveValue(htmlElement, expectedValue) { @@ -30,7 +29,7 @@ export function toHaveValue(htmlElement, expectedValue) { return { pass: expectsValue - ? isEqualWith(receivedValue, expectedValue, compareArraysAsSet) + ? compare(receivedValue, expectedValue) : Boolean(receivedValue), message: () => { const to = this.isNot ? 'not to' : 'to' diff --git a/src/utils.js b/src/utils.js index 2699d95..de84b97 100644 --- a/src/utils.js +++ b/src/utils.js @@ -230,11 +230,18 @@ function toSentence( ) } -function compareArraysAsSet(arr1, arr2) { - if (Array.isArray(arr1) && Array.isArray(arr2)) { - return [...new Set(arr1)].every(v => new Set(arr2).has(v)) +function compareArrays(arr1, arr2) { + return [...new Set(arr1)].every(v => new Set(arr2).has(v)); +} + +function compare(val1, val2) { + if (val1 === val2) { + return true + } + if (Array.isArray(val1) && Array.isArray(val2)) { + return compareArrays(val1, val2) } - return undefined + return false } export { @@ -250,5 +257,5 @@ export { getTag, getSingleElementValue, toSentence, - compareArraysAsSet, + compare, }