@brightlayer-ui/react-native-auth-workflow
Version:
Re-usable workflow components for Authentication and Registration within Eaton applications.
122 lines (121 loc) • 7.62 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Text } from 'react-native-paper';
import '@testing-library/jest-dom';
import { render, cleanup, screen, renderHook } from '@testing-library/react-native';
import { AuthContextProvider } from '../../contexts/AuthContext/provider.js';
import { useAuthContext } from '../../contexts/AuthContext/index.js';
import { authContextProviderProps } from '../../testUtils/index.js';
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
afterEach(cleanup);
describe('AuthContextProvider', () => {
it('should render AuthContextProvider without crashing', () => {
render(_jsx(AuthContextProvider, { ...authContextProviderProps, children: _jsx(Text, { children: "Hello Auth" }) }));
expect(screen.getByText('Hello Auth')).toBeTruthy();
});
it('should read values from the context', () => {
const wrapper = ({ children }) => (_jsx(AuthContextProvider, { ...authContextProviderProps, children: children }));
const { result } = renderHook(() => useAuthContext(), { wrapper });
expect(result.current.language).toBe('en');
});
it('should set values in the context', () => {
const wrapper = ({ children }) => (_jsx(AuthContextProvider, { ...authContextProviderProps, language: "es", children: children }));
const { result } = renderHook(() => useAuthContext(), { wrapper });
expect(result.current.language).not.toBe('en');
expect(result.current.language).toBe('es');
});
it('should render multiple children', () => {
render(_jsxs(AuthContextProvider, { ...authContextProviderProps, children: [_jsx(Text, { children: "Child 1" }), _jsx(Text, { children: "Child 2" })] }));
expect(screen.getByText('Child 1')).toBeTruthy();
expect(screen.getByText('Child 2')).toBeTruthy();
});
it('should work without custom i18n instance', () => {
const propsWithoutI18n = { ...authContextProviderProps };
delete propsWithoutI18n.i18n;
render(_jsx(AuthContextProvider, { ...propsWithoutI18n, children: _jsx(Text, { children: "No custom i18n" }) }));
expect(screen.getByText('No custom i18n')).toBeTruthy();
});
it('should add resource bundles when custom i18n is provided', async () => {
const customI18n = i18n.createInstance();
await customI18n.use(initReactI18next).init({
lng: 'en',
fallbackLng: 'en',
interpolation: {
escapeValue: false,
},
});
const addResourceBundleSpy = jest.spyOn(customI18n, 'addResourceBundle');
const { getByText } = render(_jsx(AuthContextProvider, { ...authContextProviderProps, i18n: customI18n, children: _jsx(Text, { children: "Custom i18n" }) }));
expect(getByText('Custom i18n')).toBeTruthy();
// Should add resource bundles for all supported languages (zh, en, fr, pt, es) x 2 namespaces (bluiAuth, bluiCommon)
expect(addResourceBundleSpy).toHaveBeenCalledTimes(10);
expect(addResourceBundleSpy).toHaveBeenCalledWith('zh', 'bluiAuth', expect.any(Object), true, false);
expect(addResourceBundleSpy).toHaveBeenCalledWith('en', 'bluiAuth', expect.any(Object), true, false);
expect(addResourceBundleSpy).toHaveBeenCalledWith('fr', 'bluiAuth', expect.any(Object), true, false);
expect(addResourceBundleSpy).toHaveBeenCalledWith('pt', 'bluiAuth', expect.any(Object), true, false);
expect(addResourceBundleSpy).toHaveBeenCalledWith('es', 'bluiAuth', expect.any(Object), true, false);
});
it('should use custom errorConfig when provided', () => {
const customErrorConfig = {
title: 'Custom Error Title',
error: 'Custom error message',
};
render(_jsx(AuthContextProvider, { ...authContextProviderProps, errorConfig: customErrorConfig, children: _jsx(Text, { children: "Custom error config" }) }));
expect(screen.getByText('Custom error config')).toBeTruthy();
});
it('should merge errorConfig with dialogConfig', () => {
const customErrorConfig = {
title: 'Custom Error',
error: 'Error message',
dialogConfig: {
dismissLabel: 'Close',
},
};
render(_jsx(AuthContextProvider, { ...authContextProviderProps, errorConfig: customErrorConfig, children: _jsx(Text, { children: "Dialog config test" }) }));
expect(screen.getByText('Dialog config test')).toBeTruthy();
});
it('should handle rememberMeDetails prop', () => {
const wrapper = ({ children }) => (_jsx(AuthContextProvider, { ...authContextProviderProps, rememberMeDetails: { email: 'test@example.com', rememberMe: true }, children: children }));
const { result } = renderHook(() => useAuthContext(), { wrapper });
expect(result.current.rememberMeDetails?.email).toBe('test@example.com');
expect(result.current.rememberMeDetails?.rememberMe).toBe(true);
});
it('should support different languages', () => {
const wrapper = ({ children }) => (_jsx(AuthContextProvider, { ...authContextProviderProps, language: "fr", children: children }));
const { result } = renderHook(() => useAuthContext(), { wrapper });
expect(result.current.language).toBe('fr');
});
it('should support Chinese language', () => {
render(_jsx(AuthContextProvider, { ...authContextProviderProps, language: "zh", children: _jsx(Text, { children: "Chinese language" }) }));
expect(screen.getByText('Chinese language')).toBeTruthy();
});
it('should support Portuguese language', () => {
render(_jsx(AuthContextProvider, { ...authContextProviderProps, language: "pt", children: _jsx(Text, { children: "Portuguese language" }) }));
expect(screen.getByText('Portuguese language')).toBeTruthy();
});
it('should provide all actions from context', () => {
const wrapper = ({ children }) => (_jsx(AuthContextProvider, { ...authContextProviderProps, children: children }));
const { result } = renderHook(() => useAuthContext(), { wrapper });
expect(result.current.actions).toBeDefined();
expect(result.current.actions.initiateSecurity).toBeDefined();
expect(result.current.actions.logIn).toBeDefined();
expect(result.current.actions.forgotPassword).toBeDefined();
expect(result.current.actions.verifyResetCode).toBeDefined();
expect(result.current.actions.setPassword).toBeDefined();
expect(result.current.actions.changePassword).toBeDefined();
});
it('should provide navigate function from context', () => {
const mockNavigate = jest.fn();
const wrapper = ({ children }) => (_jsx(AuthContextProvider, { ...authContextProviderProps, navigate: mockNavigate, children: children }));
const { result } = renderHook(() => useAuthContext(), { wrapper });
expect(result.current.navigate).toBeDefined();
expect(result.current.navigate).toBe(mockNavigate);
});
it('should provide routeConfig from context', () => {
const customRouteConfig = { LOGIN: '/login', FORGOT_PASSWORD: '/forgot' };
const wrapper = ({ children }) => (_jsx(AuthContextProvider, { ...authContextProviderProps, routeConfig: customRouteConfig, children: children }));
const { result } = renderHook(() => useAuthContext(), { wrapper });
expect(result.current.routeConfig).toBeDefined();
expect(result.current.routeConfig).toEqual(customRouteConfig);
});
});