Do not inject servers state or actions

This commit is contained in:
Alejandro Celaya
2025-11-14 19:23:48 +01:00
parent ae7aea0e2c
commit a7f2d3224b
38 changed files with 292 additions and 375 deletions

View File

@@ -6,22 +6,19 @@ import type {
import { ImportServersBtnFactory } from '../../../src/servers/helpers/ImportServersBtn';
import type { ServersImporter } from '../../../src/servers/services/ServersImporter';
import { checkAccessibility } from '../../__helpers__/accessibility';
import { renderWithEvents } from '../../__helpers__/setUpTest';
import { renderWithStore } from '../../__helpers__/setUpTest';
describe('<ImportServersBtn />', () => {
const csvFile = new File([''], 'servers.csv', { type: 'text/csv' });
const onImportMock = vi.fn();
const createServersMock = vi.fn();
const importServersFromFile = vi.fn().mockResolvedValue([]);
const serversImporterMock = fromPartial<ServersImporter>({ importServersFromFile });
const ImportServersBtn = ImportServersBtnFactory(fromPartial({ ServersImporter: serversImporterMock }));
const setUp = (props: Partial<ImportServersBtnProps> = {}, servers: ServersMap = {}) => renderWithEvents(
<ImportServersBtn
servers={servers}
{...props}
createServers={createServersMock}
onImport={onImportMock}
/>,
const setUp = (props: Partial<ImportServersBtnProps> = {}, servers: ServersMap = {}) => renderWithStore(
<ImportServersBtn {...props} onImport={onImportMock} />,
{
initialState: { servers },
},
);
it('passes a11y checks', () => checkAccessibility(setUp()));
@@ -57,11 +54,8 @@ describe('<ImportServersBtn />', () => {
it('imports servers when file input changes', async () => {
const { user } = setUp();
const input = screen.getByTestId('csv-file-input');
await user.upload(input, csvFile);
await user.upload(screen.getByTestId('csv-file-input'), csvFile);
expect(importServersFromFile).toHaveBeenCalledTimes(1);
expect(createServersMock).toHaveBeenCalledTimes(1);
});
it.each([
@@ -78,26 +72,27 @@ describe('<ImportServersBtn />', () => {
id: 'existingserver-s.test',
};
const newServer: ServerData = { name: 'newServer', url: 'http://s.test/newUrl', apiKey: 'newApiKey' };
const { user } = setUp({}, { [existingServer.id]: existingServer });
const { user, store } = setUp({}, { [existingServer.id]: existingServer });
importServersFromFile.mockResolvedValue([existingServer, newServer]);
importServersFromFile.mockResolvedValue([existingServerData, newServer]);
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
await user.upload(screen.getByTestId('csv-file-input'), csvFile);
// Once the file is uploaded, non-duplicated servers are immediately created
expect(createServersMock).toHaveBeenCalledExactlyOnceWith([expect.objectContaining(newServer)]);
const { servers } = store.getState();
expect(Object.keys(servers)).toHaveLength(2);
expect(screen.getByRole('dialog')).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: btnName }));
// If duplicated servers are saved, there's one extra call
// If duplicated servers are saved, there's one extra server creation
if (savesDuplicatedServers) {
expect(createServersMock).toHaveBeenLastCalledWith([expect.objectContaining(existingServerData)]);
const { servers } = store.getState();
expect(Object.keys(servers)).toHaveLength(3);
}
// On import is called only once, no matter what
expect(onImportMock).toHaveBeenCalledOnce();
expect(createServersMock).toHaveBeenCalledTimes(savesDuplicatedServers ? 2 : 1);
});
});