diff --git a/src/utils/__tests__/copy_to_clipboard.test.tsx b/src/utils/__tests__/copy_to_clipboard.test.tsx new file mode 100644 index 00000000..5d57eea2 --- /dev/null +++ b/src/utils/__tests__/copy_to_clipboard.test.tsx @@ -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'); +}) diff --git a/src/utils/__tests__/download_file.test.tsx b/src/utils/__tests__/download_file.test.tsx new file mode 100644 index 00000000..3c3038ed --- /dev/null +++ b/src/utils/__tests__/download_file.test.tsx @@ -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); +}); diff --git a/src/utils/copy_to_clipboard.tsx b/src/utils/copy_to_clipboard.tsx new file mode 100644 index 00000000..677afde3 --- /dev/null +++ b/src/utils/copy_to_clipboard.tsx @@ -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; diff --git a/src/utils/download_file.tsx b/src/utils/download_file.tsx new file mode 100644 index 00000000..046b089a --- /dev/null +++ b/src/utils/download_file.tsx @@ -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;