From 9970f1a51eb19e11a7323d2bfb74228421a49e02 Mon Sep 17 00:00:00 2001 From: sherzod-bakhodirov Date: Tue, 17 Dec 2024 19:58:52 +0500 Subject: [PATCH] feat: add test to react native --- .../_post.spec.ts | 21 +++++++++++++++++-- .../_post.spec.ts | 16 ++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/@magic-sdk/react-native-bare/test/spec/react-native-webview-controller/_post.spec.ts b/packages/@magic-sdk/react-native-bare/test/spec/react-native-webview-controller/_post.spec.ts index a6e65327..d79fe53b 100644 --- a/packages/@magic-sdk/react-native-bare/test/spec/react-native-webview-controller/_post.spec.ts +++ b/packages/@magic-sdk/react-native-bare/test/spec/react-native-webview-controller/_post.spec.ts @@ -1,6 +1,8 @@ import { createModalNotReadyError } from '@magic-sdk/provider'; import { createReactNativeWebViewController } from '../../factories'; import { reactNativeStyleSheetStub } from '../../mocks'; +import { ReactNativeWebViewController } from '../../../src/react-native-webview-controller'; +import AsyncStorage from '@react-native-async-storage/async-storage'; beforeEach(() => { jest.resetAllMocks(); @@ -12,12 +14,17 @@ const emitStub = jest.fn(); jest.mock('react-native-event-listeners', () => { return { EventRegister: { - emit: emitStub, + emit: (...args: unknown[]) => emitStub(...args), addEventListener: jest.fn(), }, }; }); +jest.mock('@react-native-async-storage/async-storage', () => ({ + getItem: jest.fn(), + setItem: jest.fn(), +})); + test('Calls webView._post with the expected arguments', async () => { const overlay = createReactNativeWebViewController('http://example.com'); @@ -67,6 +74,16 @@ test('Emits msg_posted_after_inactivity_event when msgPostedAfterInactivity retu overlay.msgPostedAfterInactivity = () => true; await overlay._post({ thisIsData: 'hello world' }); - expect(emitStub).toBeCalledTimes(1); + expect(emitStub).toHaveBeenCalledTimes(1); expect(emitStub).toHaveBeenCalledWith('msg_posted_after_inactivity_event', { thisIsData: 'hello world' }); }); + +test('returns true when more than 5 minutes have passed since the last post', async () => { + const controller = createReactNativeWebViewController('http://example.com'); + + const sixMinutesAgo = new Date(Date.now() - 6 * 60 * 1000).toISOString(); + (AsyncStorage.getItem as jest.Mock).mockResolvedValue(sixMinutesAgo); + const result = await (controller as any).msgPostedAfterInactivity(); + expect(result).toBe(true); + expect(AsyncStorage.getItem).toHaveBeenCalledWith('lastMessageTime'); +}) \ No newline at end of file diff --git a/packages/@magic-sdk/react-native-expo/test/spec/react-native-webview-controller/_post.spec.ts b/packages/@magic-sdk/react-native-expo/test/spec/react-native-webview-controller/_post.spec.ts index a6e65327..152cb60d 100644 --- a/packages/@magic-sdk/react-native-expo/test/spec/react-native-webview-controller/_post.spec.ts +++ b/packages/@magic-sdk/react-native-expo/test/spec/react-native-webview-controller/_post.spec.ts @@ -1,6 +1,7 @@ import { createModalNotReadyError } from '@magic-sdk/provider'; import { createReactNativeWebViewController } from '../../factories'; import { reactNativeStyleSheetStub } from '../../mocks'; +import AsyncStorage from '@react-native-async-storage/async-storage'; beforeEach(() => { jest.resetAllMocks(); @@ -18,6 +19,11 @@ jest.mock('react-native-event-listeners', () => { }; }); +jest.mock('@react-native-async-storage/async-storage', () => ({ + getItem: jest.fn(), + setItem: jest.fn(), +})); + test('Calls webView._post with the expected arguments', async () => { const overlay = createReactNativeWebViewController('http://example.com'); @@ -70,3 +76,13 @@ test('Emits msg_posted_after_inactivity_event when msgPostedAfterInactivity retu expect(emitStub).toBeCalledTimes(1); expect(emitStub).toHaveBeenCalledWith('msg_posted_after_inactivity_event', { thisIsData: 'hello world' }); }); + +test('returns true when more than 5 minutes have passed since the last post', async () => { + const controller = createReactNativeWebViewController('http://example.com'); + + const sixMinutesAgo = new Date(Date.now() - 6 * 60 * 1000).toISOString(); + (AsyncStorage.getItem as jest.Mock).mockResolvedValue(sixMinutesAgo); + const result = await (controller as any).msgPostedAfterInactivity(); + expect(result).toBe(true); + expect(AsyncStorage.getItem).toHaveBeenCalledWith('lastMessageTime'); +}) \ No newline at end of file