From 4d31079ca22b04cc4d7cfe09953644e97b0d60f2 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sun, 24 Mar 2019 13:47:00 -0400 Subject: [PATCH] Updating InstallPrompt to use react-testing-library --- .../InstallPrompt/__snapshots__/test.js.snap | 61 ++++++++++++--- src/components/InstallPrompt/index.js | 5 +- src/components/InstallPrompt/test.js | 74 +++++++++---------- 3 files changed, 91 insertions(+), 49 deletions(-) diff --git a/src/components/InstallPrompt/__snapshots__/test.js.snap b/src/components/InstallPrompt/__snapshots__/test.js.snap index 1d40561..17abdcb 100644 --- a/src/components/InstallPrompt/__snapshots__/test.js.snap +++ b/src/components/InstallPrompt/__snapshots__/test.js.snap @@ -1,16 +1,57 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`InstallPrompt rendering 1`] = `""`; +exports[`InstallPrompt accepting install prompt 1`] = ` + + + + Add to Home Screen + + + +`; -exports[`InstallPrompt rendering after an install prompt has been requested 1`] = `""`; +exports[`InstallPrompt accepting install prompt 2`] = ``; + +exports[`InstallPrompt rejecting install prompt 1`] = ` + + + + Add to Home Screen + + + +`; + +exports[`InstallPrompt rejecting install prompt 2`] = ``; + +exports[`InstallPrompt rendering 1`] = ``; + +exports[`InstallPrompt rendering after an install prompt has been requested 1`] = ``; exports[`InstallPrompt rendering after an install prompt has been requested 2`] = ` - - - Add to Home Screen - - + + + + Add to Home Screen + + + `; diff --git a/src/components/InstallPrompt/index.js b/src/components/InstallPrompt/index.js index 43f263a..57b31d9 100644 --- a/src/components/InstallPrompt/index.js +++ b/src/components/InstallPrompt/index.js @@ -43,7 +43,10 @@ class InstallPrompt extends React.PureComponent { return null; } - return + return Add to Home Screen ; } diff --git a/src/components/InstallPrompt/test.js b/src/components/InstallPrompt/test.js index e0fc673..ea3edb2 100644 --- a/src/components/InstallPrompt/test.js +++ b/src/components/InstallPrompt/test.js @@ -1,88 +1,86 @@ import React from 'react'; -import { shallow } from 'enzyme'; +import { render, fireEvent } from 'react-testing-library'; import { InstallPrompt } from 'components/InstallPrompt'; describe('InstallPrompt', () => { test('rendering', () => { - const component = shallow( + const { asFragment } = render( ); - expect(component).toMatchSnapshot(); + expect(asFragment()).toMatchSnapshot(); }); test('rendering after an install prompt has been requested', () => { - const component = shallow( + const { asFragment } = render( ); - expect(component).toMatchSnapshot(); + expect(asFragment()).toMatchSnapshot(); - component.instance().handleInstallPrompt({ + const event = new Event('beforeinstallprompt', { prompt: jest.fn() }); + fireEvent(window, event); - expect(component).toMatchSnapshot(); + expect(asFragment()).toMatchSnapshot(); }); - test('adding and removing event listener', () => { + test('removing event listener', () => { jest.spyOn(window, 'addEventListener'); jest.spyOn(window, 'removeEventListener'); - const component = shallow( + const { unmount } = render( ); - const handleInstallPrompt = component.instance().handleInstallPrompt; - expect(window.addEventListener).toHaveBeenCalledWith( - 'beforeinstallprompt', - handleInstallPrompt); - - component.unmount(); + unmount(); expect(window.removeEventListener).toHaveBeenCalledWith( 'beforeinstallprompt', - handleInstallPrompt); + expect.any(Function)); }); test('accepting install prompt', async () => { - const component = shallow( + const { asFragment, getByTestId } = render( ); - const promptObj = { - prompt: jest.fn(), - userChoice: Promise.resolve() - }; - const eventObj = { preventDefault: jest.fn() }; + const promptEvent = new Event('beforeinstallprompt'); + promptEvent.prompt = jest.fn(); + promptEvent.userChoice = Promise.resolve(); + const clickEvent = new MouseEvent('click', { bubbles: true }); + jest.spyOn(clickEvent, 'preventDefault'); - component.instance().handleInstallPrompt(promptObj); - component.find('a').simulate('click', eventObj); + fireEvent(window, promptEvent); + expect(asFragment()).toMatchSnapshot(); + fireEvent(getByTestId('install'), clickEvent); // Allow async code to run await new Promise(resolve => setTimeout(resolve)); - expect(eventObj.preventDefault).toHaveBeenCalled(); - expect(promptObj.prompt).toHaveBeenCalled(); - expect(component.state('installPrompt')).toBeNull(); + expect(clickEvent.preventDefault).toHaveBeenCalled(); + expect(promptEvent.prompt).toHaveBeenCalled(); + expect(asFragment()).toMatchSnapshot(); }); test('rejecting install prompt', async () => { - const component = shallow( + const { asFragment, getByTestId } = render( ); - const promptObj = { - prompt: jest.fn(), - userChoice: Promise.reject() - }; - const eventObj = { preventDefault: jest.fn() }; + const promptEvent = new Event('beforeinstallprompt'); + promptEvent.prompt = jest.fn(); + promptEvent.userChoice = Promise.reject(); + const clickEvent = new MouseEvent('click', { bubbles: true }); + jest.spyOn(clickEvent, 'preventDefault'); - component.instance().handleInstallPrompt(promptObj); - component.find('a').simulate('click', eventObj); + fireEvent(window, promptEvent); + expect(asFragment()).toMatchSnapshot(); + fireEvent(getByTestId('install'), clickEvent); // Allow async code to run await new Promise(resolve => setTimeout(resolve)); - expect(eventObj.preventDefault).toHaveBeenCalled(); - expect(promptObj.prompt).toHaveBeenCalled(); - expect(component.state('installPrompt')).toBeNull(); + expect(clickEvent.preventDefault).toHaveBeenCalled(); + expect(promptEvent.prompt).toHaveBeenCalled(); + expect(asFragment()).toMatchSnapshot(); }); });