-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaction.test.ts
More file actions
192 lines (157 loc) · 5.01 KB
/
Copy pathaction.test.ts
File metadata and controls
192 lines (157 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { beforeEach, describe, expect, type Mock, test, vi } from "vitest";
import { RNConfig } from "@/lib/common/config";
import { Logger } from "@/lib/common/logger";
import { shouldDisplayBasedOnPercentage } from "@/lib/common/utils";
import { track, trackAction, triggerSurvey } from "@/lib/survey/action";
import { SurveyStore } from "@/lib/survey/store";
import type { TSurvey } from "@/types/survey";
vi.mock("@/lib/common/config", () => ({
RNConfig: {
getInstance: vi.fn(() => ({
get: vi.fn(),
})),
},
}));
vi.mock("@/lib/survey/store", () => ({
SurveyStore: {
getInstance: vi.fn(() => ({
setSurvey: vi.fn(),
})),
},
}));
vi.mock("@/lib/common/logger", () => ({
Logger: {
getInstance: vi.fn(() => {
return {
debug: vi.fn(),
};
}),
},
}));
vi.mock("@/lib/common/utils", () => ({
shouldDisplayBasedOnPercentage: vi.fn(),
}));
vi.mock("@react-native-community/netinfo", () => ({
fetch: vi.fn(() => ({
isConnected: true,
})),
}));
describe("survey/action.ts", () => {
const mockSurvey = {
id: "survey_001",
displayPercentage: 50,
triggers: [
{
actionClass: { name: "testAction" },
},
],
};
const mockAppConfig = {
get: vi.fn(),
};
const mockSurveyStore = {
setSurvey: vi.fn(),
};
const mockLogger = {
debug: vi.fn(),
};
beforeEach(() => {
vi.clearAllMocks();
const getInstanceRn = vi.spyOn(RNConfig, "getInstance");
const getInstanceSurveyStore = vi.spyOn(SurveyStore, "getInstance");
const getInstanceLogger = vi.spyOn(Logger, "getInstance");
// Mock instances
getInstanceRn.mockReturnValue(
mockAppConfig as unknown as Promise<RNConfig>,
);
getInstanceSurveyStore.mockReturnValue(
mockSurveyStore as unknown as SurveyStore,
);
getInstanceLogger.mockReturnValue(mockLogger as unknown as Logger);
});
describe("triggerSurvey", () => {
test("does not trigger survey if displayPercentage criteria is not met", () => {
const shouldDisplayBasedOnPercentageMock = vi.mocked(
shouldDisplayBasedOnPercentage,
);
shouldDisplayBasedOnPercentageMock.mockReturnValueOnce(false);
triggerSurvey(mockSurvey as unknown as TSurvey);
// Ensure survey is not set
expect(mockSurveyStore.setSurvey).not.toHaveBeenCalled();
expect(mockLogger.debug).toHaveBeenCalledWith(
'Survey display of "survey_001" skipped based on displayPercentage.',
);
});
test("triggers survey if displayPercentage criteria is met", () => {
// Mock `shouldDisplayBasedOnPercentage` to return true
const shouldDisplayBasedOnPercentageMock = vi.mocked(
shouldDisplayBasedOnPercentage,
);
shouldDisplayBasedOnPercentageMock.mockReturnValueOnce(true);
triggerSurvey(mockSurvey as unknown as TSurvey);
// Ensure survey is set
expect(mockSurveyStore.setSurvey).toHaveBeenCalledWith(mockSurvey);
});
});
describe("trackAction", () => {
const mockActiveSurveys = [mockSurvey];
beforeEach(() => {
mockAppConfig.get.mockReturnValue({
filteredSurveys: mockActiveSurveys,
});
});
test("triggers survey associated with action name", async () => {
(shouldDisplayBasedOnPercentage as unknown as Mock).mockReturnValue(true);
await trackAction("testAction");
// Ensure triggerSurvey is called for the matching survey
expect(mockSurveyStore.setSurvey).toHaveBeenCalledWith(mockSurvey);
});
test("does not trigger survey if no active surveys are found", async () => {
mockAppConfig.get.mockReturnValue({
filteredSurveys: [],
});
await trackAction("testAction");
// Ensure no surveys are triggered
expect(mockSurveyStore.setSurvey).not.toHaveBeenCalled();
expect(mockLogger.debug).toHaveBeenCalledWith(
"No active surveys to display",
);
});
test("logs tracked action name", async () => {
await trackAction("testAction");
expect(mockLogger.debug).toHaveBeenCalledWith(
'Formbricks: Action "testAction" tracked',
);
});
});
describe("track", () => {
const mockActionClasses = [
{
key: "testCode",
type: "code",
name: "testAction",
},
];
beforeEach(() => {
mockAppConfig.get.mockReturnValue({
workspace: {
data: { actionClasses: mockActionClasses },
},
});
});
test("tracks a valid action by code", async () => {
const result = await track("testCode");
expect(result.ok).toBe(true);
});
test("returns error for invalid action code", async () => {
const result = await track("invalidCode");
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error.code).toBe("invalid_code");
expect(result.error.message).toBe(
"Action with identifier 'invalidCode' is unknown. Please add this action in Formbricks in order to use it via the SDK action tracking.",
);
}
});
});
});