From 2a4c1a134bfa189185fdfc9084eeb0da60e6fc58 Mon Sep 17 00:00:00 2001 From: Emma Sauerborn <70536670+esauerbo@users.noreply.github.com> Date: Thu, 16 Nov 2023 14:43:20 -0500 Subject: [PATCH] chore: switching mocks to spies (#4670) --- .../__tests__/InAppMessagingProvider.spec.tsx | 15 ++++------ .../useMessage/__tests__/useMessage.spec.ts | 28 ++++++++----------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/packages/react-core-notifications/src/InAppMessaging/context/InAppMessagingProvider/__tests__/InAppMessagingProvider.spec.tsx b/packages/react-core-notifications/src/InAppMessaging/context/InAppMessagingProvider/__tests__/InAppMessagingProvider.spec.tsx index b3343669c7f..0d32d3f7771 100644 --- a/packages/react-core-notifications/src/InAppMessaging/context/InAppMessagingProvider/__tests__/InAppMessagingProvider.spec.tsx +++ b/packages/react-core-notifications/src/InAppMessaging/context/InAppMessagingProvider/__tests__/InAppMessagingProvider.spec.tsx @@ -1,18 +1,13 @@ import React from 'react'; import TestRenderer, { ReactTestRenderer } from 'react-test-renderer'; -import { onMessageReceived } from 'aws-amplify/in-app-messaging'; +import * as InAppModule from 'aws-amplify/in-app-messaging'; import { RenderNothing } from '@aws-amplify/ui-react-core'; import { useInAppMessaging } from '../../../hooks/useInAppMessaging'; import { InAppMessagingContextType } from '../..'; import { InAppMessagingProvider } from '..'; -jest.mock('aws-amplify/in-app-messaging', () => ({ - ...jest.requireActual( - 'aws-amplify/in-app-messaging' - ), - onMessageReceived: jest.fn(), -})); +const onMessageReceivedSpy = jest.spyOn(InAppModule, 'onMessageReceived'); let onMessageReceivedCallback = null as unknown as InAppMessagingContextType['displayMessage']; @@ -42,7 +37,7 @@ describe('InAppMessagingProvider', () => { beforeEach(() => { jest.resetAllMocks(); - (onMessageReceived as jest.Mock).mockImplementation(mockOnMessageReceived); + onMessageReceivedSpy.mockImplementation(mockOnMessageReceived); TestRenderer.act(() => { renderer = TestRenderer.create( @@ -71,8 +66,8 @@ describe('InAppMessagingProvider', () => { }); it('registers a listener to InAppMessaging.onMessageReceived as expected', () => { - expect(onMessageReceived).toHaveBeenCalledTimes(1); - expect(onMessageReceived).toHaveBeenCalledWith( + expect(onMessageReceivedSpy).toHaveBeenCalledTimes(1); + expect(onMessageReceivedSpy).toHaveBeenCalledWith( expect.any(Function) as InAppMessagingContextType['displayMessage'] ); }); diff --git a/packages/react-core-notifications/src/InAppMessaging/hooks/useMessage/__tests__/useMessage.spec.ts b/packages/react-core-notifications/src/InAppMessaging/hooks/useMessage/__tests__/useMessage.spec.ts index b289ce66609..bea60ed2450 100644 --- a/packages/react-core-notifications/src/InAppMessaging/hooks/useMessage/__tests__/useMessage.spec.ts +++ b/packages/react-core-notifications/src/InAppMessaging/hooks/useMessage/__tests__/useMessage.spec.ts @@ -1,4 +1,4 @@ -import { notifyMessageInteraction } from 'aws-amplify/in-app-messaging'; +import * as InAppModule from 'aws-amplify/in-app-messaging'; import { ConsoleLogger as Logger } from 'aws-amplify/utils'; import { RenderNothing } from '@aws-amplify/ui-react-core'; import { useInAppMessaging } from '../../useInAppMessaging'; @@ -22,16 +22,10 @@ const infoSpy = jest.spyOn(Logger.prototype, 'info'); const mockUseInAppMessaging = useInAppMessaging as jest.Mock; const mockClearMessage = jest.fn(); -jest.mock('aws-amplify', () => ({ - Amplify: { configure: jest.fn() }, -})); - -jest.mock('aws-amplify/in-app-messaging', () => ({ - ...jest.requireActual( - 'aws-amplify/in-app-messaging' - ), - notifyMessageInteraction: jest.fn(), -})); +const notifyMessageInteractionSpy = jest.spyOn( + InAppModule, + 'notifyMessageInteraction' +); const header = { content: 'header one' }; const baseMessage: Partial = { @@ -189,8 +183,8 @@ describe('useMessage', () => { const { props } = useMessage({ components, onMessageAction }); (props as { onClose: () => void }).onClose(); - expect(notifyMessageInteraction).toHaveBeenCalledTimes(1); - expect(notifyMessageInteraction).toHaveBeenCalledWith({ + expect(notifyMessageInteractionSpy).toHaveBeenCalledTimes(1); + expect(notifyMessageInteractionSpy).toHaveBeenCalledWith({ type: 'messageDismissed', message, }); @@ -204,8 +198,8 @@ describe('useMessage', () => { (props as TestMessageProps).onDisplay(); - expect(notifyMessageInteraction).toHaveBeenCalledTimes(1); - expect(notifyMessageInteraction).toHaveBeenCalledWith({ + expect(notifyMessageInteractionSpy).toHaveBeenCalledTimes(1); + expect(notifyMessageInteractionSpy).toHaveBeenCalledWith({ message, type: 'messageDisplayed', }); @@ -222,8 +216,8 @@ describe('useMessage', () => { jest.runAllTimers(); - expect(notifyMessageInteraction).toHaveBeenCalledTimes(1); - expect(notifyMessageInteraction).toHaveBeenCalledWith({ + expect(notifyMessageInteractionSpy).toHaveBeenCalledTimes(1); + expect(notifyMessageInteractionSpy).toHaveBeenCalledWith({ message, type: 'messageActionTaken', });