Skip to content

Commit 72bc636

Browse files
committed
fix: tests:
1 parent 27b2fb9 commit 72bc636

File tree

2 files changed

+88
-51
lines changed

2 files changed

+88
-51
lines changed

packages/react-native/src/lib/environment/tests/state.test.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import {
2222

2323
// Mock the FormbricksAPI so we can control environment.getState
2424
vi.mock("@/lib/common/api", () => ({
25-
ApiClient: vi.fn().mockImplementation(() => ({
26-
getEnvironmentState: vi.fn(),
27-
})),
25+
ApiClient: vi.fn().mockImplementation(function () {
26+
return { getEnvironmentState: vi.fn() };
27+
}),
2828
}));
2929

3030
// Mock logger (so we don’t spam console)
@@ -70,7 +70,7 @@ describe("environment/state.ts", () => {
7070
describe("fetchEnvironmentState()", () => {
7171
test("returns ok(...) with environment state", async () => {
7272
// Setup mock
73-
(ApiClient as unknown as Mock).mockImplementationOnce(() => {
73+
(ApiClient as unknown as Mock).mockImplementationOnce(function () {
7474
return {
7575
getEnvironmentState: vi.fn().mockResolvedValue({
7676
ok: true,
@@ -103,7 +103,7 @@ describe("environment/state.ts", () => {
103103
message: "Access denied",
104104
};
105105

106-
(ApiClient as unknown as Mock).mockImplementationOnce(() => {
106+
(ApiClient as unknown as Mock).mockImplementationOnce(function () {
107107
return {
108108
getEnvironmentState: vi.fn().mockResolvedValue({
109109
ok: false,
@@ -131,7 +131,7 @@ describe("environment/state.ts", () => {
131131
responseMessage: "Network fail",
132132
};
133133

134-
(ApiClient as unknown as Mock).mockImplementationOnce(() => {
134+
(ApiClient as unknown as Mock).mockImplementationOnce(function () {
135135
return {
136136
getEnvironmentState: vi.fn().mockRejectedValue(mockNetworkError),
137137
};
@@ -208,12 +208,14 @@ describe("environment/state.ts", () => {
208208

209209
mockRNConfig.mockReturnValue(mockConfig as unknown as Promise<RNConfig>);
210210

211-
(ApiClient as Mock).mockImplementation(() => ({
212-
getEnvironmentState: vi.fn().mockResolvedValue({
213-
ok: true,
214-
data: mockNewState,
215-
}),
216-
}));
211+
(ApiClient as Mock).mockImplementation(function () {
212+
return {
213+
getEnvironmentState: vi.fn().mockResolvedValue({
214+
ok: true,
215+
data: mockNewState,
216+
}),
217+
};
218+
});
217219

218220
(filterSurveys as Mock).mockReturnValue([]);
219221

@@ -242,11 +244,13 @@ describe("environment/state.ts", () => {
242244
mockRNConfig.mockReturnValue(mockConfig as unknown as Promise<RNConfig>);
243245

244246
// Mock API to throw an error
245-
(ApiClient as Mock).mockImplementation(() => ({
246-
getEnvironmentState: vi
247-
.fn()
248-
.mockRejectedValue(new Error("Network error")),
249-
}));
247+
(ApiClient as Mock).mockImplementation(function () {
248+
return {
249+
getEnvironmentState: vi
250+
.fn()
251+
.mockRejectedValue(new Error("Network error")),
252+
};
253+
});
250254

251255
addEnvironmentStateExpiryCheckListener();
252256

@@ -272,9 +276,9 @@ describe("environment/state.ts", () => {
272276

273277
mockRNConfig.mockReturnValue(mockConfig as unknown as Promise<RNConfig>);
274278

275-
const apiMock = vi.fn().mockImplementation(() => ({
276-
getEnvironmentState: vi.fn(),
277-
}));
279+
const apiMock = vi.fn().mockImplementation(function () {
280+
return { getEnvironmentState: vi.fn() };
281+
});
278282

279283
(ApiClient as Mock).mockImplementation(apiMock);
280284

packages/react-native/src/lib/user/tests/update.test.ts

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ vi.mock("@/lib/common/utils", () => ({
3333
}));
3434

3535
vi.mock("@/lib/common/api", () => ({
36-
ApiClient: vi.fn().mockImplementation(() => ({
37-
createOrUpdateUser: vi.fn(),
38-
})),
36+
ApiClient: vi.fn().mockImplementation(function () {
37+
return { createOrUpdateUser: vi.fn() };
38+
}),
3939
}));
4040

4141
describe("sendUpdatesToBackend", () => {
@@ -56,9 +56,9 @@ describe("sendUpdatesToBackend", () => {
5656
},
5757
};
5858

59-
(ApiClient as Mock).mockImplementation(() => ({
60-
createOrUpdateUser: vi.fn().mockResolvedValue(mockResponse),
61-
}));
59+
(ApiClient as Mock).mockImplementation(function () {
60+
return { createOrUpdateUser: vi.fn().mockResolvedValue(mockResponse) };
61+
});
6262

6363
const result = await sendUpdatesToBackend({
6464
appUrl: mockAppUrl,
@@ -68,19 +68,31 @@ describe("sendUpdatesToBackend", () => {
6868

6969
expect(result.ok).toBe(true);
7070
if (result.ok) {
71-
expect(result.data.state.data).toEqual({ userId: mockUserId, attributes: mockAttributes });
71+
expect(result.data.state.data).toEqual({
72+
userId: mockUserId,
73+
attributes: mockAttributes,
74+
});
7275
}
7376
});
7477

7578
test("returns network error if API call fails", async () => {
76-
const mockUpdates: TUpdates = { userId: mockUserId, attributes: mockAttributes };
79+
const mockUpdates: TUpdates = {
80+
userId: mockUserId,
81+
attributes: mockAttributes,
82+
};
7783

78-
(ApiClient as Mock).mockImplementation(() => ({
79-
createOrUpdateUser: vi.fn().mockResolvedValue({
80-
ok: false,
81-
error: { code: "network_error", message: "Request failed", status: 500 },
82-
}),
83-
}));
84+
(ApiClient as Mock).mockImplementation(function () {
85+
return {
86+
createOrUpdateUser: vi.fn().mockResolvedValue({
87+
ok: false,
88+
error: {
89+
code: "network_error",
90+
message: "Request failed",
91+
status: 500,
92+
},
93+
}),
94+
};
95+
});
8496

8597
const result = await sendUpdatesToBackend({
8698
appUrl: mockAppUrl,
@@ -91,16 +103,25 @@ describe("sendUpdatesToBackend", () => {
91103
expect(result.ok).toBe(false);
92104
if (!result.ok) {
93105
expect(result.error.code).toBe("network_error");
94-
expect(result.error.message).toBe("Error updating user with userId user_123");
106+
expect(result.error.message).toBe(
107+
"Error updating user with userId user_123"
108+
);
95109
}
96110
});
97111

98112
test("throws error if network request fails", async () => {
99-
const mockUpdates: TUpdates = { userId: mockUserId, attributes: { plan: "premium" } };
113+
const mockUpdates: TUpdates = {
114+
userId: mockUserId,
115+
attributes: { plan: "premium" },
116+
};
100117

101-
(ApiClient as Mock).mockImplementation(() => ({
102-
createOrUpdateUser: vi.fn().mockRejectedValue(new Error("Network error")),
103-
}));
118+
(ApiClient as Mock).mockImplementation(function () {
119+
return {
120+
createOrUpdateUser: vi
121+
.fn()
122+
.mockRejectedValue(new Error("Network error")),
123+
};
124+
});
104125

105126
await expect(
106127
sendUpdatesToBackend({
@@ -146,11 +167,13 @@ describe("sendUpdates", () => {
146167
},
147168
};
148169

149-
(ApiClient as Mock).mockImplementation(() => ({
150-
createOrUpdateUser: vi.fn().mockResolvedValue(mockResponse),
151-
}));
170+
(ApiClient as Mock).mockImplementation(function () {
171+
return { createOrUpdateUser: vi.fn().mockResolvedValue(mockResponse) };
172+
});
152173

153-
const result = await sendUpdates({ updates: { userId: mockUserId, attributes: mockAttributes } });
174+
const result = await sendUpdates({
175+
updates: { userId: mockUserId, attributes: mockAttributes },
176+
});
154177

155178
expect(result.ok).toBe(true);
156179
});
@@ -165,11 +188,15 @@ describe("sendUpdates", () => {
165188
},
166189
};
167190

168-
(ApiClient as Mock).mockImplementation(() => ({
169-
createOrUpdateUser: vi.fn().mockResolvedValue(mockErrorResponse),
170-
}));
191+
(ApiClient as Mock).mockImplementation(function () {
192+
return {
193+
createOrUpdateUser: vi.fn().mockResolvedValue(mockErrorResponse),
194+
};
195+
});
171196

172-
const result = await sendUpdates({ updates: { userId: mockUserId, attributes: mockAttributes } });
197+
const result = await sendUpdates({
198+
updates: { userId: mockUserId, attributes: mockAttributes },
199+
});
173200

174201
expect(result.ok).toBe(false);
175202
if (!result.ok) {
@@ -178,11 +205,17 @@ describe("sendUpdates", () => {
178205
});
179206

180207
test("handles unexpected errors", async () => {
181-
(ApiClient as Mock).mockImplementation(() => ({
182-
createOrUpdate: vi.fn().mockRejectedValue(new Error("Unexpected error")),
183-
}));
208+
(ApiClient as Mock).mockImplementation(function () {
209+
return {
210+
createOrUpdate: vi
211+
.fn()
212+
.mockRejectedValue(new Error("Unexpected error")),
213+
};
214+
});
184215

185-
const result = await sendUpdates({ updates: { userId: mockUserId, attributes: mockAttributes } });
216+
const result = await sendUpdates({
217+
updates: { userId: mockUserId, attributes: mockAttributes },
218+
});
186219

187220
expect(result.ok).toBe(false);
188221
if (!result.ok) {

0 commit comments

Comments
 (0)