Skip to content

Commit

Permalink
on session
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed May 22, 2024
1 parent 9938ee6 commit f7c845f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { elb, Walkerjs } from '..';
import { mockDataLayer } from '@elbwalker/jest/web.setup';
import type { WebClient } from '..';

describe('Commands on consent', () => {
describe('On Consent', () => {
let walkerjs: WebClient.Instance;

beforeEach(() => {
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('Commands on consent', () => {
});
});

describe('Commands on run', () => {
describe('On Run', () => {
let walkerjs: WebClient.Instance;

beforeEach(() => {
Expand Down Expand Up @@ -270,3 +270,35 @@ describe('Commands on run', () => {
);
});
});

describe('On Session', () => {
let walkerjs: WebClient.Instance;
const mockFn = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
walkerjs = Walkerjs({ run: true });
});

test('register', () => {
walkerjs = Walkerjs({ run: true, on: { session: [mockFn] } });
const mockFnOn = jest.fn();
elb('walker on', 'session', mockFnOn);

expect(walkerjs.on.session![0]).toBe(mockFn);
expect(walkerjs.on.session![1]).toBe(mockFnOn);
});

test('session disabled', () => {
jest.clearAllMocks();
walkerjs = Walkerjs({ run: true, session: false });

elb('walker on', 'session', mockFn);
expect(mockFn).toHaveBeenCalledTimes(0);
});

test('basics', () => {
elb('walker on', 'session', mockFn);
expect(mockFn).toHaveBeenCalledTimes(1);
});
});
4 changes: 2 additions & 2 deletions packages/clients/walkerjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,11 @@ export function Walkerjs(
data: config.sessionStatic, // Static default session data
instance,
});

if (session) {
instance.session = session;
onApply(instance, 'session');
}

// @TODO on-event for session
}

tryCatch(load)(instance);
Expand Down
15 changes: 15 additions & 0 deletions packages/clients/walkerjs/src/lib/on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export function onApply(
case Const.Commands.Run:
onRun(instance, onConfig as Array<On.RunConfig>);
break;
case Const.Commands.Session:
onSession(instance, onConfig as Array<On.SessionConfig>);
break;
default:
break;
}
Expand Down Expand Up @@ -52,3 +55,15 @@ function onRun(
tryCatch(func)(instance);
});
}

function onSession(
instance: WebClient.Instance,
onConfig: Array<On.SessionConfig>,
): void {
const { session } = instance;

if (session)
onConfig.forEach((func) => {
tryCatch(func)(instance, session);
});
}
15 changes: 12 additions & 3 deletions packages/types/src/on.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { WalkerOS } from './';
import type { WalkerOS } from './';
import type { SessionData } from '@elbwalker/utils';

// Instance state for the on actions
export type Config = {
consent?: Array<ConsentConfig>;
run?: Array<RunConfig>;
session?: Array<SessionConfig>;
};

// On types
export type Types = keyof Config;

// Function definitions for the on actions
export type Functions = ConsentFn | RunFn;
export type Functions = ConsentFn | RunFn | SessionFn;

// Parameters for the onAction function calls
export type Options = ConsentConfig | RunConfig;
export type Options = ConsentConfig | RunConfig | SessionConfig;

// Consent
export interface ConsentConfig {
Expand All @@ -27,3 +29,10 @@ export type ConsentFn = (
// Run
export type RunConfig = RunFn;
export type RunFn = (instance: WalkerOS.Instance) => void;

// Session
export type SessionConfig = SessionFn;
export type SessionFn = (
instance: WalkerOS.Instance,
session: SessionData,
) => void;
2 changes: 2 additions & 0 deletions packages/utils/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type CommandTypes =
| 'On'
| 'Prefix'
| 'Run'
| 'Session'
| 'User'
| 'Walker';

Expand All @@ -35,6 +36,7 @@ export const Commands: Record<CommandTypes, WalkerOS.Commands> = {
On: 'on',
Prefix: 'data-elb',
Run: 'run',
Session: 'session',
User: 'user',
Walker: 'walker',
} as const;
Expand Down

0 comments on commit f7c845f

Please sign in to comment.