1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00
metamask-extension/ui/helpers/utils/export-utils.test.js
Nicholas Ellul 90827c54a1
Add improved downloading logic when exporting state logs (#19872)
* Add improved downloading logic when exporting state logs

* Make test for state logs download only apply to firefox

* Remove eslint override

* Add file extension to test

* Move make jest global.Blob accessible to window
2023-07-27 13:33:36 -04:00

69 lines
1.8 KiB
JavaScript

import { exportAsFile, ExportableContentType } from './export-utils';
describe('exportAsFile', () => {
let windowSpy;
beforeEach(() => {
windowSpy = jest.spyOn(window, 'window', 'get');
});
afterEach(() => {
windowSpy.mockRestore();
});
describe('when showSaveFilePicker is supported', () => {
it('uses .json file extension when content type is JSON', async () => {
const showSaveFilePicker = mockShowSaveFilePicker();
const filename = 'test.json';
const data = '{file: "content"}';
windowSpy.mockImplementation(() => ({
showSaveFilePicker,
Blob: global.Blob,
}));
await exportAsFile(filename, data, ExportableContentType.JSON);
expect(showSaveFilePicker).toHaveBeenCalledWith({
suggestedName: filename,
types: [
{
description: filename,
accept: { 'application/json': ['.json'] },
},
],
});
});
it('uses .txt file extension when content type is TXT', async () => {
const showSaveFilePicker = mockShowSaveFilePicker();
const filename = 'test.txt';
const data = 'file content';
windowSpy.mockImplementation(() => ({
showSaveFilePicker,
Blob: global.Blob,
}));
await exportAsFile(filename, data, ExportableContentType.TXT);
expect(showSaveFilePicker).toHaveBeenCalledWith({
suggestedName: filename,
types: [
{
description: filename,
accept: { 'text/plain': ['.txt'] },
},
],
});
});
});
});
function mockShowSaveFilePicker() {
return jest.fn().mockResolvedValueOnce({
createWritable: jest
.fn()
.mockResolvedValueOnce({ write: jest.fn(), close: jest.fn() }),
});
}