diff --git a/tests/integration/upload.spec.ts b/tests/integration/upload.spec.ts index 81fc927..2d91bad 100644 --- a/tests/integration/upload.spec.ts +++ b/tests/integration/upload.spec.ts @@ -11,7 +11,6 @@ import { getToken, upload } from '../../src'; -import { generateURL } from '../../src/utils/api/upload'; import { exampleCommonsMediaSnak, exampleExternalIdSnak, @@ -41,32 +40,6 @@ const testServer = 'https://test.wikidata.org'; const testItem = 'Q231400'; describe('uploading to wikidata', () => { - describe('generate url', () => { - it('should generate a url', () => { - const url = generateURL('https://wikidata.org', true); - - expect(url).toBe('https://wikidata.org/w/api.php?action=wbeditentity&format=json&new=item'); - }); - - it('should generate a url with a custom domain', () => { - const url = generateURL('https://test.wikidata.org', true); - - expect(url).toBe('https://test.wikidata.org/w/api.php?action=wbeditentity&format=json&new=item'); - }); - - it('should generate a url without new', () => { - const url = generateURL('https://wikidata.org', false); - - expect(url).toBe('https://wikidata.org/w/api.php?action=wbeditentity&format=json'); - }); - - it('should generate a url with a origin', () => { - const url = generateURL('https://wikidata.org', false, 'https://example.org'); - - expect(url).toBe('https://wikidata.org/w/api.php?action=wbeditentity&format=json&origin=https://example.org'); - }); - }); - it( 'should upload a item when requested', async function () { diff --git a/tests/unit/utils/api/upload.spec.xts b/tests/unit/utils/api/upload.spec.ts similarity index 54% rename from tests/unit/utils/api/upload.spec.xts rename to tests/unit/utils/api/upload.spec.ts index 00beef1..7609aaa 100644 --- a/tests/unit/utils/api/upload.spec.xts +++ b/tests/unit/utils/api/upload.spec.ts @@ -1,37 +1,51 @@ -// this test is commented out because the combination of the Axios instance trick -// and the mocked Axios instance is not working -// we can just inject the mock as the axiosInstance but I want to make something with the lib -// and using it might be the best way to test it - -import axios from 'axios'; import qs from 'qs'; import upload, { generateURL, validateAuthentication } from '../../../../src/utils/api/upload'; import { Item } from '../../../../src'; import { Token } from '../../../../src/utils/api/token'; -jest.mock('axios'); -const mockedAxios = axios as jest.Mocked; - const token: Token = { token: 'token', cookie: 'cookie' }; -describe('generateURL', () => { +describe('generate url', () => { + it('should generate a url', () => { + const url = generateURL('https://wikidata.org', true); + + expect(url).toBe('https://wikidata.org/w/api.php?action=wbeditentity&format=json&new=item'); + }); + + it('should generate a url with a custom domain', () => { + const url = generateURL('https://test.wikidata.org', true); + + expect(url).toBe('https://test.wikidata.org/w/api.php?action=wbeditentity&format=json&new=item'); + }); + + it('should generate a url without new', () => { + const url = generateURL('https://wikidata.org', false); + + expect(url).toBe('https://wikidata.org/w/api.php?action=wbeditentity&format=json'); + }); + + it('should generate a url with a origin', () => { + const url = generateURL('https://wikidata.org', false, 'https://example.org'); + + expect(url).toBe('https://wikidata.org/w/api.php?action=wbeditentity&format=json&origin=https%3A%2F%2Fexample.org'); + }); + it('should return the right url when a server is given', () => { - expect(generateURL('https://www.wikidata.org')).toEqual('https://www.wikidata.org/w/api.php?action=wbeditentity&format=json'); - expect(generateURL('https://wiki.openstreetmap.org')).toEqual('https://wiki.openstreetmap.org/w/api.php?action=wbeditentity&format=json'); - expect(generateURL('https://wiki.openstreetmap.org/wiki/Special:EntityData')).toEqual('https://wiki.openstreetmap.org/w/api.php?action=wbeditentity&format=json'); + expect(generateURL('https://www.wikidata.org', false)).toEqual('https://www.wikidata.org/w/api.php?action=wbeditentity&format=json'); + expect(generateURL('https://wiki.openstreetmap.org', false)).toEqual('https://wiki.openstreetmap.org/w/api.php?action=wbeditentity&format=json'); + expect(generateURL('https://wiki.openstreetmap.org/wiki/Special:EntityData', false)).toEqual('https://wiki.openstreetmap.org/w/api.php?action=wbeditentity&format=json'); - expect(generateURL('http://www.wikidata.org/wiki/Q23')).toEqual('http://www.wikidata.org/w/api.php?action=wbeditentity&format=json'); - expect(generateURL()).toEqual('https://www.wikidata.org/w/api.php?action=wbeditentity&format=json'); + expect(generateURL('http://www.wikidata.org/wiki/Q23', false)).toEqual('http://www.wikidata.org/w/api.php?action=wbeditentity&format=json'); }); it('should handle a load of rubbish', () => { // @ts-expect-error testing - expect(() => generateURL(42)).toThrow(); - expect(() => generateURL('')).toThrow(); - expect(() => generateURL('dasdasdsad')).toThrow(); + expect(() => generateURL(42, true)).toThrow(); + expect(() => generateURL('', true)).toThrow(); + expect(() => generateURL('dasdasdsad', true)).toThrow(); }); }); @@ -76,14 +90,10 @@ describe('validateAuthentication', () => { describe('upload', () => { const item = Item.fromNothing(); - afterEach(() => { - mockedAxios.get.mockReset(); - mockedAxios.post.mockReset(); - }); - describe('uploading', () => { it('should use the anonymous key if there is no key, but the anonymous key is set', async () => { - mockedAxios.post.mockResolvedValue({ + const axiosMock = jest.fn(); + axiosMock.mockResolvedValue({ data: { entity: Item.fromNothing().toJSON(), success: 1 @@ -93,21 +103,24 @@ describe('upload', () => { await upload(item, { summary: 'Upload summary', tags: [''], - anonymous: true - }); + anonymous: true, - expect(mockedAxios.post).toHaveBeenCalledTimes(1); + // @ts-expect-error testing + axiosInstance: axiosMock + }); - const arguments_ = mockedAxios.post?.mock?.calls[0][1] as string; + expect(axiosMock).toHaveBeenCalledTimes(1); - const data = qs.parse(arguments_); + const arguments_ = axiosMock.mock?.calls[0][0]; + const data = qs.parse(arguments_?.data); expect(data.token).toEqual('+\\'); expect(data.summary).toEqual('Upload summary'); expect(data.tags).toEqual(''); }); it('throw when uploading does not succeeds', async () => { - mockedAxios.post.mockResolvedValue({ + const axiosMock = jest.fn(); + axiosMock.mockResolvedValue({ data: { error: 'something went wrong' } @@ -116,7 +129,10 @@ describe('upload', () => { await expect(upload(item, { summary: 'Upload summary', tags: [''], - anonymous: true + anonymous: true, + + // @ts-expect-error testing + axiosInstance: axiosMock })).rejects.toThrow(); }); });