'use strict';

const preq   = require('preq');
const assert = require('../../utils/assert');
const mwapi = require('../../../lib/mwapi');

const logger = require('bunyan').createLogger({
    name: 'test-logger',
    level: 'warn'
});

logger.log = function(a, b) {};

describe('lib:apiUtil', function() {

    this.timeout(20000); // eslint-disable-line no-invalid-this

    it('checkForQueryPagesInResponse should return 504 when query.pages are absent', () => {
        return preq.post({
            uri: 'https://commons.wikimedia.org/w/api.php',
            body: {
                action: 'query',
                format: 'json',
                formatversion: 2,
                generator: 'images',
                prop: 'imageinfo|revisions',
                iiextmetadatafilter: 'ImageDescription',
                iiextmetadatamultilang: true,
                iiprop: 'url|extmetadata|dimensions',
                iiurlwidth: 1024,
                rawcontinue: '',
                titles: `Template:Potd/1980-07-06`
            }
        }).then((response) => {
            assert.throws(() => {
                mwapi.checkForQueryPagesInResponse({ logger }, response);
            }, /api_error/);
        });
    });
});

'use strict';

const assert = require('../../utils/assert');
const mwapi = require('../../../lib/mwapi');

const logger = require('bunyan').createLogger({
    name: 'test-logger',
    level: 'warn'
});

logger.log = function(a, b) {};

describe('lib:apiUtil', () => {

    it('checkForQueryPagesInResponse should return 504 when query.pages are absent', () => {
        return new Promise((resolve) => {
            return resolve({});
        }).then((response) => {
            assert.throws(() => {
                mwapi.checkForQueryPagesInResponse({ logger }, response);
            }, /api_error/);
        });
    });
});

Remove unnecessary API call in unit test
Remove unnecessary API call in unit test

We do not need to hit the API in this test. It's unnecessary and makes
the test harder to comprehend.

Instead of testing this way, simulate a promise that resolves without
pages.

(npm run test:unit should work without an internet connection)

Change-Id: Iee834114890a64c96ccf255601503a25efd884bc
