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

feat(expect): Add equality support for Array Buffers to ToStrictEqual method #11805

Merged
merged 13 commits into from Sep 29, 2021
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### Features

- `[expect]` Add equality checks for Array Buffers in `expect.ToStrictEqual()`
khai93 marked this conversation as resolved.
Show resolved Hide resolved

### Fixes

### Chore & Maintenance
Expand Down
12 changes: 12 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -426,6 +426,18 @@ describe('.toStrictEqual()', () => {
it('does not pass when equally sparse arrays have different values', () => {
expect([, 1]).not.toStrictEqual([, 2]);
});

it('does not pass when ArrayBuffers are not equal', () => {
expect(Uint8Array.from([1, 2]).buffer).not.toStrictEqual(Uint8Array.from([0, 0]).buffer);
expect(Uint8Array.from([2,1]).buffer).not.toStrictEqual(Uint8Array.from([2,2]).buffer);
expect(Uint8Array.from([]).buffer).not.toStrictEqual(Uint8Array.from([1]).buffer);
});

it('passes for matching buffers', () => {
expect(Uint8Array.from([1]).buffer).toStrictEqual(Uint8Array.from([1]).buffer);
expect(Uint8Array.from([]).buffer).toStrictEqual(Uint8Array.from([]).buffer);
expect(Uint8Array.from([9,3]).buffer).toStrictEqual(Uint8Array.from([9,3]).buffer);
})
/* eslint-enable */
});

Expand Down
21 changes: 21 additions & 0 deletions packages/expect/src/__tests__/utils.test.ts
Expand Up @@ -8,6 +8,7 @@

import {stringify} from 'jest-matcher-utils';
import {
arrayBufferEquality,
emptyObject,
getObjectSubset,
getPath,
Expand Down Expand Up @@ -466,3 +467,23 @@ describe('iterableEquality', () => {
expect(iterableEquality(a, b)).toBe(true);
});
});

describe('arrayBufferEquality', () => {
test('returns undefined if given a non instance of ArrayBuffer', () => {
expect(arrayBufferEquality(2, "s")).toBeUndefined();
expect(arrayBufferEquality(undefined, 2)).toBeUndefined();
expect(arrayBufferEquality(new Date(), new ArrayBuffer(2))).toBeUndefined();
});

test('returns false when given non-matching buffers', () => {
const a = Uint8Array.from([2,4]).buffer;
const b = Uint8Array.from([1,7]).buffer;
expect(arrayBufferEquality(a, b)).not.toBeTruthy();
});

test('returns true when given matching buffers', () => {
const a = Uint8Array.from([1,2]).buffer;
const b = Uint8Array.from([1,2]).buffer;
expect(arrayBufferEquality(a, b)).toBeTruthy();
})
});
2 changes: 2 additions & 0 deletions packages/expect/src/matchers.ts
Expand Up @@ -40,6 +40,7 @@ import {
} from './print';
import type {MatcherState, MatchersObject} from './types';
import {
arrayBufferEquality,
getObjectSubset,
getPath,
iterableEquality,
Expand All @@ -61,6 +62,7 @@ const toStrictEqualTesters = [
iterableEquality,
typeEquality,
sparseArrayEquality,
arrayBufferEquality,
];

type ContainIterable =
Expand Down
24 changes: 24 additions & 0 deletions packages/expect/src/utils.ts
Expand Up @@ -319,6 +319,30 @@ export const typeEquality = (a: any, b: any): boolean | undefined => {
return false;
};

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
khai93 marked this conversation as resolved.
Show resolved Hide resolved
export const arrayBufferEquality = (a: any, b: any): boolean | undefined => {
khai93 marked this conversation as resolved.
Show resolved Hide resolved
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer)) {
return undefined;
}

const dataViewA = new DataView(a);
const dataViewB = new DataView(b);

// Buffers are not equal when they do not have the same byte length
if (dataViewA.byteLength != dataViewB.byteLength) {
khai93 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

// Check if every byte value is equal to each other
for (let i = 0;i < dataViewA.byteLength; i++) {
if (dataViewA.getUint8(i) != dataViewB.getUint8(i)) {
khai93 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}

return true;
}

export const sparseArrayEquality = (
a: unknown,
b: unknown,
Expand Down