Skip to content

Commit 059eef4

Browse files
Issue #34 Backfill tests for Atoms/ToastManager
Add tests for atoms toast templates
2 parents ea8c8b2 + db64719 commit 059eef4

File tree

2 files changed

+243
-2
lines changed

2 files changed

+243
-2
lines changed
Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1+
import faker from "faker";
2+
import React from "react";
3+
import { render } from "@testing-library/react";
4+
import { ToastTemplates } from "./toast-templates";
5+
16
describe("ToastTemplates", () => {
2-
test.skip("TODO: Backfill tests for issue https://github.com/AndcultureCode/AndcultureCode.JavaScript.React.Components/issues/34", () => {});
7+
test("when toast type is info, renders info toast", () => {
8+
// Arrange
9+
const toastContent = faker.random.word();
10+
const toast = ToastTemplates.info(toastContent);
11+
12+
// Act
13+
const { getByText } = render(<React.Fragment>{toast}</React.Fragment>);
14+
15+
// Assert
16+
expect(getByText(toastContent)).not.toBeNil();
17+
});
18+
19+
test("when toast type is error, renders error toast", () => {
20+
// Arrange
21+
const toastContent = faker.random.word();
22+
const toast = ToastTemplates.error(toastContent);
23+
24+
// Act
25+
const { getByText } = render(<React.Fragment>{toast}</React.Fragment>);
26+
27+
// Assert
28+
expect(getByText(toastContent)).not.toBeNil();
29+
});
30+
31+
test("when toast type is success, renders success toast", () => {
32+
// Arrange
33+
const toastContent = faker.random.word();
34+
const toast = ToastTemplates.success(toastContent);
35+
36+
// Act
37+
const { getByText } = render(<React.Fragment>{toast}</React.Fragment>);
38+
39+
// Assert
40+
expect(getByText(toastContent)).not.toBeNil();
41+
});
42+
43+
test("when toast type is warning, renders warning toast", () => {
44+
// Arrange
45+
const toastContent = faker.random.word();
46+
const toast = ToastTemplates.warning(toastContent);
47+
48+
// Act
49+
const { getByText } = render(<React.Fragment>{toast}</React.Fragment>);
50+
51+
// Assert
52+
expect(getByText(toastContent)).not.toBeNil();
53+
});
354
});
Lines changed: 191 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,193 @@
1+
import faker from "faker";
2+
import { ToastManager } from "./toast-manager";
3+
import { toast } from "react-toastify";
4+
15
describe("ToastManager", () => {
2-
test.skip("TODO: Backfill tests for issue https://github.com/AndcultureCode/AndcultureCode.JavaScript.React.Components/issues/34", () => {});
6+
// -----------------------------------------------------------------------------------------
7+
// #region dismiss
8+
// -----------------------------------------------------------------------------------------
9+
10+
describe("dismiss", () => {
11+
test("when executed, calls dismiss", () => {
12+
// Arrange
13+
const dismissMethodSpy = jest.spyOn(toast, "dismiss");
14+
const testId = faker.random.number(99);
15+
16+
// Act
17+
ToastManager.dismiss(testId);
18+
19+
// Assert
20+
expect(dismissMethodSpy).toHaveBeenCalledWith(testId);
21+
});
22+
});
23+
24+
// #endregion dismiss
25+
26+
// -----------------------------------------------------------------------------------------
27+
// #region dismissAll
28+
// -----------------------------------------------------------------------------------------
29+
30+
describe("dismissAll", () => {
31+
test("when executed, calls dismiss", () => {
32+
// Arrange
33+
const dismissMethodSpy = jest.spyOn(toast, "dismiss");
34+
35+
// Act
36+
ToastManager.dismissAll();
37+
38+
// Assert
39+
expect(dismissMethodSpy).toHaveBeenCalled();
40+
});
41+
});
42+
43+
// #endregion dismissAll
44+
45+
// -----------------------------------------------------------------------------------------
46+
// #region error
47+
// -----------------------------------------------------------------------------------------
48+
49+
describe("error", () => {
50+
test("when toast created with toastId, returns given toastId", () => {
51+
// Arrange
52+
const expected = faker.random.number(99);
53+
const toastContent = faker.random.word();
54+
const toastOptions = { toastId: expected };
55+
56+
// Act
57+
const toastId = ToastManager.error(toastContent, toastOptions);
58+
59+
// Assert
60+
expect(toastId).toBe(expected);
61+
});
62+
63+
test("when toast created without toastId, returns new toastId", () => {
64+
// Arrange
65+
const toastContent = faker.random.word();
66+
67+
// Act
68+
const toastId = ToastManager.error(toastContent);
69+
70+
// Assert
71+
expect(toastId).not.toBeNil();
72+
});
73+
});
74+
75+
// #endregion error
76+
77+
// -----------------------------------------------------------------------------------------
78+
// #region info
79+
// -----------------------------------------------------------------------------------------
80+
81+
describe("info", () => {
82+
test("when toast created with toastId, returns given toastId", () => {
83+
// Arrange
84+
const expected = faker.random.number(99);
85+
const toastContent = faker.random.word();
86+
const toastOptions = { toastId: expected };
87+
88+
// Act
89+
const toastId = ToastManager.info(toastContent, toastOptions);
90+
91+
// Assert
92+
expect(toastId).toBe(expected);
93+
});
94+
95+
test("when toast created without toastId, returns new toastId", () => {
96+
// Arrange
97+
const toastContent = faker.random.word();
98+
99+
// Act
100+
const toastId = ToastManager.info(toastContent);
101+
102+
// Assert
103+
expect(toastId).not.toBeNil();
104+
});
105+
});
106+
107+
// #endregion info
108+
109+
// -----------------------------------------------------------------------------------------
110+
// #region success
111+
// -----------------------------------------------------------------------------------------
112+
113+
describe("success", () => {
114+
test("when toast created with toastId, returns given toastId", () => {
115+
// Arrange
116+
const expected = faker.random.number(99);
117+
const toastContent = faker.random.word();
118+
const toastOptions = { toastId: expected };
119+
120+
// Act
121+
const toastId = ToastManager.success(toastContent, toastOptions);
122+
123+
// Assert
124+
expect(toastId).toBe(expected);
125+
});
126+
127+
test("when toast created without toastId, returns new toastId", () => {
128+
// Arrange
129+
const toastContent = faker.random.word();
130+
131+
// Act
132+
const toastId = ToastManager.success(toastContent);
133+
134+
// Assert
135+
expect(toastId).not.toBeNil();
136+
});
137+
});
138+
139+
// #endregion success
140+
141+
// -----------------------------------------------------------------------------------------
142+
// #region update
143+
// -----------------------------------------------------------------------------------------
144+
145+
describe("update", () => {
146+
test("when executed, calls update", () => {
147+
// Arrange
148+
const newContent = faker.random.words();
149+
const testId = faker.random.number(99);
150+
const updateMethodSpy = jest.spyOn(ToastManager, "update");
151+
152+
// Act
153+
ToastManager.update(testId, newContent);
154+
155+
// Assert
156+
expect(updateMethodSpy).toHaveBeenCalledWith(testId, newContent);
157+
});
158+
});
159+
160+
// #endregion update
161+
162+
// -----------------------------------------------------------------------------------------
163+
// #region warn
164+
// -----------------------------------------------------------------------------------------
165+
166+
describe("warn", () => {
167+
test("when toast created with toastId, returns given toastId", () => {
168+
// Arrange
169+
const expected = faker.random.number(99);
170+
const toastContent = faker.random.word();
171+
const toastOptions = { toastId: expected };
172+
173+
// Act
174+
const toastId = ToastManager.warn(toastContent, toastOptions);
175+
176+
// Assert
177+
expect(toastId).toBe(expected);
178+
});
179+
180+
test("when toast created without toastId, returns new toastId", () => {
181+
// Arrange
182+
const toastContent = faker.random.word();
183+
184+
// Act
185+
const toastId = ToastManager.warn(toastContent);
186+
187+
// Assert
188+
expect(toastId).not.toBeNil();
189+
});
190+
});
191+
192+
// #endregion warn
3193
});

0 commit comments

Comments
 (0)