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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rum-react): use correct path when route is path array #800

Merged
merged 4 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
139 changes: 83 additions & 56 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@
"puppeteer": "^1.20.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-router": "^5.2.0",
Copy link
Member Author

Choose a reason for hiding this comment

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

path array is supported from version 5, so updated to latest to test. We dont use any breaking changes so should be good here.

"react-router-dom": "^5.2.0",
"regenerator-runtime": "^0.13.3",
"rimraf": "^3.0.0",
"serve-index": "^1.9.1",
Expand Down
27 changes: 22 additions & 5 deletions packages/rum-react/src/get-with-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ function isReactClassComponent(Component) {
return !!(prototype && prototype.isReactComponent)
}

/**
* If the path is given as array, use the computed path
* to get the current transaction name
*/
function getTransactionName(name, props) {
const { match = {} } = props

if (Array.isArray(name) && match.path) {
return match.path
}
return name
}

/**
* Usage:
* - Pure function: `withTransaction('name','route-change')(Component)`
Expand Down Expand Up @@ -90,7 +103,7 @@ function getWithTransaction(apm) {
* start the transaction only on component mounting
*/
const [transaction] = React.useState(() =>
apm.startTransaction(name, type, {
apm.startTransaction(getTransactionName(name, props), type, {
managed: true,
canReuse: true
})
Expand Down Expand Up @@ -127,10 +140,14 @@ function getWithTransaction(apm) {
* we won't be able to capture what happens in componentDidMount of child
* components since the parent component is mounted after child
*/
this.transaction = apm.startTransaction(name, type, {
managed: true,
canReuse: true
})
this.transaction = apm.startTransaction(
getTransactionName(name, this.props),
type,
{
managed: true,
canReuse: true
}
)
}

componentDidMount() {
Expand Down
43 changes: 43 additions & 0 deletions packages/rum-react/test/specs/get-apm-route.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,49 @@ describe('ApmRoute', function() {
)
})

it('should work correctly with path array in props', function() {
const ApmRoute = getApmRoute(apmBase)

const transactionService = serviceFactory.getService('TransactionService')
spyOn(transactionService, 'startTransaction')

const Home = () => 'home'
const Topics = () => 'Topics'
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved

const rendered = mount(
<Router initialEntries={['/', '/topic1', '/topic2']}>
<ApmRoute path={['/']} component={Home} />
<ApmRoute path={['/topic1', '/topic2']} component={Topics} />
</Router>
)
expect(transactionService.startTransaction).toHaveBeenCalledWith(
'/',
'route-change',
{
canReuse: true,
managed: true
}
)
expect(rendered.text()).toBe('home')
const history = rendered.find(Home).props().history
history.push({ pathname: '/topic2' })

expect(transactionService.startTransaction).toHaveBeenCalledWith(
'/topic2',
'route-change',
{
canReuse: true,
managed: true
}
)
expect(transactionService.startTransaction).toHaveBeenCalledTimes(2)
/**
* Should not create transaction as component is not rerendered
*/
history.push({ pathname: '/topic1' })
expect(transactionService.startTransaction).toHaveBeenCalledTimes(2)
})

it('should not trigger full rerender on query change', function() {
const ApmRoute = getApmRoute(apmBase)
const transactionService = serviceFactory.getService('TransactionService')
Expand Down