Skip to content

Commit b33d2df

Browse files
committed
keep the original test for sendFeedback()
1 parent b5ed596 commit b33d2df

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { BaseClient, createTransport, initAndBind } from '@sentry/core';
2+
import type { BrowserClientReplayOptions, ClientOptions, Event, SeverityLevel } from '@sentry/types';
3+
import { resolvedSyncPromise } from '@sentry/utils';
4+
5+
export interface TestClientOptions extends ClientOptions, BrowserClientReplayOptions {}
6+
7+
/**
8+
*
9+
*/
10+
export class TestClient extends BaseClient<TestClientOptions> {
11+
public constructor(options: TestClientOptions) {
12+
super(options);
13+
}
14+
15+
/**
16+
*
17+
*/
18+
public eventFromException(exception: any): PromiseLike<Event> {
19+
return resolvedSyncPromise({
20+
exception: {
21+
values: [
22+
{
23+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
24+
type: exception.name,
25+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
26+
value: exception.message,
27+
},
28+
],
29+
},
30+
});
31+
}
32+
33+
/**
34+
*
35+
*/
36+
public eventFromMessage(message: string, level: SeverityLevel = 'info'): PromiseLike<Event> {
37+
return resolvedSyncPromise({ message, level });
38+
}
39+
}
40+
41+
/**
42+
*
43+
*/
44+
export function init(options: TestClientOptions): void {
45+
initAndBind(TestClient, options);
46+
}
47+
48+
/**
49+
*
50+
*/
51+
export function getDefaultClientOptions(options: Partial<ClientOptions> = {}): ClientOptions {
52+
return {
53+
integrations: [],
54+
dsn: 'https://username@domain/123',
55+
transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => resolvedSyncPromise({})),
56+
stackParser: () => [],
57+
...options,
58+
};
59+
}

packages/feedback/src/core/mockSdk.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { Envelope, Transport, TransportMakeRequestResponse } from '@sentry/types';
2+
3+
import type { TestClientOptions } from './TestClient';
4+
import { getDefaultClientOptions, init } from './TestClient';
5+
6+
export interface MockSdkParams {
7+
sentryOptions?: Partial<TestClientOptions>;
8+
}
9+
10+
class MockTransport implements Transport {
11+
public send: (request: Envelope) => PromiseLike<TransportMakeRequestResponse>;
12+
13+
public constructor() {
14+
this.send = jest.fn(async () => {
15+
return {
16+
statusCode: 200,
17+
};
18+
});
19+
}
20+
21+
public async flush(): Promise<boolean> {
22+
return true;
23+
}
24+
public async sendEvent(_e: Event): Promise<unknown> {
25+
return {
26+
status: 'skipped',
27+
event: 'ok',
28+
type: 'transaction',
29+
};
30+
}
31+
public async sendSession(): Promise<void> {
32+
return;
33+
}
34+
public async recordLostEvent(): Promise<void> {
35+
return;
36+
}
37+
public async close(): Promise<void> {
38+
return;
39+
}
40+
}
41+
42+
/**
43+
*
44+
*/
45+
export async function mockSdk({ sentryOptions }: MockSdkParams = {}): Promise<void> {
46+
init({
47+
...getDefaultClientOptions(),
48+
dsn: 'https://[email protected]/1',
49+
autoSessionTracking: false,
50+
sendClientReports: false,
51+
transport: () => new MockTransport(),
52+
replaysSessionSampleRate: 0.0,
53+
replaysOnErrorSampleRate: 0.0,
54+
...sentryOptions,
55+
});
56+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { getClient } from '@sentry/core';
2+
3+
import { mockSdk } from './mockSdk';
4+
import { sendFeedback } from './sendFeedback';
5+
6+
describe('sendFeedback', () => {
7+
it('sends feedback', async () => {
8+
mockSdk();
9+
const mockTransport = jest.spyOn(getClient()!.getTransport()!, 'send');
10+
11+
await sendFeedback({
12+
name: 'doe',
13+
14+
message: 'mi',
15+
});
16+
expect(mockTransport).toHaveBeenCalledWith([
17+
{ event_id: expect.any(String), sent_at: expect.any(String) },
18+
[
19+
[
20+
{ type: 'feedback' },
21+
{
22+
breadcrumbs: undefined,
23+
contexts: {
24+
feedback: {
25+
contact_email: '[email protected]',
26+
message: 'mi',
27+
name: 'doe',
28+
replay_id: undefined,
29+
source: 'api',
30+
url: 'http://localhost/',
31+
},
32+
},
33+
level: 'info',
34+
environment: 'production',
35+
event_id: expect.any(String),
36+
platform: 'javascript',
37+
timestamp: expect.any(Number),
38+
type: 'feedback',
39+
},
40+
],
41+
],
42+
]);
43+
});
44+
});

0 commit comments

Comments
 (0)