Skip to content

Commit

Permalink
WIP: fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Billie He committed Sep 28, 2024
1 parent c7485c9 commit ddaefde
Show file tree
Hide file tree
Showing 74 changed files with 1,842 additions and 1,315 deletions.
5 changes: 5 additions & 0 deletions jest.components.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import React from 'react';

import { jest } from '@jest/globals';

jest.mock('@app/shared/lib/constants', () => ({
...jest.requireActual('@app/shared/lib/constants'),
STORE_ENCRYPTION_KEY: '12345',
}));

jest.mock('@georstat/react-native-image-cache', () => {
return {
CachedImage: () => <></>,
Expand Down
2 changes: 2 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const jestConfig: JestConfigWithTsJest = {
'<rootDir>/assetsTransformer.js',
'^uuid$': require.resolve('uuid'),
},
restoreMocks: true,
clearMocks: true,
};

export default jestConfig;
40 changes: 33 additions & 7 deletions jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ jest.mock('@react-native-community/geolocation', () =>

jest.mock('react-native-sensors', () => jest.mock('react-native-sensors'));

jest.mock('mixpanel-react-native', () => jest.mock('mixpanel-react-native'));

jest.mock('react-native-tcp-socket', () => {
return {
createConnection: () => {
Expand Down Expand Up @@ -210,11 +208,6 @@ jest.mock('react-native-webview', () => {
};
});

jest.mock('@app/shared/lib/constants', () => ({
...jest.requireActual('@app/shared/lib/constants'),
STORE_ENCRYPTION_KEY: '12345',
}));

jest.mock('react-i18next', () => ({
useTranslation: jest.fn().mockImplementation(() => ({
t: jest.fn().mockImplementation(key => key),
Expand All @@ -241,6 +234,39 @@ jest.mock('@react-navigation/native', () => ({
useFocusEffect: jest.fn(),
}));

class MockMixpanel {
constructor() {}
init() {}
track() {}
identify() {}
getPeople() {}
reset() {}
}

jest.mock('mixpanel-react-native', () => jest.mock('mixpanel-react-native'));

jest.mock('mixpanel-react-native', () => ({
Mixpanel: MockMixpanel,
}));

class MockReactNativeLDClient {
constructor() {}
init() {}
identify() {}
boolVariation() {}
on() {}
}

const MockAutoEnvAttributes = {
Disabled: 0,
Enabled: 1,
};

jest.mock('@launchdarkly/react-native-client-sdk', () =>
jest.mock('@launchdarkly/react-native-client-sdk'),
);

jest.mock('@launchdarkly/react-native-client-sdk', () => ({
ReactNativeLDClient: MockReactNativeLDClient,
AutoEnvAttributes: MockAutoEnvAttributes,
}));
13 changes: 8 additions & 5 deletions src/app/ui/AppProvider/AnalyticsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { FC, PropsWithChildren, useEffect } from 'react';

import { AnalyticsService } from '@app/shared/lib/analytics/AnalyticsService';
import { getDefaultAnalyticsService } from '@app/shared/lib/analytics/analyticsServiceInstance';
import { useSystemBootUp } from '@app/shared/lib/contexts/SplashContext';
import { getDefaultLogger } from '@app/shared/lib/services/loggerInstance';

export const AnalyticsProvider: FC<PropsWithChildren> = ({ children }) => {
const { onModuleInitialized } = useSystemBootUp();

useEffect(() => {
AnalyticsService.init().then(() => {
getDefaultLogger().log('[AnalyticsProvider]: Initialized');
getDefaultAnalyticsService()
.init()
.then(() => {
getDefaultLogger().log('[AnalyticsProvider]: Initialized');

onModuleInitialized('analytics');
});
onModuleInitialized('analytics');
})
.catch(console.error);
}, [onModuleInitialized]);

return <>{children}</>;
Expand Down
27 changes: 14 additions & 13 deletions src/app/ui/AppProvider/FeatureFlagsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ import {
} from '@launchdarkly/react-native-client-sdk';

import { useSystemBootUp } from '@app/shared/lib/contexts/SplashContext';
import { FeatureFlagsService } from '@app/shared/lib/featureFlags/FeatureFlagsService';
import { getDefaultFeatureFlagsService } from '@app/shared/lib/featureFlags/featureFlagsServiceInstance';
import { getDefaultLogger } from '@app/shared/lib/services/loggerInstance';

export const FeatureFlagsProvider: FC<PropsWithChildren> = ({ children }) => {
const { onModuleInitialized } = useSystemBootUp();
const [ldClient, setClient] = useState<ReactNativeLDClient>();

useEffect(() => {
FeatureFlagsService.init()
.then(client => {
getDefaultLogger().log('[FeatureFlagsProvider]: Initialized');

setClient(client);
onModuleInitialized('featureFlags');
})
.catch(error => {
getDefaultLogger().error(
`[FeatureFlagsProvider]: Failed to initialize\n${error}`,
);
});
let client: ReactNativeLDClient | undefined;
try {
client = getDefaultFeatureFlagsService().init();
} catch (err) {
getDefaultLogger().error(
`[FeatureFlagsProvider]: Failed to initialize\n${err as never}`,
);
}
if (client) {
getDefaultLogger().log('[FeatureFlagsProvider]: Initialized');
setClient(client);
onModuleInitialized('featureFlags');
}
}, [onModuleInitialized, setClient]);

return (
Expand Down
8 changes: 3 additions & 5 deletions src/app/ui/AppProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import { SafeAreaProvider } from 'react-native-safe-area-context';
import Toast from 'react-native-toast-message';

import { LocalizationProvider } from '@app/entities/localization/ui/LocalizationProvider';
import {
AnalyticsService,
MixEvents,
} from '@app/shared/lib/analytics/AnalyticsService';
import { getDefaultAnalyticsService } from '@app/shared/lib/analytics/analyticsServiceInstance';
import { MixEvents } from '@app/shared/lib/analytics/IAnalyticsService';
import { getDefaultLogger } from '@app/shared/lib/services/loggerInstance';

import { AnalyticsProvider } from './AnalyticsProvider';
Expand Down Expand Up @@ -40,7 +38,7 @@ export const AppProvider: FC<PropsWithChildren> = ({ children }) => {
const onLoadingFinished = () => {
getDefaultLogger().log('[AppProvider]: App loaded');

AnalyticsService.track(MixEvents.AppOpen);
getDefaultAnalyticsService().track(MixEvents.AppOpen);

setIsBootingUp(false);
};
Expand Down
8 changes: 3 additions & 5 deletions src/entities/activity/lib/hooks/useRetryUpload.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useState } from 'react';

import {
AnalyticsService,
MixEvents,
} from '@app/shared/lib/analytics/AnalyticsService';
import { getDefaultAnalyticsService } from '@app/shared/lib/analytics/analyticsServiceInstance';
import { MixEvents } from '@app/shared/lib/analytics/IAnalyticsService';
import { getDefaultUploadObservable } from '@app/shared/lib/observables/uploadObservableInstance';

import { showUploadErrorAlert } from '../alerts';
Expand Down Expand Up @@ -31,7 +29,7 @@ export const useRetryUpload = ({

showUploadErrorAlert({
onRetry: async () => {
AnalyticsService.track(MixEvents.RetryButtonPressed);
getDefaultAnalyticsService().track(MixEvents.RetryButtonPressed);
try {
setIsAlertOpened(false);

Expand Down
4 changes: 2 additions & 2 deletions src/entities/activity/lib/services/UploadItemPreprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mapAppletDetailsFromDto } from '@app/entities/applet/model/mappers';
import { QueryDataUtils } from '@app/shared/api/services/QueryDataUtils';
import { FeatureFlagsKeys } from '@app/shared/lib/featureFlags/FeatureFlags.types';
import { FeatureFlagsService } from '@app/shared/lib/featureFlags/FeatureFlagsService';
import { getDefaultFeatureFlagsService } from '@app/shared/lib/featureFlags/featureFlagsServiceInstance';
import { getDefaultQueryClient } from '@app/shared/lib/queryClient/queryClientInstance';
import { getDefaultLogger } from '@app/shared/lib/services/loggerInstance';
import { IPreprocessor } from '@app/shared/lib/types/service';
Expand Down Expand Up @@ -31,7 +31,7 @@ export class UploadItemPreprocessor implements IPreprocessor<UploadItem> {

if (
appletDetails.consentsCapabilityEnabled &&
FeatureFlagsService.evaluateFlag(
getDefaultFeatureFlagsService().evaluateFlag(
FeatureFlagsKeys.enableConsentsCapability,
)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,22 @@ import { AnswersQueueService, UploadItem } from '../AnswersQueueService';

const notifyMock = { notify: () => {} };

const storageMock = {
addOnValueChangedListener: jest.fn(),
getAllKeys: jest.fn(),
getString: jest.fn(),
set: jest.fn(),
delete: jest.fn(),
} as unknown as MMKV;

const getAllKeysMock = jest.fn();
const getStringMock = jest.fn();
const setMock = jest.fn();
const deleteMock = jest.fn();

jest.mock('@shared/lib/storages', () => ({
createStorage: jest.fn(),
createSecureStorage: jest.fn().mockReturnValue({
addOnValueChangedListener: jest.fn().mockImplementation((f: () => void) => {
f();
}),
getAllKeys: () => getAllKeysMock() as Array<string>,
getString: (id: string) => getStringMock(id) as string,
set: (key: string, item: UploadItem) => setMock(key, item) as void,
delete: (key: string) => deleteMock(key) as void,
const storageMock = {
addOnValueChangedListener: jest.fn().mockImplementation((f: () => void) => {
f();
}),
}));
getAllKeys: () => getAllKeysMock() as Array<string>,
getString: (id: string) => getStringMock(id) as string,
set: (key: string, item: UploadItem) => setMock(key, item) as void,
delete: (key: string) => deleteMock(key) as void,
} as unknown as MMKV;

describe('Test AnswersQueueService', () => {
afterEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});

it('Should pick an object when some keys exist', () => {
getAllKeysMock.mockReturnValue(['10', '15', '7']);

Expand Down
Loading

0 comments on commit ddaefde

Please sign in to comment.