Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove lodash #593

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one thing lost here is readability. It is easier to read the previous version.

What I suggest is that you create a function isEqualWith (or isEqualWithArraysAsSets) in the utils.js module, that would encapsulate this functionality. Makes sense?

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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And reuse the isEqualWith function here.

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,
}
Loading