From 1eed8da436ea7088a5e52775c5e767775033fdf6 Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Tue, 8 Oct 2024 15:37:31 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/zenodoClient.spec.ts | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/zenodoClient.spec.ts diff --git a/package.json b/package.json index 3320fcd..85e2aba 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "dev": "npm run copy:version && npm run link && esbuild src/cli/index.ts --bundle --outfile=dist/zenodo.cjs --platform=node --external:fsevents --watch", "test": "npm run copy:version && vitest run", "test:watch": "npm run copy:version && vitest watch", - "lint": "eslint \"src/**/*.ts*\" -c ./.eslintrc.cjs", + "lint": "eslint \"src/**/!(*.spec).ts\" -c ./.eslintrc.cjs", "lint:format": "prettier --check \"src/**/*.{ts,tsx,md}\"", "build:esm": "tsc --project ./tsconfig.json --outDir dist", "build:cli": "esbuild src/cli/index.ts --bundle --outfile=dist/zenodo.cjs --platform=node --external:fsevents", diff --git a/src/zenodoClient.spec.ts b/src/zenodoClient.spec.ts new file mode 100644 index 0000000..2748fe3 --- /dev/null +++ b/src/zenodoClient.spec.ts @@ -0,0 +1,56 @@ +import type { Mock } from 'vitest'; +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import type { DepositionMetadata } from './index.js'; +import { AccessRight, UploadType, ZenodoClient } from './index.js'; +import axios from 'axios'; + +// Mock axios module +vi.mock('axios'); + +describe('ZenodoClient Tests', () => { + let zenodoClient: ZenodoClient; + const ACCESS_TOKEN = 'fake_access_token'; + + beforeEach(() => { + // Mock axiosInstance.post to return the mock response + (axios.create as Mock).mockReturnValue({}); + // Instantiate the ZenodoClient with the fake access token + zenodoClient = new ZenodoClient(ACCESS_TOKEN, true); // Use sandbox environment for testing + }); + + it('should instantiate ZenodoClient correctly', () => { + expect(zenodoClient).toBeInstanceOf(ZenodoClient); + }); + + it('should create a deposition', async () => { + // Mock response data + const mockDepositionResponse = { + data: { + id: 123456, + metadata: { + title: 'Test Deposition', + }, + links: { + bucket: 'https://sandbox.zenodo.org/api/files/fake-bucket-id', + }, + }, + }; + + (zenodoClient as any).axiosInstance = { + post: vi.fn().mockResolvedValue(mockDepositionResponse), + }; + + const metadata: DepositionMetadata = { + upload_type: UploadType.presentation, + publication_date: '2023-10-10', + title: 'Test Presentation', + creators: [{ name: 'Doe, John' }], + description: 'A test deposition.', + access_right: AccessRight.open, + }; + + const deposition = await zenodoClient.createDeposition(metadata); + + expect(deposition).toEqual(mockDepositionResponse.data); + }); +});