55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import { act } from 'react-test-renderer';
|
|
|
|
import { useNavigationStore } from '../src/state';
|
|
|
|
jest.mock('@react-native-async-storage/async-storage', () => {
|
|
const storage = new Map<string, string>();
|
|
|
|
return {
|
|
__esModule: true,
|
|
default: {
|
|
getItem: jest.fn((key: string) => Promise.resolve(storage.get(key) ?? null)),
|
|
removeItem: jest.fn((key: string) => {
|
|
storage.delete(key);
|
|
return Promise.resolve();
|
|
}),
|
|
setItem: jest.fn((key: string, value: string) => {
|
|
storage.set(key, value);
|
|
return Promise.resolve();
|
|
}),
|
|
},
|
|
};
|
|
});
|
|
|
|
describe('useNavigationStore', () => {
|
|
beforeEach(() => {
|
|
useNavigationStore.setState({
|
|
currentRouteName: 'home',
|
|
pendingRouteName: null,
|
|
});
|
|
});
|
|
|
|
it('captures the current and pending routes in persisted state', () => {
|
|
act(() => {
|
|
useNavigationStore.getState().setPendingRoute('settings');
|
|
});
|
|
|
|
const partialize = useNavigationStore.persist.getOptions().partialize;
|
|
|
|
expect(partialize?.(useNavigationStore.getState())).toEqual({
|
|
currentRouteName: 'home',
|
|
pendingRouteName: 'settings',
|
|
});
|
|
});
|
|
|
|
it('completes navigation by promoting the pending route to current route', () => {
|
|
act(() => {
|
|
useNavigationStore.getState().setPendingRoute('settings');
|
|
useNavigationStore.getState().completeNavigation('settings');
|
|
});
|
|
|
|
expect(useNavigationStore.getState().currentRouteName).toBe('settings');
|
|
expect(useNavigationStore.getState().pendingRouteName).toBeNull();
|
|
});
|
|
});
|