Skip to content

Commit 9fbf5bf

Browse files
Copilothotlong
andcommitted
refactor: use exact path matching and helper functions in REST expand tests
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 192d425 commit 9fbf5bf

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

packages/rest/src/rest.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,18 @@ describe('RestServer', () => {
418418
});
419419

420420
describe('getData handler expand/select forwarding', () => {
421+
function getRoute(rest: any, pathSuffix: string) {
422+
const routes = rest.getRoutes();
423+
return routes.find(
424+
(r: any) => r.method === 'GET' && r.path === `/api/v1/data/${pathSuffix}`,
425+
);
426+
}
427+
421428
it('should pass expand and select query params to protocol.getData', async () => {
422429
const rest = new RestServer(server as any, protocol as any);
423430
rest.registerRoutes();
424431

425-
// Find the GET /data/:object/:id route handler
426-
const routes = rest.getRoutes();
427-
const getByIdRoute = routes.find(
428-
(r) => r.method === 'GET' && r.path.includes(':id'),
429-
);
432+
const getByIdRoute = getRoute(rest, ':object/:id');
430433
expect(getByIdRoute).toBeDefined();
431434

432435
// Simulate request with expand and select query params
@@ -461,10 +464,7 @@ describe('RestServer', () => {
461464
const rest = new RestServer(server as any, protocol as any);
462465
rest.registerRoutes();
463466

464-
const routes = rest.getRoutes();
465-
const getByIdRoute = routes.find(
466-
(r) => r.method === 'GET' && r.path.includes(':id'),
467-
);
467+
const getByIdRoute = getRoute(rest, ':object/:id');
468468

469469
const mockReq = {
470470
params: { object: 'contact', id: 'c_1' },
@@ -490,15 +490,18 @@ describe('RestServer', () => {
490490
});
491491

492492
describe('findData handler expand/populate forwarding', () => {
493+
function getListRoute(rest: any) {
494+
const routes = rest.getRoutes();
495+
return routes.find(
496+
(r: any) => r.method === 'GET' && r.path === '/api/v1/data/:object',
497+
);
498+
}
499+
493500
it('should pass query params including expand to protocol.findData', async () => {
494501
const rest = new RestServer(server as any, protocol as any);
495502
rest.registerRoutes();
496503

497-
// Find the GET /data/:object route handler (list endpoint, not the :id one)
498-
const routes = rest.getRoutes();
499-
const listRoute = routes.find(
500-
(r) => r.method === 'GET' && r.path.endsWith(':object'),
501-
);
504+
const listRoute = getListRoute(rest);
502505
expect(listRoute).toBeDefined();
503506

504507
const mockReq = {
@@ -528,10 +531,7 @@ describe('RestServer', () => {
528531
const rest = new RestServer(server as any, protocol as any);
529532
rest.registerRoutes();
530533

531-
const routes = rest.getRoutes();
532-
const listRoute = routes.find(
533-
(r) => r.method === 'GET' && r.path.endsWith(':object'),
534-
);
534+
const listRoute = getListRoute(rest);
535535

536536
const mockReq = {
537537
params: { object: 'task' },

0 commit comments

Comments
 (0)