-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
useAvgTimeToProduction.test.ts
48 lines (45 loc) · 1.81 KB
/
useAvgTimeToProduction.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { useAvgTimeToProduction } from './useAvgTimeToProduction';
import { renderHook } from '@testing-library/react-hooks';
describe('useAvgTimeToProduction', () => {
test('returns 0 when projectsData is empty', () => {
const projectsData = {};
const { result } = renderHook(() =>
useAvgTimeToProduction(projectsData),
);
expect(result.current).toBe(0);
});
test('calculates average time to production based on the latest date correctly', () => {
const projectsData = {
project1: [
{ timeToProduction: 10, date: '2023-01-01' },
{ timeToProduction: 20, date: '2023-02-01' },
],
project2: [
{ timeToProduction: 15, date: '2023-01-15' },
{ timeToProduction: 25, date: '2023-02-15' },
],
} as any;
const { result } = renderHook(() =>
useAvgTimeToProduction(projectsData),
);
// Expect average of the latest timeToProductions (20 from project1 and 25 from project2)
expect(result.current).toBe(22.5);
});
test('ignores projects without time to production data in their latest entries', () => {
const projectsData = {
project1: [
{ timeToProduction: 10, date: '2023-01-01' },
{ timeToProduction: 20, date: '2023-02-01' },
],
project2: [
{ date: '2023-01-15' },
{ timeToProduction: 25, date: '2023-01-10' },
],
} as any;
const { result } = renderHook(() =>
useAvgTimeToProduction(projectsData),
);
// Since project2's latest entry doesn't have timeToProduction, only project1's latest is considered
expect(result.current).toBe(20);
});
});