From c2e23409d4886122df2d97811c1a99fc354e0786 Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Wed, 22 Mar 2023 16:20:07 +0530 Subject: [PATCH] make title.test.js and date.test.js not depend on a local wiki --- tests/bot.test.js | 9 ++++++ tests/date.test.js | 12 ++------ tests/static_utils.test.js | 10 +++--- tests/test_base.js | 62 ++++++++++++++++++++++++++++++++++++- tests/title.test.js | 63 ++------------------------------------ 5 files changed, 79 insertions(+), 77 deletions(-) diff --git a/tests/bot.test.js b/tests/bot.test.js index 1487b58..afdcbb9 100644 --- a/tests/bot.test.js +++ b/tests/bot.test.js @@ -231,6 +231,15 @@ describe('mwn', async function () { }); }); + it('loads date locale data using API', async function () { + bot.options.defaultParams.assert = undefined; // avoid sign-in + await bot.date.populateLocaleData('fr'); + expect(bot.date.localeData.months[0]).to.equal('janvier'); + expect(bot.date.localeData.monthsShort[0]).to.equal('janv.'); + expect(bot.date.localeData.days[0]).to.equal('dimanche'); + expect(bot.date.localeData.daysShort[0]).to.equal('dim.'); + }); + ////////////////////////////////////////// // UNSUCCESSFUL // ////////////////////////////////////////// diff --git a/tests/date.test.js b/tests/date.test.js index ff43de5..4677f12 100644 --- a/tests/date.test.js +++ b/tests/date.test.js @@ -1,7 +1,8 @@ -const { bot, expect } = require('./local_wiki'); +const { mwn, expect } = require('./test_base'); // Includes some tests copied from https://github.com/wikimedia-gadgets/twinkle/blob/master/tests/morebits.date.js +const bot = new mwn(); describe('date', async function () { it('date constructor', function () { let mwTs = new bot.date('20120304050607'); @@ -67,13 +68,4 @@ describe('date', async function () { expect(bot.date.getMonthName(1)).to.eq('January'); expect(bot.date.getMonthNameAbbrev(1)).to.eq('Jan'); }); - - it('loads locale data using API', async function () { - bot.options.defaultParams.assert = undefined; // avoid sign-in - await bot.date.populateLocaleData('fr'); - expect(bot.date.localeData.months[0]).to.equal('janvier'); - expect(bot.date.localeData.monthsShort[0]).to.equal('janv.'); - expect(bot.date.localeData.days[0]).to.equal('dimanche'); - expect(bot.date.localeData.daysShort[0]).to.equal('dim.'); - }); }); diff --git a/tests/static_utils.test.js b/tests/static_utils.test.js index 66af726..d78aa4c 100644 --- a/tests/static_utils.test.js +++ b/tests/static_utils.test.js @@ -1,13 +1,11 @@ 'use strict'; -const { mwn, bot, expect, assert, setup, teardown } = require('./test_wiki'); +const { mwn, expect, assert, populateTitleData } = require('./test_base'); -describe('static utils', function () { - this.timeout(10000); - - before('logs in and gets token & namespaceInfo', setup); - after('logs out', teardown); +const bot = new mwn(); +populateTitleData(bot.title); +describe('static utils', function () { it('link', function () { expect(mwn.link('Main Page')).to.equal('[[Main Page]]'); expect(mwn.link('Main Page', 'homepage')).to.equal('[[Main Page|homepage]]'); diff --git a/tests/test_base.js b/tests/test_base.js index e40b6b0..b9e02e6 100644 --- a/tests/test_base.js +++ b/tests/test_base.js @@ -30,4 +30,64 @@ function verifyTokenAndSiteInfo(bot) { expect(bot.title.nameIdMap).to.include.all.keys('project', 'user'); } -module.exports = { mwn, log, expect, assert, sinon, verifyTokenAndSiteInfo }; +/** Populate title data without an API request **/ +function populateTitleData(titleObj) { + Object.assign(titleObj, { + idNameMap: { + '-2': 'Media', + '-1': 'Special', + '0': '', + '1': 'Talk', + '2': 'User', + '3': 'User talk', + '4': 'Wikipedia', + '5': 'Wikipedia talk', + '6': 'File', + '7': 'File talk', + '8': 'MediaWiki', + '9': 'MediaWiki talk', + '10': 'Template', + '11': 'Template talk', + '12': 'Help', + '13': 'Help talk', + '14': 'Category', + '15': 'Category talk', + // testing custom / localized namespace + '100': 'Penguins', + }, + + nameIdMap: { + 'media': -2, + 'special': -1, + '': 0, + 'talk': 1, + 'user': 2, + 'user_talk': 3, + 'wikipedia': 4, + 'wikipedia_talk': 5, + 'file': 6, + 'file_talk': 7, + 'mediawiki': 8, + 'mediawiki_talk': 9, + 'template': 10, + 'template_talk': 11, + 'help': 12, + 'help_talk': 13, + 'category': 14, + 'category_talk': 15, + 'image': 6, + 'image_talk': 7, + 'project': 4, + 'project_talk': 5, + // Testing custom namespaces and aliases + 'penguins': 100, + 'antarctic_waterfowl': 100, + }, + + caseSensitiveNamespaces: [], + + legaltitlechars: ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF', + }); +} + +module.exports = { mwn, log, expect, assert, sinon, verifyTokenAndSiteInfo, populateTitleData }; diff --git a/tests/title.test.js b/tests/title.test.js index 09a8fc2..2e5f5a1 100644 --- a/tests/title.test.js +++ b/tests/title.test.js @@ -3,68 +3,11 @@ * */ -const { mwn } = require('./test_base'); +const { mwn, assert, populateTitleData } = require('./test_base'); + const bot = new mwn(); const Title = bot.title; - -const assert = require('assert'); - -Object.assign(Title, { - idNameMap: { - '-2': 'Media', - '-1': 'Special', - '0': '', - '1': 'Talk', - '2': 'User', - '3': 'User talk', - '4': 'Wikipedia', - '5': 'Wikipedia talk', - '6': 'File', - '7': 'File talk', - '8': 'MediaWiki', - '9': 'MediaWiki talk', - '10': 'Template', - '11': 'Template talk', - '12': 'Help', - '13': 'Help talk', - '14': 'Category', - '15': 'Category talk', - // testing custom / localized namespace - '100': 'Penguins', - }, - - nameIdMap: { - 'media': -2, - 'special': -1, - '': 0, - 'talk': 1, - 'user': 2, - 'user_talk': 3, - 'wikipedia': 4, - 'wikipedia_talk': 5, - 'file': 6, - 'file_talk': 7, - 'mediawiki': 8, - 'mediawiki_talk': 9, - 'template': 10, - 'template_talk': 11, - 'help': 12, - 'help_talk': 13, - 'category': 14, - 'category_talk': 15, - 'image': 6, - 'image_talk': 7, - 'project': 4, - 'project_talk': 5, - // Testing custom namespaces and aliases - 'penguins': 100, - 'antarctic_waterfowl': 100, - }, - - caseSensitiveNamespaces: [], - - legaltitlechars: ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF', -}); +populateTitleData(Title); describe('title', function () { var repeat = function (input, multiplier) {