Skip to content

Commit 85a187a

Browse files
author
Alejandro Caicedo
committed
refactor: Clean up test files and improve mock implementations for animations and Firebase services
1 parent 542b660 commit 85a187a

12 files changed

Lines changed: 63 additions & 95 deletions

File tree

__tests__/modules/authentication/infrastructure/auth.mock.service.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('AuthMockService', () => {
170170

171171
it('should call callback with null when no user', async () => {
172172
const callback = jest.fn();
173-
const unsubscribe = authMockService.onAuthStateChanged(callback);
173+
authMockService.onAuthStateChanged(callback);
174174

175175
expect(callback).toHaveBeenCalledWith(null);
176176
});

__tests__/modules/firebase/application/firestore.hooks.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('firestore.hooks', () => {
4949
(firestoreService.create as jest.Mock).mockResolvedValue(mockDoc);
5050

5151
const { mutate } = useCreateDocument();
52-
const result = await mutate({ payload: { collection: 'users', data: { name: 'Test' } } });
52+
await mutate({ payload: { collection: 'users', data: { name: 'Test' } } });
5353

5454
expect(firestoreService.create).toHaveBeenCalled();
5555
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['firestore'] });

__tests__/modules/firebase/infrastructure/firestore.service.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ jest.mock('@react-native-firebase/firestore', () => ({
1717

1818
import {
1919
getFirestore,
20-
collection,
21-
doc,
2220
addDoc,
2321
setDoc,
2422
getDoc,
2523
getDocs,
2624
updateDoc,
2725
deleteDoc,
28-
query,
2926
where,
3027
orderBy,
3128
limit,
@@ -244,7 +241,10 @@ describe('FirebaseFirestoreService', () => {
244241
});
245242

246243
expect(where).toHaveBeenCalledWith('status', '==', 'active');
247-
expect(result!.docs).toHaveLength(1);
244+
expect(result).not.toBeInstanceOf(Error);
245+
if (result && !(result instanceof Error)) {
246+
expect(result.docs).toHaveLength(1);
247+
}
248248
});
249249

250250
it('should list documents with orderBy', async () => {

__tests__/modules/firebase/infrastructure/storage.service.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ jest.mock('@react-native-firebase/storage', () => ({
1010

1111
import {
1212
getStorage,
13-
ref,
1413
putFile,
1514
getDownloadURL,
1615
deleteObject,

__tests__/modules/products/application/product.mutations.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jest.mock('@modules/products/domain/product.adapter', () => ({
1414
}));
1515

1616
jest.mock('@modules/core/application/app.storage', () => ({
17-
useAppStorage: jest.fn((selector) => ({
17+
useAppStorage: jest.fn((_selector) => ({
1818
show: jest.fn(),
1919
})),
2020
}));
@@ -33,7 +33,6 @@ const mockUseQueryClient = useQueryClient as jest.Mock;
3333
const mockProductService = productService as jest.Mocked<typeof productService>;
3434

3535
describe('product.mutations', () => {
36-
const mockShow = jest.fn();
3736
const mockInvalidateQueries = jest.fn();
3837

3938
beforeEach(() => {
@@ -57,11 +56,11 @@ describe('product.mutations', () => {
5756

5857
describe('useProductCreate', () => {
5958
it('should create product and invalidate queries on success', async () => {
60-
const mockProduct = { id: '1', name: 'New Product' };
59+
const mockProduct = { id: '1', name: 'New Product', description: 'Desc', price: 100, createdAt: new Date(), updatedAt: new Date() };
6160
mockProductService.create.mockResolvedValue(mockProduct);
6261

6362
const { mutate } = useProductCreate();
64-
await mutate({ name: 'New Product', price: 100 });
63+
await mutate({ name: 'New Product', description: 'Desc', price: 100 });
6564

6665
expect(mockProductService.create).toHaveBeenCalled();
6766
expect(mockInvalidateQueries).toHaveBeenCalledWith({
@@ -70,11 +69,11 @@ describe('product.mutations', () => {
7069
});
7170

7271
it('should show success toast on create', async () => {
73-
const mockProduct = { id: '1', name: 'New Product' };
72+
const mockProduct = { id: '1', name: 'New Product', description: 'Desc', price: 100, createdAt: new Date(), updatedAt: new Date() };
7473
mockProductService.create.mockResolvedValue(mockProduct);
7574

7675
const { mutate } = useProductCreate();
77-
await mutate({ name: 'New Product', price: 100 });
76+
await mutate({ name: 'New Product', description: 'Desc', price: 100 });
7877

7978
// Toast is called via useAppStorage mock
8079
});
@@ -86,17 +85,17 @@ describe('product.mutations', () => {
8685
const { mutate } = useProductCreate();
8786

8887
// The mutation will throw because the service returns an Error
89-
await expect(mutate({ name: 'Test', price: 100 })).rejects.toThrow();
88+
await expect(mutate({ name: 'Test', description: 'Desc', price: 100 })).rejects.toThrow();
9089
});
9190
});
9291

9392
describe('useProductUpdate', () => {
9493
it('should update product and invalidate queries on success', async () => {
95-
const mockProduct = { id: '1', name: 'Updated Product' };
94+
const mockProduct = { id: '1', name: 'Updated Product', description: 'Desc', price: 200, createdAt: new Date(), updatedAt: new Date() };
9695
mockProductService.update.mockResolvedValue(mockProduct);
9796

9897
const { mutate } = useProductUpdate();
99-
await mutate({ id: '1', form: { name: 'Updated Product', price: 200 } });
98+
await mutate({ id: '1', form: { name: 'Updated Product', description: 'Desc', price: 200 } });
10099

101100
expect(mockProductService.update).toHaveBeenCalledWith('1', expect.any(Object));
102101
expect(mockInvalidateQueries).toHaveBeenCalledWith({
@@ -114,7 +113,7 @@ describe('product.mutations', () => {
114113
const { mutate } = useProductUpdate();
115114

116115
await expect(
117-
mutate({ id: '1', form: { name: 'Test', price: 100 } }),
116+
mutate({ id: '1', form: { name: 'Test', description: 'Desc', price: 100 } }),
118117
).rejects.toThrow();
119118
});
120119
});

__tests__/modules/products/application/product.queries.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ describe('product.queries', () => {
5858
});
5959

6060
it('should call productService.getAll in queryFn', async () => {
61-
const mockProducts = [{ id: '1', name: 'Product 1' }];
61+
const mockProducts = [{ id: '1', name: 'Product 1', description: 'Desc', price: 100, createdAt: new Date(), updatedAt: new Date() }];
6262
mockProductService.getAll.mockResolvedValue(mockProducts);
6363
mockUseQuery.mockImplementation(async ({ queryFn }) => {
64-
const data = await queryFn();
65-
return { data, isLoading: false };
64+
await queryFn();
65+
return { data: mockProducts, isLoading: false };
6666
});
6767

68-
const result = useProducts();
68+
useProducts();
6969

7070
expect(mockProductService.getAll).toHaveBeenCalled();
7171
});
@@ -114,7 +114,7 @@ describe('product.queries', () => {
114114
});
115115

116116
it('should call productService.getById in queryFn', async () => {
117-
const mockProduct = { id: '1', name: 'Product 1' };
117+
const mockProduct = { id: '1', name: 'Product 1', description: 'Desc', price: 100, createdAt: new Date(), updatedAt: new Date() };
118118
mockProductService.getById.mockResolvedValue(mockProduct);
119119
mockUseQuery.mockImplementation(async ({ queryFn }) => {
120120
const data = await queryFn();

__tests__/theme/hooks/useAnimatedLoop.test.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@ import { renderHook } from '@testing-library/react-native';
22
import { Animated } from 'react-native';
33
import { useAnimatedLoop } from '../../../src/theme/hooks/useAnimatedLoop';
44

5-
// Mock Animated methods
6-
jest.spyOn(Animated, 'timing').mockReturnValue({
5+
// Mock Animated methods with proper CompositeAnimation interface
6+
const mockAnimation = {
77
start: jest.fn(),
88
stop: jest.fn(),
9-
} as any);
9+
reset: jest.fn(),
10+
};
1011

11-
jest.spyOn(Animated, 'loop').mockImplementation((animation: any) => ({
12-
start: jest.fn(),
13-
stop: jest.fn(),
14-
}));
12+
jest.spyOn(Animated, 'timing').mockReturnValue(mockAnimation as any);
1513

16-
jest.spyOn(Animated, 'sequence').mockImplementation((animations: any) => ({
17-
start: jest.fn(),
18-
stop: jest.fn(),
19-
}));
14+
jest.spyOn(Animated, 'loop').mockReturnValue(mockAnimation as any);
15+
16+
jest.spyOn(Animated, 'sequence').mockReturnValue(mockAnimation as any);
2017

2118
describe('useAnimatedLoop', () => {
2219
beforeEach(() => {

__tests__/theme/hooks/useFadeScale.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { renderHook } from '@testing-library/react-native';
22
import { Animated } from 'react-native';
33
import { useFadeScale } from '../../../src/theme/hooks/useFadeScale';
44

5-
// Mock Animated methods
6-
jest.spyOn(Animated, 'timing').mockReturnValue({
5+
// Mock Animated methods with proper CompositeAnimation interface
6+
const mockAnimation = {
77
start: jest.fn(),
8-
} as any);
8+
stop: jest.fn(),
9+
reset: jest.fn(),
10+
};
911

10-
jest.spyOn(Animated, 'spring').mockReturnValue({
11-
start: jest.fn(),
12-
} as any);
12+
jest.spyOn(Animated, 'timing').mockReturnValue(mockAnimation as any);
1313

14-
jest.spyOn(Animated, 'parallel').mockImplementation((animations: any) => ({
15-
start: jest.fn(),
16-
}));
14+
jest.spyOn(Animated, 'spring').mockReturnValue(mockAnimation as any);
15+
16+
jest.spyOn(Animated, 'parallel').mockReturnValue(mockAnimation as any);
1717

1818
describe('useFadeScale', () => {
1919
beforeEach(() => {

__tests__/theme/hooks/useFadeSlide.test.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,18 @@ import { renderHook, act } from '@testing-library/react-native';
22
import { Animated } from 'react-native';
33
import { useFadeSlide } from '../../../src/theme/hooks/useFadeSlide';
44

5-
// Mock Animated methods
5+
// Mock Animated methods with proper CompositeAnimation interface
66
const mockStart = jest.fn((callback?: () => void) => callback?.());
7-
const mockSetValue = jest.fn();
87

9-
jest.spyOn(Animated, 'timing').mockImplementation(
10-
() =>
11-
({
12-
start: mockStart,
13-
}) as any,
14-
);
15-
16-
jest.spyOn(Animated, 'parallel').mockImplementation((animations: any) => ({
8+
const mockAnimation = {
179
start: mockStart,
18-
}));
10+
stop: jest.fn(),
11+
reset: jest.fn(),
12+
};
13+
14+
jest.spyOn(Animated, 'timing').mockReturnValue(mockAnimation as any);
1915

20-
// Mock Animated.Value
21-
jest.spyOn(Animated, 'Value').mockImplementation(() => ({
22-
setValue: mockSetValue,
23-
interpolate: jest.fn(),
24-
}));
16+
jest.spyOn(Animated, 'parallel').mockReturnValue(mockAnimation as any);
2517

2618
describe('useFadeSlide', () => {
2719
beforeEach(() => {

__tests__/theme/hooks/useFocusFadeIn.test.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,16 @@ jest.mock('@react-navigation/native', () => ({
1010
}),
1111
}));
1212

13-
// Mock Animated methods
14-
const mockStart = jest.fn();
15-
16-
jest.spyOn(Animated, 'timing').mockImplementation(
17-
() =>
18-
({
19-
start: mockStart,
20-
}) as any,
21-
);
22-
23-
jest.spyOn(Animated, 'parallel').mockImplementation((animations: any) => ({
24-
start: mockStart,
25-
}));
13+
// Mock Animated methods with proper CompositeAnimation interface
14+
const mockAnimation = {
15+
start: jest.fn(),
16+
stop: jest.fn(),
17+
reset: jest.fn(),
18+
};
2619

27-
// Mock Animated.Value
28-
jest.spyOn(Animated, 'Value').mockImplementation(() => ({
29-
setValue: jest.fn(),
30-
interpolate: jest.fn(),
31-
}));
20+
jest.spyOn(Animated, 'timing').mockReturnValue(mockAnimation as any);
21+
22+
jest.spyOn(Animated, 'parallel').mockReturnValue(mockAnimation as any);
3223

3324
describe('useFocusFadeIn', () => {
3425
beforeEach(() => {

0 commit comments

Comments
 (0)