Skip to content

Commit

Permalink
refactor: remove ramda dependency (#98)
Browse files Browse the repository at this point in the history
* refactor: migrate `toContainElement`

* refactor: migrate `toHaveProp`

* refactor: migrate `toBeEmpty`

* refactor: migrate `toBeDisabled`

* refactor: migrate `toHaveStyle`

* chore: remove `ramda` dep

* refactor: add tests for `mergeAll` and `isEmpty`

* refactor: more test cases
  • Loading branch information
mdjastrzebski authored Aug 11, 2022
1 parent 7387349 commit 2ba27a9
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 37 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"jest-diff": "^28.1.3",
"jest-matcher-utils": "^28.1.3",
"pretty-format": "^28.1.3",
"ramda": "^0.28.0",
"redent": "^2.0.0"
},
"devDependencies": {
Expand Down
25 changes: 24 additions & 1 deletion src/__tests__/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkReactElement } from '../utils';
import { checkReactElement, isEmpty, mergeAll } from '../utils';

describe('checkReactElement', () => {
test('it does not throw an error for valid native primitives', () => {
Expand All @@ -19,3 +19,26 @@ describe('checkReactElement', () => {
}).toThrow();
});
});

test('mergeAll', () => {
expect(mergeAll([{ foo: 1 }, { bar: 2 }, { baz: 3 }])).toEqual({ foo: 1, bar: 2, baz: 3 });
expect(mergeAll([{ foo: 1 }, { foo: 2 }, { bar: 2 }])).toEqual({ foo: 2, bar: 2 });
expect(mergeAll([null, { foo: 1 }, { foo: 2 }, { bar: 2 }])).toEqual({ foo: 2, bar: 2 });
expect(mergeAll([{ foo: 1 }, { foo: 2 }, { bar: 2 }, undefined])).toEqual({ foo: 2, bar: 2 });
expect(mergeAll([{ foo: 1 }, { foo: 2 }, null, { bar: 2 }])).toEqual({ foo: 2, bar: 2 });
});

test('isEmpty', () => {
expect(isEmpty(null)).toEqual(true);
expect(isEmpty(undefined)).toEqual(true);
expect(isEmpty('')).toEqual(true);
expect(isEmpty(' ')).toEqual(false);
expect(isEmpty([])).toEqual(true);
expect(isEmpty([[]])).toEqual(false);
expect(isEmpty({})).toEqual(true);
expect(isEmpty({ x: 0 })).toEqual(false);
expect(isEmpty(0)).toEqual(true);
expect(isEmpty(1)).toEqual(false);
expect(isEmpty(NaN)).toEqual(true);
expect(isEmpty([''])).toEqual(false);
});
20 changes: 6 additions & 14 deletions src/to-be-disabled.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { matcherHint } from 'jest-matcher-utils';
import { compose, defaultTo, includes, path, propEq, anyPass } from 'ramda';
import { checkReactElement, getType, printElement } from './utils';

// Elements that support 'disabled'
Expand All @@ -22,20 +21,13 @@ function isElementDisabledByParent(parent) {
}

function isElementDisabled(element) {
const propDisabled = path(['props', 'disabled'], element);
const hasStatesDisabled = compose(
includes('disabled'),
defaultTo([]),
path(['props', 'accessibilityStates']),
);
const hasStateDisabled = compose(
propEq('disabled', true),
defaultTo({}),
path(['props', 'accessibilityState']),
);
const stateDisabled = anyPass([hasStatesDisabled, hasStateDisabled])(element);
if (!DISABLE_TYPES.includes(getType(element))) return false;

return DISABLE_TYPES.includes(getType(element)) && (Boolean(propDisabled) || stateDisabled);
return (
!!element?.props?.disabled ||
!!element?.props?.accessibilityState?.disabled ||
!!element?.props?.accessibilityStates?.includes('disabled')
);
}

function isAncestorDisabled(element) {
Expand Down
5 changes: 2 additions & 3 deletions src/to-be-empty.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { matcherHint } from 'jest-matcher-utils';
import { compose, defaultTo, path, isEmpty } from 'ramda';
import { checkReactElement, printElement } from './utils';
import { checkReactElement, isEmpty, printElement } from './utils';

export function toBeEmpty(element) {
checkReactElement(element, toBeEmpty, this);

return {
pass: compose(isEmpty, defaultTo({}), path(['props', 'children']))(element),
pass: isEmpty(element?.props?.children),
message: () => {
return [
matcherHint(`${this.isNot ? '.not' : ''}.toBeEmpty`, 'element', ''),
Expand Down
6 changes: 1 addition & 5 deletions src/to-contain-element.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { matcherHint, RECEIVED_COLOR as receivedColor } from 'jest-matcher-utils';
import { equals } from 'ramda';
import { checkReactElement, printElement } from './utils';

export function toContainElement(container, element) {
Expand All @@ -13,10 +12,7 @@ export function toContainElement(container, element) {

if (element) {
matches = container.findAll((node) => {
const sameType = equals(node.type, element.type);
const sameProps = equals(node.props, element.props);

return sameType && sameProps;
return node.type === element.type && this.equals(node.props, element.props);
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/to-have-prop.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { matcherHint, stringify, printExpected } from 'jest-matcher-utils';
import { equals } from 'ramda';
import { checkReactElement, getMessage } from './utils';

function printAttribute(name, value) {
Expand All @@ -21,7 +20,7 @@ export function toHaveProp(element, name, expectedValue) {
const hasProp = name in element.props;

return {
pass: isDefined ? hasProp && equals(prop, expectedValue) : hasProp,
pass: isDefined ? hasProp && this.equals(prop, expectedValue) : hasProp,
message: () => {
const to = this.isNot ? 'not to' : 'to';

Expand Down
18 changes: 7 additions & 11 deletions src/to-have-style.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { matcherHint } from 'jest-matcher-utils';
import { diff } from 'jest-diff';
import chalk from 'chalk';
import { all, compose, flatten, mergeAll, toPairs } from 'ramda';
import { checkReactElement } from './utils';
import { checkReactElement, mergeAll } from './utils';

function isSubset(expected, received) {
return compose(
all(([prop, value]) =>
Array.isArray(value)
? isSubset(mergeAll(value), mergeAll(received[prop]))
: received[prop] === value,
),
toPairs,
)(expected);
return Object.entries(expected).every(([prop, value]) =>
Array.isArray(value)
? isSubset(mergeAll(value), mergeAll(received[prop]))
: received[prop] === value,
);
}

function mergeAllStyles(styles) {
return compose(mergeAll, flatten)(styles);
return mergeAll(styles.flat());
}

function printoutStyles(styles) {
Expand Down
22 changes: 22 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,34 @@ function normalize(text) {
return text.replace(/\s+/g, ' ').trim();
}

function mergeAll(objects) {
return Object.assign({}, ...(objects ?? [{}]));
}

function isEmpty(value) {
if (!value) {
return true;
}

if (Array.isArray(value)) {
return value.length === 0;
}

if (typeof value === 'object') {
return Object.keys(value).length === 0;
}

return false;
}

export {
ReactElementTypeError,
checkReactElement,
getType,
getMessage,
matches,
normalize,
mergeAll,
isEmpty,
printElement,
};

0 comments on commit 2ba27a9

Please sign in to comment.