UNPKG

zotero-ts-api

Version:
171 lines 8.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const ZCollection_1 = require("./ZCollection"); const Item_1 = require("./Item"); const test_helpers_1 = require("../__tests__/utils/test-helpers"); (0, vitest_1.describe)('ZCollection', () => { let collection; let mockCollectionData; (0, vitest_1.beforeEach)(() => { mockCollectionData = { key: 'COLLECTION123', version: 1, name: 'Test Collection', parentCollection: undefined }; collection = new ZCollection_1.ZCollection(mockCollectionData, 'test-api-key', 'user123', 'users'); }); (0, vitest_1.describe)('Constructor', () => { (0, vitest_1.it)('should create collection with valid data', () => { (0, vitest_1.expect)(collection.key).toBe('COLLECTION123'); (0, vitest_1.expect)(collection.name).toBe('Test Collection'); (0, vitest_1.expect)(collection.parentCollection).toBeUndefined(); }); (0, vitest_1.it)('should create child collection', () => { const childData = { key: 'CHILD123', version: 1, name: 'Child Collection', parentCollection: 'PARENT123' }; const childCollection = new ZCollection_1.ZCollection(childData, 'api-key', 'user', 'users'); (0, vitest_1.expect)(childCollection.parentCollection).toBe('PARENT123'); }); }); (0, vitest_1.describe)('Getters', () => { (0, vitest_1.it)('should return correct key', () => { (0, vitest_1.expect)(collection.key).toBe('COLLECTION123'); }); (0, vitest_1.it)('should return correct name', () => { (0, vitest_1.expect)(collection.name).toBe('Test Collection'); }); (0, vitest_1.it)('should return correct parentCollection', () => { (0, vitest_1.expect)(collection.parentCollection).toBeUndefined(); }); }); (0, vitest_1.describe)('Setters', () => { (0, vitest_1.it)('should set name correctly', () => { collection.name = 'Updated Collection'; (0, vitest_1.expect)(collection.name).toBe('Updated Collection'); }); (0, vitest_1.it)('should trim name whitespace', () => { collection.name = ' Spaced Name '; (0, vitest_1.expect)(collection.name).toBe('Spaced Name'); }); (0, vitest_1.it)('should throw error for empty name', () => { (0, vitest_1.expect)(() => { collection.name = ''; }) .toThrow('Collection name cannot be empty'); }); (0, vitest_1.it)('should throw error for whitespace-only name', () => { (0, vitest_1.expect)(() => { collection.name = ' '; }) .toThrow('Collection name cannot be empty'); }); (0, vitest_1.it)('should throw error for null name', () => { (0, vitest_1.expect)(() => { collection.name = null; }) .toThrow('Collection name cannot be empty'); }); (0, vitest_1.it)('should set parentCollection', () => { collection.parentCollection = 'PARENT456'; (0, vitest_1.expect)(collection.parentCollection).toBe('PARENT456'); }); (0, vitest_1.it)('should allow undefined parentCollection', () => { collection.parentCollection = undefined; (0, vitest_1.expect)(collection.parentCollection).toBeUndefined(); }); }); (0, vitest_1.describe)('getItems()', () => { (0, vitest_1.it)('should retrieve items successfully', async () => { const mockItems = [ { key: 'ITEM1', version: 1, title: 'Item 1', itemType: 'article', creators: [], tags: [], collections: ['COLLECTION123'], relations: {} }, { key: 'ITEM2', version: 2, title: 'Item 2', itemType: 'book', creators: [], tags: [], collections: ['COLLECTION123'], relations: {} } ]; vitest_1.vi.mocked(fetch).mockResolvedValueOnce((0, test_helpers_1.createMockResponse)(mockItems)); const items = await collection.getItems(); (0, vitest_1.expect)(items).toHaveLength(2); (0, vitest_1.expect)(items[0]).toBeInstanceOf(Item_1.Item); (0, vitest_1.expect)(items[0].title).toBe('Item 1'); (0, vitest_1.expect)(items[1].title).toBe('Item 2'); }); (0, vitest_1.it)('should handle empty items response', async () => { vitest_1.vi.mocked(fetch).mockResolvedValueOnce((0, test_helpers_1.createMockResponse)([])); const items = await collection.getItems(); (0, vitest_1.expect)(items).toEqual([]); }); (0, vitest_1.it)('should handle API error when fetching items', async () => { vitest_1.vi.mocked(fetch).mockResolvedValueOnce((0, test_helpers_1.createMockResponse)(null, { ok: false, statusText: 'Not Found' })); await (0, vitest_1.expect)(collection.getItems()) .rejects.toThrow('Failed to fetch items for collection COLLECTION123: Not Found'); }); }); (0, vitest_1.describe)('update()', () => { (0, vitest_1.it)('should update collection successfully', async () => { vitest_1.vi.mocked(fetch).mockResolvedValueOnce((0, test_helpers_1.createMockResponse)(null)); await (0, vitest_1.expect)(collection.update()).resolves.toBeUndefined(); (0, vitest_1.expect)(fetch).toHaveBeenCalledWith('https://api.zotero.org/users/user123/collections/COLLECTION123', { method: 'PUT', headers: { 'Zotero-API-Key': 'test-api-key', 'Content-Type': 'application/json' }, body: vitest_1.expect.stringContaining('"name":"Test Collection"') }); }); (0, vitest_1.it)('should handle update failure', async () => { vitest_1.vi.mocked(fetch).mockResolvedValueOnce((0, test_helpers_1.createMockResponse)(null, { ok: false, statusText: 'Conflict' })); await (0, vitest_1.expect)(collection.update()) .rejects.toThrow('Failed to update collection COLLECTION123: Conflict'); }); }); (0, vitest_1.describe)('delete()', () => { (0, vitest_1.it)('should delete collection successfully', async () => { vitest_1.vi.mocked(fetch).mockResolvedValueOnce((0, test_helpers_1.createMockResponse)(null)); await (0, vitest_1.expect)(collection.delete()).resolves.toBeUndefined(); }); (0, vitest_1.it)('should handle delete failure', async () => { vitest_1.vi.mocked(fetch).mockResolvedValueOnce((0, test_helpers_1.createMockResponse)(null, { ok: false, statusText: 'Forbidden' })); await (0, vitest_1.expect)(collection.delete()) .rejects.toThrow('Failed to delete collection COLLECTION123: Forbidden'); }); }); (0, vitest_1.describe)('toJSON()', () => { (0, vitest_1.it)('should return copy of collection data', () => { const json = collection.toJSON(); (0, vitest_1.expect)(json).toEqual(mockCollectionData); (0, vitest_1.expect)(json).not.toBe(mockCollectionData); }); (0, vitest_1.it)('should reflect current state after modifications', () => { collection.name = 'Modified Name'; const json = collection.toJSON(); (0, vitest_1.expect)(json.name).toBe('Modified Name'); }); }); }); //# sourceMappingURL=ZCollection.test.js.map