Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always allow userId override with explicit argument #101

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/main.ts
Expand Up @@ -98,13 +98,15 @@ export default function main() {
}

function resolveUser(userId?: User["userId"]): string | never {
if (userId) {
return userId;
}

if (persistUser) {
return getSessionUser();
} else if (!userId) {
} else {
err("No userId provided and persistUser is disabled");
}

return userId!;
}

/**
Expand Down Expand Up @@ -515,11 +517,7 @@ export default function main() {
err("No featureId provided");
}

if (persistUser) {
options.userId = getSessionUser();
} else if (!options.userId) {
err("No userId provided and persistUser is disabled");
}
const userId = resolveUser(options.userId);

// Wait a tick before opening the feedback form,
// to prevent the same click from closing it.
Expand All @@ -535,7 +533,7 @@ export default function main() {
onScoreSubmit: async (data) => {
const res = await feedback({
featureId: options.featureId,
userId: options.userId,
userId,
companyId: options.companyId,
source: "widget",
...data,
Expand All @@ -548,7 +546,7 @@ export default function main() {
// Default onSubmit handler
await feedback({
featureId: options.featureId,
userId: options.userId,
userId,
companyId: options.companyId,
source: "widget",
...data,
Expand Down
87 changes: 87 additions & 0 deletions test/usage.test.ts
Expand Up @@ -250,6 +250,93 @@ describe("usage", () => {
"User is not set, please call user() first",
);
});

describe("user persistence", () => {
describe("with persistence disabled", () => {
test("should reject tracking without a known userId", async () => {
const bucketInstance = bucket();
bucketInstance.init(KEY, { persistUser: false });

await expect(() =>
bucketInstance.track("fooEvent"),
).rejects.toThrowError(
"No userId provided and persistUser is disabled",
);
});

test("should accept tracking with a known userId", async () => {
nock(`${TRACKING_HOST}/${KEY}`)
.post(/.*\/user/, {
userId: "foo",
attributes: {
name: "john doe",
},
})
.reply(200);
nock(`${TRACKING_HOST}/${KEY}`)
.post(/.*\/event/, {
event: "fooEvent",
userId: "barUser",
})
.reply(200);

const bucketInstance = bucket();
bucketInstance.init(KEY, { persistUser: false });

await expect(bucketInstance.track("fooEvent", undefined, "barUser"))
.resolves;
});
});
});

describe("with persisteence enabled", () => {
test("should accept tracking with a persisted userId", async () => {
nock(`${TRACKING_HOST}/${KEY}`)
.post(/.*\/user/, {
userId: "fooUser",
attributes: {
name: "john doe",
},
})
.reply(200);
nock(`${TRACKING_HOST}/${KEY}`)
.post(/.*\/event/, {
event: "fooEvent",
userId: "fooUser",
})
.reply(200);

const bucketInstance = bucket();
bucketInstance.init(KEY, { persistUser: true });
await bucketInstance.user("fooUser", { name: "john doe" });

await expect(bucketInstance.track("fooEvent")).resolves;
});

test("should accept tracking with an overridden userId", async () => {
nock(`${TRACKING_HOST}/${KEY}`)
.post(/.*\/user/, {
userId: "fooUser",
attributes: {
name: "john doe",
},
})
.reply(200);
nock(`${TRACKING_HOST}/${KEY}`)
.post(/.*\/event/, {
event: "fooEvent",
userId: "barUser",
})
.reply(200);

const bucketInstance = bucket();
bucketInstance.init(KEY, { persistUser: true });
await bucketInstance.user("fooUser", { name: "john doe" });

await expect(bucketInstance.track("fooEvent", undefined, "barUser"))
.resolves;
});
});
});

describe("feedback prompting", () => {
Expand Down