function expectHookNamesToEqual(map, expectedNamesArray) {
expect(Array.from(map.values())).toEqual(expectedNamesArray);
}
function requireText(path, encoding) {
const {existsSync, readFileSync} = require('fs');
if (existsSync(path)) {
return Promise.resolve(readFileSync(path, encoding));
} else {
return Promise.reject(`File not found "${path}"`);
}
}
function initFetchMock() {
const fetchMock = require('jest-fetch-mock');
fetchMock.enableMocks();
fetchMock.mockIf(/.+$/, request => {
const url = request.url;
const isLoadingExternalSourceMap = /external\/.*\.map/.test(url);
if (isLoadingExternalSourceMap) {
expect(url.includes('?foo=bar¶m=some_value')).toBe(true);
const fileSystemPath = url.split('?')[0];
return requireText(fileSystemPath, 'utf8');
}
return requireText(url, 'utf8');
});
return fetchMock;
}
describe('parseHookNames', () => {
let fetchMock;
let inspectHooks;
let parseHookNames;
beforeEach(() => {
jest.resetModules();
jest.mock('source-map-support', () => {
console.trace('source-map-support');
});
fetchMock = initFetchMock();
inspectHooks =
require('react-debug-tools/src/ReactDebugHooks').inspectHooks;
const {
flattenHooksList,
loadSourceAndMetadata,
} = require('../parseHookNames/loadSourceAndMetadata');
const parseSourceAndMetadata =
require('../parseHookNames/parseSourceAndMetadata').parseSourceAndMetadata;
parseHookNames = async hooksTree => {
const hooksList = flattenHooksList(hooksTree);
const locationKeyToHookSourceAndMetadata =
await loadSourceAndMetadata(hooksList);
return parseSourceAndMetadata(
hooksList,
locationKeyToHookSourceAndMetadata,
);
};
Error.prepareStackTrace = (error, trace) => {
return error.stack;
};
});
afterEach(() => {
fetch.resetMocks();
});
async function getHookNamesForComponent(Component, props = {}) {
const hooksTree = inspectHooks(Component, props, undefined);
const hookNames = await parseHookNames(hooksTree);
return hookNames;
}
it('should parse names for useState()', async () => {
const Component =
require('./__source__/__untransformed__/ComponentWithUseState').Component;
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, ['foo', 'bar', 'baz', null]);
});
it('should parse names for useReducer()', async () => {
const Component =
require('./__source__/__untransformed__/ComponentWithUseReducer').Component;
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, ['foo', 'bar', 'baz']);
});
it('should skip loading source files for unnamed hooks like useEffect', async () => {
const Component =
require('./__source__/__untransformed__/ComponentWithUseEffect').Component;
fetchMock.mockIf(/.+$/, request => {
throw Error(`Unexpected file request for "${request.url}"`);
});
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, []);
});
it('should skip loading source files for unnamed hooks like useEffect (alternate)', async () => {
const Component =
require('./__source__/__untransformed__/ComponentWithExternalUseEffect').Component;
fetchMock.mockIf(/.+$/, request => {
if (request.url.endsWith('useCustom.js')) {
throw Error(`Unexpected file request for "${request.url}"`);
}
return requireText(request.url, 'utf8');
});
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, ['count', null]);
});
it('should parse names for custom hooks', async () => {
const Component =
require('./__source__/__untransformed__/ComponentWithNamedCustomHooks').Component;
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'foo',
null,
'baz',
]);
});
it('should parse names for code using hooks indirectly', async () => {
const Component =
require('./__source__/__untransformed__/ComponentUsingHooksIndirectly').Component;
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, ['count', 'darkMode', 'isDarkMode']);
});
it('should parse names for code using nested hooks', async () => {
const Component =
require('./__source__/__untransformed__/ComponentWithNestedHooks').Component;
let InnerComponent;
const hookNames = await getHookNamesForComponent(Component, {
callback: innerComponent => {
InnerComponent = innerComponent;
},
});
const innerHookNames = await getHookNamesForComponent(InnerComponent);
expectHookNamesToEqual(hookNames, ['InnerComponent']);
expectHookNamesToEqual(innerHookNames, ['state']);
});
it('should return null for custom hooks without explicit names', async () => {
const Component =
require('./__source__/__untransformed__/ComponentWithUnnamedCustomHooks').Component;
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
null,
null,
null,
]);
});
describe('inline, external and bundle source maps', () => {
it('should work for simple components', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count',
]);
}
await testFor('./__source__/Example');
await testFor('./__source__/__compiled__/inline/Example');
await testFor('./__source__/__compiled__/external/Example');
await testFor('./__source__/__compiled__/inline/index-map/Example');
await testFor('./__source__/__compiled__/external/index-map/Example');
await testFor('./__source__/__compiled__/bundle/index', 'Example');
await testFor('./__source__/__compiled__/no-columns/Example');
});
it('should work with more complex files and components', async () => {
async function testFor(path, name = undefined) {
const components = name != null ? require(path)[name] : require(path);
let hookNames = await getHookNamesForComponent(components.List);
expectHookNamesToEqual(hookNames, [
'newItemText',
'items',
'uid',
'handleClick',
'handleKeyPress',
'handleChange',
'removeItem',
'toggleItem',
]);
hookNames = await getHookNamesForComponent(components.ListItem, {
item: {},
});
expectHookNamesToEqual(hookNames, [
'handleDelete',
'handleToggle',
]);
}
await testFor('./__source__/ToDoList');
await testFor('./__source__/__compiled__/inline/ToDoList');
await testFor('./__source__/__compiled__/external/ToDoList');
await testFor('./__source__/__compiled__/inline/index-map/ToDoList');
await testFor('./__source__/__compiled__/external/index-map/ToDoList');
await testFor('./__source__/__compiled__/bundle', 'ToDoList');
await testFor('./__source__/__compiled__/no-columns/ToDoList');
});
it('should work for custom hook', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count',
'isDarkMode',
'isDarkMode',
null,
]);
}
await testFor('./__source__/ComponentWithCustomHook');
await testFor('./__source__/__compiled__/inline/ComponentWithCustomHook');
await testFor(
'./__source__/__compiled__/external/ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/inline/index-map/ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/external/index-map/ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/bundle',
'ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/no-columns/ComponentWithCustomHook',
);
});
it('should work when code is using hooks indirectly', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count',
'darkMode',
'isDarkMode',
]);
}
await testFor(
'./__source__/__compiled__/inline/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/external/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/inline/index-map/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/external/index-map/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/bundle',
'ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/no-columns/ComponentUsingHooksIndirectly',
);
});
it('should work when code is using nested hooks', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
let InnerComponent;
const hookNames = await getHookNamesForComponent(Component, {
callback: innerComponent => {
InnerComponent = innerComponent;
},
});
const innerHookNames = await getHookNamesForComponent(InnerComponent);
expectHookNamesToEqual(hookNames, [
'InnerComponent',
]);
expectHookNamesToEqual(innerHookNames, [
'state',
]);
}
await testFor(
'./__source__/__compiled__/inline/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/external/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/inline/index-map/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/external/index-map/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/bundle',
'ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/no-columns/ComponentWithNestedHooks',
);
});
it('should work for external hooks', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'theme',
'theme',
]);
}
await testFor(
'./__source__/__compiled__/inline/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/external/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/inline/index-map/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/external/index-map/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/bundle',
'ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/no-columns/ComponentWithExternalCustomHooks',
);
});
it('should work when multiple hooks are on a line', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'a',
'b',
'c',
'd',
]);
}
await testFor(
'./__source__/__compiled__/inline/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/external/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/inline/index-map/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/external/index-map/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/bundle',
'ComponentWithMultipleHooksPerLine',
);
async function noColumntest(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'a',
'b',
null,
null,
]);
}
await noColumntest(
'./__source__/__compiled__/no-columns/ComponentWithMultipleHooksPerLine',
);
});
it.skip('should work for inline requires', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count',
]);
}
await testFor('./__source__/InlineRequire');
await testFor('./__source__/__compiled__/inline/InlineRequire');
await testFor('./__source__/__compiled__/external/InlineRequire');
await testFor('./__source__/__compiled__/inline/index-map/InlineRequire');
await testFor(
'./__source__/__compiled__/external/index-map/InlineRequire',
);
await testFor('./__source__/__compiled__/bundle', 'InlineRequire');
await testFor('./__source__/__compiled__/no-columns/InlineRequire');
});
it('should support sources that contain the string "sourceMappingURL="', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count',
]);
}
console.warn = () => {};
await testFor('./__source__/ContainingStringSourceMappingURL');
await testFor(
'./__source__/__compiled__/inline/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/external/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/inline/index-map/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/external/index-map/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/bundle',
'ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/no-columns/ContainingStringSourceMappingURL',
);
});
});
describe('extended source maps', () => {
beforeEach(() => {
const babelParser = require('@babel/parser');
const generateHookMapModule = require('../generateHookMap');
jest.spyOn(babelParser, 'parse');
jest.spyOn(generateHookMapModule, 'decodeHookMap');
});
it('should work for simple components', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count',
]);
expect(require('@babel/parser').parse).toHaveBeenCalledTimes(0);
expect(require('../generateHookMap').decodeHookMap).toHaveBeenCalled();
}
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/Example',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/Example',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/Example',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/Example',
);
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/index-map/Example',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/index-map/Example',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/index-map/Example',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/index-map/Example',
);
});
it('should work with more complex files and components', async () => {
async function testFor(path, name = undefined) {
const components = name != null ? require(path)[name] : require(path);
let hookNames = await getHookNamesForComponent(components.List);
expectHookNamesToEqual(hookNames, [
'newItemText',
'items',
'uid',
'handleClick',
'handleKeyPress',
'handleChange',
'removeItem',
'toggleItem',
]);
hookNames = await getHookNamesForComponent(components.ListItem, {
item: {},
});
expectHookNamesToEqual(hookNames, [
'handleDelete',
'handleToggle',
]);
expect(require('@babel/parser').parse).toHaveBeenCalledTimes(0);
expect(require('../generateHookMap').decodeHookMap).toHaveBeenCalled();
}
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/ToDoList',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/ToDoList',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/ToDoList',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/ToDoList',
);
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/index-map/ToDoList',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/index-map/ToDoList',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/index-map/ToDoList',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/index-map/ToDoList',
);
});
it('should work for custom hook', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count',
'isDarkMode',
'isDarkMode',
null,
]);
expect(require('@babel/parser').parse).toHaveBeenCalledTimes(0);
expect(require('../generateHookMap').decodeHookMap).toHaveBeenCalled();
}
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/index-map/ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/index-map/ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/index-map/ComponentWithCustomHook',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/index-map/ComponentWithCustomHook',
);
});
it('should work when code is using hooks indirectly', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count',
'darkMode',
'isDarkMode',
]);
expect(require('@babel/parser').parse).toHaveBeenCalledTimes(0);
expect(require('../generateHookMap').decodeHookMap).toHaveBeenCalled();
}
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/index-map/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/index-map/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/index-map/ComponentUsingHooksIndirectly',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/index-map/ComponentUsingHooksIndirectly',
);
});
it('should work when code is using nested hooks', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
let InnerComponent;
const hookNames = await getHookNamesForComponent(Component, {
callback: innerComponent => {
InnerComponent = innerComponent;
},
});
const innerHookNames = await getHookNamesForComponent(InnerComponent);
expectHookNamesToEqual(hookNames, [
'InnerComponent',
]);
expectHookNamesToEqual(innerHookNames, [
'state',
]);
expect(require('@babel/parser').parse).toHaveBeenCalledTimes(0);
expect(require('../generateHookMap').decodeHookMap).toHaveBeenCalled();
}
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/index-map/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/index-map/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/index-map/ComponentWithNestedHooks',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/index-map/ComponentWithNestedHooks',
);
});
it('should work for external hooks', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'theme',
'theme',
]);
expect(require('@babel/parser').parse).toHaveBeenCalledTimes(0);
expect(require('../generateHookMap').decodeHookMap).toHaveBeenCalled();
}
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/index-map/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/index-map/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/index-map/ComponentWithExternalCustomHooks',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/index-map/ComponentWithExternalCustomHooks',
);
});
it('should work when multiple hooks are on a line', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'a',
'b',
'c',
'd',
]);
expect(require('@babel/parser').parse).toHaveBeenCalledTimes(0);
expect(require('../generateHookMap').decodeHookMap).toHaveBeenCalled();
}
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/index-map/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/index-map/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/index-map/ComponentWithMultipleHooksPerLine',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/index-map/ComponentWithMultipleHooksPerLine',
);
});
it.skip('should work for inline requires', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count',
]);
expect(require('@babel/parser').parse).toHaveBeenCalledTimes(0);
expect(require('../generateHookMap').decodeHookMap).toHaveBeenCalled();
}
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/InlineRequire',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/InlineRequire',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/InlineRequire',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/InlineRequire',
);
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/index-map/InlineRequire',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/index-map/InlineRequire',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/index-map/InlineRequire',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/index-map/InlineRequire',
);
});
it('should support sources that contain the string "sourceMappingURL="', async () => {
async function testFor(path, name = 'Component') {
const Component = require(path)[name];
const hookNames = await getHookNamesForComponent(Component);
expectHookNamesToEqual(hookNames, [
'count',
]);
expect(require('@babel/parser').parse).toHaveBeenCalledTimes(0);
expect(require('../generateHookMap').decodeHookMap).toHaveBeenCalled();
}
console.warn = () => {};
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/inline/fb-sources-extended/index-map/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/external/fb-sources-extended/index-map/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/inline/react-sources-extended/index-map/ContainingStringSourceMappingURL',
);
await testFor(
'./__source__/__compiled__/external/react-sources-extended/index-map/ContainingStringSourceMappingURL',
);
});
});
});
describe('parseHookNames worker', () => {
let inspectHooks;
let parseHookNames;
let workerizedParseSourceAndMetadataMock;
beforeEach(() => {
window.Worker = undefined;
workerizedParseSourceAndMetadataMock = jest.fn();
initFetchMock();
jest.mock('../parseHookNames/parseSourceAndMetadata.worker.js', () => {
return {
__esModule: true,
default: () => ({
parseSourceAndMetadata: workerizedParseSourceAndMetadataMock,
}),
};
});
inspectHooks =
require('react-debug-tools/src/ReactDebugHooks').inspectHooks;
parseHookNames = require('../parseHookNames').parseHookNames;
});
async function getHookNamesForComponent(Component, props = {}) {
const hooksTree = inspectHooks(Component, props, undefined);
const hookNames = await parseHookNames(hooksTree);
return hookNames;
}
it('should use worker', async () => {
const Component =
require('./__source__/__untransformed__/ComponentWithUseState').Component;
window.Worker = true;
jest.resetModules();
parseHookNames = require('../parseHookNames').parseHookNames;
await getHookNamesForComponent(Component);
expect(workerizedParseSourceAndMetadataMock).toHaveBeenCalledTimes(1);
});
});