-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper functions to export all loaded annotations
- Loading branch information
Showing
4 changed files
with
123 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 @@ | ||
/** | ||
* Provided an object and a filename, allows to generate a downloadable file | ||
* containing a JSON-serialized version of that object | ||
* | ||
* @param _document - Test seam | ||
* @return The contents of the downloaded file | ||
*/ | ||
export function exportJSONFile( | ||
exportData: object, | ||
filename: string, | ||
_document = document | ||
): string { | ||
const link = _document.createElement('a'); | ||
const fileContent = JSON.stringify(exportData, null, 2); | ||
const blob = new Blob([fileContent], { | ||
type: 'application/json', | ||
}); | ||
const url = URL.createObjectURL(blob); | ||
|
||
link.setAttribute('href', url); | ||
link.setAttribute('download', filename); | ||
link.style.visibility = 'hidden'; | ||
|
||
_document.body.appendChild(link); | ||
link.click(); | ||
_document.body.removeChild(link); | ||
|
||
return fileContent; | ||
} |
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,43 @@ | ||
import { exportJSONFile } from '../export-json-file'; | ||
|
||
describe('export-json-file', () => { | ||
let fakeLink; | ||
let fakeDocument; | ||
|
||
beforeEach(() => { | ||
fakeLink = { | ||
setAttribute: sinon.stub(), | ||
click: sinon.stub(), | ||
style: {}, | ||
}; | ||
|
||
fakeDocument = { | ||
createElement: sinon.stub().returns(fakeLink), | ||
body: { | ||
appendChild: sinon.stub(), | ||
removeChild: sinon.stub(), | ||
}, | ||
}; | ||
}); | ||
|
||
it('generates export file with provided annotations', () => { | ||
const filename = 'my-file.json'; | ||
const data = { foo: ['bar', 'baz'] }; | ||
|
||
const fileContent = exportJSONFile(data, filename, fakeDocument); | ||
|
||
assert.equal(fileContent, JSON.stringify(data, null, 2)); | ||
|
||
assert.calledWith(fakeDocument.createElement, 'a'); | ||
assert.calledWith(fakeDocument.body.appendChild, fakeLink); | ||
assert.calledWith(fakeDocument.body.removeChild, fakeLink); | ||
|
||
assert.calledWith( | ||
fakeLink.setAttribute.firstCall, | ||
'href', | ||
sinon.match.string | ||
); | ||
assert.calledWith(fakeLink.setAttribute.secondCall, 'download', filename); | ||
assert.equal('hidden', fakeLink.style.visibility); | ||
}); | ||
}); |
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,31 @@ | ||
import type { Annotation } from '../../types/api'; | ||
|
||
export type ExportAnnotationsData = { | ||
annotations: Annotation[]; | ||
clientVersion: string; | ||
userId: string; | ||
}; | ||
|
||
export type ExportContent = { | ||
export_date: string; | ||
export_userid: string; | ||
client_version: string; | ||
annotations: Annotation[]; | ||
}; | ||
|
||
/** | ||
* @param now - Test seam | ||
*/ | ||
export function buildExportFileContent( | ||
{ annotations, clientVersion, userId }: ExportAnnotationsData, | ||
now = new Date() | ||
): ExportContent { | ||
return { | ||
export_date: now.toISOString(), | ||
export_userid: userId, | ||
client_version: clientVersion, | ||
|
||
// TODO clean-up annotations, removing attributes generated by the client | ||
annotations, | ||
}; | ||
} |
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,20 @@ | ||
import { buildExportFileContent } from '../export-annotations'; | ||
|
||
describe('export-annotations', () => { | ||
it('generates export content with provided annotations', () => { | ||
const now = new Date(); | ||
const annotations = [{}, {}]; | ||
|
||
const result = buildExportFileContent( | ||
{ annotations, clientVersion: '1.0.0', userId: 'userId' }, | ||
now | ||
); | ||
|
||
assert.deepEqual(result, { | ||
export_date: now.toISOString(), | ||
export_userid: 'userId', | ||
client_version: '1.0.0', | ||
annotations, | ||
}); | ||
}); | ||
}); |