Skip to content

Commit

Permalink
chore: switching mocks to spies (#4670)
Browse files Browse the repository at this point in the history
  • Loading branch information
esauerbo authored Nov 16, 2023
1 parent a097a8d commit 2a4c1a1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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<typeof import('aws-amplify/in-app-messaging')>(
'aws-amplify/in-app-messaging'
),
onMessageReceived: jest.fn(),
}));
const onMessageReceivedSpy = jest.spyOn(InAppModule, 'onMessageReceived');

let onMessageReceivedCallback =
null as unknown as InAppMessagingContextType['displayMessage'];
Expand Down Expand Up @@ -42,7 +37,7 @@ describe('InAppMessagingProvider', () => {
beforeEach(() => {
jest.resetAllMocks();

(onMessageReceived as jest.Mock).mockImplementation(mockOnMessageReceived);
onMessageReceivedSpy.mockImplementation(mockOnMessageReceived);

TestRenderer.act(() => {
renderer = TestRenderer.create(
Expand Down Expand Up @@ -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']
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<typeof import('aws-amplify/in-app-messaging')>(
'aws-amplify/in-app-messaging'
),
notifyMessageInteraction: jest.fn(),
}));
const notifyMessageInteractionSpy = jest.spyOn(
InAppModule,
'notifyMessageInteraction'
);

const header = { content: 'header one' };
const baseMessage: Partial<InAppMessage> = {
Expand Down Expand Up @@ -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,
});
Expand All @@ -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',
});
Expand All @@ -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',
});
Expand Down

0 comments on commit 2a4c1a1

Please sign in to comment.