Skip to content

Commit

Permalink
refactor: use lodash/isEqualWith for covering edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
re-taro committed Mar 31, 2024
1 parent 254b60e commit 1695ddf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"chalk": "^3.0.0",
"css.escape": "^1.5.1",
"dom-accessibility-api": "^0.6.3",
"lodash": "^4.17.21",
"redent": "^3.0.0"
},
"devDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions src/to-have-form-values.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import isEqualWith from 'lodash/isEqualWith'
import escape from 'css.escape'
import {
checkHtmlElement,
getSingleElementValue,
isEqualWithArraysAsSets,
compareArraysAsSet,
} from './utils'

// Returns the combined value of several elements that have the same name
Expand Down Expand Up @@ -68,7 +69,7 @@ export function toHaveFormValues(formElement, expectedValues) {
const formValues = getAllFormValues(formElement)
return {
pass: Object.entries(expectedValues).every(([name, expectedValue]) =>
isEqualWithArraysAsSets(formValues[name], expectedValue),
isEqualWith(formValues[name], expectedValue, compareArraysAsSet),
),
message: () => {
const to = this.isNot ? 'not to' : 'to'
Expand Down
5 changes: 3 additions & 2 deletions src/to-have-value.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import isEqualWith from 'lodash/isEqualWith'
import {
checkHtmlElement,
getMessage,
getSingleElementValue,
isEqualWithArraysAsSets,
compareArraysAsSet,
} from './utils'

export function toHaveValue(htmlElement, expectedValue) {
Expand All @@ -29,7 +30,7 @@ export function toHaveValue(htmlElement, expectedValue) {

return {
pass: expectsValue
? isEqualWithArraysAsSets(receivedValue, expectedValue)
? isEqualWith(receivedValue, expectedValue, compareArraysAsSet)
: Boolean(receivedValue),
message: () => {
const to = this.isNot ? 'not to' : 'to'
Expand Down
7 changes: 3 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,11 @@ function toSentence(
)
}

function isEqualWithArraysAsSets(arr1, arr2) {
function compareArraysAsSet(arr1, arr2) {
if (Array.isArray(arr1) && Array.isArray(arr2)) {
return [...new Set(arr1)].every(v => new Set(arr2).has(v))
} else {
return arr1 === arr2
}
return undefined
}

export {
Expand All @@ -241,5 +240,5 @@ export {
getTag,
getSingleElementValue,
toSentence,
isEqualWithArraysAsSets,
compareArraysAsSet,
}

0 comments on commit 1695ddf

Please sign in to comment.