Skip to content

Commit

Permalink
test: tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtechVidra committed Jan 19, 2024
1 parent 9ac0d1e commit ebbf0d0
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/cloud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const init = async (options: FlowsCloudOptions): Promise<void> => {
tracking: (event) => {
options.tracking?.(event);

const { flowHash, flowId, type, projectId, stepIndex, stepHash, userId } = event;
const { flowHash, flowId, type, projectId = "", stepIndex, stepHash, userId } = event;

void (async () =>
api(apiUrl)
Expand Down
7 changes: 4 additions & 3 deletions src/flows-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ export class FlowsContext {

track(props: Omit<TrackingEvent, "userId" | "projectId" | "location">): this {
if (!this.tracking) return this;
this.tracking({
const event: TrackingEvent = {
userId: this.userId,
projectId: this.projectId,
location: getPathname(),
...props,
});
};
if (this.projectId) event.projectId = this.projectId;
this.tracking(event);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface TrackingEvent {
*/
flowHash: string;
userId?: string;
projectId: string;
projectId?: string;
/**
* Browser location
* @example
Expand Down
5 changes: 4 additions & 1 deletion src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ const OptionsStruct: Describe<FlowsInitOptions> = type({
customApiUrl: optional(string()),
onLocationChange: optional(func()) as Describe<FlowsInitOptions["onLocationChange"]>,
});
const CloudOptionsStruct: Describe<Omit<FlowsCloudOptions, keyof FlowsInitOptions>> = type({
const CloudOptionsStruct: Describe<
Omit<FlowsCloudOptions, keyof Omit<FlowsInitOptions, "projectId">>
> = type({
customApiUrl: optional(string()),
projectId: string(),
});

const validateStruct =
Expand Down
15 changes: 15 additions & 0 deletions tests/tracking/tracking.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/tests/reset.css" />
<link rel="stylesheet" href="/public/flows.css" />
</head>
<body>
<button class="start">Start flow</button>
<div class="log"></div>

<script type="module" src="./tracking.ts"></script>
</body>
</html>
13 changes: 13 additions & 0 deletions tests/tracking/tracking.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, test } from "@playwright/test";

test("Emits correct events", async ({ page }) => {
await page.goto("/tracking/tracking.html");
await page.locator(".start").click();
await page.locator(".flows-continue").click();
await page.locator(".flows-back").click();
await page.locator(".flows-cancel").click();
await page.locator(".start").click();
await page.locator(".flows-continue").click();
await page.locator(".flows-finish").click();
await expect(page).toHaveScreenshot({ scale: "css" });
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions tests/tracking/tracking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { init } from "../../dist";

void init({
flows: [
{
id: "flow",
element: ".start",
frequency: "every-time",
steps: [
{
title: "Step 1",
},
{
title: "Step 2",
},
],
},
],
tracking: (e) => {
const p = document.createElement("p");
p.classList.add("log-item");
p.innerText = JSON.stringify(e);
document.querySelector(".log")?.appendChild(p);
},
});

0 comments on commit ebbf0d0

Please sign in to comment.