1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-02 20:35:25 +02:00

chore(deps): upgrade to msw v2 [EE-6489] (#10911)

This commit is contained in:
Chaim Lev-Ari 2024-01-04 16:57:21 +07:00 committed by GitHub
parent ecd603db8c
commit 400a80c07d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 2602 additions and 1669 deletions

View file

@ -0,0 +1,42 @@
/* eslint-disable import/order */
/* eslint-disable @typescript-eslint/no-var-requires */
/**
* @note The block below contains polyfills for Node.js globals
* required for Jest to function when running JSDOM tests.
* These HAVE to be require's and HAVE to be in this exact
* order, since "undici" depends on the "TextEncoder" global API.
*
* Consider migrating to a more modern test runner if
* you don't want to deal with this.
*/
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { TextDecoder, TextEncoder } = require('node:util');
Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
});
const { ReadableStream } = require('node:stream/web');
Object.defineProperties(globalThis, {
ReadableStream: { value: ReadableStream },
});
const { Blob, File } = require('node:buffer');
const { fetch, Headers, FormData, Request, Response } = require('undici');
Object.defineProperties(globalThis, {
fetch: { value: fetch, writable: true },
Blob: { value: Blob },
File: { value: File },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
});
/* eslint-enable @typescript-eslint/no-var-requires */
/* eslint-enable import/order */

View file

@ -1,4 +1,4 @@
import { DefaultBodyType, PathParams, rest } from 'msw';
import { http, HttpResponse } from 'msw';
import {
Edition,
@ -35,64 +35,56 @@ const licenseInfo: LicenseInfo = {
};
export const handlers = [
rest.get('/api/teams', async (req, res, ctx) =>
res(ctx.json(createMockTeams(10)))
),
http.get('/api/teams', async () => HttpResponse.json(createMockTeams(10))),
rest.post<{ name: string }>('/api/teams', (req, res, ctx) =>
res(ctx.status(204))
http.post<{ name: string }>('/api/teams', () =>
HttpResponse.json(null, { status: 204 })
),
rest.post<{ userId: UserId }>('/api/team_memberships', (req, res, ctx) =>
res(ctx.status(204))
http.post<never, { userId: UserId }>('/api/team_memberships', () =>
HttpResponse.json(null, { status: 204 })
),
...azureHandlers,
...dockerHandlers,
...userHandlers,
rest.get('/api/licenses/info', (req, res, ctx) => res(ctx.json(licenseInfo))),
rest.get('/api/status/nodes', (req, res, ctx) => res(ctx.json({ nodes: 3 }))),
rest.get('/api/backup/s3/status', (req, res, ctx) =>
res(ctx.json({ Failed: false }))
),
rest.get('/api/endpoint_groups', (req, res, ctx) => res(ctx.json([]))),
rest.get('/api/endpoint_groups/:groupId', (req, res, ctx) => {
if (req.params.groupId instanceof Array) {
http.get('/api/licenses/info', () => HttpResponse.json(licenseInfo)),
http.get('/api/status/nodes', () => HttpResponse.json({ nodes: 3 })),
http.get('/api/backup/s3/status', () => HttpResponse.json({ Failed: false })),
http.get('/api/endpoint_groups', () => HttpResponse.json([])),
http.get('/api/endpoint_groups/:groupId', ({ params }) => {
if (params.groupId instanceof Array) {
throw new Error('should be string');
}
const id = parseInt(req.params.groupId, 10);
const id = parseInt(params.groupId, 10);
const group: Partial<EnvironmentGroup> = {
Id: id,
Name: `group${id}`,
};
return res(ctx.json(group));
return HttpResponse.json(group);
}),
rest.get('/api/tags', (req, res, ctx) => res(ctx.json(tags))),
rest.post<{ name: string }>('/api/tags', (req, res, ctx) => {
const tagName = req.body.name;
http.get('/api/tags', () => HttpResponse.json(tags)),
http.post<never, { name: string }>('/api/tags', async ({ request }) => {
const body = await request.json();
const tagName = body.name;
const tag = { ID: tags.length + 1, Name: tagName, Endpoints: {} };
tags.push(tag);
return res(ctx.json(tag));
return HttpResponse.json(tag);
}),
rest.get<DefaultBodyType, PathParams, Partial<PublicSettingsResponse>>(
http.get<never, never, Partial<PublicSettingsResponse>>(
'/api/settings/public',
(req, res, ctx) =>
res(
ctx.json({
Edge: {
AsyncMode: false,
CheckinInterval: 60,
CommandInterval: 60,
PingInterval: 60,
SnapshotInterval: 60,
},
})
)
() =>
HttpResponse.json({
Edge: {
AsyncMode: false,
CheckinInterval: 60,
CommandInterval: 60,
PingInterval: 60,
SnapshotInterval: 60,
},
})
),
rest.get<DefaultBodyType, PathParams, Partial<StatusResponse>>(
'/api/status',
(req, res, ctx) => res(ctx.json({}))
),
rest.get('/api/teams/:id/memberships', (req, res, ctx) => res(ctx.json([]))),
rest.get('/api/endpoints/agent_versions', (req, res, ctx) =>
res(ctx.json([]))
http.get<never, never, Partial<StatusResponse>>('/api/status', () =>
HttpResponse.json({})
),
http.get('/api/teams/:id/memberships', () => HttpResponse.json([])),
http.get('/api/endpoints/agent_versions', () => HttpResponse.json([])),
];

View file

@ -1,7 +1,7 @@
import { rest } from 'msw';
import { http } from 'msw';
import { setupServer } from 'msw/node';
import { handlers } from './server-handlers';
const server = setupServer(...handlers);
export { server, rest };
export { server, http };

View file

@ -1,76 +1,70 @@
import { rest } from 'msw';
import { http, HttpResponse } from 'msw';
export const azureHandlers = [
rest.get('/api/endpoints/:endpointId/azure/subscriptions', (req, res, ctx) =>
res(
ctx.json({
value: [
http.get('/api/endpoints/:endpointId/azure/subscriptions', () =>
HttpResponse.json({
value: [
{
id: '/subscriptions/sub1',
authorizationSource: 'RoleBased',
subscriptionId: 'sub1',
displayName: 'Portainer',
state: 'Enabled',
},
],
})
),
http.get(
'/api/endpoints/:endpointId/azure/subscriptions/:subscriptionId/providers/Microsoft.ContainerInstance',
({ params }) =>
HttpResponse.json({
id: `/subscriptions/${params.subscriptionId}/providers/Microsoft.ContainerInstance`,
namespace: 'Microsoft.ContainerInstance',
resourceTypes: [
{
id: '/subscriptions/sub1',
authorizationSource: 'RoleBased',
subscriptionId: 'sub1',
displayName: 'Portainer',
state: 'Enabled',
resourceType: 'containerGroups',
locations: [
'Australia East',
'Australia Southeast',
'Brazil South',
],
},
{
resourceType: 'serviceAssociationLinks',
locations: [
'Korea Central',
'North Central US',
'North Europe',
'Norway East',
'South Africa North',
'South Central US',
],
},
{
resourceType: 'locations',
locations: [],
},
],
})
)
),
rest.get(
'/api/endpoints/:endpointId/azure/subscriptions/:subscriptionId/providers/Microsoft.ContainerInstance',
(req, res, ctx) =>
res(
ctx.json({
id: `/subscriptions/${req.params.subscriptionId}/providers/Microsoft.ContainerInstance`,
namespace: 'Microsoft.ContainerInstance',
resourceTypes: [
{
resourceType: 'containerGroups',
locations: [
'Australia East',
'Australia Southeast',
'Brazil South',
],
},
{
resourceType: 'serviceAssociationLinks',
locations: [
'Korea Central',
'North Central US',
'North Europe',
'Norway East',
'South Africa North',
'South Central US',
],
},
{
resourceType: 'locations',
locations: [],
},
],
})
)
),
rest.get(
http.get(
'/api/endpoints/:endpointId/azure/subscriptions/:subsriptionId/resourcegroups',
(res, req, ctx) =>
req(
ctx.json({
value: [
{
id: `/subscriptions/${res.params.subscriptionId}/resourceGroups/rg1`,
name: 'rg1',
location: 'southcentralus',
properties: { provisioningState: 'Succeeded' },
},
{
id: `/subscriptions/${res.params.subscriptionId}/resourceGroups/rg2`,
name: 'rg2',
location: 'southcentralus',
properties: { provisioningState: 'Succeeded' },
},
],
})
)
({ params }) =>
HttpResponse.json({
value: [
{
id: `/subscriptions/${params.subscriptionId}/resourceGroups/rg1`,
name: 'rg1',
location: 'southcentralus',
properties: { provisioningState: 'Succeeded' },
},
{
id: `/subscriptions/${params.subscriptionId}/resourceGroups/rg2`,
name: 'rg2',
location: 'southcentralus',
properties: { provisioningState: 'Succeeded' },
},
],
})
),
];

View file

@ -1,21 +1,19 @@
import { DefaultBodyType, PathParams, rest } from 'msw';
import { http, HttpResponse } from 'msw';
import { SystemInfo, SystemVersion } from 'docker-types/generated/1.41';
export const dockerHandlers = [
rest.get<DefaultBodyType, PathParams, SystemInfo>(
http.get<never, never, SystemInfo>(
'/api/endpoints/:endpointId/docker/info',
(req, res, ctx) =>
res(
ctx.json({
Plugins: { Authorization: [], Log: [], Network: [], Volume: [] },
MemTotal: 0,
NCPU: 0,
Runtimes: { runc: { path: 'runc' } },
})
)
() =>
HttpResponse.json({
Plugins: { Authorization: [], Log: [], Network: [], Volume: [] },
MemTotal: 0,
NCPU: 0,
Runtimes: { runc: { path: 'runc' } },
})
),
rest.get<DefaultBodyType, PathParams, SystemVersion>(
http.get<never, never, SystemVersion>(
'/api/endpoints/:endpointId/docker/version',
(req, res, ctx) => res(ctx.json({ ApiVersion: '1.24' }))
() => HttpResponse.json({ ApiVersion: '1.24' })
),
];

View file

@ -1,14 +1,12 @@
import { DefaultBodyType, PathParams, rest } from 'msw';
import { http, HttpResponse } from 'msw';
import { TeamMembership } from '@/react/portainer/users/teams/types';
import { createMockUsers } from '@/react-tools/test-mocks';
export const userHandlers = [
rest.get('/api/users', async (req, res, ctx) =>
res(ctx.json(createMockUsers(10)))
),
rest.get<DefaultBodyType, PathParams, TeamMembership[]>(
http.get('/api/users', async () => HttpResponse.json(createMockUsers(10))),
http.get<never, never, TeamMembership[]>(
'/api/users/:userId/memberships',
(req, res, ctx) => res(ctx.json([]))
() => HttpResponse.json([])
),
];