@osimatic/helpers-js 1.5.2 → 1.5.4

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.
@@ -4,84 +4,28 @@
4
4
  const { FlashMessage } = require('../flash_message');
5
5
 
6
6
  describe('FlashMessage', () => {
7
- let mockElements;
8
- let mockSnackbar;
9
- let appendedElements;
10
-
11
7
  beforeEach(() => {
12
- // Use Jest fake timers
13
8
  jest.useFakeTimers();
14
-
15
- // Track appended elements
16
- appendedElements = [];
17
-
18
- // Mock snackbar element
19
- mockSnackbar = {
20
- html: jest.fn().mockReturnThis(),
21
- addClass: jest.fn().mockReturnThis(),
22
- remove: jest.fn()
23
- };
24
-
25
- // Mock existing elements to remove
26
- mockElements = {
27
- remove: jest.fn(),
28
- forEach: jest.fn()
29
- };
30
-
31
- // Mock jQuery
32
- global.$ = jest.fn((selector) => {
33
- if (selector === 'div.snackbar') {
34
- return mockElements;
35
- }
36
- if (typeof selector === 'string' && selector.startsWith('<div')) {
37
- return mockSnackbar;
38
- }
39
- if (selector === 'html body') {
40
- return {
41
- append: jest.fn((element) => {
42
- appendedElements.push(element);
43
- })
44
- };
45
- }
46
- return {
47
- append: jest.fn(),
48
- html: jest.fn(),
49
- addClass: jest.fn()
50
- };
51
- });
52
-
53
- // Mock document.location.reload
54
- const reloadMock = jest.fn();
55
- delete global.document.location;
56
- global.document.location = { reload: reloadMock };
57
- delete global.window.location;
58
- global.window.location = { reload: reloadMock };
9
+ document.body.innerHTML = '';
59
10
  });
60
11
 
61
12
  afterEach(() => {
62
13
  jest.clearAllTimers();
63
14
  jest.useRealTimers();
64
- delete global.$;
65
15
  });
66
16
 
67
17
  describe('displaySuccess', () => {
68
18
  test('should call display with success type', () => {
69
19
  const displaySpy = jest.spyOn(FlashMessage, 'display');
70
-
71
20
  FlashMessage.displaySuccess('Test message');
72
-
73
21
  expect(displaySpy).toHaveBeenCalledWith('success', 'Test message', false, null, null, null);
74
-
75
22
  displaySpy.mockRestore();
76
23
  });
77
24
 
78
25
  test('should pass reload parameter', () => {
79
26
  const displaySpy = jest.spyOn(FlashMessage, 'display');
80
-
81
27
  FlashMessage.displaySuccess('Test message', true);
82
-
83
28
  expect(displaySpy).toHaveBeenCalledWith('success', 'Test message', true, null, null, null);
84
-
85
29
  displaySpy.mockRestore();
86
30
  });
87
31
 
@@ -89,11 +33,8 @@ describe('FlashMessage', () => {
89
33
  const displaySpy = jest.spyOn(FlashMessage, 'display');
90
34
  const mockModal = { modal: jest.fn() };
91
35
  const mockCallback = jest.fn();
92
-
93
36
  FlashMessage.displaySuccess('Test', false, mockModal, mockCallback, 'test-id');
94
-
95
37
  expect(displaySpy).toHaveBeenCalledWith('success', 'Test', false, mockModal, mockCallback, 'test-id');
96
-
97
38
  displaySpy.mockRestore();
98
39
  });
99
40
  });
@@ -101,21 +42,8 @@ describe('FlashMessage', () => {
101
42
  describe('displayWarning', () => {
102
43
  test('should call display with warning type', () => {
103
44
  const displaySpy = jest.spyOn(FlashMessage, 'display');
104
-
105
- FlashMessage.displayWarning('Warning message');
106
-
107
- expect(displaySpy).toHaveBeenCalledWith('warning', 'Warning message', false, null, null, null);
108
-
109
- displaySpy.mockRestore();
110
- });
111
-
112
- test('should never reload for warnings', () => {
113
- const displaySpy = jest.spyOn(FlashMessage, 'display');
114
-
115
45
  FlashMessage.displayWarning('Warning message');
116
-
117
46
  expect(displaySpy).toHaveBeenCalledWith('warning', 'Warning message', false, null, null, null);
118
-
119
47
  displaySpy.mockRestore();
120
48
  });
121
49
  });
@@ -123,93 +51,63 @@ describe('FlashMessage', () => {
123
51
  describe('displayError', () => {
124
52
  test('should call display with danger type', () => {
125
53
  const displaySpy = jest.spyOn(FlashMessage, 'display');
126
-
127
54
  FlashMessage.displayError('Error message');
128
-
129
55
  expect(displaySpy).toHaveBeenCalledWith('danger', 'Error message', false, null, null, null);
130
-
131
- displaySpy.mockRestore();
132
- });
133
-
134
- test('should never reload for errors', () => {
135
- const displaySpy = jest.spyOn(FlashMessage, 'display');
136
-
137
- FlashMessage.displayError('Error message');
138
-
139
- expect(displaySpy).toHaveBeenCalledWith('danger', 'Error message', false, null, null, null);
140
-
141
56
  displaySpy.mockRestore();
142
57
  });
143
58
  });
144
59
 
145
60
  describe('display', () => {
146
61
  test('should remove existing snackbars', () => {
62
+ document.body.innerHTML = '<div class="snackbar show">old</div>';
147
63
  FlashMessage.display('success', 'Test message');
148
-
149
- expect(global.$).toHaveBeenCalledWith('div.snackbar');
150
- expect(mockElements.remove).toHaveBeenCalled();
64
+ expect(document.querySelectorAll('div.snackbar')).toHaveLength(1);
65
+ expect(document.querySelector('div.snackbar').innerHTML).toBe('Test message');
151
66
  });
152
67
 
153
68
  test('should create snackbar with correct type', () => {
154
69
  FlashMessage.display('success', 'Test message');
155
-
156
- expect(global.$).toHaveBeenCalledWith(expect.stringContaining('snackbar success'));
70
+ expect(document.querySelector('div.snackbar.success')).not.toBeNull();
157
71
  });
158
72
 
159
73
  test('should create snackbar with danger type', () => {
160
74
  FlashMessage.display('danger', 'Error message');
161
-
162
- expect(global.$).toHaveBeenCalledWith(expect.stringContaining('snackbar danger'));
75
+ expect(document.querySelector('div.snackbar.danger')).not.toBeNull();
163
76
  });
164
77
 
165
78
  test('should create snackbar with warning type', () => {
166
79
  FlashMessage.display('warning', 'Warning message');
167
-
168
- expect(global.$).toHaveBeenCalledWith(expect.stringContaining('snackbar warning'));
80
+ expect(document.querySelector('div.snackbar.warning')).not.toBeNull();
169
81
  });
170
82
 
171
83
  test('should set message content', () => {
172
84
  FlashMessage.display('success', 'Test message');
173
-
174
- expect(mockSnackbar.html).toHaveBeenCalledWith('Test message');
85
+ expect(document.querySelector('div.snackbar').innerHTML).toBe('Test message');
175
86
  });
176
87
 
177
88
  test('should add show class', () => {
178
89
  FlashMessage.display('success', 'Test message');
179
-
180
- expect(mockSnackbar.addClass).toHaveBeenCalledWith('show');
90
+ expect(document.querySelector('div.snackbar').classList.contains('show')).toBe(true);
181
91
  });
182
92
 
183
93
  test('should append snackbar to body', () => {
184
94
  FlashMessage.display('success', 'Test message');
185
-
186
- expect(global.$).toHaveBeenCalledWith('html body');
187
- expect(appendedElements).toHaveLength(1);
188
- expect(appendedElements[0]).toBe(mockSnackbar);
95
+ expect(document.body.contains(document.querySelector('div.snackbar'))).toBe(true);
189
96
  });
190
97
 
191
98
  test('should include domId when provided', () => {
192
99
  FlashMessage.display('success', 'Test', false, null, null, 'my-id');
193
-
194
- expect(global.$).toHaveBeenCalledWith(expect.stringContaining('id="my-id"'));
100
+ expect(document.querySelector('div.snackbar').id).toBe('my-id');
195
101
  });
196
102
 
197
103
  test('should not include id attribute when domId is null', () => {
198
104
  FlashMessage.display('success', 'Test', false, null, null, null);
199
-
200
- const calls = global.$.mock.calls.filter(call =>
201
- typeof call[0] === 'string' && call[0].includes('<div')
202
- );
203
- expect(calls[0][0]).not.toContain('id=');
105
+ expect(document.querySelector('div.snackbar').id).toBe('');
204
106
  });
205
107
 
206
108
  test('should hide modal if provided', () => {
207
- const mockModal = {
208
- modal: jest.fn()
209
- };
210
-
109
+ const mockModal = { modal: jest.fn() };
211
110
  FlashMessage.display('success', 'Test', false, mockModal);
212
-
213
111
  expect(mockModal.modal).toHaveBeenCalledWith('hide');
214
112
  });
215
113
 
@@ -221,14 +119,9 @@ describe('FlashMessage', () => {
221
119
 
222
120
  test('should call onMessageHidden callback after timeout', () => {
223
121
  const mockCallback = jest.fn();
224
-
225
122
  FlashMessage.display('success', 'Test', false, null, mockCallback);
226
-
227
123
  expect(mockCallback).not.toHaveBeenCalled();
228
-
229
- // Fast-forward time by 6000ms
230
124
  jest.advanceTimersByTime(6000);
231
-
232
125
  expect(mockCallback).toHaveBeenCalledTimes(1);
233
126
  });
234
127
 
@@ -241,57 +134,32 @@ describe('FlashMessage', () => {
241
134
 
242
135
  test('should remove snackbar after 6 seconds', () => {
243
136
  FlashMessage.display('success', 'Test');
244
-
245
- // Reset mock to check second call
246
- mockElements.remove.mockClear();
247
-
137
+ expect(document.querySelector('div.snackbar')).not.toBeNull();
248
138
  jest.advanceTimersByTime(6000);
249
-
250
- expect(global.$).toHaveBeenCalledWith('div.snackbar');
251
- expect(mockElements.remove).toHaveBeenCalled();
252
- });
253
-
254
- test.skip('should reload page when reload is true', () => {
255
- // Skip: jsdom location.reload mocking is complex
256
- FlashMessage.display('success', 'Test', true);
257
-
258
- expect(document.location.reload).toHaveBeenCalled();
259
- });
260
-
261
- test.skip('should not reload page when reload is false', () => {
262
- // Skip: jsdom location.reload mocking is complex
263
- FlashMessage.display('success', 'Test', false);
264
-
265
- expect(document.location.reload).not.toHaveBeenCalled();
139
+ expect(document.querySelector('div.snackbar')).toBeNull();
266
140
  });
267
141
 
268
142
  test('should handle HTML message content', () => {
269
143
  const htmlMessage = '<strong>Bold</strong> message';
270
-
271
144
  FlashMessage.display('success', htmlMessage);
272
-
273
- expect(mockSnackbar.html).toHaveBeenCalledWith(htmlMessage);
145
+ expect(document.querySelector('div.snackbar').innerHTML).toBe(htmlMessage);
274
146
  });
275
147
 
276
148
  test('should handle empty message', () => {
277
149
  FlashMessage.display('success', '');
278
-
279
- expect(mockSnackbar.html).toHaveBeenCalledWith('');
150
+ expect(document.querySelector('div.snackbar').innerHTML).toBe('');
280
151
  });
281
152
 
282
153
  test('should handle all parameters together', () => {
283
154
  const mockModal = { modal: jest.fn() };
284
155
  const mockCallback = jest.fn();
285
-
286
156
  FlashMessage.display('warning', 'Test message', false, mockModal, mockCallback, 'test-id');
287
-
288
157
  expect(mockModal.modal).toHaveBeenCalledWith('hide');
289
- expect(mockElements.remove).toHaveBeenCalled();
290
- expect(mockSnackbar.html).toHaveBeenCalledWith('Test message');
291
- expect(mockSnackbar.addClass).toHaveBeenCalledWith('show');
292
-
158
+ expect(document.querySelector('div.snackbar.warning')).not.toBeNull();
159
+ expect(document.querySelector('div.snackbar').innerHTML).toBe('Test message');
160
+ expect(document.querySelector('div.snackbar').classList.contains('show')).toBe(true);
293
161
  jest.advanceTimersByTime(6000);
294
162
  expect(mockCallback).toHaveBeenCalled();
295
163
  });
296
164
  });
297
- });
165
+ });