@1doc/1ds-react 2.9.0 → 2.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +434 -0
- package/dist/components/Accordion/Accordion.css +13 -0
- package/dist/components/Accordion/Accordion.d.ts +3 -0
- package/dist/components/Accordion/Accordion.js +1 -0
- package/dist/components/AppSubheader/AppSubheader.css +10 -1
- package/dist/components/AttachmentCard/AttachmentCard.css +10 -8
- package/dist/components/AttachmentCard/AttachmentCard.d.ts +3 -0
- package/dist/components/AttachmentCard/AttachmentCard.js +8 -1
- package/dist/components/Badge/Badge.css +14 -1
- package/dist/components/Badge/Badge.d.ts +11 -2
- package/dist/components/Badge/Badge.js +3 -0
- package/dist/components/Calendar/Calendar.d.ts +7 -6
- package/dist/components/Coachmark/Coachmark.css +10 -0
- package/dist/components/Coachmark/Coachmark.d.ts +12 -0
- package/dist/components/Coachmark/Coachmark.js +46 -5
- package/dist/components/ColorPicker/ColorPicker.css +8 -1
- package/dist/components/ColorPicker/ColorPicker.d.ts +2 -2
- package/dist/components/DataTable/DataTable.css +174 -0
- package/dist/components/DataTable/DataTable.d.ts +63 -2
- package/dist/components/DataTable/DataTable.js +278 -6
- package/dist/components/DescriptionList/DescriptionList.css +14 -0
- package/dist/components/DescriptionList/DescriptionList.d.ts +3 -0
- package/dist/components/DescriptionList/DescriptionList.js +1 -0
- package/dist/components/EmptyState/EmptyState.d.ts +4 -2
- package/dist/components/EmptyState/EmptyState.js +2 -2
- package/dist/components/Input/Input.css +21 -0
- package/dist/components/Input/Input.d.ts +7 -0
- package/dist/components/Input/Input.js +2 -1
- package/dist/components/List/ListItem.d.ts +14 -2
- package/dist/components/List/ListItem.js +3 -2
- package/dist/components/RichTextEditor/RichTextEditor.css +47 -0
- package/dist/components/RichTextEditor/RichTextEditor.d.ts +22 -8
- package/dist/components/RichTextEditor/RichTextEditor.js +460 -3
- package/dist/components/SegmentedControl/SegmentedControl.css +24 -0
- package/dist/components/SegmentedControl/SegmentedControl.d.ts +2 -0
- package/dist/components/SegmentedControl/SegmentedControl.js +4 -1
- package/dist/components/Select/Select.css +94 -0
- package/dist/components/Select/Select.d.ts +26 -4
- package/dist/components/Select/Select.js +210 -35
- package/dist/components/StatCard/StatCard.css +65 -0
- package/dist/components/StatCard/StatCard.d.ts +9 -0
- package/dist/components/StatCard/StatCard.js +10 -1
- package/dist/components/Toast/Toast.css +32 -0
- package/dist/components/Toast/Toast.d.ts +18 -0
- package/dist/components/Toast/Toast.js +14 -1
- package/dist/components/Toast/ToastStack.js +8 -1
- package/dist/teste/AccordionTituloNeutro.test.d.ts +1 -0
- package/dist/teste/AccordionTituloNeutro.test.js +23 -0
- package/dist/teste/AppSubheaderRegiao.test.d.ts +1 -0
- package/dist/teste/AppSubheaderRegiao.test.js +13 -0
- package/dist/teste/AttachmentCardLista.test.d.ts +1 -0
- package/dist/teste/AttachmentCardLista.test.js +49 -0
- package/dist/teste/AttachmentCardSm.test.d.ts +1 -0
- package/dist/teste/AttachmentCardSm.test.js +26 -0
- package/dist/teste/BadgeNeutroColado.test.d.ts +1 -0
- package/dist/teste/BadgeNeutroColado.test.js +11 -0
- package/dist/teste/BadgeTag.test.js +13 -0
- package/dist/teste/CalendarCabecalho.test.d.ts +1 -0
- package/dist/teste/CalendarCabecalho.test.js +11 -0
- package/dist/teste/CoachmarkSemVeu.test.d.ts +1 -0
- package/dist/teste/CoachmarkSemVeu.test.js +85 -0
- package/dist/teste/ColorPickerXs.test.d.ts +1 -0
- package/dist/teste/ColorPickerXs.test.js +21 -0
- package/dist/teste/DataTableArvoreEVariantes.test.d.ts +1 -0
- package/dist/teste/DataTableArvoreEVariantes.test.js +190 -0
- package/dist/teste/DescriptionListCompacta.test.d.ts +1 -0
- package/dist/teste/DescriptionListCompacta.test.js +22 -0
- package/dist/teste/EmptyStateTituloRico.test.d.ts +1 -0
- package/dist/teste/EmptyStateTituloRico.test.js +23 -0
- package/dist/teste/InputAgrupado.test.d.ts +1 -0
- package/dist/teste/InputAgrupado.test.js +24 -0
- package/dist/teste/ListItemTooltipStandalone.test.d.ts +1 -0
- package/dist/teste/ListItemTooltipStandalone.test.js +39 -0
- package/dist/teste/RichTextEditorAlinhaImagemTabela.test.d.ts +1 -0
- package/dist/teste/RichTextEditorAlinhaImagemTabela.test.js +212 -0
- package/dist/teste/RichTextEditorEstiloDeTexto.test.d.ts +1 -0
- package/dist/teste/RichTextEditorEstiloDeTexto.test.js +120 -0
- package/dist/teste/SegmentedControlContador.test.d.ts +1 -0
- package/dist/teste/SegmentedControlContador.test.js +39 -0
- package/dist/teste/SelectAbertoControladoEFlutuante.test.d.ts +1 -0
- package/dist/teste/SelectAbertoControladoEFlutuante.test.js +145 -0
- package/dist/teste/SelectFiltroCorETamanho.test.d.ts +1 -0
- package/dist/teste/SelectFiltroCorETamanho.test.js +77 -0
- package/dist/teste/StatCardSecundariaETom.test.d.ts +1 -0
- package/dist/teste/StatCardSecundariaETom.test.js +45 -0
- package/dist/teste/ToastAcaoEDetalhes.test.d.ts +1 -0
- package/dist/teste/ToastAcaoEDetalhes.test.js +94 -0
- package/dist/utils/html-seguro.d.ts +19 -1
- package/dist/utils/html-seguro.js +113 -2
- package/dist/utils/toast.d.ts +10 -1
- package/dist/utils/toast.js +2 -1
- package/package.json +2 -2
- package/src/teste/AccordionTituloNeutro.test.tsx +25 -0
- package/src/teste/AppSubheaderRegiao.test.tsx +17 -0
- package/src/teste/AttachmentCardLista.test.tsx +53 -0
- package/src/teste/AttachmentCardSm.test.tsx +29 -0
- package/src/teste/BadgeNeutroColado.test.tsx +15 -0
- package/src/teste/BadgeTag.test.tsx +21 -0
- package/src/teste/CalendarCabecalho.test.tsx +11 -0
- package/src/teste/CoachmarkSemVeu.test.tsx +101 -0
- package/src/teste/ColorPickerXs.test.tsx +22 -0
- package/src/teste/DataTableArvoreEVariantes.test.tsx +240 -0
- package/src/teste/DescriptionListCompacta.test.tsx +24 -0
- package/src/teste/EmptyStateTituloRico.test.tsx +34 -0
- package/src/teste/InputAgrupado.test.tsx +26 -0
- package/src/teste/ListItemTooltipStandalone.test.tsx +64 -0
- package/src/teste/RichTextEditorAlinhaImagemTabela.test.tsx +238 -0
- package/src/teste/RichTextEditorEstiloDeTexto.test.tsx +137 -0
- package/src/teste/SegmentedControlContador.test.tsx +43 -0
- package/src/teste/SelectAbertoControladoEFlutuante.test.tsx +180 -0
- package/src/teste/SelectFiltroCorETamanho.test.tsx +90 -0
- package/src/teste/StatCardSecundariaETom.test.tsx +51 -0
- package/src/teste/ToastAcaoEDetalhes.test.tsx +109 -0
- package/src/utils/html-seguro.ts +105 -2
- package/src/utils/toast.ts +12 -2
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { render, screen, fireEvent } from "@testing-library/react";
|
|
3
|
+
import { faBoxOpen } from "@fortawesome/free-solid-svg-icons";
|
|
4
|
+
import DataTable from "../components/DataTable/DataTable";
|
|
5
|
+
|
|
6
|
+
const COLUNAS = [
|
|
7
|
+
{ key: "nome", label: "Nome" },
|
|
8
|
+
{ key: "sigla", label: "Sigla" },
|
|
9
|
+
];
|
|
10
|
+
const DADOS = [
|
|
11
|
+
{ id: "a", nome: "Ana", sigla: "SADM" },
|
|
12
|
+
{ id: "b", nome: "Bruno", sigla: "SOBR" },
|
|
13
|
+
{ id: "c", nome: "Carla", sigla: "SEFIN" },
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
describe("DataTable: cabeçalho, zebra e destaque", () => {
|
|
17
|
+
it("headerVariant dark e headerUppercase ligam as classes; sem as props, nada muda", () => {
|
|
18
|
+
const { container, rerender } = render(<DataTable columns={COLUNAS} data={DADOS} />);
|
|
19
|
+
const raiz = () => container.querySelector(".umds-datatable") as HTMLElement;
|
|
20
|
+
expect(raiz().className).not.toMatch(/header-dark|header-uppercase|striped|header-hidden/);
|
|
21
|
+
rerender(<DataTable columns={COLUNAS} data={DADOS} headerVariant="dark" headerUppercase striped hideHeader />);
|
|
22
|
+
expect(raiz()).toHaveClass("umds-datatable--header-dark");
|
|
23
|
+
expect(raiz()).toHaveClass("umds-datatable--header-uppercase");
|
|
24
|
+
expect(raiz()).toHaveClass("umds-datatable--striped");
|
|
25
|
+
expect(raiz()).toHaveClass("umds-datatable--header-hidden");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("hideHeader não remove o cabeçalho: as colunas continuam nomeadas", () => {
|
|
29
|
+
render(<DataTable columns={COLUNAS} data={DADOS} hideHeader />);
|
|
30
|
+
expect(screen.getAllByRole("columnheader")).toHaveLength(2);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("striped marca as linhas ímpares", () => {
|
|
34
|
+
const { container } = render(<DataTable columns={COLUNAS} data={DADOS} striped />);
|
|
35
|
+
const linhas = [...container.querySelectorAll("tbody tr")];
|
|
36
|
+
expect(linhas.map((l) => l.classList.contains("umds-datatable__row--striped"))).toEqual([false, true, false]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("selectedRowKeys destaca a linha sem checkbox e marca aria-selected", () => {
|
|
40
|
+
const { container } = render(<DataTable columns={COLUNAS} data={DADOS} rowKey="id" selectedRowKeys={["b"]} />);
|
|
41
|
+
const linhas = [...container.querySelectorAll("tbody tr")];
|
|
42
|
+
expect(linhas[1]).toHaveClass("umds-datatable__row--selected");
|
|
43
|
+
expect(linhas[1]).toHaveClass("umds-datatable__row--highlighted");
|
|
44
|
+
expect(linhas[1]).toHaveAttribute("aria-selected", "true");
|
|
45
|
+
expect(linhas[0]).not.toHaveClass("umds-datatable__row--selected");
|
|
46
|
+
expect(linhas[0]).not.toHaveAttribute("aria-selected");
|
|
47
|
+
expect(screen.queryByRole("checkbox")).toBeNull();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("isRowSelected destaca por função", () => {
|
|
51
|
+
const { container } = render(
|
|
52
|
+
<DataTable columns={COLUNAS} data={DADOS} isRowSelected={(row) => row.sigla === "SEFIN"} />
|
|
53
|
+
);
|
|
54
|
+
const linhas = [...container.querySelectorAll("tbody tr")];
|
|
55
|
+
expect(linhas[2]).toHaveClass("umds-datatable__row--selected");
|
|
56
|
+
expect(linhas[0]).not.toHaveClass("umds-datatable__row--selected");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("emptyIcon e emptyDescription entram no estado vazio", () => {
|
|
60
|
+
const { container } = render(
|
|
61
|
+
<DataTable
|
|
62
|
+
columns={COLUNAS}
|
|
63
|
+
data={[]}
|
|
64
|
+
emptyMessage="Nada por aqui"
|
|
65
|
+
emptyDescription="Ajuste o filtro de setor."
|
|
66
|
+
emptyIcon={faBoxOpen}
|
|
67
|
+
/>
|
|
68
|
+
);
|
|
69
|
+
expect(screen.getByText("Nada por aqui")).toBeInTheDocument();
|
|
70
|
+
expect(screen.getByText("Ajuste o filtro de setor.")).toBeInTheDocument();
|
|
71
|
+
expect(container.querySelector(".umds-datatable__empty-icon svg")).toHaveAttribute("data-icon", "box-open");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("o ícone padrão do estado vazio continua o inbox", () => {
|
|
75
|
+
const { container } = render(<DataTable columns={COLUNAS} data={[]} />);
|
|
76
|
+
expect(container.querySelector(".umds-datatable__empty-icon svg")).toHaveAttribute("data-icon", "inbox");
|
|
77
|
+
expect(container.querySelector(".umds-datatable__empty-desc")).toBeNull();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("compact na tabela de documento soma as duas classes", () => {
|
|
81
|
+
const { container } = render(<DataTable variant="document" density="compact" columns={COLUNAS} data={DADOS} />);
|
|
82
|
+
const raiz = container.querySelector(".umds-datatable") as HTMLElement;
|
|
83
|
+
expect(raiz).toHaveClass("umds-datatable--document");
|
|
84
|
+
expect(raiz).toHaveClass("umds-datatable--compact");
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const ARVORE = [
|
|
89
|
+
{
|
|
90
|
+
id: "gab",
|
|
91
|
+
nome: "Gabinete",
|
|
92
|
+
sigla: "GAB",
|
|
93
|
+
children: [
|
|
94
|
+
{ id: "sadm", nome: "Administração", sigla: "SADM", children: [{ id: "rh", nome: "Recursos Humanos", sigla: "RH" }] },
|
|
95
|
+
{ id: "sobr", nome: "Obras", sigla: "SOBR" },
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
{ id: "sefin", nome: "Finanças", sigla: "SEFIN" },
|
|
99
|
+
];
|
|
100
|
+
|
|
101
|
+
const linhas = (c: HTMLElement) => [...c.querySelectorAll("tbody tr")] as HTMLElement[];
|
|
102
|
+
const nomes = (c: HTMLElement) => linhas(c).map((l) => l.querySelectorAll("td")[0].textContent);
|
|
103
|
+
|
|
104
|
+
describe("DataTable treeData", () => {
|
|
105
|
+
it("vira treegrid; fechada, mostra só as raízes, com aria-level, posinset, setsize e expanded", () => {
|
|
106
|
+
const { container } = render(<DataTable columns={COLUNAS} data={ARVORE} treeData rowKey="id" />);
|
|
107
|
+
expect(screen.getByRole("treegrid")).toBeInTheDocument();
|
|
108
|
+
expect(nomes(container)).toEqual(["Gabinete", "Finanças"]);
|
|
109
|
+
const [gab, fin] = linhas(container);
|
|
110
|
+
expect(gab).toHaveAttribute("aria-level", "1");
|
|
111
|
+
expect(gab).toHaveAttribute("aria-posinset", "1");
|
|
112
|
+
expect(gab).toHaveAttribute("aria-setsize", "2");
|
|
113
|
+
expect(gab).toHaveAttribute("aria-expanded", "false");
|
|
114
|
+
expect(fin).not.toHaveAttribute("aria-expanded");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("o botão expande e recolhe, com nome acessível e onExpand", () => {
|
|
118
|
+
const aoExpandir = vi.fn();
|
|
119
|
+
const { container } = render(<DataTable columns={COLUNAS} data={ARVORE} treeData rowKey="id" onExpand={aoExpandir} />);
|
|
120
|
+
fireEvent.click(screen.getByRole("button", { name: "Expandir" }));
|
|
121
|
+
expect(nomes(container)).toEqual(["Gabinete", "Administração", "Obras", "Finanças"]);
|
|
122
|
+
expect(aoExpandir).toHaveBeenCalledWith(["gab"], ARVORE[0], true);
|
|
123
|
+
const filha = linhas(container)[1];
|
|
124
|
+
expect(filha).toHaveAttribute("aria-level", "2");
|
|
125
|
+
expect(filha).toHaveAttribute("aria-expanded", "false");
|
|
126
|
+
fireEvent.click(screen.getAllByRole("button", { name: "Recolher" })[0]);
|
|
127
|
+
expect(nomes(container)).toEqual(["Gabinete", "Finanças"]);
|
|
128
|
+
expect(aoExpandir).toHaveBeenLastCalledWith([], ARVORE[0], false);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("expandLabel e collapseLabel traduzem os botões", () => {
|
|
132
|
+
render(
|
|
133
|
+
<DataTable
|
|
134
|
+
columns={COLUNAS}
|
|
135
|
+
data={ARVORE}
|
|
136
|
+
treeData
|
|
137
|
+
rowKey="id"
|
|
138
|
+
defaultExpandedKeys={["gab"]}
|
|
139
|
+
expandLabel="Expand"
|
|
140
|
+
collapseLabel="Collapse"
|
|
141
|
+
/>
|
|
142
|
+
);
|
|
143
|
+
expect(screen.getByRole("button", { name: "Collapse" })).toBeInTheDocument();
|
|
144
|
+
expect(screen.getByRole("button", { name: "Expand" })).toBeInTheDocument();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("defaultExpandAll abre tudo, e o primeiro recolher parte da árvore aberta", () => {
|
|
148
|
+
const { container } = render(<DataTable columns={COLUNAS} data={ARVORE} treeData rowKey="id" defaultExpandAll />);
|
|
149
|
+
expect(nomes(container)).toEqual(["Gabinete", "Administração", "Recursos Humanos", "Obras", "Finanças"]);
|
|
150
|
+
expect(linhas(container)[2]).toHaveAttribute("aria-level", "3");
|
|
151
|
+
fireEvent.click(screen.getAllByRole("button", { name: "Recolher" })[1]);
|
|
152
|
+
expect(nomes(container)).toEqual(["Gabinete", "Administração", "Obras", "Finanças"]);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("expandedKeys controla: o clique avisa, mas só muda quando o pai muda a prop", () => {
|
|
156
|
+
const aoExpandir = vi.fn();
|
|
157
|
+
const { container, rerender } = render(
|
|
158
|
+
<DataTable columns={COLUNAS} data={ARVORE} treeData rowKey="id" expandedKeys={[]} onExpand={aoExpandir} />
|
|
159
|
+
);
|
|
160
|
+
fireEvent.click(screen.getByRole("button", { name: "Expandir" }));
|
|
161
|
+
expect(aoExpandir).toHaveBeenCalledWith(["gab"], ARVORE[0], true);
|
|
162
|
+
expect(nomes(container)).toEqual(["Gabinete", "Finanças"]);
|
|
163
|
+
rerender(<DataTable columns={COLUNAS} data={ARVORE} treeData rowKey="id" expandedKeys={["gab"]} onExpand={aoExpandir} />);
|
|
164
|
+
expect(nomes(container)).toEqual(["Gabinete", "Administração", "Obras", "Finanças"]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("sem rowKey, a chave é o caminho de índices", () => {
|
|
168
|
+
const aoExpandir = vi.fn();
|
|
169
|
+
render(<DataTable columns={COLUNAS} data={ARVORE} treeData onExpand={aoExpandir} />);
|
|
170
|
+
fireEvent.click(screen.getByRole("button", { name: "Expandir" }));
|
|
171
|
+
expect(aoExpandir.mock.calls[0][0]).toEqual(["0"]);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("desenha os fios: tee para quem tem irmã depois, elbow para a última, line para o que continua", () => {
|
|
175
|
+
const { container } = render(<DataTable columns={COLUNAS} data={ARVORE} treeData rowKey="id" defaultExpandAll />);
|
|
176
|
+
const guias = (l: HTMLElement) => [...l.querySelectorAll(".umds-datatable__guide")].map((g) => g.className.replace("umds-datatable__guide umds-datatable__guide--", ""));
|
|
177
|
+
const [gab, sadm, rh, sobr] = linhas(container);
|
|
178
|
+
expect(guias(gab)).toEqual([]);
|
|
179
|
+
expect(guias(sadm)).toEqual(["tee"]);
|
|
180
|
+
expect(guias(rh)).toEqual(["line", "elbow"]);
|
|
181
|
+
expect(guias(sobr)).toEqual(["elbow"]);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("o recuo cresce por nível", () => {
|
|
185
|
+
const { container } = render(<DataTable columns={COLUNAS} data={ARVORE} treeData rowKey="id" defaultExpandAll />);
|
|
186
|
+
const recuo = (l: HTMLElement) => (l.querySelector(".umds-datatable__tree-content") as HTMLElement).style.paddingLeft;
|
|
187
|
+
expect(recuo(linhas(container)[0])).toContain("calc(0 *");
|
|
188
|
+
expect(recuo(linhas(container)[2])).toContain("calc(2 *");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("teclado: ↓ ↑ andam, → expande e desce, ← recolhe e sobe, Home e End", () => {
|
|
192
|
+
const { container } = render(<DataTable columns={COLUNAS} data={ARVORE} treeData rowKey="id" />);
|
|
193
|
+
const gab = linhas(container)[0];
|
|
194
|
+
gab.focus();
|
|
195
|
+
expect(gab).toHaveAttribute("tabindex", "0");
|
|
196
|
+
expect(linhas(container)[1]).toHaveAttribute("tabindex", "-1");
|
|
197
|
+
|
|
198
|
+
fireEvent.keyDown(gab, { key: "ArrowRight" });
|
|
199
|
+
expect(nomes(container)).toEqual(["Gabinete", "Administração", "Obras", "Finanças"]);
|
|
200
|
+
expect(document.activeElement).toBe(gab);
|
|
201
|
+
|
|
202
|
+
fireEvent.keyDown(gab, { key: "ArrowRight" });
|
|
203
|
+
expect(document.activeElement).toBe(linhas(container)[1]);
|
|
204
|
+
|
|
205
|
+
fireEvent.keyDown(document.activeElement as Element, { key: "ArrowDown" });
|
|
206
|
+
expect(document.activeElement).toBe(linhas(container)[2]);
|
|
207
|
+
expect(linhas(container)[2]).toHaveAttribute("tabindex", "0");
|
|
208
|
+
|
|
209
|
+
fireEvent.keyDown(document.activeElement as Element, { key: "ArrowLeft" });
|
|
210
|
+
expect(document.activeElement).toBe(linhas(container)[0]);
|
|
211
|
+
|
|
212
|
+
fireEvent.keyDown(document.activeElement as Element, { key: "End" });
|
|
213
|
+
expect(document.activeElement).toBe(linhas(container)[3]);
|
|
214
|
+
fireEvent.keyDown(document.activeElement as Element, { key: "Home" });
|
|
215
|
+
expect(document.activeElement).toBe(linhas(container)[0]);
|
|
216
|
+
|
|
217
|
+
fireEvent.keyDown(document.activeElement as Element, { key: "ArrowLeft" });
|
|
218
|
+
expect(nomes(container)).toEqual(["Gabinete", "Finanças"]);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("clicar no botão não aciona o onRowClick da linha", () => {
|
|
222
|
+
const aoClicar = vi.fn();
|
|
223
|
+
render(<DataTable columns={COLUNAS} data={ARVORE} treeData rowKey="id" onRowClick={aoClicar} />);
|
|
224
|
+
fireEvent.click(screen.getByRole("button", { name: "Expandir" }));
|
|
225
|
+
expect(aoClicar).not.toHaveBeenCalled();
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("sem treeData, a tabela é a de sempre: sem role, sem aria-level", () => {
|
|
229
|
+
const { container } = render(<DataTable columns={COLUNAS} data={ARVORE} />);
|
|
230
|
+
expect(screen.queryByRole("treegrid")).toBeNull();
|
|
231
|
+
expect(container.querySelector("[aria-level]")).toBeNull();
|
|
232
|
+
expect(container.querySelector(".umds-datatable__tree-toggle")).toBeNull();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("childrenKey troca o campo das filhas", () => {
|
|
236
|
+
const dados = [{ id: 1, nome: "Pai", sigla: "P", itens: [{ id: 2, nome: "Filho", sigla: "F" }] }];
|
|
237
|
+
const { container } = render(<DataTable columns={COLUNAS} data={dados} treeData childrenKey="itens" rowKey="id" defaultExpandAll />);
|
|
238
|
+
expect(nomes(container)).toEqual(["Pai", "Filho"]);
|
|
239
|
+
});
|
|
240
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { render } from "@testing-library/react";
|
|
3
|
+
import DescriptionList from "../components/DescriptionList/DescriptionList";
|
|
4
|
+
|
|
5
|
+
const ITENS = [{ label: "Dotação", value: "02.031.0001" }];
|
|
6
|
+
|
|
7
|
+
describe("DescriptionList · density", () => {
|
|
8
|
+
it("compact escreve a classe; o padrão não", () => {
|
|
9
|
+
const { container, rerender } = render(<DescriptionList items={ITENS} density="compact" />);
|
|
10
|
+
const dl = () => container.querySelector("dl") as HTMLElement;
|
|
11
|
+
expect(dl()).toHaveClass("umds-description-list--compact");
|
|
12
|
+
rerender(<DescriptionList items={ITENS} density="default" />);
|
|
13
|
+
expect(dl()).not.toHaveClass("umds-description-list--compact");
|
|
14
|
+
rerender(<DescriptionList items={ITENS} />);
|
|
15
|
+
expect(dl()).not.toHaveClass("umds-description-list--compact");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("compact vale nos dois desenhos", () => {
|
|
19
|
+
const { container, rerender } = render(<DescriptionList items={ITENS} density="compact" layout="stacked" bordered />);
|
|
20
|
+
expect(container.querySelector("dl")).toHaveClass("umds-description-list--stacked", "umds-description-list--compact");
|
|
21
|
+
rerender(<DescriptionList items={ITENS} density="compact" fit />);
|
|
22
|
+
expect(container.querySelector("dl")).toHaveClass("umds-description-list--inline", "umds-description-list--compact");
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { render, screen } from "@testing-library/react";
|
|
3
|
+
import { faInbox } from "@fortawesome/free-solid-svg-icons";
|
|
4
|
+
import EmptyState from "../components/EmptyState/EmptyState";
|
|
5
|
+
|
|
6
|
+
describe("EmptyState · título rico", () => {
|
|
7
|
+
it("título em texto continua igual", () => {
|
|
8
|
+
const { container } = render(<EmptyState icon={faInbox} title="Nenhuma mensagem" description="Tudo em dia." />);
|
|
9
|
+
expect(container.querySelector(".umds-empty-state__title")).toHaveTextContent("Nenhuma mensagem");
|
|
10
|
+
expect(screen.getByText("Tudo em dia.")).toBeInTheDocument();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("título aceita conteúdo rico: negrito, link", () => {
|
|
14
|
+
const { container } = render(
|
|
15
|
+
<EmptyState
|
|
16
|
+
title={
|
|
17
|
+
<>
|
|
18
|
+
Nenhum processo para <strong data-testid="termo">SOBR</strong>{" "}
|
|
19
|
+
<a href="#limpar">Limpar filtro</a>
|
|
20
|
+
</>
|
|
21
|
+
}
|
|
22
|
+
/>,
|
|
23
|
+
);
|
|
24
|
+
const titulo = container.querySelector(".umds-empty-state__title") as HTMLElement;
|
|
25
|
+
expect(titulo).toContainElement(screen.getByTestId("termo"));
|
|
26
|
+
expect(titulo.querySelector("a")).toHaveAttribute("href", "#limpar");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("o título é um div (conteúdo de bloco não vira HTML inválido dentro de p)", () => {
|
|
30
|
+
const { container } = render(<EmptyState title={<div data-testid="bloco">Bloco</div>} />);
|
|
31
|
+
expect(container.querySelector(".umds-empty-state__title")!.tagName).toBe("DIV");
|
|
32
|
+
expect(screen.getByTestId("bloco").parentElement).toHaveClass("umds-empty-state__title");
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { render } from "@testing-library/react";
|
|
3
|
+
import Input from "../components/Input/Input";
|
|
4
|
+
|
|
5
|
+
describe("Input · grouped", () => {
|
|
6
|
+
it("grouped liga a classe própria e a do borderless; sem a prop, nenhuma das duas", () => {
|
|
7
|
+
const { container, rerender } = render(<Input ariaLabel="Adicionar setor" grouped />);
|
|
8
|
+
const raiz = () => container.firstElementChild as HTMLElement;
|
|
9
|
+
expect(raiz()).toHaveClass("umds-input--grouped");
|
|
10
|
+
expect(raiz()).toHaveClass("umds-input--borderless");
|
|
11
|
+
rerender(<Input ariaLabel="Adicionar setor" />);
|
|
12
|
+
expect(raiz()).not.toHaveClass("umds-input--grouped");
|
|
13
|
+
expect(raiz()).not.toHaveClass("umds-input--borderless");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("borderless sozinho não ganha a classe do grupo (segue com altura de campo e foco próprio)", () => {
|
|
17
|
+
const { container } = render(<Input ariaLabel="x" borderless />);
|
|
18
|
+
expect(container.firstElementChild).toHaveClass("umds-input--borderless");
|
|
19
|
+
expect(container.firstElementChild).not.toHaveClass("umds-input--grouped");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("o campo continua digitável dentro do grupo", () => {
|
|
23
|
+
const { container } = render(<Input ariaLabel="Adicionar setor" grouped placeholder="SADM, SOBR..." />);
|
|
24
|
+
expect(container.querySelector("input")).toHaveAttribute("placeholder", "SADM, SOBR...");
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { render, screen, fireEvent } from "@testing-library/react";
|
|
3
|
+
import ListItem from "../components/List/ListItem";
|
|
4
|
+
|
|
5
|
+
describe("ListItem: tooltip, standalone e atributos só no elemento interativo", () => {
|
|
6
|
+
it("tooltip vira o atributo title do elemento interativo, e o li fica limpo", () => {
|
|
7
|
+
const { container } = render(
|
|
8
|
+
<ul>
|
|
9
|
+
<ListItem title="Memorando 114/2025" tooltip="Abrir o memorando" aria-label="Memorando" role="menuitem" data-origem="home" />
|
|
10
|
+
</ul>,
|
|
11
|
+
);
|
|
12
|
+
const li = container.querySelector("li") as HTMLElement;
|
|
13
|
+
const botao = screen.getByRole("menuitem");
|
|
14
|
+
expect(botao).toHaveAttribute("title", "Abrir o memorando");
|
|
15
|
+
expect(botao).toHaveAttribute("aria-label", "Memorando");
|
|
16
|
+
expect(botao).toHaveAttribute("data-origem", "home");
|
|
17
|
+
for (const atributo of ["title", "aria-label", "role", "data-origem"]) expect(li).not.toHaveAttribute(atributo);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("tooltip funciona no link e na linha div; sem tooltip não há title", () => {
|
|
21
|
+
const { container } = render(
|
|
22
|
+
<ul>
|
|
23
|
+
<ListItem title="Link" href="/x" tooltip="Dica do link" />
|
|
24
|
+
<ListItem as="div" title="Div" tooltip="Dica da div" />
|
|
25
|
+
<ListItem title="Sem dica" />
|
|
26
|
+
</ul>,
|
|
27
|
+
);
|
|
28
|
+
const titulos = [...container.querySelectorAll(".umds-list-item")].map((e) => e.getAttribute("title"));
|
|
29
|
+
expect(titulos).toEqual(["Dica do link", "Dica da div", null]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("o título da linha continua sendo texto, não o atributo", () => {
|
|
33
|
+
const { container } = render(
|
|
34
|
+
<ul>
|
|
35
|
+
<ListItem title="Só o título" />
|
|
36
|
+
</ul>,
|
|
37
|
+
);
|
|
38
|
+
expect(container.querySelector("[title]")).toBeNull();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("standalone renderiza sem li, só o elemento interativo", () => {
|
|
42
|
+
const aoClicar = vi.fn();
|
|
43
|
+
const { container } = render(
|
|
44
|
+
<div>
|
|
45
|
+
<ListItem standalone title="12" tooltip="Página 12" onClick={aoClicar} />
|
|
46
|
+
</div>,
|
|
47
|
+
);
|
|
48
|
+
expect(container.querySelector("li")).toBeNull();
|
|
49
|
+
const botao = screen.getByRole("button", { name: /12/ });
|
|
50
|
+
expect(botao.parentElement?.tagName).toBe("DIV");
|
|
51
|
+
expect(botao).toHaveAttribute("title", "Página 12");
|
|
52
|
+
fireEvent.click(botao);
|
|
53
|
+
expect(aoClicar).toHaveBeenCalledTimes(1);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("sem standalone o li continua lá", () => {
|
|
57
|
+
const { container } = render(
|
|
58
|
+
<ul>
|
|
59
|
+
<ListItem title="Normal" />
|
|
60
|
+
</ul>,
|
|
61
|
+
);
|
|
62
|
+
expect(container.querySelectorAll("li")).toHaveLength(1);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
import RichTextEditor from "../components/RichTextEditor/RichTextEditor";
|
|
5
|
+
import { htmlDaTabela, htmlVazio, imagemSegura, sanitizarHtml } from "../utils/html-seguro";
|
|
6
|
+
|
|
7
|
+
let execCommand: ReturnType<typeof vi.fn>;
|
|
8
|
+
let estados: Record<string, boolean>;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
estados = {};
|
|
12
|
+
execCommand = vi.fn(() => true);
|
|
13
|
+
(document as any).execCommand = execCommand;
|
|
14
|
+
(document as any).queryCommandState = (comando: string) => !!estados[comando];
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const area = () => screen.getByRole("textbox") as HTMLElement;
|
|
18
|
+
const PNG = "data:image/png;base64,iVBORw0KGgo=";
|
|
19
|
+
|
|
20
|
+
describe("html-seguro: alinhamento, imagem e tabela", () => {
|
|
21
|
+
it("text-align fica nos blocos, em style ou em align; o resto do style sai", () => {
|
|
22
|
+
expect(sanitizarHtml('<p style="text-align: center; color: red">a</p>')).toBe('<p style="text-align: center;">a</p>');
|
|
23
|
+
expect(sanitizarHtml('<div align="right">a</div>')).toBe('<div style="text-align: right;">a</div>');
|
|
24
|
+
expect(sanitizarHtml('<p style="text-align: expression(alert(1))">a</p>')).toBe("<p>a</p>");
|
|
25
|
+
expect(sanitizarHtml('<li style="text-align:justify">a</li>')).toBe('<li style="text-align: justify;">a</li>');
|
|
26
|
+
expect(sanitizarHtml('<b style="text-align:center">a</b>')).toBe("<b>a</b>");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("imagem: http(s), caminho e data:image em PNG, JPEG, GIF e WebP ficam; o resto some inteiro", () => {
|
|
30
|
+
expect(sanitizarHtml('<img src="https://a.com/x.png" alt="Brasão" onerror="x()">')).toBe('<img src="https://a.com/x.png" alt="Brasão">');
|
|
31
|
+
expect(sanitizarHtml('<img src="/img/x.png">')).toBe('<img src="/img/x.png" alt="">');
|
|
32
|
+
expect(sanitizarHtml(`<img src="${PNG}" alt="a">`)).toBe(`<img src="${PNG}" alt="a">`);
|
|
33
|
+
for (const mau of ["javascript:alert(1)", "x", "data:image/svg+xml;base64,PHN2Zz4=", "data:text/html;base64,PGI+", "//evil.com/x.png", "ftp://a/x.png"]) {
|
|
34
|
+
expect(sanitizarHtml(`<p>a<img src="${mau}">b</p>`)).toBe("<p>ab</p>");
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("imagemSegura devolve vazio para o perigoso", () => {
|
|
39
|
+
expect(imagemSegura("java\nscript:alert(1)")).toBe("");
|
|
40
|
+
expect(imagemSegura("https://a.com/x.png")).toBe("https://a.com/x.png");
|
|
41
|
+
expect(imagemSegura("")).toBe("");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("tabela simples: tags e colspan/rowspan ficam; atributos soltos saem", () => {
|
|
45
|
+
const html = '<table class="x" onclick="a()"><thead><tr><th colspan="2">T</th></tr></thead><tbody><tr><td rowspan="3" style="color:red">a</td><td colspan="x">b</td></tr></tbody></table>';
|
|
46
|
+
expect(sanitizarHtml(html)).toBe('<table><thead><tr><th colspan="2">T</th></tr></thead><tbody><tr><td rowspan="3">a</td><td>b</td></tr></tbody></table>');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("htmlVazio: imagem e tabela contam como conteúdo", () => {
|
|
50
|
+
expect(htmlVazio("<img src='/a.png'>")).toBe(false);
|
|
51
|
+
expect(htmlVazio(htmlDaTabela(1, 1))).toBe(false);
|
|
52
|
+
expect(htmlVazio("<p><br></p>")).toBe(true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("htmlDaTabela faz linhas × colunas de células com quebra", () => {
|
|
56
|
+
const celulas = (htmlDaTabela(2, 3).match(/<td><br><\/td>/g) || []).length;
|
|
57
|
+
expect(celulas).toBe(6);
|
|
58
|
+
expect((htmlDaTabela(2, 3).match(/<tr>/g) || []).length).toBe(2);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe("RichTextEditor: alinhamento", () => {
|
|
63
|
+
it("as quatro ferramentas de alinhamento pedem o comando e seguem o cursor", async () => {
|
|
64
|
+
const usuario = userEvent.setup();
|
|
65
|
+
render(<RichTextEditor ariaLabel="x" toolbar={["alignLeft", "alignCenter", "alignRight", "alignJustify"]} defaultValue="<p>x</p>" />);
|
|
66
|
+
const esperado: Record<string, string> = {
|
|
67
|
+
"Alinhar à esquerda": "justifyLeft",
|
|
68
|
+
Centralizar: "justifyCenter",
|
|
69
|
+
"Alinhar à direita": "justifyRight",
|
|
70
|
+
Justificar: "justifyFull",
|
|
71
|
+
};
|
|
72
|
+
for (const [nome, comando] of Object.entries(esperado)) {
|
|
73
|
+
execCommand.mockClear();
|
|
74
|
+
await usuario.click(screen.getByRole("button", { name: nome }));
|
|
75
|
+
expect(execCommand).toHaveBeenCalledWith(comando, false, undefined);
|
|
76
|
+
}
|
|
77
|
+
estados.justifyCenter = true;
|
|
78
|
+
const faixa = document.createRange();
|
|
79
|
+
faixa.selectNodeContents(area());
|
|
80
|
+
window.getSelection()!.removeAllRanges();
|
|
81
|
+
window.getSelection()!.addRange(faixa);
|
|
82
|
+
fireEvent(document, new Event("selectionchange"));
|
|
83
|
+
expect(screen.getByRole("button", { name: "Centralizar" })).toHaveAttribute("aria-pressed", "true");
|
|
84
|
+
expect(screen.getByRole("button", { name: "Justificar" })).toHaveAttribute("aria-pressed", "false");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("a barra padrão não ganha as ferramentas novas", () => {
|
|
88
|
+
render(<RichTextEditor ariaLabel="x" />);
|
|
89
|
+
for (const nome of ["Centralizar", "Imagem", "Tabela"]) expect(screen.queryByRole("button", { name: nome })).toBeNull();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("RichTextEditor: imagem", () => {
|
|
94
|
+
const abrirImagem = async (props: any = {}, botao = "Imagem") => {
|
|
95
|
+
const usuario = userEvent.setup();
|
|
96
|
+
render(<RichTextEditor ariaLabel="x" toolbar={["image"]} defaultValue="<p>texto</p>" {...props} />);
|
|
97
|
+
await usuario.click(screen.getByRole("button", { name: botao }));
|
|
98
|
+
return usuario;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
it("o botão abre o painel com foco no endereço; Esc fecha", async () => {
|
|
102
|
+
const usuario = await abrirImagem();
|
|
103
|
+
expect(screen.getByLabelText("Endereço da imagem")).toHaveFocus();
|
|
104
|
+
expect(screen.getByLabelText("Texto alternativo")).toBeInTheDocument();
|
|
105
|
+
await usuario.keyboard("{Escape}");
|
|
106
|
+
expect(screen.queryByLabelText("Endereço da imagem")).toBeNull();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("endereço e texto alternativo inserem o img com alt", async () => {
|
|
110
|
+
const aoMudar = vi.fn();
|
|
111
|
+
const usuario = await abrirImagem({ onChange: aoMudar });
|
|
112
|
+
await usuario.type(screen.getByLabelText("Endereço da imagem"), "exemplo.com.br/brasao.png");
|
|
113
|
+
await usuario.type(screen.getByLabelText("Texto alternativo"), "Brasão da prefeitura");
|
|
114
|
+
await usuario.click(screen.getByRole("button", { name: "Inserir imagem" }));
|
|
115
|
+
expect(execCommand).toHaveBeenCalledWith("insertHTML", false, '<img src="https://exemplo.com.br/brasao.png" alt="Brasão da prefeitura">');
|
|
116
|
+
expect(screen.queryByLabelText("Endereço da imagem")).toBeNull();
|
|
117
|
+
expect(aoMudar).toHaveBeenCalled();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("endereço perigoso mostra o erro e o painel continua aberto", async () => {
|
|
121
|
+
const usuario = await abrirImagem();
|
|
122
|
+
await usuario.type(screen.getByLabelText("Endereço da imagem"), "javascript:alert(1)");
|
|
123
|
+
await usuario.click(screen.getByRole("button", { name: "Inserir imagem" }));
|
|
124
|
+
expect(screen.getByText("Use um endereço http ou https, ou escolha um arquivo.")).toBeInTheDocument();
|
|
125
|
+
expect(execCommand).not.toHaveBeenCalledWith("insertHTML", expect.anything(), expect.anything());
|
|
126
|
+
expect(screen.getByLabelText("Endereço da imagem")).toBeInTheDocument();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("arquivo vira data URL e entra no img", async () => {
|
|
130
|
+
const usuario = await abrirImagem();
|
|
131
|
+
const entrada = document.querySelector('input[type="file"]') as HTMLInputElement;
|
|
132
|
+
const arquivo = new File([new Uint8Array([137, 80, 78, 71])], "brasao.png", { type: "image/png" });
|
|
133
|
+
fireEvent.change(entrada, { target: { files: [arquivo] } });
|
|
134
|
+
await waitFor(() => expect(screen.getByText("brasao.png")).toBeInTheDocument());
|
|
135
|
+
await usuario.click(screen.getByRole("button", { name: "Inserir imagem" }));
|
|
136
|
+
const chamada = execCommand.mock.calls.find((c) => c[0] === "insertHTML");
|
|
137
|
+
expect(chamada![2]).toMatch(/^<img src="data:image\/png;base64,[A-Za-z0-9+/=]+" alt="">$/);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("arquivo que não é PNG, JPEG, GIF ou WebP é recusado; acima de 2 MB também", async () => {
|
|
141
|
+
await abrirImagem();
|
|
142
|
+
const entrada = document.querySelector('input[type="file"]') as HTMLInputElement;
|
|
143
|
+
fireEvent.change(entrada, { target: { files: [new File(["x"], "a.svg", { type: "image/svg+xml" })] } });
|
|
144
|
+
expect(await screen.findByText("Use uma imagem PNG, JPEG, GIF ou WebP.")).toBeInTheDocument();
|
|
145
|
+
const grande = new File(["x"], "g.png", { type: "image/png" });
|
|
146
|
+
Object.defineProperty(grande, "size", { value: 3 * 1024 * 1024 });
|
|
147
|
+
fireEvent.change(entrada, { target: { files: [grande] } });
|
|
148
|
+
expect(await screen.findByText("A imagem passa de 2 MB.")).toBeInTheDocument();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("os textos do painel vêm por labels", async () => {
|
|
152
|
+
await abrirImagem({ labels: { image: "Image", imageUrl: "Image address", imageAlt: "Alt text", imageApply: "Insert image", imageCancel: "Cancel", imageFile: "Choose file" } }, "Image");
|
|
153
|
+
expect(screen.getByLabelText("Image address")).toBeInTheDocument();
|
|
154
|
+
expect(screen.getByLabelText("Alt text")).toBeInTheDocument();
|
|
155
|
+
expect(screen.getByRole("button", { name: "Insert image" })).toBeInTheDocument();
|
|
156
|
+
expect(screen.getByRole("button", { name: "Choose file" })).toBeInTheDocument();
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe("RichTextEditor: tabela", () => {
|
|
161
|
+
it("o painel insere linhas × colunas; o padrão é 3 × 3", async () => {
|
|
162
|
+
const usuario = userEvent.setup();
|
|
163
|
+
render(<RichTextEditor ariaLabel="x" toolbar={["table"]} />);
|
|
164
|
+
await usuario.click(screen.getByRole("button", { name: "Tabela" }));
|
|
165
|
+
expect(screen.getByLabelText("Linhas")).toHaveFocus();
|
|
166
|
+
await usuario.clear(screen.getByLabelText("Linhas"));
|
|
167
|
+
await usuario.type(screen.getByLabelText("Linhas"), "2");
|
|
168
|
+
await usuario.click(screen.getByRole("button", { name: "Inserir tabela" }));
|
|
169
|
+
const chamada = execCommand.mock.calls.find((c) => c[0] === "insertHTML");
|
|
170
|
+
expect(chamada![2]).toBe(htmlDaTabela(2, 3));
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("fora de 1 a 20 linhas e 1 a 10 colunas mostra o erro e não insere", async () => {
|
|
174
|
+
const usuario = userEvent.setup();
|
|
175
|
+
render(<RichTextEditor ariaLabel="x" toolbar={["table"]} />);
|
|
176
|
+
await usuario.click(screen.getByRole("button", { name: "Tabela" }));
|
|
177
|
+
await usuario.clear(screen.getByLabelText("Colunas"));
|
|
178
|
+
await usuario.type(screen.getByLabelText("Colunas"), "11");
|
|
179
|
+
await usuario.click(screen.getByRole("button", { name: "Inserir tabela" }));
|
|
180
|
+
expect(screen.getByText("Use de 1 a 20 linhas e de 1 a 10 colunas.")).toBeInTheDocument();
|
|
181
|
+
expect(execCommand).not.toHaveBeenCalledWith("insertHTML", expect.anything(), expect.anything());
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const TABELA = "<table><tbody><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></tbody></table>";
|
|
185
|
+
const cursorNaCelula = (indice: number) => {
|
|
186
|
+
const celula = area().querySelectorAll("td")[indice];
|
|
187
|
+
const faixa = document.createRange();
|
|
188
|
+
faixa.selectNodeContents(celula);
|
|
189
|
+
window.getSelection()!.removeAllRanges();
|
|
190
|
+
window.getSelection()!.addRange(faixa);
|
|
191
|
+
fireEvent(document, new Event("selectionchange"));
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
it("com o cursor numa célula aparece a barra; sem tabela na toolbar, não aparece", async () => {
|
|
195
|
+
const { rerender } = render(<RichTextEditor ariaLabel="x" toolbar={["table"]} defaultValue={TABELA} />);
|
|
196
|
+
expect(screen.queryByRole("group", { name: "Tabela" })).toBeNull();
|
|
197
|
+
cursorNaCelula(0);
|
|
198
|
+
expect(await screen.findByRole("group", { name: "Tabela" })).toBeInTheDocument();
|
|
199
|
+
rerender(<RichTextEditor ariaLabel="x" toolbar={["bold"]} defaultValue={TABELA} />);
|
|
200
|
+
expect(screen.queryByRole("group", { name: "Tabela" })).toBeNull();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("adiciona e remove linha e coluna na célula do cursor e avisa onChange", async () => {
|
|
204
|
+
const usuario = userEvent.setup();
|
|
205
|
+
const aoMudar = vi.fn();
|
|
206
|
+
render(<RichTextEditor ariaLabel="x" toolbar={["table"]} defaultValue={TABELA} onChange={aoMudar} />);
|
|
207
|
+
cursorNaCelula(0);
|
|
208
|
+
await usuario.click(await screen.findByRole("button", { name: "Adicionar coluna" }));
|
|
209
|
+
expect(area().querySelectorAll("tr")[0].cells.length).toBe(3);
|
|
210
|
+
expect(area().querySelectorAll("tr")[1].cells.length).toBe(3);
|
|
211
|
+
expect(aoMudar).toHaveBeenCalled();
|
|
212
|
+
|
|
213
|
+
cursorNaCelula(0);
|
|
214
|
+
await usuario.click(await screen.findByRole("button", { name: "Adicionar linha" }));
|
|
215
|
+
expect(area().querySelectorAll("tr")).toHaveLength(3);
|
|
216
|
+
|
|
217
|
+
cursorNaCelula(0);
|
|
218
|
+
await usuario.click(await screen.findByRole("button", { name: "Remover linha" }));
|
|
219
|
+
expect(area().querySelectorAll("tr")).toHaveLength(2);
|
|
220
|
+
|
|
221
|
+
cursorNaCelula(0);
|
|
222
|
+
await usuario.click(await screen.findByRole("button", { name: "Remover coluna" }));
|
|
223
|
+
expect(area().querySelectorAll("tr")[0].cells.length).toBe(2);
|
|
224
|
+
|
|
225
|
+
cursorNaCelula(0);
|
|
226
|
+
await usuario.click(await screen.findByRole("button", { name: "Remover tabela" }));
|
|
227
|
+
expect(area().querySelector("table")).toBeNull();
|
|
228
|
+
expect(screen.queryByRole("group", { name: "Tabela" })).toBeNull();
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("remover a única linha ou coluna remove a tabela", async () => {
|
|
232
|
+
const usuario = userEvent.setup();
|
|
233
|
+
render(<RichTextEditor ariaLabel="x" toolbar={["table"]} defaultValue="<table><tbody><tr><td>a</td></tr></tbody></table>" />);
|
|
234
|
+
cursorNaCelula(0);
|
|
235
|
+
await usuario.click(await screen.findByRole("button", { name: "Remover linha" }));
|
|
236
|
+
expect(area().querySelector("table")).toBeNull();
|
|
237
|
+
});
|
|
238
|
+
});
|