-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInstrumentSessionView.test.tsx
More file actions
88 lines (73 loc) · 3.01 KB
/
Copy pathInstrumentSessionView.test.tsx
File metadata and controls
88 lines (73 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { render, screen, userEvent, within } from "@atlas/vitest-conf";
import { InstrumentSessionView } from "./InstrumentSessionView";
import { InstrumentSessionProvider } from "./InstrumentSessionProvider";
import { describe, it, expect, vi } from "vitest";
function renderComponentWithProvider() {
return render(
<InstrumentSessionProvider>
<InstrumentSessionView />
</InstrumentSessionProvider>,
);
}
vi.mock(import("./InstrumentSessionProvider"), async (importOriginal) => {
const actual =
await importOriginal<typeof import("./InstrumentSessionProvider")>();
return {
...actual,
useInstrumentSession: () => ({
instrumentSession: "cm54321-1",
setInstrumentSession: vi.fn(),
sessionsList: ["cm123-4", "cm567-8"],
}),
};
});
describe("InstrumentSessionView", () => {
beforeEach(() => {
localStorage.clear();
});
it("shows only the current instrument session", () => {
renderComponentWithProvider();
expect(screen.getByTestId("session-input-button")).toBeInTheDocument();
expect(screen.queryByTestId("session-input-menu")).not.toBeInTheDocument();
});
it("shows sessions from the provided sessions list when clicked", async () => {
renderComponentWithProvider();
const user = userEvent.setup();
await user.click(screen.getByTestId("session-input-button"));
expect(screen.getByTestId("visit-field")).toBeInTheDocument();
expect(screen.getByTestId("session-input-menu")).toBeInTheDocument();
expect(screen.getByText("cm123-4")).toBeInTheDocument();
expect(screen.getByText("cm567-8")).toBeInTheDocument();
});
it("closes menu when a menu item has been clicked", async () => {
renderComponentWithProvider();
const user = userEvent.setup();
await user.click(screen.getByTestId("session-input-button"));
expect(screen.getByTestId("session-input-menu")).toBeInTheDocument();
await user.click(screen.getByText("cm123-4"));
expect(screen.queryByTestId("session-input-menu")).not.toBeInTheDocument();
});
it("closes menu when button has been clicked again", async () => {
renderComponentWithProvider();
const user = userEvent.setup();
await user.click(screen.getByTestId("session-input-button"));
expect(screen.getByTestId("session-input-menu")).toBeInTheDocument();
const backdrop = screen.getByRole("presentation").firstChild;
if (backdrop instanceof Element) {
await user.click(backdrop);
}
expect(screen.queryByTestId("session-input-menu")).not.toBeInTheDocument();
});
it("lets you input a valid session", async () => {
const onSubmit = vi.fn();
renderComponentWithProvider();
const user = userEvent.setup();
await user.click(screen.getByTestId("session-input-button"));
const visitInput = screen.getByTestId("session-input-menu");
const visitTextBox = within(visitInput).getByRole("textbox");
await user.click(visitTextBox);
await user.clear(visitTextBox);
await user.type(visitTextBox, "cm1-1");
await user.keyboard("{Enter}");
});
});