Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/__tests__/main.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import setup from '../index';
import { Component, createRef } from 'react';
import { Component, createRef, useCallback } from 'react';
import { render } from '@testing-library/react';
import '@testing-library/jest-dom';

Expand Down Expand Up @@ -60,6 +60,22 @@ class NoClickHandler extends Component {
NoClickHandler.displayName = 'NoClickHandler';

describe('logrocket-react', () => {
function FunctionComponentWithoutDisplayName(props) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: Would it be possible to create some format of factory function to create these function components to reduce duplication?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah, definitely possible. I'll see what I can whip up!

const ref = useCallback((element) => {
if (element) element.click();
});
return <div ref={ref} />;
}

function FunctionComponentWithDisplayName(props) {
const ref = useCallback((element) => {
if (element) element.click();
});
return <div ref={ref} />;
}

FunctionComponentWithDisplayName.displayName = 'FCWithDisplayName';

let clickEvents;

beforeAll(() => {
Expand Down Expand Up @@ -102,4 +118,23 @@ describe('logrocket-react', () => {
expect(clickEvents).toHaveLength(1);
expect(clickEvents[0].__lrName).toEqual(['NoClickHandler']);
});

describe('given a function component', function () {
describe('without a display name', function () {
it('it reports the function name instead', function () {
render(<FunctionComponentWithoutDisplayName />);
expect(clickEvents).toHaveLength(1);
expect(clickEvents[0].__lrName).toEqual([
'FunctionComponentWithoutDisplayName',
]);
});
});
describe('with a display name', function () {
it('it reports the function name instead', function () {
render(<FunctionComponentWithDisplayName />);
expect(clickEvents).toHaveLength(1);
expect(clickEvents[0].__lrName).toEqual(['FCWithDisplayName']);
});
});
});
});
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default function setupReact() {
while (currentElement) {
var name =
typeof currentElement.elementType === 'function' &&
currentElement.elementType.displayName;
(currentElement.elementType.displayName ||
currentElement.elementType.name);
if (name) {
names.push(name);
}
Expand Down