This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding helper functions to copy and download strings
- Loading branch information
1 parent
30f8030
commit c060c1f
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |