Skip to content

Commit

Permalink
refactor(@jest/expect-utils): move immutable utils to a separate file (
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas authored Oct 2, 2022
1 parent d75433d commit 0b4a6ec
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 69 deletions.
62 changes: 62 additions & 0 deletions packages/expect-utils/src/immutableUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

// SENTINEL constants are from https://github.com/immutable-js/immutable-js/tree/main/src/predicates
const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';
const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';
const IS_RECORD_SYMBOL = '@@__IMMUTABLE_RECORD__@@';

function isObjectLiteral(source: unknown): source is Record<string, unknown> {
return source != null && typeof source === 'object' && !Array.isArray(source);
}

export function isImmutableUnorderedKeyed(source: unknown): boolean {
return Boolean(
source &&
isObjectLiteral(source) &&
source[IS_KEYED_SENTINEL] &&
!source[IS_ORDERED_SENTINEL],
);
}

export function isImmutableUnorderedSet(source: unknown): boolean {
return Boolean(
source &&
isObjectLiteral(source) &&
source[IS_SET_SENTINEL] &&
!source[IS_ORDERED_SENTINEL],
);
}

export function isImmutableList(source: unknown): boolean {
return Boolean(source && isObjectLiteral(source) && source[IS_LIST_SENTINEL]);
}

export function isImmutableOrderedKeyed(source: unknown): boolean {
return Boolean(
source &&
isObjectLiteral(source) &&
source[IS_KEYED_SENTINEL] &&
source[IS_ORDERED_SENTINEL],
);
}

export function isImmutableOrderedSet(source: unknown): boolean {
return Boolean(
source &&
isObjectLiteral(source) &&
source[IS_SET_SENTINEL] &&
source[IS_ORDERED_SENTINEL],
);
}

export function isImmutableRecord(source: unknown): boolean {
return Boolean(source && isObjectLiteral(source) && source[IS_RECORD_SYMBOL]);
}
83 changes: 17 additions & 66 deletions packages/expect-utils/src/jasmineUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/* eslint-disable */

import type {Tester} from './types';

export type EqualsFunction = (
Expand All @@ -44,8 +42,8 @@ function isAsymmetric(obj: any) {
}

function asymmetricMatch(a: any, b: any) {
var asymmetricA = isAsymmetric(a),
asymmetricB = isAsymmetric(b);
const asymmetricA = isAsymmetric(a);
const asymmetricB = isAsymmetric(b);

if (asymmetricA && asymmetricB) {
return undefined;
Expand All @@ -70,15 +68,15 @@ function eq(
customTesters: Array<Tester>,
strictCheck: boolean | undefined,
): boolean {
var result = true;
let result = true;

var asymmetricResult = asymmetricMatch(a, b);
const asymmetricResult = asymmetricMatch(a, b);
if (asymmetricResult !== undefined) {
return asymmetricResult;
}

for (var i = 0; i < customTesters.length; i++) {
var customTesterResult = customTesters[i](a, b);
for (let i = 0; i < customTesters.length; i++) {
const customTesterResult = customTesters[i](a, b);
if (customTesterResult !== undefined) {
return customTesterResult;
}
Expand All @@ -95,7 +93,7 @@ function eq(
if (a === null || b === null) {
return a === b;
}
var className = Object.prototype.toString.call(a);
const className = Object.prototype.toString.call(a);
if (className != Object.prototype.toString.call(b)) {
return false;
}
Expand Down Expand Up @@ -132,7 +130,7 @@ function eq(
}

// Used to detect circular references.
var length = aStack.length;
let length = aStack.length;
while (length--) {
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
Expand All @@ -154,19 +152,19 @@ function eq(
}

// Deep compare objects.
var aKeys = keys(a, hasKey),
key;
const aKeys = keys(a, hasKey);
let key;

var bKeys = keys(b, hasKey);
const bKeys = keys(b, hasKey);
// Add keys corresponding to asymmetric matchers if they miss in non strict check mode
if (!strictCheck) {
for (var index = 0; index !== bKeys.length; ++index) {
for (let index = 0; index !== bKeys.length; ++index) {
key = bKeys[index];
if ((isAsymmetric(b[key]) || b[key] === undefined) && !hasKey(a, key)) {
aKeys.push(key);
}
}
for (var index = 0; index !== aKeys.length; ++index) {
for (let index = 0; index !== aKeys.length; ++index) {
key = aKeys[index];
if ((isAsymmetric(a[key]) || a[key] === undefined) && !hasKey(b, key)) {
bKeys.push(key);
Expand All @@ -175,7 +173,7 @@ function eq(
}

// Ensure that both objects contain the same number of properties before comparing deep equality.
var size = aKeys.length;
let size = aKeys.length;
if (bKeys.length !== size) {
return false;
}
Expand Down Expand Up @@ -205,8 +203,8 @@ function eq(
}

function keys(obj: object, hasKey: (obj: object, key: string) => boolean) {
var keys = [];
for (var key in obj) {
const keys = [];
for (const key in obj) {
if (hasKey(obj, key)) {
keys.push(key);
}
Expand All @@ -225,7 +223,7 @@ function hasKey(obj: any, key: string) {
}

export function isA<T>(typeName: string, value: unknown): value is T {
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
return Object.prototype.toString.apply(value) === `[object ${typeName}]`;
}

function isDomNode(obj: any): boolean {
Expand All @@ -237,50 +235,3 @@ function isDomNode(obj: any): boolean {
typeof obj.isEqualNode === 'function'
);
}

// SENTINEL constants are from https://github.com/immutable-js/immutable-js/tree/main/src/predicates
const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';
const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';
const IS_RECORD_SYMBOL = '@@__IMMUTABLE_RECORD__@@';

export function isImmutableUnorderedKeyed(maybeKeyed: any) {
return !!(
maybeKeyed &&
maybeKeyed[IS_KEYED_SENTINEL] &&
!maybeKeyed[IS_ORDERED_SENTINEL]
);
}

export function isImmutableUnorderedSet(maybeSet: any) {
return !!(
maybeSet &&
maybeSet[IS_SET_SENTINEL] &&
!maybeSet[IS_ORDERED_SENTINEL]
);
}

export function isImmutableList(maybeList: any) {
return !!(maybeList && maybeList[IS_LIST_SENTINEL]);
}

export function isImmutableOrderedKeyed(maybeKeyed: any) {
return !!(
maybeKeyed &&
maybeKeyed[IS_KEYED_SENTINEL] &&
maybeKeyed[IS_ORDERED_SENTINEL]
);
}

export function isImmutableOrderedSet(maybeSet: any) {
return !!(
maybeSet &&
maybeSet[IS_SET_SENTINEL] &&
maybeSet[IS_ORDERED_SENTINEL]
);
}

export function isImmutableRecord(maybeSet: any) {
return !!(maybeSet && maybeSet[IS_RECORD_SYMBOL]);
}
5 changes: 2 additions & 3 deletions packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

import {isPrimitive} from 'jest-get-type';
import {
equals,
isA,
isImmutableList,
isImmutableOrderedKeyed,
isImmutableOrderedSet,
isImmutableRecord,
isImmutableUnorderedKeyed,
isImmutableUnorderedSet,
} from './jasmineUtils';
} from './immutableUtils';
import {equals, isA} from './jasmineUtils';

type GetPath = {
hasEndProp?: boolean;
Expand Down

0 comments on commit 0b4a6ec

Please sign in to comment.