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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to ensure no duplicate values in ReactSymbols #12845

Merged
merged 4 commits into from
May 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/shared/__tests__/ReactSymbols-test.internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';

describe('ReactSymbols', () => {
beforeEach(() => jest.resetModules());

const expectToBeUnique = keyValuePairs => {
const map = new Map();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe would be easier to get an array with Object.values() and then verify its length is the same as new Set(arr).size?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be easier but this provides a more meaningful error message if it fails. 馃槃

keyValuePairs.forEach(([key, value]) => {
if (map.has(value)) {
throw Error(
`${key} value ${value.toString()} is the same as ${map.get(value)}.`,
);
}
map.set(value, key);
});
};

it('Symbol values should be unique', () => {
expectToBeUnique(Object.entries(require('shared/ReactSymbols')));
});

it('numeric values should be unique', () => {
const originalSymbolFor = global.Symbol.for;
global.Symbol.for = null;
try {
expectToBeUnique(Object.entries(require('shared/ReactSymbols')));
} finally {
global.Symbol.for = originalSymbolFor;
}
});
});