|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import React from 'react'; |
| 6 | +import { screen } from '@testing-library/react'; |
| 7 | +import { createHistory, createMemorySource } from '@reach/router'; |
| 8 | +import { PromoQrMobile, PromoQrMobileIntegration } from '.'; |
| 9 | +import { IntegrationType } from '../../models/integrations'; |
| 10 | +import { renderWithRouter } from '../../models/mocks'; |
| 11 | +import GleanMetrics from '../../lib/glean'; |
| 12 | + |
| 13 | +jest.mock('../../lib/glean', () => ({ |
| 14 | + __esModule: true, |
| 15 | + default: { |
| 16 | + promoQrMobile: { |
| 17 | + view: jest.fn(), |
| 18 | + }, |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +// jsdom does not implement matchMedia |
| 23 | +const mockMatchMedia = jest.fn(); |
| 24 | +beforeAll(() => { |
| 25 | + Object.defineProperty(window, 'matchMedia', { |
| 26 | + writable: true, |
| 27 | + value: mockMatchMedia, |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +function createIntegration(type: IntegrationType): PromoQrMobileIntegration { |
| 32 | + return { type }; |
| 33 | +} |
| 34 | + |
| 35 | +function renderAtRoute( |
| 36 | + pathname: string, |
| 37 | + integration: PromoQrMobileIntegration |
| 38 | +) { |
| 39 | + const history = createHistory(createMemorySource(pathname)); |
| 40 | + return renderWithRouter(<PromoQrMobile integration={integration} />, { |
| 41 | + history, |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +describe('PromoQrMobile', () => { |
| 46 | + beforeEach(() => { |
| 47 | + jest.clearAllMocks(); |
| 48 | + mockMatchMedia.mockReturnValue({ matches: true }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('visibility based on integration type', () => { |
| 52 | + it('renders for web integrations', () => { |
| 53 | + renderAtRoute('/', createIntegration(IntegrationType.Web)); |
| 54 | + expect(screen.getByRole('complementary')).toBeInTheDocument(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('does not render for OAuth integrations', () => { |
| 58 | + renderAtRoute('/', createIntegration(IntegrationType.OAuthWeb)); |
| 59 | + expect(screen.queryByRole('complementary')).not.toBeInTheDocument(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('does not render for Sync integrations', () => { |
| 63 | + renderAtRoute('/', createIntegration(IntegrationType.OAuthNative)); |
| 64 | + expect(screen.queryByRole('complementary')).not.toBeInTheDocument(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('visibility based on route', () => { |
| 69 | + const webIntegration = createIntegration(IntegrationType.Web); |
| 70 | + |
| 71 | + it.each([ |
| 72 | + '/', |
| 73 | + '/signin', |
| 74 | + '/signin_totp_code', |
| 75 | + '/signin_recovery_choice', |
| 76 | + '/signin_recovery_code', |
| 77 | + '/signin_passwordless_code', |
| 78 | + '/signup', |
| 79 | + '/confirm_signup_code', |
| 80 | + '/inline_totp_setup', |
| 81 | + '/inline_recovery_setup', |
| 82 | + '/inline_recovery_key_setup', |
| 83 | + ])('renders on %s', (route) => { |
| 84 | + renderAtRoute(route, webIntegration); |
| 85 | + expect(screen.getByRole('complementary')).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + |
| 88 | + it.each([ |
| 89 | + '/reset_password', |
| 90 | + '/confirm_reset_password', |
| 91 | + '/complete_reset_password', |
| 92 | + '/settings', |
| 93 | + '/oauth', |
| 94 | + '/authorization', |
| 95 | + '/legal', |
| 96 | + ])('does not render on %s', (route) => { |
| 97 | + renderAtRoute(route, webIntegration); |
| 98 | + expect(screen.queryByRole('complementary')).not.toBeInTheDocument(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('Glean view event', () => { |
| 103 | + const webIntegration = createIntegration(IntegrationType.Web); |
| 104 | + |
| 105 | + it('fires the view event once on desktop', () => { |
| 106 | + mockMatchMedia.mockReturnValue({ matches: true }); |
| 107 | + renderAtRoute('/', webIntegration); |
| 108 | + expect(GleanMetrics.promoQrMobile.view).toHaveBeenCalledTimes(1); |
| 109 | + }); |
| 110 | + |
| 111 | + it('does not fire the view event on mobile viewports', () => { |
| 112 | + mockMatchMedia.mockReturnValue({ matches: false }); |
| 113 | + renderAtRoute('/', webIntegration); |
| 114 | + expect(GleanMetrics.promoQrMobile.view).not.toHaveBeenCalled(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('does not fire when integration is not web', () => { |
| 118 | + mockMatchMedia.mockReturnValue({ matches: true }); |
| 119 | + renderAtRoute('/', createIntegration(IntegrationType.OAuthWeb)); |
| 120 | + expect(GleanMetrics.promoQrMobile.view).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('does not fire on excluded routes', () => { |
| 124 | + mockMatchMedia.mockReturnValue({ matches: true }); |
| 125 | + renderAtRoute('/reset_password', webIntegration); |
| 126 | + expect(GleanMetrics.promoQrMobile.view).not.toHaveBeenCalled(); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('content', () => { |
| 131 | + it('renders Firefox logo, heading, description, and QR code', () => { |
| 132 | + renderAtRoute('/', createIntegration(IntegrationType.Web)); |
| 133 | + |
| 134 | + expect(screen.getByAltText('Firefox logo')).toBeInTheDocument(); |
| 135 | + expect(screen.getByText('Your phone. Your rules.')).toBeInTheDocument(); |
| 136 | + expect(screen.getByText('Scan to get the app')).toBeInTheDocument(); |
| 137 | + expect( |
| 138 | + screen.getByAltText(/QR code to download the Firefox mobile app/) |
| 139 | + ).toBeInTheDocument(); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments