diff options
| author | stonedDiscord <Tukz@gmx.de> | 2022-03-26 00:52:12 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-26 00:52:12 +0100 |
| commit | feb7789f9a7988971135ae47a425dafa276008e7 (patch) | |
| tree | 9c4865f30e0b42ee9e85f041fb8b1715865653a1 /webAO/services | |
| parent | d1472f152c8ca9de8d790f9cc88e79d1e3b11be6 (diff) | |
| parent | 3b697cd8dba78e840e9d85380bf4e8dab0d7c672 (diff) | |
Merge pull request #131 from AttorneyOnline/saveIcChatlog
Added support for saving chatlog
Diffstat (limited to 'webAO/services')
| -rw-r--r-- | webAO/services/__tests__/downloadFile.test.ts | 27 | ||||
| -rw-r--r-- | webAO/services/downloadFile.ts | 8 |
2 files changed, 35 insertions, 0 deletions
diff --git a/webAO/services/__tests__/downloadFile.test.ts b/webAO/services/__tests__/downloadFile.test.ts new file mode 100644 index 0000000..b217b2c --- /dev/null +++ b/webAO/services/__tests__/downloadFile.test.ts @@ -0,0 +1,27 @@ +import downloadFile from '../downloadFile' +jest + .useFakeTimers() + .setSystemTime(new Date('2020-01-01').getTime()); + + global.URL.createObjectURL = jest.fn(); +(window as any).global.Blob = function (content, options){return ({content, options})} + +describe('downloadFile', () => { + it('Creates an <a> tag', () => { + const createElementSpy = jest.spyOn(document, 'createElement'); + downloadFile('hi', 'filename') + expect(createElementSpy).toBeCalled() + }) + it('Creates the blob with the correct data', () => { + const data = 'writingtestsishard' + global.URL.createObjectURL = jest.fn(() => data); + downloadFile(data, 'filename') + const expected = { + content: [data], + options: { + type: "text" + } + } + expect(global.URL.createObjectURL).toBeCalledWith(expected) + }) +})
\ No newline at end of file diff --git a/webAO/services/downloadFile.ts b/webAO/services/downloadFile.ts new file mode 100644 index 0000000..058075f --- /dev/null +++ b/webAO/services/downloadFile.ts @@ -0,0 +1,8 @@ +const downloadFile = (content: string, filename: string) => { + const a = document.createElement('a'); + const file = new Blob([content], {type: 'text'}); + a.href= URL.createObjectURL(file); + a.download = filename; + a.click(); +} +export default downloadFile
\ No newline at end of file |
