Skip to content

Commit

Permalink
refactor: remove lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
re-taro committed Mar 22, 2024
1 parent a93c0c4 commit 1c489df
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 29 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
"chalk": "^3.0.0",
"css.escape": "^1.5.1",
"dom-accessibility-api": "^0.6.3",
"lodash": "^4.17.15",
"redent": "^3.0.0"
},
"devDependencies": {
Expand Down
22 changes: 11 additions & 11 deletions src/to-have-form-values.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import isEqualWith from 'lodash/isEqualWith.js'
import uniq from 'lodash/uniq.js'
import escape from 'css.escape'
import {
checkHtmlElement,
compareArraysAsSet,
getSingleElementValue,
} from './utils'
import {checkHtmlElement, getSingleElementValue} from './utils'

// Returns the combined value of several elements that have the same name
// e.g. radio buttons or groups of checkboxes
function getMultiElementValue(elements) {
const types = uniq(elements.map(element => element.type))
const types = [...new Set(elements.map(element => element.type))]
if (types.length !== 1) {
throw new Error(
'Multiple form elements with the same name must be of the same type',
Expand Down Expand Up @@ -69,9 +63,15 @@ export function toHaveFormValues(formElement, expectedValues) {
}
const formValues = getAllFormValues(formElement)
return {
pass: Object.entries(expectedValues).every(([name, expectedValue]) =>
isEqualWith(formValues[name], expectedValue, compareArraysAsSet),
),
pass: Object.entries(expectedValues).every(([name, expectedValue]) => {
if (Array.isArray(formValues[name]) && Array.isArray(expectedValue)) {
return [...new Set(formValues[name])].every(v =>
new Set(expectedValue).has(v),
)
} else {
return formValues[name] === expectedValue
}
}),
message: () => {
const to = this.isNot ? 'not to' : 'to'
const matcher = `${this.isNot ? '.not' : ''}.toHaveFormValues`
Expand Down
12 changes: 4 additions & 8 deletions src/to-have-value.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import isEqualWith from 'lodash/isEqualWith.js'
import {
checkHtmlElement,
compareArraysAsSet,
getMessage,
getSingleElementValue,
} from './utils'
import {checkHtmlElement, getMessage, getSingleElementValue} from './utils'

export function toHaveValue(htmlElement, expectedValue) {
checkHtmlElement(htmlElement, toHaveValue, this)
Expand All @@ -30,7 +24,9 @@ export function toHaveValue(htmlElement, expectedValue) {

return {
pass: expectsValue
? isEqualWith(receivedValue, expectedValue, compareArraysAsSet)
? Array.isArray(receivedValue) && Array.isArray(expectedValue)
? [...new Set(receivedValue)].every(v => new Set(expectedValue).has(v))
: receivedValue === expectedValue
: Boolean(receivedValue),
message: () => {
const to = this.isNot ? 'not to' : 'to'
Expand Down
9 changes: 0 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import redent from 'redent'
import isEqual from 'lodash/isEqual.js'
import {parse} from '@adobe/css-tools'

class GenericTypeError extends Error {
Expand Down Expand Up @@ -212,13 +211,6 @@ function getSingleElementValue(element) {
}
}

function compareArraysAsSet(a, b) {
if (Array.isArray(a) && Array.isArray(b)) {
return isEqual(new Set(a), new Set(b))
}
return undefined
}

function toSentence(
array,
{wordConnector = ', ', lastWordConnector = ' and '} = {},
Expand All @@ -240,6 +232,5 @@ export {
normalize,
getTag,
getSingleElementValue,
compareArraysAsSet,
toSentence,
}

0 comments on commit 1c489df

Please sign in to comment.