Skip to content

Commit a1e6b57

Browse files
committed
linter: improve typing in test
1 parent 8b7893c commit a1e6b57

File tree

1 file changed

+72
-40
lines changed

1 file changed

+72
-40
lines changed

packages/platform-ui/src/server/index.spec.ts

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import { vi, describe, test, expect, beforeEach } from 'vitest';
22
import serverRequest from './index';
3-
import { ServerRequest, Status } from '@vue-skuilder/common';
4-
5-
// Create a minimal type that matches ServerRequest for testing
6-
type TestRequest = ServerRequest & {
7-
type: 'TEST_REQUEST';
8-
timeout?: number;
9-
};
3+
import { ServerRequestType, Status, CreateClassroom } from '@vue-skuilder/common';
104

5+
// Instead of extending ServerRequest, directly use the CreateClassroom type
116
describe('serverRequest', () => {
127
let mockXHR: {
138
open: ReturnType<typeof vi.fn>;
149
send: ReturnType<typeof vi.fn>;
1510
setRequestHeader: ReturnType<typeof vi.fn>;
1611
withCredentials: boolean;
12+
timeout: number;
1713
onload?: () => void;
1814
ontimeout?: () => void;
1915
onerror?: () => void;
@@ -22,71 +18,104 @@ describe('serverRequest', () => {
2218
responseText?: string;
2319
};
2420

21+
// Create a base test request that matches CreateClassroom exactly
22+
const baseTestRequest: CreateClassroom = {
23+
type: ServerRequestType.CREATE_CLASSROOM,
24+
user: 'testuser',
25+
data: {
26+
students: [],
27+
teachers: ['testuser'],
28+
name: 'Test Classroom',
29+
classMeetingSchedule: 'Never',
30+
peerAssist: false,
31+
joinCode: 'TEST123',
32+
},
33+
response: null,
34+
};
35+
2536
beforeEach(() => {
2637
mockXHR = {
2738
open: vi.fn(),
2839
send: vi.fn(),
2940
setRequestHeader: vi.fn(),
3041
withCredentials: false,
42+
timeout: 0,
3143
};
3244

45+
// eslint-disable-next-line
3346
global.XMLHttpRequest = vi.fn(() => mockXHR) as any;
3447
});
3548

3649
test('configures XMLHttpRequest correctly', async () => {
37-
// Set up the mock to do nothing on send
38-
mockXHR.send = vi.fn();
39-
40-
// Create a promise that resolves when onload is called
41-
const responsePromise = new Promise<void>((resolve) => {
42-
mockXHR.onload = () => resolve();
43-
});
50+
// Create a valid request with a timeout
51+
const testRequest: CreateClassroom = {
52+
...baseTestRequest,
53+
timeout: 7000,
54+
};
4455

45-
// Start the request but don't wait for it
46-
const requestPromise = serverRequest<TestRequest>({ type: 'TEST_REQUEST' });
56+
// Start the request
57+
const requestPromise = serverRequest(testRequest);
4758

4859
// Check that XMLHttpRequest was configured correctly
4960
expect(mockXHR.open).toHaveBeenCalledWith('POST', expect.any(String), true);
5061
expect(mockXHR.withCredentials).toBe(true);
5162
expect(mockXHR.setRequestHeader).toHaveBeenCalledWith('Content-Type', 'application/json');
5263
expect(mockXHR.timeout).toBe(7000);
53-
expect(mockXHR.send).toHaveBeenCalledWith(JSON.stringify({ type: 'TEST_REQUEST' }));
54-
55-
// Simulate successful response after validation
56-
mockXHR.responseText = JSON.stringify({ data: 'test', ok: true });
57-
mockXHR.onload && mockXHR.onload();
64+
expect(mockXHR.send).toHaveBeenCalledWith(JSON.stringify(testRequest));
65+
66+
// Simulate successful response with the expected format for CreateClassroom
67+
mockXHR.responseText = JSON.stringify({
68+
status: Status.ok,
69+
ok: true,
70+
joincode: 'ABC123',
71+
uuid: '123-456-789',
72+
});
73+
if (mockXHR.onload) mockXHR.onload();
5874

5975
// Wait for the request to complete
6076
const result = await requestPromise;
6177

62-
// Verify the result
63-
expect(result.response).toEqual({ data: 'test', ok: true });
78+
// Verify the result matches the expected format
79+
expect(result.response).toEqual({
80+
status: Status.ok,
81+
ok: true,
82+
joincode: 'ABC123',
83+
uuid: '123-456-789',
84+
});
6485
});
6586

6687
test('handles successful response', async () => {
67-
const responseData = { status: Status.success, ok: true, data: { id: '123' } };
68-
6988
// Create a promise that will complete when onload is triggered
70-
const requestPromise = serverRequest<TestRequest>({ type: 'TEST_REQUEST' });
71-
72-
// Simulate a successful response
73-
mockXHR.responseText = JSON.stringify(responseData);
74-
mockXHR.onload && mockXHR.onload();
89+
const requestPromise = serverRequest(baseTestRequest);
90+
91+
// Simulate a successful response that matches the expected structure
92+
mockXHR.responseText = JSON.stringify({
93+
status: Status.ok,
94+
ok: true,
95+
joincode: 'TEST123',
96+
uuid: '123-456',
97+
});
98+
if (mockXHR.onload) mockXHR.onload();
7599

76100
// Wait for the request to complete
77101
const result = await requestPromise;
78102

79103
// Verify the response was processed correctly
80-
expect(result.response).toEqual(responseData);
104+
expect(result.response).toEqual({
105+
status: Status.ok,
106+
ok: true,
107+
joincode: 'TEST123',
108+
uuid: '123-456',
109+
});
81110
});
82111

83112
test('handles JSON parse error in response', async () => {
84113
// Create a promise that will complete when onload is triggered
85-
const requestPromise = serverRequest<TestRequest>({ type: 'TEST_REQUEST' });
114+
const requestPromise = serverRequest(baseTestRequest);
86115

87116
// Simulate an invalid JSON response
88117
mockXHR.responseText = 'Not valid JSON';
89-
mockXHR.onload && mockXHR.onload();
118+
if (mockXHR.onload) mockXHR.onload();
90119

91120
// Wait for the request to complete
92121
const result = await requestPromise;
@@ -101,16 +130,19 @@ describe('serverRequest', () => {
101130

102131
test('handles request timeout', async () => {
103132
// Create a request with custom timeout
104-
const requestPromise = serverRequest<TestRequest>({
105-
type: 'TEST_REQUEST',
133+
const testRequest: CreateClassroom = {
134+
...baseTestRequest,
106135
timeout: 5000,
107-
});
136+
};
137+
138+
// Create a promise
139+
const requestPromise = serverRequest(testRequest);
108140

109141
// Verify timeout is set correctly
110142
expect(mockXHR.timeout).toBe(5000);
111143

112144
// Simulate timeout
113-
mockXHR.ontimeout && mockXHR.ontimeout();
145+
if (mockXHR.ontimeout) mockXHR.ontimeout();
114146

115147
// Wait for the request to complete
116148
const result = await requestPromise;
@@ -125,10 +157,10 @@ describe('serverRequest', () => {
125157

126158
test('handles network error', async () => {
127159
// Create a request
128-
const requestPromise = serverRequest<TestRequest>({ type: 'TEST_REQUEST' });
160+
const requestPromise = serverRequest(baseTestRequest);
129161

130162
// Simulate network error
131-
mockXHR.onerror && mockXHR.onerror();
163+
if (mockXHR.onerror) mockXHR.onerror();
132164

133165
// Wait for the request to complete
134166
const result = await requestPromise;
@@ -148,7 +180,7 @@ describe('serverRequest', () => {
148180
});
149181

150182
// Make the request which should catch the error
151-
const result = await serverRequest<TestRequest>({ type: 'TEST_REQUEST' });
183+
const result = await serverRequest(baseTestRequest);
152184

153185
// Verify error is caught and handled
154186
expect(result.response).toEqual({

0 commit comments

Comments
 (0)