Skip to content

Commit 33d6cf1

Browse files
Add tests for MainLayout and utility functions for API response handling
- Implement tests for MainLayout to verify rendering based on authentication state and route. - Create utility functions `extractList` and `extractPagination` for handling API responses. - Set up testing environment with necessary imports for testing-library and vitest.
1 parent a77e5d4 commit 33d6cf1

40 files changed

+4520
-2456
lines changed

package-lock.json

Lines changed: 2431 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"build": "vite build",
1010
"lint": "eslint . --fix",
1111
"lint:check": "eslint .",
12+
"test": "vitest",
1213
"preview": "vite preview",
1314
"electron": "electron .",
1415
"electron:dev": "concurrently \"npm run dev\" \"wait-on http://localhost:5173 && NODE_ENV=development electron .\"",
@@ -74,6 +75,8 @@
7475
},
7576
"devDependencies": {
7677
"@eslint/js": "^9.22.0",
78+
"@testing-library/jest-dom": "^6.6.3",
79+
"@testing-library/react": "^16.1.0",
7780
"@types/react": "^18.2.0",
7881
"@types/react-dom": "^18.2.0",
7982
"@vitejs/plugin-react": "^4.3.4",
@@ -84,8 +87,10 @@
8487
"eslint-plugin-react-hooks": "^5.2.0",
8588
"eslint-plugin-react-refresh": "^0.4.19",
8689
"globals": "^16.0.0",
90+
"jsdom": "^24.1.0",
8791
"tailwindcss": "^3.4.17",
88-
"vite": "^6.3.1"
92+
"vite": "^6.3.1",
93+
"vitest": "^1.6.1"
8994
},
9095
"build": {
9196
"appId": "com.chiropractor.app",

src/api/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export {
3737
useCreateTreatmentPlanMutation,
3838
useGetPatientIncidentsQuery,
3939
useGetIncidentDetailsQuery,
40-
useGetDoctorPatientsQuery,
4140
} from './services/clinicalNotes';
4241

4342
export {
@@ -51,6 +50,13 @@ export {
5150
useGetScheduleStatisticsQuery,
5251
} from './services/doctorScheduleApi';
5352

53+
export {
54+
doctorApi,
55+
useGetDoctorPatientsQuery,
56+
useGetPatientDetailsForDoctorQuery,
57+
useGetDoctorStatsQuery,
58+
} from './services/doctorApi';
59+
5460
// Report/Incident API exports
5561
export {
5662
reportApi,

src/api/services/authApi.js

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@ import { baseQueryWithReauth, CACHE_TIMES } from "../core/baseApi";
33
import { setCredentials, logOut, updateUserProfile } from "../../state/data/authSlice";
44
import { startPeriodicTokenCheck, stopPeriodicTokenCheck, setLoggingOut } from "../core/tokenManager";
55

6+
const normalizeAuthResponse = (response) => {
7+
if (!response) return {};
8+
9+
const container = response.data ?? response;
10+
11+
const user = container.user ?? container.profile ?? container.patient ?? null;
12+
const tokens = container.tokens ?? {
13+
accessToken: container.accessToken ?? response.accessToken ?? response.token ?? null,
14+
refreshToken: container.refreshToken ?? response.refreshToken ?? null,
15+
};
16+
17+
return {
18+
success: response.success ?? container.success ?? null,
19+
message: response.message ?? container.message ?? null,
20+
user,
21+
tokens,
22+
profile: container.profile ?? container.patient ?? null,
23+
raw: response,
24+
};
25+
};
26+
627
export const authApi = createApi({
728
reducerPath: "authApi",
829
baseQuery: baseQueryWithReauth,
@@ -18,27 +39,42 @@ export const authApi = createApi({
1839
method: "POST",
1940
body: credentials,
2041
}),
42+
transformResponse: (response) => normalizeAuthResponse(response),
2143
async onQueryStarted(arg, { dispatch, queryFulfilled, getState }) {
2244
try {
2345
const { data } = await queryFulfilled;
24-
dispatch(setCredentials(data));
46+
47+
if (!data?.user) {
48+
console.error("normalizeAuthResponse returned no user", data);
49+
return;
50+
}
51+
52+
dispatch(
53+
setCredentials({
54+
user: data.user,
55+
accessToken: data.tokens?.accessToken ?? data.token ?? null,
56+
refreshToken: data.tokens?.refreshToken ?? null,
57+
})
58+
);
2559

2660
// Start periodic token checking after successful login
2761
startPeriodicTokenCheck({ dispatch, getState });
2862

2963
// Update user profile if additional data is available
30-
if (data.user) {
64+
const profileSource = data.profile ?? data.user;
65+
66+
if (profileSource) {
3167
dispatch(updateUserProfile({
32-
firstName: data.user.first_name,
33-
lastName: data.user.last_name,
34-
fullName: data.user.full_name,
35-
phoneNumber: data.user.phone_number,
36-
dateOfBirth: data.user.date_of_birth,
37-
gender: data.user.gender,
38-
marriageStatus: data.user.marriage_status,
39-
race: data.user.race,
40-
isVerified: data.user.is_verified,
41-
phoneVerified: data.user.phone_verified,
68+
firstName: profileSource.first_name,
69+
lastName: profileSource.last_name,
70+
fullName: profileSource.full_name,
71+
phoneNumber: profileSource.phone_number,
72+
dateOfBirth: profileSource.date_of_birth,
73+
gender: profileSource.gender,
74+
marriageStatus: profileSource.marriage_status,
75+
race: profileSource.race,
76+
isVerified: profileSource.is_verified,
77+
phoneVerified: profileSource.phone_verified,
4278
}));
4379
}
4480
} catch (error) {

src/api/services/chatApi.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ export const chatApi = createApi({
1010
refetchOnFocus: false, // Prevent automatic refetch on window focus
1111
refetchOnReconnect: true, // Keep this for actual network issues
1212
endpoints: (builder) => ({
13-
// ===============================================
14-
// CONVERSATION ROUTES
15-
// ===============================================
1613

1714
// Create a new conversation
1815
createConversation: builder.mutation({
@@ -124,9 +121,6 @@ export const chatApi = createApi({
124121
invalidatesTags: ["Conversations"],
125122
}),
126123

127-
// ===============================================
128-
// MESSAGE ROUTES
129-
// ===============================================
130124

131125
// Get messages for a conversation (fallback for initial load)
132126
getMessages: builder.query({
@@ -177,9 +171,6 @@ export const chatApi = createApi({
177171
],
178172
}),
179173

180-
// ===============================================
181-
// LONG-POLLING AND REAL-TIME MESSAGING
182-
// ===============================================
183174

184175
// Long-polling endpoint to get new messages (replaces regular GET messages)
185176
pollForNewMessages: builder.query({
@@ -221,9 +212,6 @@ export const chatApi = createApi({
221212
keepUnusedDataFor: 30,
222213
}),
223214

224-
// ===============================================
225-
// LEGACY USER ROUTES
226-
// ===============================================
227215

228216
// Get available users (legacy endpoint)
229217
getAvailableUsers: builder.query({

src/api/services/reportApi.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export const reportApi = createApi({
1010
refetchOnFocus: false, // Prevent automatic refetch on window focus
1111
refetchOnReconnect: true, // Keep this for actual network issues
1212
endpoints: (builder) => ({
13-
// ===== INCIDENT-BASED ENDPOINTS (NEW SYSTEM) =====
1413

1514
// 1.1 Create Incident
1615
createIncident: builder.mutation({
@@ -174,7 +173,6 @@ export const reportApi = createApi({
174173
],
175174
}),
176175

177-
// ===== SIMPLE FORM SUBMISSION ENDPOINTS (NEW) =====
178176

179177
// Submit Patient Info Form
180178
submitPatientInfoForm: builder.mutation({

0 commit comments

Comments
 (0)