Skip to content
This repository has been archived by the owner on Dec 21, 2021. It is now read-only.

Commit

Permalink
Adding helper functions to copy and download strings
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Oct 24, 2018
1 parent 30f8030 commit c060c1f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/utils/__tests__/copy_to_clipboard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2018 Ultimaker B.V.
import copyToClipboard from '../copy_to_clipboard';

test('Copy to clipboard', () => {
// prepare mock
const elementMock = {focus: jest.fn(), select: jest.fn(), style: {}};
const createElementSpy = jest.spyOn(document, 'createElement').mockReturnValue(elementMock);
const appendChildSpy = jest.spyOn(document.body, 'appendChild').mockImplementation();
const removeChildSpy = jest.spyOn(document.body, 'removeChild').mockImplementation();
const execCommandMock = jest.fn();
document.execCommand = execCommandMock;

// click copy button
copyToClipboard('the string that should be copied');

// copy must have been executed
expect(createElementSpy).toHaveBeenCalledWith('textarea');
expect(elementMock).toEqual({
style: {left: '0', opacity: '0', position: "fixed", top: '0'},
focus: expect.any(Function),
select: expect.any(Function),
value: "the string that should be copied",
});
expect(elementMock.focus).toHaveBeenCalledWith();
expect(elementMock.select).toHaveBeenCalledWith();
expect(appendChildSpy).toHaveBeenCalledWith(elementMock);
expect(removeChildSpy).toHaveBeenCalledWith(elementMock);
expect(execCommandMock).toHaveBeenCalledWith('copy');
})
24 changes: 24 additions & 0 deletions src/utils/__tests__/download_file.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2018 Ultimaker B.V.

import downloadFile from '../download_file';

test('Downloading a file', () => {
// prepare mock
const elementMock = {click: jest.fn()};
const createElementSpy = jest.spyOn(document, 'createElement').mockReturnValue(elementMock);
const createObjectURL = jest.fn().mockReturnValue('obj://test-url');
global['URL']['createObjectURL'] = createObjectURL;

// click download button
downloadFile('file-name.txt', 'file contents');

// download must have been done
expect(createObjectURL).toHaveBeenCalledWith(expect.any(Blob));
expect(createElementSpy).toHaveBeenCalledWith('a');
expect(elementMock).toEqual({
click: expect.any(Function),
download: 'file-name.txt',
href: 'obj://test-url'
});
expect(elementMock.click).toHaveBeenCalledTimes(1);
});
21 changes: 21 additions & 0 deletions src/utils/copy_to_clipboard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2018 Ultimaker B.V.

/**
* Copies a string to the user's clipboard.
* @param value - The string to be copied.
*/
export function copyToClipboard(value: string){
let selectionTextarea = document.createElement('textarea');
selectionTextarea.style.position = 'fixed';
selectionTextarea.style.left = '0';
selectionTextarea.style.top = '0';
selectionTextarea.style.opacity = '0';
selectionTextarea.value = value;
document.body.appendChild(selectionTextarea);
selectionTextarea.focus();
selectionTextarea.select();
document.execCommand('copy');
document.body.removeChild(selectionTextarea);
}

export default copyToClipboard;
17 changes: 17 additions & 0 deletions src/utils/download_file.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2018 Ultimaker B.V.

/**
* Allows the user to download a file with the given contents.
* @param fileName - The default name of the file.
* @param fileContent - The content of the file.
* @param contentType - The content type, including the charset. Defaults to "text/plain;charset=utf-8".
*/
function downloadFile(fileName: string, fileContent: string, contentType?: string) {
const blob = new Blob([fileContent], { type: contentType || 'text/plain;charset=utf-8' });
const element = document.createElement('a');
element.href = URL.createObjectURL(blob);
element.download = fileName;
element.click();
}

export default downloadFile;

0 comments on commit c060c1f

Please sign in to comment.