|
| 1 | +import { afterEach, expect, test, vi } from "vitest"; |
| 2 | + |
| 3 | +import { ReflagClient } from "../src/client"; |
| 4 | +import { AutoFeedback } from "../src/feedback/feedback"; |
| 5 | +import * as feedbackUi from "../src/feedback/ui"; |
| 6 | +import { HttpClient } from "../src/httpClient"; |
| 7 | +import { testLogger } from "./testLogger"; |
| 8 | + |
| 9 | +const data = { question: "How was it?", score: 4, comment: "Useful!" }; |
| 10 | + |
| 11 | +afterEach(() => vi.restoreAllMocks()); |
| 12 | + |
| 13 | +test.each(["manual", "automated"] as const)( |
| 14 | + "%s feedback rejects a failed Send without notifying, then allows retry", |
| 15 | + async (source) => { |
| 16 | + const open = vi |
| 17 | + .spyOn(feedbackUi, "openFeedbackForm") |
| 18 | + .mockImplementation(() => undefined); |
| 19 | + const onAfterSubmit = vi.fn(); |
| 20 | + let status = 200; |
| 21 | + vi.spyOn(HttpClient.prototype, "post").mockImplementation(async () => |
| 22 | + Response.json( |
| 23 | + status === 200 ? { feedbackId: "123" } : { message: "Unavailable" }, |
| 24 | + { status }, |
| 25 | + ), |
| 26 | + ); |
| 27 | + |
| 28 | + if (source === "manual") { |
| 29 | + const client = new ReflagClient({ |
| 30 | + publishableKey: "test-key", |
| 31 | + user: { id: "user" }, |
| 32 | + logger: testLogger, |
| 33 | + }); |
| 34 | + client.requestFeedback({ flagKey: "flag", onAfterSubmit }); |
| 35 | + await vi.waitFor(() => expect(open).toHaveBeenCalledOnce()); |
| 36 | + } else { |
| 37 | + const auto = new AutoFeedback( |
| 38 | + "https://front.reflag.com", |
| 39 | + testLogger, |
| 40 | + new HttpClient("test-key"), |
| 41 | + (_prompt, handlers) => handlers.openFeedbackForm({ onAfterSubmit }), |
| 42 | + "user", |
| 43 | + ); |
| 44 | + await auto.triggerFeedbackPrompt( |
| 45 | + "user", |
| 46 | + { |
| 47 | + question: data.question, |
| 48 | + promptId: "prompt", |
| 49 | + featureId: "feature", |
| 50 | + showAfter: new Date(), |
| 51 | + showBefore: new Date(Date.now() + 60000), |
| 52 | + }, |
| 53 | + vi.fn(), |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + const { onSubmit } = open.mock.calls[0]![0]; |
| 58 | + status = 500; |
| 59 | + await expect(onSubmit(data)).rejects.toThrow("Couldn't submit feedback"); |
| 60 | + expect(onAfterSubmit).not.toHaveBeenCalled(); |
| 61 | + |
| 62 | + status = 200; |
| 63 | + await expect(onSubmit(data)).resolves.toBeUndefined(); |
| 64 | + }, |
| 65 | +); |
0 commit comments