Skip to content

Commit

Permalink
Make AnnotationsExporter not depend on SidebarStore
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Jan 5, 2024
1 parent 8ad4483 commit 31c1555
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
9 changes: 6 additions & 3 deletions src/sidebar/components/ShareDialog/ExportAnnotations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ function ExportAnnotations({
);

// User whose annotations are going to be exported.
const currentUser = store.profile().userid;
const profile = store.profile();
const currentUser = profile.userid;
const allAnnotationsOption: Omit<UserAnnotations, 'userid'> = useMemo(
() => ({
annotations: exportableAnnotations,
Expand Down Expand Up @@ -131,8 +132,10 @@ function ExportAnnotations({

switch (format) {
case 'json': {
const exportData =
annotationsExporter.buildJSONExportContent(annotationsToExport);
const exportData = annotationsExporter.buildJSONExportContent(
annotationsToExport,
{ profile },
);
downloadJSONFile(exportData, filename);
break;
}
Expand Down
22 changes: 11 additions & 11 deletions src/sidebar/services/annotations-exporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { trimAndDedent } from '../../shared/trim-and-dedent';
import type { APIAnnotationData } from '../../types/api';
import type { APIAnnotationData, Profile } from '../../types/api';
import {
documentMetadata,
isReply,
Expand All @@ -9,7 +9,6 @@ import {
import { annotationDisplayName } from '../helpers/annotation-user';
import { stripInternalProperties } from '../helpers/strip-internal-properties';
import { VersionData } from '../helpers/version-data';
import type { SidebarStore } from '../store';

export type JSONExportContent = {
export_date: string;
Expand All @@ -18,6 +17,11 @@ export type JSONExportContent = {
annotations: APIAnnotationData[];
};

export type JSONExportOptions = {
profile: Profile;
now?: Date;
};

export type TextExportOptions = {
defaultAuthority?: string;
displayNamesEnabled?: boolean;
Expand All @@ -31,18 +35,14 @@ export type TextExportOptions = {
* @inject
*/
export class AnnotationsExporter {
private _store: SidebarStore;

constructor(store: SidebarStore) {
this._store = store;
}

buildJSONExportContent(
annotations: APIAnnotationData[],
/* istanbul ignore next - test seam */
now = new Date(),
{
profile,
/* istanbul ignore next - test seam */
now = new Date(),
}: JSONExportOptions,
): JSONExportContent {
const profile = this._store.profile();
const versionData = new VersionData(profile, []);

return {
Expand Down
55 changes: 28 additions & 27 deletions src/sidebar/services/test/annotations-exporter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,41 @@ import {
import { AnnotationsExporter } from '../annotations-exporter';

describe('AnnotationsExporter', () => {
let fakeStore;
let now;
let exporter;

beforeEach(() => {
fakeStore = {
profile: sinon.stub().returns({ userid: 'userId' }),
};
now = new Date();

exporter = new AnnotationsExporter(fakeStore);
exporter = new AnnotationsExporter();
});

it('generates JSON content with provided annotations', () => {
const firstBaseAnnotation = publicAnnotation();
const secondBaseAnnotation = publicAnnotation();
const annotations = [
{
...firstBaseAnnotation,
$tag: '',
},
{
...secondBaseAnnotation,
$highlight: true,
},
];

const result = exporter.buildJSONExportContent(annotations, now);

assert.deepEqual(result, {
export_date: now.toISOString(),
export_userid: 'userId',
client_version: '__VERSION__',
annotations: [firstBaseAnnotation, secondBaseAnnotation],
describe('buildJSONExportContent', () => {
it('generates JSON content with provided annotations', () => {
const profile = { userid: 'userId' };
const firstBaseAnnotation = publicAnnotation();
const secondBaseAnnotation = publicAnnotation();
const annotations = [
{
...firstBaseAnnotation,
$tag: '',
},
{
...secondBaseAnnotation,
$highlight: true,
},
];

const result = exporter.buildJSONExportContent(annotations, {
now,
profile,
});

assert.deepEqual(result, {
export_date: now.toISOString(),
export_userid: 'userId',
client_version: '__VERSION__',
annotations: [firstBaseAnnotation, secondBaseAnnotation],
});
});
});

Expand Down

0 comments on commit 31c1555

Please sign in to comment.