Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Feb 21, 2025
1 parent d7a3dfa commit 858ac12
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 38 deletions.
7 changes: 7 additions & 0 deletions packages/destinations/web/api/examples/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getEvent } from '@elbwalker/utils';

export function entity_action() {
const event = getEvent('entity action');

return JSON.stringify(event.data);
}
2 changes: 2 additions & 0 deletions packages/destinations/web/api/examples/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as events from './events';
export * as mapping from './mapping';
10 changes: 10 additions & 0 deletions packages/destinations/web/api/examples/mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Mapping } from '@elbwalker/types';
import type { DestinationWebAPI } from '../src';

export const entity_action: DestinationWebAPI.EventConfig = {
data: 'data',
};

export const config = {
entity: { action: entity_action },
} satisfies Mapping.Config;
74 changes: 44 additions & 30 deletions packages/destinations/web/api/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import type { WalkerOS } from '@elbwalker/types';
import type { DestinationWebAPI } from '.';
import { createEvent } from '@elbwalker/utils';
import { elb, Walkerjs } from '@elbwalker/walker.js';
import { events, mapping } from '../examples';

describe('Destination API', () => {
const mockSendWeb = jest.fn();
const mockSendWeb = jest.fn(); //.mockImplementation(console.log);

jest.mock('@elbwalker/utils/web', () => ({
...jest.requireActual('@elbwalker/utils/web'),
sendWeb: mockSendWeb,
}));

let destination: DestinationWebAPI.Destination;
const event = { event: 'entity action' } as WalkerOS.Event;
const data = JSON.stringify(event);
let event: WalkerOS.Event;
const url = 'https://api.example.com/';

function push(
event: WalkerOS.Event,
custom: DestinationWebAPI.Custom = { url },
mapping = {},
options = {},
) {
destination.push(event, { custom }, mapping, options);
}

beforeEach(async () => {
beforeEach(() => {
destination = jest.requireActual('.').default;
event = createEvent();
Walkerjs({ pageview: false, session: false, run: true });
jest.clearAllMocks();
});

test('init', () => {
push(event, { url: '' }); // No url
expect(mockSendWeb).not.toHaveBeenCalled();
destination.config = {};
elb('walker destination', destination);

push(event);
elb(event); // No url
expect(mockSendWeb).not.toHaveBeenCalled();

destination.config.custom = { url };
elb(event);
expect(mockSendWeb).toHaveBeenCalledTimes(1);
expect(mockSendWeb).toHaveBeenCalledWith(
url,
data,

const [calledUrl, calledData, calledOptions] = mockSendWeb.mock.calls[0];
expect(calledUrl).toBe(url);
expect(JSON.parse(calledData)).toEqual(event);
expect(calledOptions).toEqual(
expect.objectContaining({
transport: 'fetch',
}),
Expand All @@ -49,7 +51,10 @@ describe('Destination API', () => {
});

test('transform', () => {
push(event, { url, transform: () => 'transformed' });
elb('walker destination', destination, {
custom: { url, transform: () => 'transformed' },
});
elb(event);
expect(mockSendWeb).toHaveBeenCalledWith(
url,
'transformed',
Expand All @@ -58,35 +63,44 @@ describe('Destination API', () => {
});

test('headers', () => {
push(event, { url, headers: { foo: 'bar' } });
elb('walker destination', destination, {
custom: { url, headers: { foo: 'bar' } },
});
elb(event);
expect(mockSendWeb).toHaveBeenCalledWith(
url,
data,
expect.any(String),
expect.objectContaining({
headers: { foo: 'bar' },
}),
);
});

test('method', () => {
push(event, { url, method: 'POST' });
elb('walker destination', destination, {
custom: { url, method: 'POST' },
});
elb(event);
expect(mockSendWeb).toHaveBeenCalledWith(
url,
data,
expect.any(String),
expect.objectContaining({
method: 'POST',
}),
);
});

test('mapping data', () => {
push(event, { url, method: 'POST' }, {}, { data: { foo: 'bar' } });
test('event entity action', () => {
elb('walker destination', destination, {
custom: { url },
mapping: mapping.config,
});
elb(event);

expect(mockSendWeb).toHaveBeenCalledWith(
url,
JSON.stringify({ foo: 'bar' }),
expect.objectContaining({
method: 'POST',
}),
events.entity_action(),
expect.any(Object),
);
});
});
8 changes: 4 additions & 4 deletions packages/destinations/web/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Custom, Destination } from './types';
import { isArray, isDefined } from '@elbwalker/utils';
import { isDefined } from '@elbwalker/utils';
import { sendWeb } from '@elbwalker/utils/web';

// Types
export * as DestinationWebAPI from './types';

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

const data = isDefined(options.data) ? options.data : event;
const value = isArray(data) ? data[0] : data;
const body = transform
? transform(value, config, mapping) // Transform event data
: JSON.stringify(value);
? transform(data, config, mapping) // Transform event data
: JSON.stringify(data);

const func = fn || sendWeb;
func(url, body, { headers, method, transport });
Expand Down
6 changes: 4 additions & 2 deletions packages/destinations/web/api/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WalkerOS } from '@elbwalker/types';
import type { Mapping } from '@elbwalker/types';
import type { SendDataValue, SendHeaders } from '@elbwalker/utils';
import type { SendWebTransport } from '@elbwalker/utils/web';

Expand All @@ -19,8 +19,10 @@ export interface Custom {

export interface CustomEvent {}

export type EventConfig = Mapping.EventConfig<CustomEvent>;

export type Transform = (
event?: WalkerOS.Event | WalkerOS.Property,
data?: unknown,
config?: Config,
mapping?: DestinationWeb.EventMapping<CustomEvent>,
) => SendDataValue;
3 changes: 1 addition & 2 deletions packages/destinations/web/google-gtm/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ describe('destination google-tag-manager', () => {
destination = jest.requireActual('.').default;
destination.config = config;
event = createEvent();
Walkerjs({ pageview: false, session: false });
elb('walker run');
Walkerjs({ pageview: false, session: false, run: true });
});

test('init', () => {
Expand Down

0 comments on commit 858ac12

Please sign in to comment.