UNPKG

@yoroi/common

Version:

The Common package of Yoroi SDK

44 lines (43 loc) 2.15 kB
import React from 'react'; import renderer from 'react-test-renderer'; import { SyncStorageProvider, useSyncStorage } from './sync-storage-reactjs'; // Update with the actual module path import { mountMMKVStorage } from '../adapters/mmkv-storage'; const rootStorage = mountMMKVStorage({ path: '/' }); describe('SyncStorageProvider and useSyncStorage Tests', () => { test('SyncStorageProvider provides storage context', () => { const TestComponent = () => { const storage = useSyncStorage(); return /*#__PURE__*/React.createElement("div", null, storage ? 'Storage Available' : 'Storage Unavailable'); }; const tree = renderer.create( /*#__PURE__*/React.createElement(SyncStorageProvider, { storage: rootStorage }, /*#__PURE__*/React.createElement(TestComponent, null))); const treeInstance = tree.root; const textElement = treeInstance.findByType('div'); expect(textElement.props.children).toBe('Storage Available'); }); test('SyncStorageProvider provides the default rootStorage context', () => { const TestComponent = () => { const storage = useSyncStorage(); return /*#__PURE__*/React.createElement("div", null, storage ? 'Storage Available' : 'Storage Unavailable'); }; const tree = renderer.create( /*#__PURE__*/React.createElement(SyncStorageProvider, null, /*#__PURE__*/React.createElement(TestComponent, null))); const treeInstance = tree.root; const textElement = treeInstance.findByType('div'); expect(textElement.props.children).toBe('Storage Available'); }); test('useSyncStorage throws error without SyncStorageProvider', () => { const InvalidComponent = () => { useSyncStorage(); return /*#__PURE__*/React.createElement("div", null, "Invalid Component"); }; // Suppress console error caused by the 'invalid' function const originalError = console.error; console.error = jest.fn(); expect(() => renderer.create( /*#__PURE__*/React.createElement(InvalidComponent, null))).toThrow('Missing SyncStorageProvider'); console.error = originalError; }); }); //# sourceMappingURL=sync-storage-reactjs.test.js.map