Skip to content

Commit

Permalink
fixed the upload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wouter van der Plas committed Jan 7, 2024
1 parent 2ea7a28 commit 5648cc3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 58 deletions.
27 changes: 0 additions & 27 deletions tests/integration/upload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
getToken,
upload
} from '../../src';
import { generateURL } from '../../src/utils/api/upload';
import {
exampleCommonsMediaSnak,
exampleExternalIdSnak,
Expand Down Expand Up @@ -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 () {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<typeof axios>;

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();
});
});

Expand Down Expand Up @@ -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
Expand All @@ -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'
}
Expand All @@ -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();
});
});
Expand Down

0 comments on commit 5648cc3

Please sign in to comment.