From 6ddac11dc0aa159c7420bb4a48355ce72852d450 Mon Sep 17 00:00:00 2001 From: Alie Langston Date: Thu, 1 Oct 2020 12:53:21 -0400 Subject: [PATCH] added tracking events added testing --- src/id-verification/Camera.jsx | 18 +++++++++ src/id-verification/tests/Camera.test.jsx | 46 +++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/id-verification/Camera.jsx b/src/id-verification/Camera.jsx index 746af4d..d9a0b74 100644 --- a/src/id-verification/Camera.jsx +++ b/src/id-verification/Camera.jsx @@ -1,5 +1,6 @@ import React from 'react'; import PropTypes from 'prop-types'; +import { sendTrackEvent } from '@edx/frontend-platform/analytics'; import * as blazeface from '@tensorflow-models/blazeface'; import CameraPhoto, { FACING_MODES } from 'jslib-html5-camera-photo'; import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; @@ -37,6 +38,22 @@ class Camera extends React.Component { this.cameraPhoto.stopCamera(); } + sendEvent() { + let eventName = 'edx.id_verification'; + if (this.props.isPortrait) { + eventName += '.user_photo'; + } else { + eventName += '.id_photo'; + } + + if (this.state.shouldDetect) { + eventName += '.face_detection_enabled'; + } else { + eventName += '.face_detection_disabled'; + } + sendTrackEvent(eventName); + } + setDetection() { this.setState( { shouldDetect: !this.state.shouldDetect }, @@ -45,6 +62,7 @@ class Camera extends React.Component { this.setState({ isFinishedLoadingDetection: false }); this.startDetection(); } + this.sendEvent(); }, ); } diff --git a/src/id-verification/tests/Camera.test.jsx b/src/id-verification/tests/Camera.test.jsx index fbe64ff..66f8186 100644 --- a/src/id-verification/tests/Camera.test.jsx +++ b/src/id-verification/tests/Camera.test.jsx @@ -5,11 +5,15 @@ import '@testing-library/jest-dom/extend-expect'; import { render, cleanup, screen, act, fireEvent } from '@testing-library/react'; import { injectIntl, IntlProvider } from '@edx/frontend-platform/i18n'; import * as blazeface from '@tensorflow-models/blazeface'; +import * as analytics from '@edx/frontend-platform/analytics'; import { IdVerificationContext } from '../IdVerificationContext'; import Camera from '../Camera'; jest.mock('jslib-html5-camera-photo'); jest.mock('@tensorflow-models/blazeface'); +jest.mock('@edx/frontend-platform/analytics'); + +analytics.sendTrackEvent = jest.fn(); window.HTMLMediaElement.prototype.play = () => {}; @@ -135,4 +139,46 @@ describe('SubmittedPanel', () => { await fireEvent.click(checkbox); setTimeout(() => { expect(blazeface.load).toHaveBeenCalled(); }, 2000); }); + + it('sends tracking events on portrait photo page', async () => { + blazeface.load = jest.fn().mockResolvedValue({ estimateFaces: jest.fn().mockResolvedValue([]) }); + + await act(async () => render(( + + + + + + + + ))); + + await fireEvent.loadedData(screen.queryByTestId('video')); + const checkbox = await screen.findByLabelText('Enable Face Detection'); + await fireEvent.click(checkbox); + expect(analytics.sendTrackEvent).toHaveBeenCalledWith('edx.id_verification.user_photo.face_detection_enabled'); + await fireEvent.click(checkbox); + expect(analytics.sendTrackEvent).toHaveBeenCalledWith('edx.id_verification.user_photo.face_detection_disabled'); + }); + + it('sends tracking events on id photo page', async () => { + blazeface.load = jest.fn().mockResolvedValue({ estimateFaces: jest.fn().mockResolvedValue([]) }); + + await act(async () => render(( + + + + + + + + ))); + + await fireEvent.loadedData(screen.queryByTestId('video')); + const checkbox = await screen.findByLabelText('Enable Face Detection'); + await fireEvent.click(checkbox); + expect(analytics.sendTrackEvent).toHaveBeenCalledWith('edx.id_verification.id_photo.face_detection_enabled'); + await fireEvent.click(checkbox); + expect(analytics.sendTrackEvent).toHaveBeenCalledWith('edx.id_verification.id_photo.face_detection_disabled'); + }); });