Files
cloudbeaver/webapp/packages/core-utils/src/getTextFileReadingProcess.test.ts
T
sergeyteleshevandmr-anton-t 78daa21c87 [CB] update frontend deps (#3973)
* dbeaver/pro#7748 updates yarn.locks

* fixes incorrect types

* updates helper libs

* updates vitest + fixes test cases incorrect mocks

* runs tests sequentially with document and windows objects to prevent mock collisions

* Revert "runs tests sequentially with document and windows objects to prevent mock collisions"

This reverts commit f871837df3.

* skips download test

* removes export from resetDeprecatedSettings

* updates react-data-grid to beta-59 and react to 19.2

* updates storybook

* pr fixes

* updates yarn lock

* removes skip for download test

* cleanups download ts with empty body html

* updates vitest for core-connections

* updates yarn.lock

* linter fix + mocks once

* fixes hotkeys

* reverts version of react-hotkeys-hook library

---------

Co-authored-by: mr-anton-t <42037741+mr-anton-t@users.noreply.github.com>
2025-12-18 12:49:11 +01:00

94 lines
2.8 KiB
TypeScript

/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2025 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { afterEach, beforeEach, describe, expect, it, vitest, type MockInstance } from 'vitest';
import { addKnownError, initKnownConsoleMessages } from '@cloudbeaver/tests-runner';
import { getTextFileReadingProcess } from './getTextFileReadingProcess.js';
describe('getTextFileReadingProcess', () => {
let file: File;
let mockFileReader: Partial<FileReader>;
let fileReaderMock: MockInstance<new (fileReader: FileReader) => FileReader>;
initKnownConsoleMessages(() => {
addKnownError(/Error: Read error/);
});
beforeEach(() => {
file = new File(['file content'], 'test.txt', { type: 'text/plain' });
mockFileReader = {
readAsText: vitest.fn(),
onload: null,
onerror: null,
onabort: null,
};
fileReaderMock = vitest.spyOn(window, 'FileReader').mockImplementation(function FileReader() {
return mockFileReader;
});
});
afterEach(() => {
fileReaderMock.mockRestore();
});
it('should return correct instances', () => {
const { promise, reader } = getTextFileReadingProcess(file);
expect(promise).toBeInstanceOf(Promise);
expect(reader).toBe(mockFileReader);
});
it('should resolve with file content on successful read', async () => {
const { promise } = getTextFileReadingProcess(file);
(mockFileReader.onload as any)({
target: { result: 'file content' },
});
await expect(promise).resolves.toBe('file content');
expect(mockFileReader.readAsText).toHaveBeenCalledWith(file);
});
it('should reject with an error if file has no data', async () => {
const { promise } = getTextFileReadingProcess(file);
const results = [null, undefined, ''];
for (const result of results) {
(mockFileReader.onload as any)({
target: { result: result },
});
await expect(promise).rejects.toThrow(`No data for the file: "${file.name}"`);
}
});
it('should reject with an error if reading fails', async () => {
const { consoleSpy } = await import('@cloudbeaver/tests-runner');
const { promise } = getTextFileReadingProcess(file);
const error = new Error('Read error');
(mockFileReader.onerror as any)({
target: { error: error },
});
await expect(promise).rejects.toThrow(`Error occurred reading file: "${file.name}"`);
expect(consoleSpy.error).toHaveBeenCalledOnce();
});
it('should reject if the read is aborted', async () => {
const { promise } = getTextFileReadingProcess(file);
(mockFileReader.onabort as any)();
await expect(promise).rejects.toThrow(`Reading "${file.name}" is aborted`);
});
});