|
1 | 1 | import { afterEach, describe, expect, it, Mock, vi } from 'vitest'; |
2 | 2 | import { getAdapter } from '../../src/browser/notifications/adapter'; |
3 | | -import { notifyError, notifyInfo, notifySuccess, notifyWarning } from '../../src/browser'; |
| 3 | +import { notifyByResponse, notifyError, notifyFetchError, notifyInfo, notifySuccess, notifyWarning } from '../../src/browser'; |
4 | 4 |
|
5 | 5 | const createAdapter = () => { |
6 | 6 | const success = vi.fn().mockResolvedValue(undefined); |
@@ -81,3 +81,48 @@ describe('notify error', () => { |
81 | 81 | expect(error).toHaveBeenCalledWith('some error'); |
82 | 82 | }); |
83 | 83 | }); |
| 84 | + |
| 85 | +describe('notify fetch error', () => { |
| 86 | + it('notify', async () => { |
| 87 | + const {getAdapter, error} = createAdapter(); |
| 88 | + |
| 89 | + await notifyFetchError(); |
| 90 | + |
| 91 | + expect(getAdapter).toHaveBeenCalledOnce(); |
| 92 | + expect(error).toHaveBeenCalledOnce(); |
| 93 | + expect(error).toHaveBeenCalledWith('Network error: unable to fetch the API request.'); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe('notify by response', () => { |
| 98 | + it.each([ |
| 99 | + [200], |
| 100 | + [201], |
| 101 | + [204] |
| 102 | + ])('success status - %d', async (status) => { |
| 103 | + const {getAdapter, success} = createAdapter(); |
| 104 | + |
| 105 | + await notifyByResponse(new Response(null, {status: status}), 'some foo bar'); |
| 106 | + |
| 107 | + expect(getAdapter).toHaveBeenCalledOnce(); |
| 108 | + expect(success).toHaveBeenCalledWith('some foo bar'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('success without text', async () => { |
| 112 | + const {getAdapter, success} = createAdapter(); |
| 113 | + |
| 114 | + await notifyByResponse(new Response(null, {status: 200})); |
| 115 | + |
| 116 | + expect(getAdapter).not.toBeCalled(); |
| 117 | + expect(success).not.toBeCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('wrong status', async () => { |
| 121 | + const {getAdapter, error} = createAdapter(); |
| 122 | + |
| 123 | + await notifyByResponse(new Response(null, {status: 500, statusText: 'Internal Server Error'})); |
| 124 | + |
| 125 | + expect(getAdapter).toHaveBeenCalledOnce(); |
| 126 | + expect(error).toHaveBeenCalledWith('Receive wrong status - Internal Server Error'); |
| 127 | + }); |
| 128 | +}); |
0 commit comments