Skip to content

Commit 035df93

Browse files
committed
add tests
1 parent daf2bcb commit 035df93

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

packages/client/test/clients/guide/guide.test.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,4 +3271,118 @@ describe("KnockGuideClient", () => {
32713271
);
32723272
});
32733273
});
3274+
3275+
describe("setDebug", () => {
3276+
test("sets debug state with debugging: true", () => {
3277+
const client = new KnockGuideClient(mockKnock, channelId);
3278+
client.store.state.debug = undefined;
3279+
3280+
const fetchSpy = vi
3281+
.spyOn(client, "fetch")
3282+
.mockImplementation(() => Promise.resolve({ status: "ok" }));
3283+
const subscribeSpy = vi
3284+
.spyOn(client, "subscribe")
3285+
.mockImplementation(() => {});
3286+
3287+
client.setDebug();
3288+
3289+
expect(client.store.state.debug!.debugging!).toBe(true);
3290+
3291+
// calls fetch and subscribe when not already debugging
3292+
expect(fetchSpy).toHaveBeenCalled();
3293+
expect(subscribeSpy).toHaveBeenCalled();
3294+
});
3295+
3296+
test("sets debug state with provided options", () => {
3297+
const client = new KnockGuideClient(mockKnock, channelId);
3298+
client.store.state.debug = undefined;
3299+
3300+
vi.spyOn(client, "fetch").mockImplementation(() => Promise.resolve({ status: "ok" }));
3301+
vi.spyOn(client, "subscribe").mockImplementation(() => {});
3302+
3303+
client.setDebug({ forcedGuideKey: "test_guide" });
3304+
3305+
expect(client.store.state.debug!.debugging!).toBe(true);
3306+
expect(client.store.state.debug!.forcedGuideKey!).toBe("test_guide");
3307+
});
3308+
3309+
test("does not call fetch and subscribe when already debugging", () => {
3310+
const client = new KnockGuideClient(mockKnock, channelId);
3311+
client.store.state.debug = { debugging: true };
3312+
3313+
const fetchSpy = vi
3314+
.spyOn(client, "fetch")
3315+
.mockImplementation(() => Promise.resolve({ status: "ok" }));
3316+
const subscribeSpy = vi
3317+
.spyOn(client, "subscribe")
3318+
.mockImplementation(() => {});
3319+
3320+
client.setDebug();
3321+
3322+
expect(client.store.state.debug!.debugging!).toBe(true);
3323+
3324+
expect(fetchSpy).not.toHaveBeenCalled();
3325+
expect(subscribeSpy).not.toHaveBeenCalled();
3326+
});
3327+
});
3328+
3329+
describe("unsetDebug", () => {
3330+
test("sets debug state to undefined", () => {
3331+
const client = new KnockGuideClient(mockKnock, channelId);
3332+
client.store.state.debug = { debugging: true };
3333+
3334+
const fetchSpy = vi
3335+
.spyOn(client, "fetch")
3336+
.mockImplementation(() => Promise.resolve({ status: "ok" }));
3337+
const subscribeSpy = vi
3338+
.spyOn(client, "subscribe")
3339+
.mockImplementation(() => {});
3340+
3341+
client.unsetDebug();
3342+
3343+
expect(client.store.state.debug).toBe(undefined);
3344+
3345+
// calls fetch and subscribe when was debugging
3346+
expect(fetchSpy).toHaveBeenCalled();
3347+
expect(subscribeSpy).toHaveBeenCalled();
3348+
});
3349+
3350+
test("does not call fetch and subscribe when was not debugging", () => {
3351+
const client = new KnockGuideClient(mockKnock, channelId);
3352+
client.store.state.debug = undefined;
3353+
3354+
const fetchSpy = vi
3355+
.spyOn(client, "fetch")
3356+
.mockImplementation(() => Promise.resolve({ status: "ok" }));
3357+
const subscribeSpy = vi
3358+
.spyOn(client, "subscribe")
3359+
.mockImplementation(() => {});
3360+
3361+
client.unsetDebug();
3362+
3363+
expect(client.store.state.debug).toBe(undefined);
3364+
3365+
expect(fetchSpy).not.toHaveBeenCalled();
3366+
expect(subscribeSpy).not.toHaveBeenCalled();
3367+
});
3368+
3369+
test("does not call fetch and subscribe when debug exists but debugging is false", () => {
3370+
const client = new KnockGuideClient(mockKnock, channelId);
3371+
client.store.state.debug = { debugging: false };
3372+
3373+
const fetchSpy = vi
3374+
.spyOn(client, "fetch")
3375+
.mockImplementation(() => Promise.resolve({ status: "ok" }));
3376+
const subscribeSpy = vi
3377+
.spyOn(client, "subscribe")
3378+
.mockImplementation(() => {});
3379+
3380+
client.unsetDebug();
3381+
3382+
expect(client.store.state.debug).toBe(undefined);
3383+
3384+
expect(fetchSpy).not.toHaveBeenCalled();
3385+
expect(subscribeSpy).not.toHaveBeenCalled();
3386+
});
3387+
});
32743388
});

0 commit comments

Comments
 (0)