Skip to content

Commit 9352f76

Browse files
committed
add objectify tests
1 parent 99024b2 commit 9352f76

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

packages/utils/test/object.test.ts

+72-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
*/
44

55
import * as isModule from '../src/is';
6-
import { dropUndefinedKeys, extractExceptionKeysForMessage, fill, normalize, urlEncode } from '../src/object';
6+
import {
7+
dropUndefinedKeys,
8+
extractExceptionKeysForMessage,
9+
fill,
10+
normalize,
11+
objectify,
12+
urlEncode,
13+
} from '../src/object';
714
import { testOnlyIfNodeVersionAtLeast } from './testutils';
815

916
describe('fill()', () => {
@@ -638,3 +645,67 @@ describe('dropUndefinedKeys()', () => {
638645
});
639646
});
640647
});
648+
649+
describe('objectify()', () => {
650+
it('turns undefined and null into `String` objects', () => {
651+
const objectifiedUndefined = objectify(undefined);
652+
const objectifiedNull = objectify(null);
653+
654+
// not string literals but instances of the class `String`
655+
expect(objectifiedUndefined).toEqual(expect.any(String));
656+
expect(objectifiedNull).toEqual(expect.any(String));
657+
658+
expect(objectifiedUndefined.valueOf()).toEqual('undefined');
659+
expect(objectifiedNull.valueOf()).toEqual('null');
660+
});
661+
662+
it('wraps other primitives with their respective object wrapper classes', () => {
663+
// Note: BigInts are tested separately in order to be able to restrict the Node version on which the tests run
664+
const numberPrimitive = 1121;
665+
const stringPrimitive = 'Dogs are great!';
666+
const booleanPrimitive = true;
667+
const symbolPrimitive = Symbol('Maisey');
668+
669+
const objectifiedNumber = objectify(numberPrimitive);
670+
const objectifiedString = objectify(stringPrimitive);
671+
const objectifiedBoolean = objectify(booleanPrimitive);
672+
const objectifiedSymbol = objectify(symbolPrimitive);
673+
674+
// not literals but instances of the respective wrapper classes
675+
expect(objectifiedNumber).toEqual(expect.any(Number));
676+
expect(objectifiedString).toEqual(expect.any(String));
677+
678+
// TODO: There's currently a bug in Jest - if you give it the `Boolean` class, it runs `typeof received ===
679+
// 'boolean'` but not `received instanceof Boolean` (the way it correctly does for other primitive wrappers, like
680+
// `Number` and `String). (See https://github.com/facebook/jest/pull/11976.) Once that is fixed and we upgrade jest,
681+
// we can comment the test below back in. (The tests for symbols and bigints are working only because our current
682+
// version of jest is sufficiently old that they're not even considered in the relevant check and just fall to the
683+
// default `instanceof` check jest uses for all unknown classes.)
684+
685+
// expect(objectifiedBoolean).toEqual(expect.any(Boolean));
686+
expect(objectifiedSymbol).toEqual(expect.any(Symbol));
687+
688+
expect(objectifiedNumber.valueOf()).toEqual(numberPrimitive);
689+
expect(objectifiedString.valueOf()).toEqual(stringPrimitive);
690+
expect(objectifiedBoolean.valueOf()).toEqual(booleanPrimitive);
691+
expect(objectifiedSymbol.valueOf()).toEqual(symbolPrimitive);
692+
});
693+
694+
// `BigInt` doesn't exist in Node < 10.
695+
testOnlyIfNodeVersionAtLeast(10)('wraps bigints with the `BigInt` class', () => {
696+
// @ts-ignore BigInts are new enough that our TS `target` value rules them out.
697+
const bigintPrimitive = 'BigInt' in global ? 1231n : NaN;
698+
const objectifiedBigInt = objectify(bigintPrimitive);
699+
700+
expect(objectifiedBigInt).toEqual(expect.any(BigInt));
701+
expect(objectifiedBigInt.valueOf()).toEqual(bigintPrimitive);
702+
});
703+
704+
it('leaves objects alone', () => {
705+
const notAPrimitive = new Object();
706+
const objectifiedNonPrimtive = objectify(notAPrimitive);
707+
708+
// `.toBe()` tests on identity, so this shows no wrapping has occurred
709+
expect(objectifiedNonPrimtive).toBe(notAPrimitive);
710+
});
711+
});

0 commit comments

Comments
 (0)