Skip to content

Commit 45c5214

Browse files
committed
test: adjust unit tests
1 parent bbf13ca commit 45c5214

File tree

3 files changed

+30
-104
lines changed

3 files changed

+30
-104
lines changed

src/core/components/ChatInput.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ const ChatInput = ({ setShouldAutoScroll }: ChatInputProps) => {
8686
const hasAvailableUploads = useAvailableUploads({
8787
uploadedFiles,
8888
maximumFileUploads,
89-
setUploadingState,
90-
resetUploadState,
89+
setUploadError,
9190
});
9291

9392
const buttonRef = useRef<HTMLButtonElement>(null);

src/core/hooks/useAvailableUploads.test.ts

+23-81
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { useAvailableUploads } from './useAvailableUploads';
33
import { renderHook } from '@testing-library/react';
44

55
describe('useAvailableUploads', () => {
6+
const setUploadError = vi.fn();
7+
68
beforeEach(() => {
79
vi.useFakeTimers();
810
});
@@ -13,30 +15,24 @@ describe('useAvailableUploads', () => {
1315
vi.useRealTimers();
1416
});
1517

16-
it('should return a function that returns true when imageIDs length is less than maximumFileUploads', () => {
17-
const setUploadingState = vi.fn();
18-
const resetUploadState = vi.fn();
19-
18+
it('no error when imageIDs length is less than maximumFileUploads', () => {
2019
const { result } = renderHook(() =>
2120
useAvailableUploads({
2221
uploadedFiles: [
2322
{ id: 'image1', fileName: 'mock 1', fileType: 'image/png' },
2423
{ id: 'image2', fileName: 'mock2', fileType: 'image/png' },
2524
],
2625
maximumFileUploads: 4,
27-
setUploadingState,
28-
resetUploadState,
26+
setUploadError,
2927
}),
3028
);
3129

3230
expect(result.current()).toBe(true);
33-
expect(setUploadingState).not.toHaveBeenCalled();
34-
expect(resetUploadState).not.toHaveBeenCalled();
31+
expect(setUploadError).not.toHaveBeenCalled();
3532
});
3633

37-
it('should return a function that returns false and sets error state when imageIDs length equals maximumFileUploads', () => {
38-
const setUploadingState = vi.fn();
39-
const resetUploadState = vi.fn();
34+
it('sets error state when imageIDs length equals maximumFileUploads', () => {
35+
const setUploadError = vi.fn();
4036

4137
const { result } = renderHook(() =>
4238
useAvailableUploads({
@@ -47,23 +43,15 @@ describe('useAvailableUploads', () => {
4743
{ id: 'image4', fileName: 'mock 4', fileType: 'image/png' },
4844
],
4945
maximumFileUploads: 4,
50-
setUploadingState,
51-
resetUploadState,
46+
setUploadError,
5247
}),
5348
);
5449

5550
expect(result.current()).toBe(false);
56-
expect(setUploadingState).toHaveBeenCalledWith({
57-
isActive: true,
58-
isSupportedFile: false,
59-
errorMessage: 'Maximum 4 files allowed!',
60-
});
51+
expect(setUploadError).toHaveBeenCalledWith('Maximum 4 files allowed!');
6152
});
6253

63-
it('should return a function that returns false and sets error state when imageIDs length exceeds maximumFileUploads', () => {
64-
const setUploadingState = vi.fn();
65-
const resetUploadState = vi.fn();
66-
54+
it('sets error state when imageIDs length exceeds maximumFileUploads', () => {
6755
const { result } = renderHook(() =>
6856
useAvailableUploads({
6957
uploadedFiles: [
@@ -74,50 +62,15 @@ describe('useAvailableUploads', () => {
7462
{ id: 'image5', fileName: 'mock 5', fileType: 'image/png' },
7563
],
7664
maximumFileUploads: 4,
77-
setUploadingState,
78-
resetUploadState,
65+
setUploadError,
7966
}),
8067
);
8168

8269
expect(result.current()).toBe(false);
83-
expect(setUploadingState).toHaveBeenCalledWith({
84-
isActive: true,
85-
isSupportedFile: false,
86-
errorMessage: 'Maximum 4 files allowed!',
87-
});
88-
});
89-
90-
it('should call resetUploadState after timeout when exceeding maximumFileUploads', () => {
91-
const setUploadingState = vi.fn();
92-
const resetUploadState = vi.fn();
93-
94-
const { result } = renderHook(() =>
95-
useAvailableUploads({
96-
uploadedFiles: [
97-
{ id: 'image1', fileName: 'mock 1', fileType: 'image/png' },
98-
{ id: 'image2', fileName: 'mock 2', fileType: 'image/png' },
99-
{ id: 'image3', fileName: 'mock 3', fileType: 'image/png' },
100-
{ id: 'image4', fileName: 'mock 4', fileType: 'image/png' },
101-
{ id: 'image5', fileName: 'mock 5', fileType: 'image/png' },
102-
],
103-
maximumFileUploads: 4,
104-
setUploadingState,
105-
resetUploadState,
106-
}),
107-
);
108-
109-
result.current();
110-
111-
expect(resetUploadState).not.toHaveBeenCalled();
112-
113-
vi.advanceTimersByTime(2000);
114-
115-
expect(resetUploadState).toHaveBeenCalledTimes(1);
70+
expect(setUploadError).toHaveBeenCalledWith('Maximum 4 files allowed!');
11671
});
11772

11873
it('should update error message based on maximumFileUploads value', () => {
119-
const setUploadingState = vi.fn();
120-
const resetUploadState = vi.fn();
12174
const customMaxUploads = 2;
12275

12376
const { result } = renderHook(() =>
@@ -127,36 +80,28 @@ describe('useAvailableUploads', () => {
12780
{ id: 'image2', fileName: 'mock 2', fileType: 'image/png' },
12881
],
12982
maximumFileUploads: customMaxUploads,
130-
setUploadingState,
131-
resetUploadState,
83+
setUploadError,
13284
}),
13385
);
13486

13587
result.current();
13688

137-
expect(setUploadingState).toHaveBeenCalledWith({
138-
isActive: true,
139-
isSupportedFile: false,
140-
errorMessage: `Maximum ${customMaxUploads} files allowed!`,
141-
});
89+
expect(setUploadError).toHaveBeenCalledWith(
90+
`Maximum ${customMaxUploads} files allowed!`,
91+
);
14292
});
14393

14494
it('should handle different input scenarios correctly', () => {
145-
const setUploadingState = vi.fn();
146-
const resetUploadState = vi.fn();
147-
14895
let { result } = renderHook(() =>
14996
useAvailableUploads({
15097
uploadedFiles: [],
15198
maximumFileUploads: 4,
152-
setUploadingState,
153-
resetUploadState,
99+
setUploadError,
154100
}),
155101
);
156102
expect(result.current()).toBe(true);
157103

158-
setUploadingState.mockClear();
159-
resetUploadState.mockClear();
104+
setUploadError.mockClear();
160105

161106
result = renderHook(() =>
162107
useAvailableUploads({
@@ -167,25 +112,22 @@ describe('useAvailableUploads', () => {
167112
{ id: 'image4', fileName: 'mock 4', fileType: 'image/png' },
168113
],
169114
maximumFileUploads: 4,
170-
setUploadingState,
171-
resetUploadState,
115+
setUploadError,
172116
}),
173117
).result;
174118
expect(result.current()).toBe(false);
175-
expect(setUploadingState).toHaveBeenCalled();
119+
expect(setUploadError).toHaveBeenCalled();
176120

177-
setUploadingState.mockClear();
178-
resetUploadState.mockClear();
121+
setUploadError.mockClear();
179122

180123
result = renderHook(() =>
181124
useAvailableUploads({
182125
uploadedFiles: [],
183126
maximumFileUploads: 0,
184-
setUploadingState,
185-
resetUploadState,
127+
setUploadError,
186128
}),
187129
).result;
188130
expect(result.current()).toBe(false);
189-
expect(setUploadingState).toHaveBeenCalled();
131+
expect(setUploadError).toHaveBeenCalled();
190132
});
191133
});

src/core/hooks/useAvailableUploads.ts

+6-21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import type { FileUpload, UploadingState } from '../components/ChatInput';
2-
import { Dispatch, SetStateAction, useCallback } from 'react';
1+
import type { FileUpload } from '../components/ChatInput';
2+
import { useCallback } from 'react';
33

44
interface UseAvailableUploadsParams {
55
uploadedFiles: FileUpload[];
66
maximumFileUploads: number;
7-
setUploadingState: Dispatch<SetStateAction<UploadingState>>;
8-
resetUploadState: () => void;
7+
setUploadError: (errorMessage: string) => void;
98
}
109

1110
/**
@@ -22,28 +21,14 @@ interface UseAvailableUploadsParams {
2221
export const useAvailableUploads = ({
2322
uploadedFiles,
2423
maximumFileUploads,
25-
setUploadingState,
26-
resetUploadState,
24+
setUploadError,
2725
}: UseAvailableUploadsParams): (() => boolean) => {
2826
return useCallback((): boolean => {
2927
if (uploadedFiles.length >= maximumFileUploads) {
30-
setUploadingState({
31-
isActive: true,
32-
isSupportedFile: false,
33-
errorMessage: `Maximum ${maximumFileUploads} files allowed!`,
34-
});
35-
36-
setTimeout(() => {
37-
resetUploadState();
38-
}, 2000);
28+
setUploadError(`Maximum ${maximumFileUploads} files allowed!`);
3929

4030
return false;
4131
}
4232
return true;
43-
}, [
44-
uploadedFiles.length,
45-
maximumFileUploads,
46-
setUploadingState,
47-
resetUploadState,
48-
]);
33+
}, [uploadedFiles.length, maximumFileUploads, setUploadError]);
4934
};

0 commit comments

Comments
 (0)