-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #719 from rollbar/wj-react-example
Add React example, with test automation
- Loading branch information
Showing
13 changed files
with
505 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["env", "react"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# React Rollbar Integration | ||
|
||
This project demonstrates rollbar.js in a basic React app configuration. | ||
|
||
## Rollbar configuration | ||
|
||
To send live reports to Rollbar, replace `POST_CLIENT_ITEM_TOKEN` in index.js | ||
with your valid client token before building your webpack bundle. | ||
|
||
## Node modules | ||
|
||
Run `npm install` to install node modules. | ||
|
||
## Build | ||
|
||
Run `npm run build` to build the project. The build artifacts will be stored in the `dist/` directory. | ||
|
||
Run `npm run start` to launch index.html in a browser. | ||
|
||
## Preparing for rollbar.js tests | ||
|
||
(For rollbar.js maintainers) | ||
|
||
Rollbar.js test automation includes tests that load and exercise this example app. | ||
For those tests to work, main.js must be available and up to date in ./examples/webpack/dist/. | ||
If the example app has changed or changes to rollbar.js need to be pulled in, | ||
update and commit a new main.js. | ||
|
||
``` | ||
# Build the rollbar.js dist if needed. | ||
npm run build | ||
# Prepare the example's npm bundle. | ||
cd examples/react && npm install | ||
# Build the output files. | ||
npm run build | ||
# The rollbar.js dist is no longer needed, and can be reverted. | ||
cd ../.. && git checkout dist | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>React Example</title> | ||
</head> | ||
<body> | ||
<section id="index"></section> | ||
<script type="text/javascript" src="/examples/react/dist/main.js"></script></body> | ||
</html> |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "react-example", | ||
"version": "0.0.0", | ||
"scripts": { | ||
"build": "webpack --mode development --build", | ||
"start": "webpack-dev-server --mode development --open" | ||
}, | ||
"private": true, | ||
"devDependencies": { | ||
"babel-core": "^6.26.3", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-preset-react": "^6.24.1", | ||
"html-webpack-plugin": "^3.2.0", | ||
"webpack": "^4.29.6", | ||
"webpack-cli": "^3.3.0", | ||
"webpack-dev-server": "^3.2.1" | ||
}, | ||
"dependencies": { | ||
"babel-loader": "^7.1.5", | ||
"react": "^16.8.6", | ||
"react-dom": "^16.8.6", | ||
"rollbar": "file:../.." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>React Example</title> | ||
</head> | ||
<body> | ||
<section id="index"></section> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import Rollbar from "rollbar"; | ||
|
||
class App extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
rollbar: new Rollbar({ | ||
accessToken: 'POST_CLIENT_ITEM_TOKEN', | ||
captureUncaught: true, | ||
captureUnhandledRejections: true, | ||
}) | ||
}; | ||
|
||
this.logInfo = this.logInfo.bind(this); | ||
this.throwError = this.throwError.bind(this); | ||
} | ||
|
||
logInfo() { | ||
// Example log event using the rollbar object. | ||
this.state.rollbar.info('react test log'); | ||
} | ||
|
||
throwError() { | ||
// Example error, which will be reported to rollbar. | ||
throw new Error('react test error'); | ||
} | ||
|
||
render() { | ||
return ( | ||
<React.Fragment> | ||
<h1>Rollbar Example for React</h1> | ||
<button id='rollbar-info' onClick={ this.logInfo }>Log Info</button> | ||
<button id='throw-error' onClick={ this.throwError }>ThrowError</button> | ||
</React.Fragment> | ||
); | ||
} | ||
} | ||
|
||
ReactDOM.render(<App />, document.getElementById("index")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const HtmlWebPackPlugin = require("html-webpack-plugin"); | ||
|
||
const htmlPlugin = new HtmlWebPackPlugin({ | ||
template: "./src/index.html", | ||
filename: "./index.html" | ||
}); | ||
|
||
module.exports = (_env, argv) => ({ | ||
output: { | ||
// rollbar.js tests require modified asset path. | ||
// Detect whether running JIT or building the webpack bundle. | ||
publicPath: (argv.build ? '/examples/react/dist/' : '') | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/, | ||
exclude: /node_modules/, | ||
use: { | ||
loader: "babel-loader" | ||
} | ||
} | ||
] | ||
}, | ||
plugins: [htmlPlugin] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* globals expect */ | ||
/* globals describe */ | ||
/* globals it */ | ||
/* globals sinon */ | ||
|
||
describe('react app', function() { | ||
this.timeout(4000); | ||
|
||
before(function (done) { | ||
// Load the HTML page. | ||
document.write(window.__html__['examples/react/dist/index.html']); | ||
|
||
// Set a timer before stubbing the XHR server, else it will interfere with | ||
// scripts loaded from the HTML page. | ||
setTimeout(function() { | ||
// Stub the xhr interface. | ||
window.server = sinon.createFakeServer(); | ||
|
||
done(); | ||
}, 3000); | ||
}); | ||
|
||
after(function () { | ||
window.server.restore(); | ||
}); | ||
|
||
function stubResponse(server) { | ||
server.respondWith('POST', 'api/1/item', | ||
[ | ||
200, | ||
{ 'Content-Type': 'application/json' }, | ||
'{"err": 0, "result":{ "uuid": "d4c7acef55bf4c9ea95e4fe9428a8287"}}' | ||
] | ||
); | ||
} | ||
|
||
it('should send a valid log event', function(done) { | ||
var server = window.server; | ||
|
||
stubResponse(server); | ||
server.requests.length = 0; | ||
|
||
var element = document.getElementById('rollbar-info'); | ||
element.click(); | ||
server.respond(); | ||
|
||
var body = JSON.parse(server.requests[0].requestBody); | ||
|
||
expect(body.access_token).to.eql('POST_CLIENT_ITEM_TOKEN'); | ||
expect(body.data.body.message.body).to.eql('react test log'); | ||
|
||
done(); | ||
}); | ||
|
||
it('should report uncaught error', function(done) { | ||
var server = window.server; | ||
|
||
stubResponse(server) | ||
server.requests.length = 0; | ||
|
||
var element = document.getElementById('throw-error'); | ||
element.click(); | ||
server.respond(); | ||
|
||
var body = JSON.parse(server.requests[0].requestBody); | ||
|
||
expect(body.access_token).to.eql('POST_CLIENT_ITEM_TOKEN'); | ||
expect(body.data.body.trace.exception.message).to.eql('react test error'); | ||
|
||
done(); | ||
}); | ||
}); |