Skip to content

Commit

Permalink
Adds event reporting when react-recurly is initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrudner committed Aug 6, 2020
1 parent 323ffd9 commit 0b6c128
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/provider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { version } from '../package.json';

export const RecurlyContext = React.createContext({ recurly: null });

Expand Down Expand Up @@ -72,6 +73,13 @@ export default class RecurlyProvider extends React.Component {

const { children, ...options } = this.props;
this._recurly = fetchRecurlyInstance(options);

if (!RecurlyProvider.hasReportedInitialization && this._recurly.report) {
this._recurly.ready(() => {
this._recurly.report('react-recurly', { version });
});
RecurlyProvider.hasReportedInitialization = true;
}
}

render() {
Expand Down
19 changes: 19 additions & 0 deletions test/provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { suppressConsoleErrors } from './support/helpers';

import { RecurlyProvider } from '../lib';
import { RecurlyContext } from '../lib/provider';
import { version } from '../package.json';
import isEqual from 'lodash/isEqual';

const api = `http://localhost:${process.env.PORT || 9877}`

Expand Down Expand Up @@ -39,6 +41,23 @@ describe('<RecurlyProvider />', function () {
});

describe('with a publicKey', function () {
it('reports a "react-recurly" event on initialization once', function () {
const reportSpy = jest.spyOn(global.recurly.Recurly.prototype, 'report');

render(
<>
<RecurlyProvider publicKey='test-public-key' api={api} />
<RecurlyProvider publicKey='test-public-key' api={api} />
<RecurlyProvider publicKey='test-public-key-2' api={api} />
</>
);

const expected = ['react-recurly', { version }];
const { calls } = reportSpy.mock;
const reactRecurlyEventCalls = calls.filter(call => isEqual(call, expected));
expect(reactRecurlyEventCalls.length).toBe(1);
});

it('does not throw an error', function () {
expect(() => {
render(<RecurlyProvider publicKey="test-public-key" api={api} />);
Expand Down

0 comments on commit 0b6c128

Please sign in to comment.