From 31c1555bf52d82e6bf4a971c38e7b6cf2c4ea7b8 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 5 Jan 2024 17:25:14 +0100 Subject: [PATCH] Make AnnotationsExporter not depend on SidebarStore --- .../ShareDialog/ExportAnnotations.tsx | 9 ++- src/sidebar/services/annotations-exporter.ts | 22 ++++---- .../test/annotations-exporter-test.js | 55 ++++++++++--------- 3 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/sidebar/components/ShareDialog/ExportAnnotations.tsx b/src/sidebar/components/ShareDialog/ExportAnnotations.tsx index 18c93a2e671..8d5711261b8 100644 --- a/src/sidebar/components/ShareDialog/ExportAnnotations.tsx +++ b/src/sidebar/components/ShareDialog/ExportAnnotations.tsx @@ -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 = useMemo( () => ({ annotations: exportableAnnotations, @@ -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; } diff --git a/src/sidebar/services/annotations-exporter.ts b/src/sidebar/services/annotations-exporter.ts index f59e1d48b14..19cf03cc209 100644 --- a/src/sidebar/services/annotations-exporter.ts +++ b/src/sidebar/services/annotations-exporter.ts @@ -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, @@ -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; @@ -18,6 +17,11 @@ export type JSONExportContent = { annotations: APIAnnotationData[]; }; +export type JSONExportOptions = { + profile: Profile; + now?: Date; +}; + export type TextExportOptions = { defaultAuthority?: string; displayNamesEnabled?: boolean; @@ -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 { diff --git a/src/sidebar/services/test/annotations-exporter-test.js b/src/sidebar/services/test/annotations-exporter-test.js index 9c6b1eced2d..77cffdd5019 100644 --- a/src/sidebar/services/test/annotations-exporter-test.js +++ b/src/sidebar/services/test/annotations-exporter-test.js @@ -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], + }); }); });