Skip to content

Commit 858ac12

Browse files
examples
1 parent d7a3dfa commit 858ac12

File tree

7 files changed

+72
-38
lines changed

7 files changed

+72
-38
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getEvent } from '@elbwalker/utils';
2+
3+
export function entity_action() {
4+
const event = getEvent('entity action');
5+
6+
return JSON.stringify(event.data);
7+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * as events from './events';
2+
export * as mapping from './mapping';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { Mapping } from '@elbwalker/types';
2+
import type { DestinationWebAPI } from '../src';
3+
4+
export const entity_action: DestinationWebAPI.EventConfig = {
5+
data: 'data',
6+
};
7+
8+
export const config = {
9+
entity: { action: entity_action },
10+
} satisfies Mapping.Config;
Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
import type { WalkerOS } from '@elbwalker/types';
22
import type { DestinationWebAPI } from '.';
3+
import { createEvent } from '@elbwalker/utils';
4+
import { elb, Walkerjs } from '@elbwalker/walker.js';
5+
import { events, mapping } from '../examples';
36

47
describe('Destination API', () => {
5-
const mockSendWeb = jest.fn();
8+
const mockSendWeb = jest.fn(); //.mockImplementation(console.log);
9+
610
jest.mock('@elbwalker/utils/web', () => ({
711
...jest.requireActual('@elbwalker/utils/web'),
812
sendWeb: mockSendWeb,
913
}));
1014

1115
let destination: DestinationWebAPI.Destination;
12-
const event = { event: 'entity action' } as WalkerOS.Event;
13-
const data = JSON.stringify(event);
16+
let event: WalkerOS.Event;
1417
const url = 'https://api.example.com/';
1518

16-
function push(
17-
event: WalkerOS.Event,
18-
custom: DestinationWebAPI.Custom = { url },
19-
mapping = {},
20-
options = {},
21-
) {
22-
destination.push(event, { custom }, mapping, options);
23-
}
24-
25-
beforeEach(async () => {
19+
beforeEach(() => {
2620
destination = jest.requireActual('.').default;
21+
event = createEvent();
22+
Walkerjs({ pageview: false, session: false, run: true });
23+
jest.clearAllMocks();
2724
});
2825

2926
test('init', () => {
30-
push(event, { url: '' }); // No url
31-
expect(mockSendWeb).not.toHaveBeenCalled();
27+
destination.config = {};
28+
elb('walker destination', destination);
3229

33-
push(event);
30+
elb(event); // No url
31+
expect(mockSendWeb).not.toHaveBeenCalled();
3432

33+
destination.config.custom = { url };
34+
elb(event);
3535
expect(mockSendWeb).toHaveBeenCalledTimes(1);
36-
expect(mockSendWeb).toHaveBeenCalledWith(
37-
url,
38-
data,
36+
37+
const [calledUrl, calledData, calledOptions] = mockSendWeb.mock.calls[0];
38+
expect(calledUrl).toBe(url);
39+
expect(JSON.parse(calledData)).toEqual(event);
40+
expect(calledOptions).toEqual(
3941
expect.objectContaining({
4042
transport: 'fetch',
4143
}),
@@ -49,7 +51,10 @@ describe('Destination API', () => {
4951
});
5052

5153
test('transform', () => {
52-
push(event, { url, transform: () => 'transformed' });
54+
elb('walker destination', destination, {
55+
custom: { url, transform: () => 'transformed' },
56+
});
57+
elb(event);
5358
expect(mockSendWeb).toHaveBeenCalledWith(
5459
url,
5560
'transformed',
@@ -58,35 +63,44 @@ describe('Destination API', () => {
5863
});
5964

6065
test('headers', () => {
61-
push(event, { url, headers: { foo: 'bar' } });
66+
elb('walker destination', destination, {
67+
custom: { url, headers: { foo: 'bar' } },
68+
});
69+
elb(event);
6270
expect(mockSendWeb).toHaveBeenCalledWith(
6371
url,
64-
data,
72+
expect.any(String),
6573
expect.objectContaining({
6674
headers: { foo: 'bar' },
6775
}),
6876
);
6977
});
7078

7179
test('method', () => {
72-
push(event, { url, method: 'POST' });
80+
elb('walker destination', destination, {
81+
custom: { url, method: 'POST' },
82+
});
83+
elb(event);
7384
expect(mockSendWeb).toHaveBeenCalledWith(
7485
url,
75-
data,
86+
expect.any(String),
7687
expect.objectContaining({
7788
method: 'POST',
7889
}),
7990
);
8091
});
8192

82-
test('mapping data', () => {
83-
push(event, { url, method: 'POST' }, {}, { data: { foo: 'bar' } });
93+
test('event entity action', () => {
94+
elb('walker destination', destination, {
95+
custom: { url },
96+
mapping: mapping.config,
97+
});
98+
elb(event);
99+
84100
expect(mockSendWeb).toHaveBeenCalledWith(
85101
url,
86-
JSON.stringify({ foo: 'bar' }),
87-
expect.objectContaining({
88-
method: 'POST',
89-
}),
102+
events.entity_action(),
103+
expect.any(Object),
90104
);
91105
});
92106
});

packages/destinations/web/api/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Custom, Destination } from './types';
2-
import { isArray, isDefined } from '@elbwalker/utils';
2+
import { isDefined } from '@elbwalker/utils';
33
import { sendWeb } from '@elbwalker/utils/web';
4+
45
// Types
56
export * as DestinationWebAPI from './types';
67

@@ -16,10 +17,9 @@ export const destinationWebAPI: Destination = {
1617
if (!url) return;
1718

1819
const data = isDefined(options.data) ? options.data : event;
19-
const value = isArray(data) ? data[0] : data;
2020
const body = transform
21-
? transform(value, config, mapping) // Transform event data
22-
: JSON.stringify(value);
21+
? transform(data, config, mapping) // Transform event data
22+
: JSON.stringify(data);
2323

2424
const func = fn || sendWeb;
2525
func(url, body, { headers, method, transport });

packages/destinations/web/api/src/types/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { WalkerOS } from '@elbwalker/types';
1+
import type { Mapping } from '@elbwalker/types';
22
import type { SendDataValue, SendHeaders } from '@elbwalker/utils';
33
import type { SendWebTransport } from '@elbwalker/utils/web';
44

@@ -19,8 +19,10 @@ export interface Custom {
1919

2020
export interface CustomEvent {}
2121

22+
export type EventConfig = Mapping.EventConfig<CustomEvent>;
23+
2224
export type Transform = (
23-
event?: WalkerOS.Event | WalkerOS.Property,
25+
data?: unknown,
2426
config?: Config,
2527
mapping?: DestinationWeb.EventMapping<CustomEvent>,
2628
) => SendDataValue;

packages/destinations/web/google-gtm/src/index.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ describe('destination google-tag-manager', () => {
1919
destination = jest.requireActual('.').default;
2020
destination.config = config;
2121
event = createEvent();
22-
Walkerjs({ pageview: false, session: false });
23-
elb('walker run');
22+
Walkerjs({ pageview: false, session: false, run: true });
2423
});
2524

2625
test('init', () => {

0 commit comments

Comments
 (0)