| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import { vi, beforeEach } from 'vitest'
- /**
- * In-memory store for uni mocked storage APIs.
- * Cleared between tests via beforeEach.
- * Exposed as uni.__mockStorage so tests can pre-populate storage values
- * before store creation (e.g. simulating a saved token).
- */
- const mockStorage = {}
- /**
- * Mock all uni-app APIs used by stores and common pages.
- *
- * The storage mocks (getStorageSync / setStorageSync / removeStorageSync)
- * operate on the mockStorage object above, which persists across calls
- * within a test but is wiped before each test.
- *
- * Tests can push values into storage by setting:
- * uni.__mockStorage.theme = 'light'
- * uni.__mockStorage.token = 'my-token'
- */
- globalThis.uni = {
- /** Storage — backed by mockStorage */
- getStorageSync: vi.fn((key) => mockStorage[key]),
- setStorageSync: vi.fn((key, value) => { mockStorage[key] = value }),
- removeStorageSync: vi.fn((key) => { delete mockStorage[key] }),
- /** System info */
- getSystemInfoSync: vi.fn(() => ({ theme: 'dark' })),
- onThemeChange: vi.fn(),
- /** Tab bar */
- setTabBarStyle: vi.fn(),
- /** Navigation */
- getCurrentPages: vi.fn(() => []),
- navigateTo: vi.fn(),
- reLaunch: vi.fn(),
- /** Feedback */
- showModal: vi.fn(),
- showToast: vi.fn(),
- /** Network */
- request: vi.fn(),
- downloadFile: vi.fn(),
- uploadFile: vi.fn(),
- /** Exposed for test convenience */
- __mockStorage: mockStorage,
- }
- // ── Per-test cleanup ─────────────────────────────────────────────
- beforeEach(() => {
- // Wipe the in-memory storage so no state leaks between tests
- Object.keys(mockStorage).forEach((k) => delete mockStorage[k])
- // Reset call history and custom return-values for all vi.fn() mocks
- // (implementations provided in the factory are *not* removed)
- vi.clearAllMocks()
- })
|