diff --git a/__fixtures__/web_annotation.json b/__fixtures__/web_annotation.json new file mode 100644 index 0000000000000000000000000000000000000000..e31e60673d3b1d5f5e21b35194b3b1931e6babca --- /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 0000000000000000000000000000000000000000..b19c3e312276f11fa8542b85b3c7a3f692550ec8 --- /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 22585e0f83302cdbc192b4e66f5d9da90abd844c..dcb1b9625297aec0f5264acc628ccc801e52c984 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 d9b397a1e9817bd976a1bcfadedc859ee53e1509..94a56372fd333992a0a515cbe93aee35f790555b 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",