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

@@ -5,7 +5,7 @@ import { Router } from 'react-router';
import { CreateServerFactory } from '../../src/servers/CreateServer';
import type { ServersMap } from '../../src/servers/data';
import { checkAccessibility } from '../__helpers__/accessibility';
import { renderWithEvents } from '../__helpers__/setUpTest';
import { renderWithStore } from '../__helpers__/setUpTest';
type SetUpOptions = {
serversImported?: boolean;
@@ -14,9 +14,8 @@ type SetUpOptions = {
};
describe('<CreateServer />', () => {
const createServersMock = vi.fn();
const defaultServers: ServersMap = {
foo: fromPartial({ url: 'https://existing_url.com', apiKey: 'existing_api_key' }),
foo: fromPartial({ url: 'https://existing_url.com', apiKey: 'existing_api_key', id: 'foo' }),
};
const setUp = ({ serversImported = false, importFailed = false, servers = defaultServers }: SetUpOptions = {}) => {
let callCount = 0;
@@ -33,10 +32,13 @@ describe('<CreateServer />', () => {
return {
history,
...renderWithEvents(
...renderWithStore(
<Router location={history.location} navigator={history}>
<CreateServer createServers={createServersMock} servers={servers} />
<CreateServer />
</Router>,
{
initialState: { servers },
},
),
};
};
@@ -68,21 +70,23 @@ describe('<CreateServer />', () => {
});
it('creates server data when form is submitted', async () => {
const { user, history } = setUp();
expect(createServersMock).not.toHaveBeenCalled();
const { user, history, store } = setUp();
const expectedServerId = 'the_name-the_url.com';
await user.type(screen.getByLabelText(/^Name/), 'the_name');
await user.type(screen.getByLabelText(/^URL/), 'https://the_url.com');
await user.type(screen.getByLabelText(/^API key/), 'the_api_key');
fireEvent.submit(screen.getByRole('form'));
expect(createServersMock).toHaveBeenCalledWith([expect.objectContaining({
expect(store.getState().servers[expectedServerId]).not.toBeDefined();
fireEvent.submit(screen.getByRole('form'));
expect(store.getState().servers[expectedServerId]).toEqual(expect.objectContaining({
id: expectedServerId,
name: 'the_name',
url: 'https://the_url.com',
apiKey: 'the_api_key',
})]);
expect(history.location.pathname).toEqual(expect.stringMatching(/^\/server\//));
}));
expect(history.location.pathname).toEqual(`/server/${expectedServerId}`);
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});
@@ -92,12 +96,12 @@ describe('<CreateServer />', () => {
await user.type(screen.getByLabelText(/^Name/), 'the_name');
await user.type(screen.getByLabelText(/^URL/), 'https://existing_url.com');
await user.type(screen.getByLabelText(/^API key/), 'existing_api_key');
fireEvent.submit(screen.getByRole('form'));
await waitFor(() => expect(screen.getByRole('dialog')).toBeInTheDocument());
await user.click(screen.getByRole('button', { name: 'Discard' }));
expect(createServersMock).not.toHaveBeenCalled();
expect(history.location.pathname).toEqual('/foo'); // Goes back to first route from history's initialEntries
});
});