mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-05 13:55:44 -05:00
fix egg export downloading as a json string (#7289)
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import type { AdminEgg } from '@/api/admin/eggs/queries';
|
||||
import ExportEggButton from '@/components/admin/eggs/ExportEggButton';
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
refetch: vi.fn(),
|
||||
notifyError: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/plugins/notifications', () => ({ notifyHttpError: mocks.notifyError }));
|
||||
vi.mock('@/api/admin/eggs/queries', () => ({
|
||||
useExportAdminEgg: () => ({ refetch: mocks.refetch, isFetching: false }),
|
||||
}));
|
||||
|
||||
// The export endpoint is requested with `responseType: 'text'`, so the query hands back the
|
||||
// raw egg file exactly as the panel wrote it.
|
||||
const exported = JSON.stringify(
|
||||
{
|
||||
_comment: 'DO NOT EDIT: FILE GENERATED AUTOMATICALLY BY PTERODACTYL PANEL - PTERODACTYL.IO',
|
||||
meta: { version: 'PTDL_v2', update_url: null },
|
||||
name: 'Paper',
|
||||
config: { files: '{}', startup: '{}', logs: '{}', stop: 'stop' },
|
||||
scripts: { installation: { script: 'echo hi', container: 'alpine', entrypoint: 'ash' } },
|
||||
variables: [],
|
||||
},
|
||||
null,
|
||||
4
|
||||
);
|
||||
|
||||
const egg = { attributes: { id: 3, name: 'Paper' } } as AdminEgg;
|
||||
|
||||
describe('ExportEggButton', () => {
|
||||
let blobs: Blob[];
|
||||
|
||||
beforeEach(() => {
|
||||
blobs = [];
|
||||
mocks.refetch.mockResolvedValue({ data: exported });
|
||||
window.URL.createObjectURL = vi.fn((blob: Blob) => {
|
||||
blobs.push(blob);
|
||||
|
||||
return 'blob:egg';
|
||||
});
|
||||
window.URL.revokeObjectURL = vi.fn();
|
||||
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => undefined);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('downloads the exported file as-is so it can be imported again', async () => {
|
||||
render(<ExportEggButton egg={egg} />);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Export' }));
|
||||
|
||||
await waitFor(() => expect(blobs).toHaveLength(1));
|
||||
|
||||
// jsdom's Blob has no text(), so read it back with a FileReader.
|
||||
const contents = await new Promise<string>((resolve) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result as string);
|
||||
reader.readAsText(blobs[0]);
|
||||
});
|
||||
|
||||
expect(contents).toBe(exported);
|
||||
expect(JSON.parse(contents)).toMatchObject({ meta: { version: 'PTDL_v2' }, name: 'Paper' });
|
||||
expect(mocks.notifyError).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -25,7 +25,7 @@ export default function ExportEggButton({ egg }: Props) {
|
||||
return;
|
||||
}
|
||||
|
||||
const blob = new Blob([JSON.stringify(json, null, 2)], { type: 'application/json' });
|
||||
const blob = new Blob([json], { type: 'application/json' });
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user