Skip to content

Commit f56f924

Browse files
committed
Add notification helpers
1 parent 8f4f0be commit f56f924

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/browser/notifications.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,20 @@ export async function notifyInfo(message: string) {
1818
export async function notifyWarning(message: string) {
1919
await getAdapter().warning(message);
2020
}
21+
22+
export async function notifyFetchError() {
23+
await getAdapter().error('Network error: unable to fetch the API request.');
24+
}
25+
26+
export async function notifyByResponse(response: Response): Promise<void>;
27+
export async function notifyByResponse(response: Response, message: string): Promise<void>
28+
29+
export async function notifyByResponse(response: Response, message?: string) {
30+
if ([200, 201, 204].includes(response.status)) {
31+
if (message) {
32+
await notifySuccess(message);
33+
}
34+
} else {
35+
await notifyError(`Receive wrong status - ${response.statusText}`);
36+
}
37+
}

tests/browser/notifications.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, describe, expect, it, Mock, vi } from 'vitest';
22
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';
44

55
const createAdapter = () => {
66
const success = vi.fn().mockResolvedValue(undefined);
@@ -81,3 +81,48 @@ describe('notify error', () => {
8181
expect(error).toHaveBeenCalledWith('some error');
8282
});
8383
});
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

Comments
 (0)