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