From d9af19ca63e5b4f23667d7b2bb1851bef3301332 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Thu, 15 Feb 2018 17:33:14 -0500 Subject: [PATCH] Cleanup setup modules --- package.json | 4 +-- src/components/RavenError/index.js | 2 +- src/components/RavenError/test.js | 6 ++-- src/pages/404/browser.js | 7 +++-- src/pages/index/browser.js | 9 +++--- src/{ => setup}/analytics.js | 4 +-- src/setup/analytics.test.js | 37 +++++++++++++++++++++++ src/{sentry.js => setup/raven.js} | 2 +- src/setup/raven.test.js | 48 ++++++++++++++++++++++++++++++ src/{ => setup}/service-worker.js | 2 +- 10 files changed, 104 insertions(+), 17 deletions(-) rename src/{ => setup}/analytics.js (75%) create mode 100644 src/setup/analytics.test.js rename src/{sentry.js => setup/raven.js} (90%) create mode 100644 src/setup/raven.test.js rename src/{ => setup}/service-worker.js (93%) diff --git a/package.json b/package.json index dd8da5f..b02e092 100644 --- a/package.json +++ b/package.json @@ -44,8 +44,8 @@ "collectCoverageFrom": [ "src/**/*.js", "!src/prerender.js", - "!src/service-worker.js", - "!src/setup-jest.js", + "!src/setup/service-worker.js", + "!src/setup/jest.js", "!src/pages/**/config.js", "!src/pages/**/browser.js" ], diff --git a/src/components/RavenError/index.js b/src/components/RavenError/index.js index 79032e9..71cfaf2 100644 --- a/src/components/RavenError/index.js +++ b/src/components/RavenError/index.js @@ -1,7 +1,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import { translate, Trans } from 'react-i18next'; -import { Raven } from 'sentry'; +import Raven from 'raven-js'; import Message from 'components/Message'; diff --git a/src/components/RavenError/test.js b/src/components/RavenError/test.js index 3e30fb9..37996bb 100644 --- a/src/components/RavenError/test.js +++ b/src/components/RavenError/test.js @@ -1,10 +1,10 @@ +jest.mock('raven-js'); + import React from 'react'; import { shallow } from 'enzyme'; - -jest.mock('sentry'); +import Raven from 'raven-js'; import { RavenError } from 'components/RavenError'; -import { Raven } from 'sentry'; import translate from '__mocks__/translate'; const testError = { error: 'test error' }; diff --git a/src/pages/404/browser.js b/src/pages/404/browser.js index 74fcbb4..47ffb08 100644 --- a/src/pages/404/browser.js +++ b/src/pages/404/browser.js @@ -1,18 +1,19 @@ import React from 'react'; import ReactDOM from 'react-dom'; +import Raven from 'raven-js'; import Component from './Component'; import RavenBoundary from 'components/RavenBoundary'; import 'site.css'; import 'i18n'; -import { setupGA } from 'analytics'; -import { Raven, setupRaven } from 'sentry'; +import setupAnalytics from 'setup/analytics'; +import setupRaven from 'setup/raven'; setupRaven(); try { - setupGA(); + setupAnalytics(); ReactDOM.render( diff --git a/src/pages/index/browser.js b/src/pages/index/browser.js index e2deee8..d4d4198 100644 --- a/src/pages/index/browser.js +++ b/src/pages/index/browser.js @@ -1,19 +1,20 @@ import React from 'react'; import ReactDOM from 'react-dom'; +import Raven from 'raven-js'; import Component from './Component'; import RavenBoundary from 'components/RavenBoundary'; import 'site.css'; import 'i18n'; -import { setupServiceWorker } from 'service-worker'; -import { setupGA } from 'analytics'; -import { Raven, setupRaven } from 'sentry'; +import setupServiceWorker from 'setup/service-worker'; +import setupAnalytics from 'setup/analytics'; +import setupRaven from 'setup/raven'; setupRaven(); try { - setupGA(); + setupAnalytics(); if (process.env.NODE_ENV === 'production') { setupServiceWorker(); } diff --git a/src/analytics.js b/src/setup/analytics.js similarity index 75% rename from src/analytics.js rename to src/setup/analytics.js index b9595d4..7669152 100644 --- a/src/analytics.js +++ b/src/setup/analytics.js @@ -1,10 +1,10 @@ import ReactGA from 'react-ga'; -const setupGA = () => { +const setupAnalytics = () => { ReactGA.initialize(process.env.GA_PROPERTY, { debug: (process.env.NODE_ENV !== 'production') }); ReactGA.pageview(document.location.pathname); }; -export { setupGA }; +export default setupAnalytics; diff --git a/src/setup/analytics.test.js b/src/setup/analytics.test.js new file mode 100644 index 0000000..629a158 --- /dev/null +++ b/src/setup/analytics.test.js @@ -0,0 +1,37 @@ +jest.mock('react-ga'); + +import ReactGA from 'react-ga'; + +import setupAnalytics from './analytics'; + +describe('setupAnalytics', () => { + beforeEach(() => { + process.env.GA_PROPERTY = 'test property'; + }); + + it('initializes with the GA_PROPERTY', () => { + setupAnalytics(); + expect(ReactGA.initialize).toHaveBeenCalledWith('test property', expect.anything()); + }); + + it('enables debug mode in development', () => { + process.env.NODE_ENV = 'development'; + setupAnalytics(); + expect(ReactGA.initialize).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ + debug: true + })); + }); + + it('disabled debug mode in production', () => { + process.env.NODE_ENV = 'production'; + setupAnalytics(); + expect(ReactGA.initialize).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ + debug: false + })); + }); + + it('triggers a pageview', () => { + setupAnalytics(); + expect(ReactGA.pageview).toHaveBeenCalled(); + }); +}); diff --git a/src/sentry.js b/src/setup/raven.js similarity index 90% rename from src/sentry.js rename to src/setup/raven.js index 73faa96..9778ce8 100644 --- a/src/sentry.js +++ b/src/setup/raven.js @@ -9,4 +9,4 @@ const setupRaven = () => { }); }; -export { Raven, setupRaven }; +export default setupRaven; diff --git a/src/setup/raven.test.js b/src/setup/raven.test.js new file mode 100644 index 0000000..49c01f0 --- /dev/null +++ b/src/setup/raven.test.js @@ -0,0 +1,48 @@ +jest.mock('raven-js'); + +import Raven from 'raven-js'; + +import setupRaven from './raven'; + +describe('setupRaven', () => { + beforeEach(() => { + process.env.SENTRY_KEY='test key'; + }); + + it('intializes with the SENTRY_KEY', () => { + setupRaven(); + expect(Raven.config).toHaveBeenCalledWith('test key', expect.anything()); + }); + + it('sets the environment', () => { + process.env.NODE_ENV='test environment'; + setupRaven(); + expect(Raven.config).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ + environment: 'test environment' + })); + }); + + it('enables debug mode for development', () => { + process.env.NODE_ENV='development'; + setupRaven(); + expect(Raven.config).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ + debug: true + })); + }); + + it('disables debug mode for production', () => { + process.env.NODE_ENV='production'; + setupRaven(); + expect(Raven.config).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ + debug: false + })); + }); + + it('sets the release', () => { + process.env.BUILD_ID='test ID'; + setupRaven(); + expect(Raven.config).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ + release: 'test ID' + })); + }); +}); diff --git a/src/service-worker.js b/src/setup/service-worker.js similarity index 93% rename from src/service-worker.js rename to src/setup/service-worker.js index c9da17a..3e50f6d 100644 --- a/src/service-worker.js +++ b/src/setup/service-worker.js @@ -12,4 +12,4 @@ const setupServiceWorker = () => { } }; -export { setupServiceWorker }; +export default setupServiceWorker;