test: caminhos-base de components
This commit is contained in:
1 parent
9a0e0205dc
commit
60c98fc51e
11 files changed
+1148
-1
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { act, fireEvent, render, screen } from '@testing-library/react';
|
||||
import { useState } from 'react';
|
||||
import { McpJsonEditor } from './mcp-json-editor';
|
||||
|
||||
// Tema claro/escuro do realce e o anel vermelho do textarea inválido são só visuais: mapeados sem teste.
|
||||
|
||||
function Controlled({ initial, onChange }: { initial: string; onChange?: (v: string) => void }) {
|
||||
const [value, setValue] = useState(initial);
|
||||
return <McpJsonEditor value={value} onChange={(v) => { setValue(v); onChange?.(v); }} />;
|
||||
}
|
||||
|
||||
const editor = () => screen.getByRole('textbox') as HTMLTextAreaElement;
|
||||
const formatButton = () => screen.getByRole('button', { name: 'Formatar' });
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers({ toFake: ['setTimeout'] });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe('McpJsonEditor', () => {
|
||||
it('vazio: sem erro nem "Válido" e Formatar desativado', () => {
|
||||
render(<Controlled initial="" />);
|
||||
expect(screen.queryByText('Válido')).toBeNull();
|
||||
expect(formatButton()).toHaveProperty('disabled', true);
|
||||
});
|
||||
|
||||
it('JSON válido: mostra "Válido" e a contagem de linhas no plural', () => {
|
||||
render(<Controlled initial={'{\n"a": 1\n}'} />);
|
||||
expect(screen.getByText('Válido')).toBeTruthy();
|
||||
expect(screen.getByText('3 linhas')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('uma linha: contagem no singular', () => {
|
||||
render(<Controlled initial='{"a":1}' />);
|
||||
expect(screen.getByText('1 linha')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('digitar JSON inválido: mostra a mensagem do parser e repassa o texto', () => {
|
||||
const onChange = vi.fn();
|
||||
render(<Controlled initial="{}" onChange={onChange} />);
|
||||
fireEvent.change(editor(), { target: { value: '{"a":' } });
|
||||
expect(onChange).toHaveBeenCalledWith('{"a":');
|
||||
expect(screen.queryByText('Válido')).toBeNull();
|
||||
expect(screen.getByText(/JSON/)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('Formatar JSON válido: reindenta com 2 espaços e mostra "Formatado" por 2 s', () => {
|
||||
render(<Controlled initial='{"a":{"b":1}}' />);
|
||||
fireEvent.click(formatButton());
|
||||
expect(editor().value).toBe('{\n "a": {\n "b": 1\n }\n}');
|
||||
expect(screen.getByText('Formatado')).toBeTruthy();
|
||||
act(() => vi.advanceTimersByTime(2000));
|
||||
expect(screen.queryByText('Formatado')).toBeNull();
|
||||
});
|
||||
|
||||
it('Formatar JSON inválido: não altera o texto', () => {
|
||||
const onChange = vi.fn();
|
||||
render(<Controlled initial="{oops" onChange={onChange} />);
|
||||
fireEvent.click(formatButton());
|
||||
expect(onChange).not.toHaveBeenCalled();
|
||||
expect(editor().value).toBe('{oops');
|
||||
expect(screen.queryByText('Formatado')).toBeNull();
|
||||
});
|
||||
|
||||
it('editar depois de formatar: some o "Formatado"', () => {
|
||||
render(<Controlled initial='{"a":1}' />);
|
||||
fireEvent.click(formatButton());
|
||||
fireEvent.change(editor(), { target: { value: '{"a":2}' } });
|
||||
expect(screen.queryByText('Formatado')).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user