summaryrefslogtreecommitdiffstats
path: root/src/utils/__tests__/fileUpload.spec.js
blob: fe8541e664bbbd15ea56d16d8b0dccd88545bc55 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {
	getFileExtension,
	extractFileName,
	findUniquePath,
} from '../fileUpload.js'

const client = {
	exists: jest.fn(),
}

describe('fileUpload', () => {
	describe('getFileExtension', () => {
		it('should return correct file extension when it exists in the path', () => {
			const path = 'file.mock.txt'
			const extension = getFileExtension(path)
			expect(extension).toBe('.txt')
		})

		it('should return an empty string when no file extension exists in the path', () => {
			const path = 'file'
			const extension = getFileExtension(path)
			expect(extension).toBe('')
		})
	})

	describe('extractFileName', () => {
		it('should return the file name as-is when there is no extension or digit suffix', () => {
			const path = 'file'
			const fileName = extractFileName(path)
			expect(fileName).toBe('file')
		})

		it('should return the correctly extracted file name without extension and digit suffix', () => {
			const paths = ['file (1).txt', 'file (1)', 'file.txt', 'file (10).txt', 'file 1.txt', 'file (1) (2).txt', 'file (N).txt']
			const fileNames = paths.map(path => extractFileName(path))
			expect(fileNames).toStrictEqual(['file', 'file', 'file', 'file', 'file 1', 'file (1)', 'file (N)'])
		})
	})

	describe('findUniquePath', () => {
		const userRoot = '/files/userid/'
		const path = 'file.txt'

		afterEach(() => {
			jest.clearAllMocks()
		})

		it('should return the input path if it does not exist in the destination folder', async () => {
			// Arrange
			client.exists.mockResolvedValue(false) // Simulate resolving unique path on 1st attempt

			// Act
			const result = await findUniquePath(client, userRoot, path)

			// Assert
			expect(result).toBe(path)
			expect(client.exists).toHaveBeenCalledWith(userRoot + path)
		})

		it('should return a unique path when the input path already exists in the destination folder', async () => {
			// Arrange
			const existingPath = 'file (2).txt'
			const uniquePath = 'file (3).txt'
			client.exists
				.mockResolvedValueOnce(true)
				.mockResolvedValueOnce(true)
				.mockResolvedValueOnce(false) // Simulate resolving unique path on 3rd attempt

			// Act
			const result = await findUniquePath(client, userRoot, path)

			// Assert
			expect(result).toBe(uniquePath)
			expect(client.exists).toHaveBeenNthCalledWith(1, userRoot + path)
			expect(client.exists).toHaveBeenNthCalledWith(2, userRoot + existingPath)
			expect(client.exists).toHaveBeenNthCalledWith(3, userRoot + uniquePath)
		})
	})
})