Skip to content

Commit 35adf41

Browse files
committed
add objectify tests
1 parent 09045de commit 35adf41

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

packages/utils/test/object.test.ts

+75-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,70 @@ 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+
// Hack to get around the fact that literal bigints cause a syntax error in older versions of Node, so the
697+
// assignment needs to not even be parsed as code in those versions
698+
let bigintPrimitive;
699+
eval('bigintPrimitive = 1231n;');
700+
701+
const objectifiedBigInt = objectify(bigintPrimitive);
702+
703+
expect(objectifiedBigInt).toEqual(expect.any(BigInt));
704+
expect(objectifiedBigInt.valueOf()).toEqual(bigintPrimitive);
705+
});
706+
707+
it('leaves objects alone', () => {
708+
const notAPrimitive = new Object();
709+
const objectifiedNonPrimtive = objectify(notAPrimitive);
710+
711+
// `.toBe()` tests on identity, so this shows no wrapping has occurred
712+
expect(objectifiedNonPrimtive).toBe(notAPrimitive);
713+
});
714+
});

0 commit comments

Comments
 (0)