From f7c845fd441c40d799c2a45f37f357b274acf82a Mon Sep 17 00:00:00 2001 From: alexander Date: Wed, 22 May 2024 19:18:40 +0200 Subject: [PATCH] on session --- .../{commands.test.ts => on.test.ts} | 36 +++++++++++++++++-- packages/clients/walkerjs/src/index.ts | 4 +-- packages/clients/walkerjs/src/lib/on.ts | 15 ++++++++ packages/types/src/on.ts | 15 ++++++-- packages/utils/src/core/constants.ts | 2 ++ 5 files changed, 65 insertions(+), 7 deletions(-) rename packages/clients/walkerjs/src/__tests__/{commands.test.ts => on.test.ts} (89%) diff --git a/packages/clients/walkerjs/src/__tests__/commands.test.ts b/packages/clients/walkerjs/src/__tests__/on.test.ts similarity index 89% rename from packages/clients/walkerjs/src/__tests__/commands.test.ts rename to packages/clients/walkerjs/src/__tests__/on.test.ts index 3ca2aaf8..9025f60c 100644 --- a/packages/clients/walkerjs/src/__tests__/commands.test.ts +++ b/packages/clients/walkerjs/src/__tests__/on.test.ts @@ -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(() => { @@ -133,7 +133,7 @@ describe('Commands on consent', () => { }); }); -describe('Commands on run', () => { +describe('On Run', () => { let walkerjs: WebClient.Instance; beforeEach(() => { @@ -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); + }); +}); diff --git a/packages/clients/walkerjs/src/index.ts b/packages/clients/walkerjs/src/index.ts index 9f3ef967..a56104ea 100644 --- a/packages/clients/walkerjs/src/index.ts +++ b/packages/clients/walkerjs/src/index.ts @@ -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); diff --git a/packages/clients/walkerjs/src/lib/on.ts b/packages/clients/walkerjs/src/lib/on.ts index dbb7dc19..b1aee693 100644 --- a/packages/clients/walkerjs/src/lib/on.ts +++ b/packages/clients/walkerjs/src/lib/on.ts @@ -19,6 +19,9 @@ export function onApply( case Const.Commands.Run: onRun(instance, onConfig as Array); break; + case Const.Commands.Session: + onSession(instance, onConfig as Array); + break; default: break; } @@ -52,3 +55,15 @@ function onRun( tryCatch(func)(instance); }); } + +function onSession( + instance: WebClient.Instance, + onConfig: Array, +): void { + const { session } = instance; + + if (session) + onConfig.forEach((func) => { + tryCatch(func)(instance, session); + }); +} diff --git a/packages/types/src/on.ts b/packages/types/src/on.ts index 06c0aebe..64ded77f 100644 --- a/packages/types/src/on.ts +++ b/packages/types/src/on.ts @@ -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; run?: Array; + session?: Array; }; // 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 { @@ -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; diff --git a/packages/utils/src/core/constants.ts b/packages/utils/src/core/constants.ts index 664f0bfd..f04817dd 100644 --- a/packages/utils/src/core/constants.ts +++ b/packages/utils/src/core/constants.ts @@ -16,6 +16,7 @@ export type CommandTypes = | 'On' | 'Prefix' | 'Run' + | 'Session' | 'User' | 'Walker'; @@ -35,6 +36,7 @@ export const Commands: Record = { On: 'on', Prefix: 'data-elb', Run: 'run', + Session: 'session', User: 'user', Walker: 'walker', } as const;