Skip to content

Commit

Permalink
feat: add test to react native
Browse files Browse the repository at this point in the history
  • Loading branch information
sherzod-bakhodirov committed Dec 17, 2024
1 parent e09ddd3 commit 9970f1a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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');

Expand Down Expand Up @@ -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');
})
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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');

Expand Down Expand Up @@ -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');
})

0 comments on commit 9970f1a

Please sign in to comment.