From a85a952eb95cbc6c29233d7cb9f976193e0b3a72 Mon Sep 17 00:00:00 2001
From: Jack Reed <phillipjreed@gmail.com>
Date: Mon, 11 May 2020 14:14:59 -0600
Subject: [PATCH] Backfill tests for LocalStorageAdapter

---
 __fixtures__/web_annotation.json      | 23 ++++++++++++++
 __tests__/LocalStorageAdapter.test.js | 46 +++++++++++++++++++++++++++
 jest.config.js                        |  2 +-
 package.json                          |  1 +
 4 files changed, 71 insertions(+), 1 deletion(-)
 create mode 100644 __fixtures__/web_annotation.json
 create mode 100644 __tests__/LocalStorageAdapter.test.js

diff --git a/__fixtures__/web_annotation.json b/__fixtures__/web_annotation.json
new file mode 100644
index 0000000..e31e606
--- /dev/null
+++ b/__fixtures__/web_annotation.json
@@ -0,0 +1,23 @@
+{
+  "id": "localStorage://?canvasId=https://iiif.harvardartmuseums.org/manifests/object/299843/canvas/canvas-47174896",
+  "items": [
+    {
+      "body": {
+        "language": "en",
+        "type": "TextualBody",
+        "value": "eye"
+      },
+      "id": "https://example.org/iiif/book1/page/manifest/efb5757d-899b-4551-a7ab-5cb5d1d0eb02",
+      "motivation": "commenting",
+      "target": {
+        "id": "https://iiif.harvardartmuseums.org/manifests/object/299843/canvas/canvas-47174896",
+        "selector": {
+          "type": "SvgSelector",
+          "value": "<path xmlns=\"http://www.w3.org/2000/svg\" d=\"M784.24634,1028.82823v-183.35553h200.33289v183.35553z\" fill=\"none\" fill-rule=\"nonzero\" stroke=\"#00bfff\" stroke-width=\"1\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke-miterlimit=\"10\" stroke-dasharray=\"\" stroke-dashoffset=\"0\" font-family=\"none\" font-weight=\"none\" font-size=\"none\" text-anchor=\"none\" style=\"mix-blend-mode: normal\"/>"
+        }
+      },
+      "type": "Annotation"
+    }
+  ],
+  "type": "AnnotationPage"
+}
diff --git a/__tests__/LocalStorageAdapter.test.js b/__tests__/LocalStorageAdapter.test.js
new file mode 100644
index 0000000..b19c3e3
--- /dev/null
+++ b/__tests__/LocalStorageAdapter.test.js
@@ -0,0 +1,46 @@
+import LocalStorageAdapter from '../src/LocalStorageAdapter';
+import fixture from '../__fixtures__/web_annotation.json';
+
+describe('LocalStorageAdapter', () => {
+  let subject;
+  beforeEach(() => {
+    subject = new LocalStorageAdapter('//foo');
+    localStorage.clear();
+    localStorage.setItem('//foo', JSON.stringify(fixture));
+  });
+  describe('create', () => {
+    it('adds an item to the AnnotationPage items', () => {
+      expect(subject.all().items.length).toBe(1);
+      const annoPage = subject.create({});
+      expect(annoPage.items.length).toBe(2);
+    });
+    it('if there is no AnnotationPage, create one', () => {
+      subject = new LocalStorageAdapter('//bar');
+      expect(subject.all()).toBe(null);
+      subject.create({});
+      const annoPage = subject.all();
+      expect(annoPage.type).toBe('AnnotationPage');
+      expect(annoPage.items.length).toBe(1);
+    });
+  });
+  describe('delete', () => {
+    it('removes an item from an AnnotationPage', () => {
+      expect(subject.all().items.length).toBe(1);
+      const annoPage = subject.delete('https://example.org/iiif/book1/page/manifest/efb5757d-899b-4551-a7ab-5cb5d1d0eb02');
+      expect(annoPage.items.length).toBe(0);
+    });
+  });
+  describe('update', () => {
+    xit('not implemented', () => {
+
+    });
+  });
+  describe('all', () => {
+    it('parses and returns an item based on its annotationPageId', () => {
+      const annoPage = subject.all();
+      expect(localStorage.getItem).toHaveBeenLastCalledWith('//foo');
+      expect(annoPage.items.length).toBe(1);
+      expect(annoPage.type).toBe('AnnotationPage');
+    });
+  });
+});
diff --git a/jest.config.js b/jest.config.js
index 22585e0..dcb1b96 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -8,7 +8,7 @@ module.exports = {
   // The directory where Jest should output its coverage files
   coverageDirectory: 'coverage',
   setupFiles: [
-    '<rootDir>/setupJest.js',
+    '<rootDir>/setupJest.js', 'jest-localstorage-mock',
   ],
   // Ignore Mirador code from jest transforms
   transformIgnorePatterns: [
diff --git a/package.json b/package.json
index d9b397a..94a5637 100644
--- a/package.json
+++ b/package.json
@@ -56,6 +56,7 @@
     "eslint-plugin-react": "^7.19.0",
     "eslint-plugin-react-hooks": "^4.0.0",
     "jest": "^26.0.1",
+    "jest-localstorage-mock": "^2.4.2",
     "mirador": "^3.0.0-beta.8",
     "nwb": "0.24.x",
     "prop-types": "^15.7.2",
-- 
GitLab