import * as React from 'react' import { Linking as RNLinking } from 'react-native' import { AudioAdapter, Clipboard, ClipboardAdapter, DocumentPickerAdapter, ImagePickerAdapter, LinkingAdapter, VideoAdapter, } from '../../../utils/native_adapters' import { ChatAdapters, DocumentPicker, Linking, Haptic, } from '../../../utils/native_adapters/configuration' import { HapticAdapter } from '../../../utils/native_adapters/haptic' import { VideoPlayerHandle, VideoPlayerProps } from '../../../utils/native_adapters/video' describe('ChatAdapters', () => { const getStringAsync = jest.fn(async () => '') const setStringAsync = jest.fn(async (_: string) => {}) const clipboard = new ClipboardAdapter({ getStringAsync, setStringAsync, }) const audio = new AudioAdapter({ useAudio: (_: string) => ({ play: jest.fn(), pause: jest.fn(), isPlaying: false, duration: 0, position: 0, AudioPlayer: null, }), }) const Player = React.forwardRef((_props, _ref) => null) const video = new VideoAdapter({ Player, }) const imagePicker = new ImagePickerAdapter({ openCameraAsync: async () => ({ canceled: true, assets: null }), openImageLibraryAsync: async () => ({ canceled: true, assets: null }), }) const haptic = new HapticAdapter({ impactLight: jest.fn(), impactMedium: jest.fn(), impactHeavy: jest.fn(), rigid: jest.fn(), soft: jest.fn(), notificationSuccess: jest.fn(), notificationWarning: jest.fn(), notificationError: jest.fn(), }) it('should be defined', () => { expect(ChatAdapters).toBeDefined() }) it('should configure the clipboard', () => { ChatAdapters.configure({ clipboard, audio, video, imagePicker, haptic, }) expect(Clipboard).toEqual(clipboard) }) describe('linking adapter', () => { describe('defaults', () => { it('should be configured', () => { ChatAdapters.configure({ clipboard, audio, video, imagePicker, }) expect(Linking).toBeDefined() expect(Linking.openURL).toBeDefined() expect(Linking.canOpenURL).toBeDefined() }) describe('openUrl', () => { Linking.openURL('https://www.google.com') expect(RNLinking.openURL).toHaveBeenCalledWith('https://www.google.com') }) }) describe('Adapter is bound to Linking context', () => { const linking = new LinkingAdapter({ openURL: async function (url: string) { expect(url).toBe('https://www.google.com') expect(this).toBe(RNLinking) }, canOpenURL: async function (url: string) { expect(url).toBe('https://www.google.com') expect(this).toBe(RNLinking) return true }, }) linking.openURL('https://www.google.com') linking.canOpenURL('https://www.google.com') }) }) describe('document picker adapter', () => { it('uses the configured adapter when provided', () => { const documentPicker = new DocumentPickerAdapter({ openAsync: jest.fn(async () => ({ canceled: true as true, assets: null })), }) ChatAdapters.configure({ clipboard, audio, video, imagePicker, documentPicker, }) expect(DocumentPicker).toEqual(documentPicker) expect(DocumentPicker.configured).toBe(true) }) it('falls back to an unconfigured adapter when omitted', async () => { ChatAdapters.configure({ clipboard, audio, video, imagePicker, }) expect(DocumentPicker).toBeDefined() expect(DocumentPicker.configured).toBe(false) await expect(DocumentPicker.openAsync()).resolves.toEqual({ canceled: true, assets: null, }) }) }) describe('haptic adapter', () => { it('should configure the haptic adapter', () => { ChatAdapters.configure({ clipboard, audio, video, imagePicker, haptic, }) expect(Haptic).toEqual(haptic) }) it('should be configured with defaults', () => { ChatAdapters.configure({ clipboard, audio, video, imagePicker, }) expect(Haptic).toBeDefined() expect(Haptic.impactLight).toBeDefined() expect(Haptic.impactMedium).toBeDefined() expect(Haptic.impactHeavy).toBeDefined() expect(Haptic.rigid).toBeDefined() expect(Haptic.soft).toBeDefined() expect(Haptic.notificationSuccess).toBeDefined() expect(Haptic.notificationWarning).toBeDefined() expect(Haptic.notificationError).toBeDefined() }) }) })