file-download.test.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { afterEach, describe, expect, it, vi } from 'vitest';
  2. import { FileManager } from '@/utils';
  3. const { createObjectURL, revokeObjectURL } = URL;
  4. afterEach(() => {
  5. URL.createObjectURL = createObjectURL;
  6. URL.revokeObjectURL = revokeObjectURL;
  7. vi.restoreAllMocks();
  8. });
  9. describe('FileManager.downloadTextFile', () => {
  10. // Android's MediaStore appends the blob type's own extension when the name's
  11. // extension maps elsewhere, so a text/plain peer.conf lands as peer.conf.txt.
  12. it('hands the browser an untyped blob so a mobile save keeps the given name', () => {
  13. const blobs: Blob[] = [];
  14. URL.createObjectURL = vi.fn((blob: Blob) => {
  15. blobs.push(blob);
  16. return 'blob:download';
  17. });
  18. URL.revokeObjectURL = vi.fn();
  19. let savedAs = '';
  20. vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (
  21. this: HTMLAnchorElement,
  22. ) {
  23. savedAs = this.download;
  24. });
  25. FileManager.downloadTextFile('[Interface]\n', 'alice.conf');
  26. expect(savedAs).toBe('alice.conf');
  27. expect(blobs.map((blob) => blob.type)).toEqual(['application/octet-stream']);
  28. });
  29. });