Skip to content

Commit

Permalink
Add helper functions to export all loaded annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Jul 20, 2023
1 parent 1bfb577 commit 7b44f1b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/shared/export-json-file.ts
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

Check warning on line 11 in src/shared/export-json-file.ts

View check run for this annotation

Codecov / codecov/patch

src/shared/export-json-file.ts#L11

Added line #L11 was not covered by tests
): 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;
}
43 changes: 43 additions & 0 deletions src/shared/test/export-json-file-test.js
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);
});
});
31 changes: 31 additions & 0 deletions src/sidebar/helpers/export-annotations.ts
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()

Check warning on line 21 in src/sidebar/helpers/export-annotations.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/helpers/export-annotations.ts#L21

Added line #L21 was not covered by tests
): ExportContent {
return {
export_date: now.toISOString(),
export_userid: userId,
client_version: clientVersion,

// TODO clean-up annotations, removing attributes generated by the client
annotations,
};
}
20 changes: 20 additions & 0 deletions src/sidebar/helpers/test/export-annotations-test.js
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,
});
});
});

0 comments on commit 7b44f1b

Please sign in to comment.