Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/__tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, test, mock, afterEach, afterAll } from 'bun:test';

// Must set __DEV__ before importing context.ts
const _origDEV = (globalThis as any).__DEV__;
(globalThis as any).__DEV__ = true;

const mockUseContext = mock(() => ({}));

mock.module('react', () => {
return {
createContext: mock((defaultValue) => defaultValue),
useContext: mockUseContext,
};
});

// Import context after setting up mocks
const { useUpdate } = await import('../context');
const { default: i18n } = await import('../i18n');

describe('context', () => {
afterAll(() => {
(globalThis as any).__DEV__ = _origDEV;
});

afterEach(() => {
mockUseContext.mockClear();
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

test('useUpdate throws error when used outside UpdateProvider in __DEV__', () => {
mockUseContext.mockReturnValue({});

expect(() => useUpdate()).toThrow(i18n.t('error_use_update_outside_provider'));
});

test('useUpdate returns context when used inside UpdateProvider', () => {
const mockContext = { client: {} };
mockUseContext.mockReturnValue(mockContext);

expect(useUpdate()).toBe(mockContext as any);
});
});