From 1c489df5f6f55974321b4c66060ebc93b764b110 Mon Sep 17 00:00:00 2001 From: Rintaro Itokawa Date: Fri, 22 Mar 2024 23:18:44 +0900 Subject: [PATCH 1/3] refactor: remove lodash --- package.json | 1 - src/to-have-form-values.js | 22 +++++++++++----------- src/to-have-value.js | 12 ++++-------- src/utils.js | 9 --------- 4 files changed, 15 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index f2721772..d3afbd66 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/to-have-form-values.js b/src/to-have-form-values.js index fe8c890a..9db8665a 100644 --- a/src/to-have-form-values.js +++ b/src/to-have-form-values.js @@ -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', @@ -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` diff --git a/src/to-have-value.js b/src/to-have-value.js index da79e416..cfe8e6b3 100644 --- a/src/to-have-value.js +++ b/src/to-have-value.js @@ -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) @@ -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' diff --git a/src/utils.js b/src/utils.js index 903f24c0..673f7475 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,4 @@ import redent from 'redent' -import isEqual from 'lodash/isEqual.js' import {parse} from '@adobe/css-tools' class GenericTypeError extends Error { @@ -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 '} = {}, @@ -240,6 +232,5 @@ export { normalize, getTag, getSingleElementValue, - compareArraysAsSet, toSentence, } From 254b60efdab82ce368e6cb17fcac25abe4575cad Mon Sep 17 00:00:00 2001 From: Rintaro Itokawa Date: Sun, 24 Mar 2024 21:49:55 +0900 Subject: [PATCH 2/3] refactor: impl same logic as lodash.isEqualWith --- src/to-have-form-values.js | 18 ++++++++---------- src/to-have-value.js | 11 +++++++---- src/utils.js | 9 +++++++++ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/to-have-form-values.js b/src/to-have-form-values.js index 9db8665a..adeff37d 100644 --- a/src/to-have-form-values.js +++ b/src/to-have-form-values.js @@ -1,5 +1,9 @@ import escape from 'css.escape' -import {checkHtmlElement, getSingleElementValue} from './utils' +import { + checkHtmlElement, + getSingleElementValue, + isEqualWithArraysAsSets, +} from './utils' // Returns the combined value of several elements that have the same name // e.g. radio buttons or groups of checkboxes @@ -63,15 +67,9 @@ export function toHaveFormValues(formElement, expectedValues) { } const formValues = getAllFormValues(formElement) return { - 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 - } - }), + pass: Object.entries(expectedValues).every(([name, expectedValue]) => + isEqualWithArraysAsSets(formValues[name], expectedValue), + ), message: () => { const to = this.isNot ? 'not to' : 'to' const matcher = `${this.isNot ? '.not' : ''}.toHaveFormValues` diff --git a/src/to-have-value.js b/src/to-have-value.js index cfe8e6b3..121a41f2 100644 --- a/src/to-have-value.js +++ b/src/to-have-value.js @@ -1,4 +1,9 @@ -import {checkHtmlElement, getMessage, getSingleElementValue} from './utils' +import { + checkHtmlElement, + getMessage, + getSingleElementValue, + isEqualWithArraysAsSets, +} from './utils' export function toHaveValue(htmlElement, expectedValue) { checkHtmlElement(htmlElement, toHaveValue, this) @@ -24,9 +29,7 @@ export function toHaveValue(htmlElement, expectedValue) { return { pass: expectsValue - ? Array.isArray(receivedValue) && Array.isArray(expectedValue) - ? [...new Set(receivedValue)].every(v => new Set(expectedValue).has(v)) - : receivedValue === expectedValue + ? isEqualWithArraysAsSets(receivedValue, expectedValue) : Boolean(receivedValue), message: () => { const to = this.isNot ? 'not to' : 'to' diff --git a/src/utils.js b/src/utils.js index 673f7475..5ef8afdb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -220,6 +220,14 @@ function toSentence( ) } +function isEqualWithArraysAsSets(arr1, arr2) { + if (Array.isArray(arr1) && Array.isArray(arr2)) { + return [...new Set(arr1)].every(v => new Set(arr2).has(v)) + } else { + return arr1 === arr2 + } +} + export { HtmlElementTypeError, NodeTypeError, @@ -233,4 +241,5 @@ export { getTag, getSingleElementValue, toSentence, + isEqualWithArraysAsSets, } From 1695ddf7102f94ba0d10e0a9cfb61a957e212065 Mon Sep 17 00:00:00 2001 From: Rintaro Itokawa Date: Sun, 31 Mar 2024 22:29:32 +0900 Subject: [PATCH 3/3] refactor: use lodash/isEqualWith for covering edge case --- package.json | 1 + src/to-have-form-values.js | 5 +++-- src/to-have-value.js | 5 +++-- src/utils.js | 7 +++---- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index d3afbd66..f63fbb09 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/to-have-form-values.js b/src/to-have-form-values.js index adeff37d..034f633c 100644 --- a/src/to-have-form-values.js +++ b/src/to-have-form-values.js @@ -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 @@ -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' diff --git a/src/to-have-value.js b/src/to-have-value.js index 121a41f2..bbd3c467 100644 --- a/src/to-have-value.js +++ b/src/to-have-value.js @@ -1,8 +1,9 @@ +import isEqualWith from 'lodash/isEqualWith' import { checkHtmlElement, getMessage, getSingleElementValue, - isEqualWithArraysAsSets, + compareArraysAsSet, } from './utils' export function toHaveValue(htmlElement, expectedValue) { @@ -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' diff --git a/src/utils.js b/src/utils.js index 5ef8afdb..ee8e484b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 { @@ -241,5 +240,5 @@ export { getTag, getSingleElementValue, toSentence, - isEqualWithArraysAsSets, + compareArraysAsSet, }