diff --git a/tests/.eslintrc.json b/tests/.eslintrc.json index dbdf8c927..1e8473272 100644 --- a/tests/.eslintrc.json +++ b/tests/.eslintrc.json @@ -5,6 +5,15 @@ "env": { "jest": true }, + "globals": { + "assert": "readonly", + "document": "readonly", + "jQuery": "readonly", + "Morebits": "readonly", + "mw": "readonly", + "Twinkle": "readonly", + "window": "readonly" + }, "root": true, "rules": { "array-bracket-spacing": "off", @@ -12,10 +21,5 @@ "no-shadow": "off", "object-curly-spacing": "off", "space-in-parens": "off", - - "camelcase": "warn", - "new-cap": "warn", - "no-undef": "warn", - "strict": "warn" } } diff --git a/tests/jest.setup.js b/tests/jest.setup.js index a63a116eb..3cb094332 100644 --- a/tests/jest.setup.js +++ b/tests/jest.setup.js @@ -1,3 +1,5 @@ +'use strict'; + // Tweak some mw.configs as needed by tests mw.config.set({ wgPageName: 'Macbeth,_King_of_Scotland', diff --git a/tests/morebits.array.js b/tests/morebits.array.js index c64b9d8a9..b8c29542c 100644 --- a/tests/morebits.array.js +++ b/tests/morebits.array.js @@ -1,3 +1,5 @@ +'use strict'; + describe('Morebits.array', () => { test('chunk', () => { assert.deepEqual(Morebits.array.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3), [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]], '10 over 3'); diff --git a/tests/morebits.batchOperation.js b/tests/morebits.batchOperation.js index 76bab9616..9fa045008 100644 --- a/tests/morebits.batchOperation.js +++ b/tests/morebits.batchOperation.js @@ -1,8 +1,10 @@ +'use strict'; + describe('Morebits.batchOperation', () => { test('Contruction', () => { - assert.true(new Morebits.batchOperation() instanceof Morebits.batchOperation, 'Correct instance'); + assert.true(new Morebits.BatchOperation() instanceof Morebits.batchOperation, 'Correct instance'); }); - const batch = new Morebits.batchOperation(); + const batch = new Morebits.BatchOperation(); const pageList = ['Macbeth', 'Hamlet', 'Romeo and Juliet', 'Much Ado About Nothing', 'Tempest']; batch.setPageList(pageList); const chunkLength = 2; diff --git a/tests/morebits.createHtml.js b/tests/morebits.createHtml.js index 63563c9e4..07be8fe88 100644 --- a/tests/morebits.createHtml.js +++ b/tests/morebits.createHtml.js @@ -1,3 +1,5 @@ +'use strict'; + describe('Morebits.createHtml', () => { test('createHtml', () => { diff --git a/tests/morebits.date.js b/tests/morebits.date.js index c39cbef26..13f419ed3 100644 --- a/tests/morebits.date.js +++ b/tests/morebits.date.js @@ -1,24 +1,26 @@ +'use strict'; + describe('Morebits.date', () => { const now = Date.now(); - const ts_mw = '16:26, 7 November 2020 (UTC)', ts_iso = '2020-11-07T16:26:00.000Z', naive = 20201107162600; + const timestampMediaWiki = '16:26, 7 November 2020 (UTC)', timestampIso = '2020-11-07T16:26:00.000Z', naive = 20201107162600; test('Construction', () => { // getTime and toISOString imply testing of inherited methods // Allow off-by-one values in milliseconds for not-quite-simultaneous date contructions - expect(Math.abs(new Morebits.date().getTime() - Date.now())).toBeLessThanOrEqual(1); + expect(Math.abs(new Morebits.Date().getTime() - Date.now())).toBeLessThanOrEqual(1); - assert.strictEqual(new Morebits.date(now).getTime(), new Date(now).getTime(), 'Constructor from timestring'); - assert.strictEqual(new Morebits.date(2020, 11, 7, 16, 26).getTime(), new Date(2020, 11, 7, 16, 26).getTime(), 'Constructor from parts'); - assert.strictEqual(new Morebits.date(ts_mw).toISOString(), ts_iso, 'enWiki timestamp format'); - assert.strictEqual(new Morebits.date(naive).toISOString(), ts_iso, 'MediaWiki 14-digit number'); - assert.strictEqual(new Morebits.date(naive.toString()).toISOString(), ts_iso, 'MediaWiki 14-digit string'); - assert.strictEqual(new Morebits.date(parseInt(naive / 10, 10)).toISOString(), new Date(parseInt(naive / 10, 10)).toISOString(), 'native 13 digit'); - assert.strictEqual(new Morebits.date(naive * 10).toISOString(), new Date(naive * 10).toISOString(), 'native 15 digit'); + assert.strictEqual(new Morebits.Date(now).getTime(), new Date(now).getTime(), 'Constructor from timestring'); + assert.strictEqual(new Morebits.Date(2020, 11, 7, 16, 26).getTime(), new Date(2020, 11, 7, 16, 26).getTime(), 'Constructor from parts'); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).toISOString(), timestampIso, 'enWiki timestamp format'); + assert.strictEqual(new Morebits.Date(naive).toISOString(), timestampIso, 'MediaWiki 14-digit number'); + assert.strictEqual(new Morebits.Date(naive.toString()).toISOString(), timestampIso, 'MediaWiki 14-digit string'); + assert.strictEqual(new Morebits.Date(parseInt(naive / 10, 10)).toISOString(), new Date(parseInt(naive / 10, 10)).toISOString(), 'native 13 digit'); + assert.strictEqual(new Morebits.Date(naive * 10).toISOString(), new Date(naive * 10).toISOString(), 'native 15 digit'); }); - const date = new Morebits.date(ts_mw); + const date = new Morebits.Date(timestampMediaWiki); test('Methods', () => { assert.true(date.isValid(), 'Valid'); // Logs a message; not a failure, but annoying - assert.false(new Morebits.date('no').isValid(), 'Invalid'); + assert.false(new Morebits.Date('no').isValid(), 'Invalid'); // Ideally we would test the differences between UTC and non-UTC dates assert.strictEqual(date.getUTCDayName(), 'Saturday', 'getUTCDayName'); @@ -26,7 +28,7 @@ describe('Morebits.date', () => { assert.strictEqual(date.getUTCMonthName(), 'November', 'getUTCMonthName'); assert.strictEqual(date.getUTCMonthNameAbbrev(), 'Nov', 'getUTCMonthNameAbbrev'); - assert.true(new Morebits.date(now).isAfter(date), 'isAfter'); + assert.true(new Morebits.Date(now).isAfter(date), 'isAfter'); assert.true(date.isBefore(new Date(now)), 'isBefore'); }); test('RegEx headers', () => { @@ -41,25 +43,25 @@ describe('Morebits.date', () => { assert.false(date.monthHeaderRegex().test('==December 2020=='), 'Wrong month'); }); test('add/subtract', () => { - assert.strictEqual(new Morebits.date(ts_mw).add(1, 'day').toISOString(), '2020-11-08T16:26:00.000Z', 'Add 1 day'); - assert.strictEqual(new Morebits.date(ts_mw).add(1, 'DaY').toISOString(), '2020-11-08T16:26:00.000Z', 'Loudly add 1 day'); - assert.strictEqual(new Morebits.date(ts_mw).add('1', 'day').toISOString(), '2020-11-08T16:26:00.000Z', "Add 1 day but it's a string"); - assert.strictEqual(new Morebits.date(ts_mw).subtract(1, 'day').toISOString(), '2020-11-06T16:26:00.000Z', 'Subtract 1 day'); - assert.strictEqual(new Morebits.date(ts_mw).add(2, 'weeks').toISOString(), '2020-11-21T16:26:00.000Z', 'Add 2 weeks'); - assert.strictEqual(new Morebits.date(ts_mw).add(2, 'weeks').subtract(2, 'weeks').toISOString(), ts_iso, '2 weeks roundtrip'); // Note, this intentionally twice-crosses a US DST + assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'day').toISOString(), '2020-11-08T16:26:00.000Z', 'Add 1 day'); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'DaY').toISOString(), '2020-11-08T16:26:00.000Z', 'Loudly add 1 day'); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).add('1', 'day').toISOString(), '2020-11-08T16:26:00.000Z', "Add 1 day but it's a string"); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).subtract(1, 'day').toISOString(), '2020-11-06T16:26:00.000Z', 'Subtract 1 day'); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(2, 'weeks').toISOString(), '2020-11-21T16:26:00.000Z', 'Add 2 weeks'); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(2, 'weeks').subtract(2, 'weeks').toISOString(), timestampIso, '2 weeks roundtrip'); // Note, this intentionally twice-crosses a US DST - assert.strictEqual(new Morebits.date(ts_mw).add(1, 'second').toISOString(), '2020-11-07T16:26:01.000Z', 'Add 1 second'); - assert.strictEqual(new Morebits.date(ts_mw).add(1, 'minute').toISOString(), '2020-11-07T16:27:00.000Z', 'Add 1 minute'); - assert.strictEqual(new Morebits.date(ts_mw).add(1, 'hour').toISOString(), '2020-11-07T17:26:00.000Z', 'Add 1 hour'); - assert.strictEqual(new Morebits.date(ts_mw).add(1, 'month').toISOString(), '2020-12-07T16:26:00.000Z', 'Add 1 month'); - assert.strictEqual(new Morebits.date(ts_mw).add(1, 'year').toISOString(), '2021-11-07T16:26:00.000Z', 'Add 1 year'); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'second').toISOString(), '2020-11-07T16:26:01.000Z', 'Add 1 second'); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'minute').toISOString(), '2020-11-07T16:27:00.000Z', 'Add 1 minute'); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'hour').toISOString(), '2020-11-07T17:26:00.000Z', 'Add 1 hour'); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'month').toISOString(), '2020-12-07T16:26:00.000Z', 'Add 1 month'); + assert.strictEqual(new Morebits.Date(timestampMediaWiki).add(1, 'year').toISOString(), '2021-11-07T16:26:00.000Z', 'Add 1 year'); - assert.throws(() => new Morebits.date(ts_mw).add('forty-two'), 'throws: non-number provided'); - assert.throws(() => new Morebits.date(ts_mw).add(1), 'throws: no unit'); - assert.throws(() => new Morebits.date(ts_mw).subtract(1, 'dayo'), 'throws: bad unit'); + assert.throws(() => new Morebits.Date(timestampMediaWiki).add('forty-two'), 'throws: non-number provided'); + assert.throws(() => new Morebits.Date(timestampMediaWiki).add(1), 'throws: no unit'); + assert.throws(() => new Morebits.Date(timestampMediaWiki).subtract(1, 'dayo'), 'throws: bad unit'); }); test('Formats', () => { - assert.strictEqual(new Morebits.date(now).format('YYYY-MM-DDTHH:mm:ss.SSSZ', 'utc'), new Date(now).toISOString(), 'ISO format'); + assert.strictEqual(new Morebits.Date(now).format('YYYY-MM-DDTHH:mm:ss.SSSZ', 'utc'), new Date(now).toISOString(), 'ISO format'); assert.strictEqual(date.format('dddd D MMMM YY h:mA', 'utc'), 'Saturday 7 November 20 4:26PM', 'Some weirder stuff'); assert.strictEqual(date.format('MMt[h month], [d]a[y] D, h [o\'clock] A', 'utc'), '11th month, day 7, 4 o\'clock PM', 'Format escapes'); assert.strictEqual(date.format('dddd D MMMM YY h:mA', 600), 'Sunday 8 November 20 2:26AM', 'non-UTC formatting'); @@ -68,7 +70,7 @@ describe('Morebits.date', () => { test('Calendar', () => { assert.strictEqual(date.calendar('utc'), '2020-11-07', 'Old calendar'); assert.strictEqual(date.calendar(600), '2020-11-08', 'Old non-UTC'); - assert.strictEqual(new Morebits.date(now).calendar('utc'), 'Today at ' + new Morebits.date(now).format('h:mm A', 'utc'), 'New calendar'); - assert.strictEqual(new Morebits.date(now).subtract(1, 'day').calendar('utc'), 'Yesterday at ' + new Morebits.date(now).format('h:mm A', 'utc'), 'Close calendar'); + assert.strictEqual(new Morebits.Date(now).calendar('utc'), 'Today at ' + new Morebits.Date(now).format('h:mm A', 'utc'), 'New calendar'); + assert.strictEqual(new Morebits.Date(now).subtract(1, 'day').calendar('utc'), 'Yesterday at ' + new Morebits.Date(now).format('h:mm A', 'utc'), 'Close calendar'); }); }); diff --git a/tests/morebits.ip.js b/tests/morebits.ip.js index 06e5b5516..d4f2da800 100644 --- a/tests/morebits.ip.js +++ b/tests/morebits.ip.js @@ -1,3 +1,5 @@ +'use strict'; + describe('Morebits.ip', () => { test('sanitizeIPv6', () => { assert.strictEqual(Morebits.ip.sanitizeIPv6('2001:0db8:0010:0000:0000:0000:0000:0001'), '2001:DB8:10:0:0:0:0:1', 'Shorten IPv6'); diff --git a/tests/morebits.js b/tests/morebits.js index 38bd355c5..eb03a660f 100644 --- a/tests/morebits.js +++ b/tests/morebits.js @@ -1,3 +1,5 @@ +'use strict'; + describe('constants', () => { test('userIsSysop', () => { assert.true(Morebits.userIsSysop, 'Is sysop'); diff --git a/tests/morebits.quickForm.js b/tests/morebits.quickForm.js index 964aa59dd..c3f2d5e4e 100644 --- a/tests/morebits.quickForm.js +++ b/tests/morebits.quickForm.js @@ -1,10 +1,12 @@ +'use strict'; + /** * Simple helper to render a quickform element * * @param data */ function renderElement(data) { - return new Morebits.quickForm.element(data).render(); + return new Morebits.QuickForm.Element(data).render(); } describe('quickform', () => { @@ -75,7 +77,7 @@ describe('quickform', () => { }; const getRenderedForm = () => { - const form = new Morebits.quickForm(() => {}); + const form = new Morebits.QuickForm(() => {}); form.append(inputConfig); form.append(checkboxesConfig); form.append(checkboxListConfig); diff --git a/tests/morebits.string.js b/tests/morebits.string.js index cdedfc1f0..bce71c9b9 100644 --- a/tests/morebits.string.js +++ b/tests/morebits.string.js @@ -1,3 +1,5 @@ +'use strict'; + describe('Morebits.string', () => { test('escapeRegExp', () => { assert.strictEqual(Morebits.string.escapeRegExp('Four score and seven years ago'), 'Four[_ ]score[_ ]and[_ ]seven[_ ]years[_ ]ago', 'Spaces'); diff --git a/tests/morebits.taskManager.js b/tests/morebits.taskManager.js index b44ad04fc..bcd03db49 100644 --- a/tests/morebits.taskManager.js +++ b/tests/morebits.taskManager.js @@ -1,6 +1,8 @@ +'use strict'; + describe('Morebits.taskManager', () => { test('Contruction', () => { - const tm = new Morebits.taskManager(); + const tm = new Morebits.TaskManager(); assert.true(tm instanceof Morebits.taskManager, 'Correct instance'); }); @@ -19,7 +21,7 @@ describe('Morebits.taskManager', () => { test('Simple', () => { generateFuncs(); - const simple = new Morebits.taskManager(); + const simple = new Morebits.TaskManager(); simple.add(data.one, []); simple.add(data.two, [data.one]); simple.add(data.three, [data.two]); @@ -30,7 +32,7 @@ describe('Morebits.taskManager', () => { }); test('Complex', () => { generateFuncs(); - const complex = new Morebits.taskManager(); + const complex = new Morebits.TaskManager(); complex.add(data.one, [data.two]); complex.add(data.two, [data.three, data.four]); complex.add(data.three, []); diff --git a/tests/morebits.unbinder.js b/tests/morebits.unbinder.js index 2540f457c..4c4cd6644 100644 --- a/tests/morebits.unbinder.js +++ b/tests/morebits.unbinder.js @@ -1,19 +1,21 @@ +'use strict'; + describe('Morebits.unbinder', () => { test('Construction', () => { - assert.throws(() => new Morebits.unbinder(), 'throws: no string'); - assert.throws(() => new Morebits.unbinder([42]), 'throws: not a string'); - const u = new Morebits.unbinder('Hello world'); + assert.throws(() => new Morebits.Unbinder(), 'throws: no string'); + assert.throws(() => new Morebits.Unbinder([42]), 'throws: not a string'); + const u = new Morebits.Unbinder('Hello world'); assert.true(u instanceof Morebits.unbinder, 'Correct instance'); assert.throws(() => u.unbind(), 'throws: Missing prefix'); assert.throws(() => u.unbind('w'), 'throws: Missing postfix'); }); test('Run', () => { - let u = new Morebits.unbinder('Hello world world'); + let u = new Morebits.Unbinder('Hello world world'); u.unbind(''); u.content = u.content.replace(/world/g, 'earth'); assert.strictEqual(u.rebind(), 'Hello earth earth', 'Simple replace'); - u = new Morebits.unbinder('Hello world world [link link] [[link|link]]'); + u = new Morebits.Unbinder('Hello world world [link link] [[link|link]]'); assert.true(u instanceof Morebits.unbinder, 'Correct instance'); u.unbind(''); u.unbind('\\[\\[', '\\]\\]'); diff --git a/tests/morebits.wikitext.js b/tests/morebits.wikitext.js index 2bf15abd0..a6e5a3b45 100644 --- a/tests/morebits.wikitext.js +++ b/tests/morebits.wikitext.js @@ -1,3 +1,5 @@ +'use strict'; + describe('Morebits.wikitext', () => { test('parseTemplate', () => { // Function to help build a template from a sample object @@ -81,13 +83,13 @@ describe('Morebits.wikitext', () => { test('Morebits.wikitext.page', () => { const text = '{{short description}}{{about}}[[File:Fee.svg]]O, [[Juliet|she]] doth {{plural|teach}} the torches to burn bright!'; - const page = new Morebits.wikitext.page(text); + const page = new Morebits.wikitext.Page(text); assert.true(page instanceof Morebits.wikitext.page, 'Correct instance'); assert.strictEqual(page.getText(), text, 'Got text'); // Throws - assert.throws(() => new Morebits.wikitext.page(text).insertAfterTemplates(), 'throws: no tag'); - assert.throws(() => new Morebits.wikitext.page(text).insertAfterTemplates('tag'), 'throws: no regex'); + assert.throws(() => new Morebits.wikitext.Page(text).insertAfterTemplates(), 'throws: no tag'); + assert.throws(() => new Morebits.wikitext.Page(text).insertAfterTemplates('tag'), 'throws: no regex'); // Define all the tests individually, with the appropriate method, // input, expected output, and (spreaded) parameters. @@ -354,7 +356,7 @@ describe('Morebits.wikitext', () => { ]; tests.forEach((test) => { - const page = new Morebits.wikitext.page(test.input); + const page = new Morebits.wikitext.Page(test.input); assert.strictEqual(page[test.method](...test.params).getText(), test.expected, test.method + ' - ' + test.name); }); }); diff --git a/tests/twinkleblock.test.js b/tests/twinkleblock.test.js index 732af83da..83ffe60bb 100644 --- a/tests/twinkleblock.test.js +++ b/tests/twinkleblock.test.js @@ -1,3 +1,5 @@ +'use strict'; + describe('modules/twinkleblock', () => { describe('combineFormDataAndFieldTemplateOptions', () => { // https://github.com/wikimedia-gadgets/twinkle/issues/2106 diff --git a/tests/twinkletag.test.js b/tests/twinkletag.test.js index dc9586b0d..eeb228d24 100644 --- a/tests/twinkletag.test.js +++ b/tests/twinkletag.test.js @@ -1,3 +1,5 @@ +'use strict'; + describe('modules/twinkletag', () => { describe('checkIncompatible', () => { test('no conflicts, 0 tags to check', () => { diff --git a/tests/twinklewarn.test.js b/tests/twinklewarn.test.js index 3684775a1..5e054df2c 100644 --- a/tests/twinklewarn.test.js +++ b/tests/twinklewarn.test.js @@ -1,3 +1,5 @@ +'use strict'; + describe('modules/twinklewarn', () => { describe('getTemplateProperty', () => { const templates = { diff --git a/tests/twinklexfd.test.js b/tests/twinklexfd.test.js index 0fd9812bb..c895b249d 100644 --- a/tests/twinklexfd.test.js +++ b/tests/twinklexfd.test.js @@ -1,3 +1,5 @@ +'use strict'; + describe('modules/twinklexfd', () => { describe('insertRMTR', () => { test('0 rows, 1 line breaks', () => {