Files
react-native/packages/polyfills/__tests__/Object.es8-test.js
T
Chris Shepherd 8a62583f79 Fix mislabelled polyfills for Object.entries and Object.values (#31880)
Summary:
The polyfills for `Object.entries` and `Object.values` are in a file named `Object.es7.js` when these APIs form part of ES8/ES2017 (https://en.wikipedia.org/wiki/ECMAScript#8th_Edition_–_ECMAScript_2017).

The docs (https://reactnative.dev/docs/javascript-environment#polyfills) list these correctly as ES8 so I thought it might reduce confusion if anyone starts looking into the polyfills in the future like I did.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Internal] [Fixed] - Fix filename to include correct ECMA spec

Pull Request resolved: https://github.com/facebook/react-native/pull/31880

Test Plan: All unit tests pass.

Reviewed By: yungsters

Differential Revision: D29820165

Pulled By: ShikaSD

fbshipit-source-id: 2a4eb58bed7b7a4089406665c5c9115cb1773ff6
2021-07-21 15:37:12 -07:00

141 lines
3.5 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+jsinfra
*/
'use strict';
describe('Object (ES8)', () => {
beforeEach(() => {
delete Object.entries;
delete Object.values;
jest.resetModules();
require('../Object.es8');
});
describe('Object.entries', () => {
it('should have a length of 1', () => {
expect(Object.entries.length).toBe(1);
});
it('should check for type', () => {
expect(Object.entries.bind(null, null)).toThrow(
TypeError('Object.entries called on non-object'),
);
expect(Object.entries.bind(null, undefined)).toThrow(
TypeError('Object.entries called on non-object'),
);
expect(Object.entries.bind(null, [])).not.toThrow();
expect(Object.entries.bind(null, () => {})).not.toThrow();
expect(Object.entries.bind(null, {})).not.toThrow();
expect(Object.entries.bind(null, 'abc')).not.toThrow();
});
it('should return enumerable entries', () => {
const foo = Object.defineProperties(
{},
{
x: {value: 10, enumerable: true},
y: {value: 20},
},
);
expect(Object.entries(foo)).toEqual([['x', 10]]);
const bar = {x: 10, y: 20};
expect(Object.entries(bar)).toEqual([
['x', 10],
['y', 20],
]);
});
it('should work with proto-less objects', () => {
const foo = Object.create(null, {
x: {value: 10, enumerable: true},
y: {value: 20},
});
expect(Object.entries(foo)).toEqual([['x', 10]]);
});
it('should return only own entries', () => {
const foo = Object.create(
{z: 30},
{
x: {value: 10, enumerable: true},
y: {value: 20},
},
);
expect(Object.entries(foo)).toEqual([['x', 10]]);
});
it('should convert to object primitive string', () => {
expect(Object.entries('ab')).toEqual([
['0', 'a'],
['1', 'b'],
]);
});
});
describe('Object.values', () => {
it('should have a length of 1', () => {
expect(Object.values.length).toBe(1);
});
it('should check for type', () => {
expect(Object.values.bind(null, null)).toThrow(
TypeError('Object.values called on non-object'),
);
expect(Object.values.bind(null, [])).not.toThrow();
expect(Object.values.bind(null, () => {})).not.toThrow();
expect(Object.values.bind(null, {})).not.toThrow();
});
it('should return enumerable values', () => {
const foo = Object.defineProperties(
{},
{
x: {value: 10, enumerable: true},
y: {value: 20},
},
);
expect(Object.values(foo)).toEqual([10]);
const bar = {x: 10, y: 20};
expect(Object.values(bar)).toEqual([10, 20]);
});
it('should work with proto-less objects', () => {
const foo = Object.create(null, {
x: {value: 10, enumerable: true},
y: {value: 20},
});
expect(Object.values(foo)).toEqual([10]);
});
it('should return only own values', () => {
const foo = Object.create(
{z: 30},
{
x: {value: 10, enumerable: true},
y: {value: 20},
},
);
expect(Object.values(foo)).toEqual([10]);
});
it('should convert to object primitive string', () => {
expect(Object.values('ab')).toEqual(['a', 'b']);
});
});
});