-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeedback-widget.test.js
More file actions
113 lines (97 loc) · 3.62 KB
/
Copy pathfeedback-widget.test.js
File metadata and controls
113 lines (97 loc) · 3.62 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const { NJFeedbackWidget, LANG_TO_CONTENT } = require("./feedback-widget.js");
require("@testing-library/jest-dom");
const { screen, fireEvent, getAllByRole } = require("@testing-library/dom");
const userEvent = require("@testing-library/user-event").default;
beforeAll(() => {
global.fetch = jest.fn(() =>
Promise.resolve({
json: () =>
Promise.resolve({ message: "Success", feedbackId: "test123" }),
})
);
});
beforeEach(() => {
const widget = new NJFeedbackWidget();
document.body.appendChild(widget);
});
afterEach(() => {
document.body.innerHTML = "";
});
describe("feedbackWidget", () => {
describe("handleRating", () => {
it("shows positive prompt text when 'Yes' is clicked", async () => {
const yesButton = screen.getByRole("button", {
name: LANG_TO_CONTENT.en.ratingPositive,
});
await userEvent.click(yesButton);
expect(
screen.queryByText(LANG_TO_CONTENT.en.commentPromptNegative)
).not.toBeVisible();
expect(
screen.queryByText(LANG_TO_CONTENT.en.ratingPrompt)
).not.toBeVisible();
expect(
screen.getByLabelText(LANG_TO_CONTENT.en.commentPromptPositive)
).toBeVisible();
});
it("shows negative prompt text when 'No' is clicked", async () => {
const noButton = screen.getByRole("button", {
name: LANG_TO_CONTENT.en.ratingNegative,
});
await userEvent.click(noButton);
expect(
screen.getByLabelText(LANG_TO_CONTENT.en.commentPromptNegative)
).toBeVisible();
expect(
screen.queryByText(LANG_TO_CONTENT.en.commentPromptPositive)
).not.toBeVisible();
expect(
screen.queryByText(LANG_TO_CONTENT.en.ratingNegative)
).not.toBeVisible();
});
});
describe("aria-disabled", () => {
it("sets aria-disabled on comment form button when clicked", async () => {
const yesButton = screen.getByRole("button", {
name: LANG_TO_CONTENT.en.ratingPositive,
});
await userEvent.click(yesButton);
const commentForm = screen.getByTestId("commentForm");
const commentSubmitButton = screen.getByRole("button", {
name: LANG_TO_CONTENT.en.commentSubmit,
});
expect(commentSubmitButton.getAttribute("aria-disabled")).toBe(null);
fireEvent.submit(commentForm);
expect(commentSubmitButton.getAttribute("aria-disabled")).toBe("true");
const commentSubmitLoading = screen.getByText(
LANG_TO_CONTENT.en.commentSubmitLoading
);
expect(commentSubmitLoading).toBeVisible();
});
it("sets aria-disabled on email submit button when clicked", async () => {
const yesButton = screen.getByRole("button", {
name: LANG_TO_CONTENT.en.ratingPositive,
});
await userEvent.click(yesButton);
const commentTextarea = screen.getByRole("textbox", {
name: LANG_TO_CONTENT.en.commentPromptPositive,
});
await userEvent.type(commentTextarea, "Test");
const commentSubmitButton = screen.getByRole("button", {
name: LANG_TO_CONTENT.en.commentSubmit,
});
await userEvent.click(commentSubmitButton);
const emailForm = screen.getByTestId("emailForm");
const emailSubmitButton = screen.getByRole("button", {
name: LANG_TO_CONTENT.en.emailSubmit,
});
expect(emailSubmitButton.getAttribute("aria-disabled")).toBe(null);
fireEvent.submit(emailForm);
expect(emailSubmitButton.getAttribute("aria-disabled")).toBe("true");
const emailSubmitLoading = screen.getByText(
LANG_TO_CONTENT.en.emailSubmitLoading
);
expect(emailSubmitLoading).toBeVisible();
});
});
});