Skip to content

Commit

Permalink
tests: write test for AFCH.Text.cleanUp() (wikimedia-gadgets#388)
Browse files Browse the repository at this point in the history
* tests: write test for AFCH.Text.cleanUp()

* rename

* requireScript() -> require()

* fix

* fix fatal error outside of tests

* add DreamRimmer's tests from wikimedia-gadgets#387

from deleted commit f5df016

* fix expectedOutput

* Co-authored-by: DR-WP <[email protected]>

* null edit
  • Loading branch information
NovemLinguae authored Oct 22, 2024
1 parent a76e7f2 commit 72f9c47
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/modules/submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
afchPage, afchSubmission, afchViews, afchViewer;

// Die if reviewing a nonexistent page or a userjs/css page
if ( mw.config.get( 'wgArticleId' ) === 0 ||
mw.config.get( 'wgPageContentModel' ) !== 'wikitext' ) {
return;
if ( typeof inUnitTestEnvironment === 'undefined' ) {
if ( mw.config.get( 'wgArticleId' ) === 0 ||
mw.config.get( 'wgPageContentModel' ) !== 'wikitext' ) {
return;
}
}

/**
Expand Down Expand Up @@ -750,25 +752,27 @@
}
};

// Add the launch link
$afchLaunchLink = $( mw.util.addPortletLink( AFCH.prefs.launchLinkPosition, '#', 'Review (AFCH)',
'afch-launch', 'Review submission using afc-helper', '1' ) );

if ( AFCH.prefs.autoOpen &&
// Don't autoload in userspace -- too many false positives
AFCH.consts.pagename.indexOf( 'User:' ) !== 0 &&
// Only autoload if viewing or editing the page
[ 'view', 'edit', null ].indexOf( AFCH.getParam( 'action' ) ) !== -1 &&
!AFCH.getParam( 'diff' ) && !AFCH.getParam( 'oldid' ) ) {
// Launch the script immediately if preference set
createAFCHInstance();
} else {
// Otherwise, wait for a click (`one` to prevent spawning multiple panels)
$afchLaunchLink.one( 'click', createAFCHInstance );
}
if ( typeof inUnitTestEnvironment === 'undefined' ) {
// Add the launch link
$afchLaunchLink = $( mw.util.addPortletLink( AFCH.prefs.launchLinkPosition, '#', 'Review (AFCH)',
'afch-launch', 'Review submission using afc-helper', '1' ) );

if ( AFCH.prefs.autoOpen &&
// Don't autoload in userspace -- too many false positives
AFCH.consts.pagename.indexOf( 'User:' ) !== 0 &&
// Only autoload if viewing or editing the page
[ 'view', 'edit', null ].indexOf( AFCH.getParam( 'action' ) ) !== -1 &&
!AFCH.getParam( 'diff' ) && !AFCH.getParam( 'oldid' ) ) {
// Launch the script immediately if preference set
createAFCHInstance();
} else {
// Otherwise, wait for a click (`one` to prevent spawning multiple panels)
$afchLaunchLink.one( 'click', createAFCHInstance );
}

// Mark launch link for the old helper script as "old" if present on page
$( '#p-cactions #ca-afcHelper > a' ).append( ' (old)' );
// Mark launch link for the old helper script as "old" if present on page
$( '#p-cactions #ca-afcHelper > a' ).append( ' (old)' );
}

// If AFCH is destroyed via AFCH.destroy(),
// remove the $afch window and the launch link
Expand Down
2 changes: 2 additions & 0 deletions tests/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ resetToAFCApplicablePage = function () {
require( './../src/afch.js' );
};

inUnitTestEnvironment = true;

jest.autoMockOff();

// Mocked later
Expand Down
108 changes: 108 additions & 0 deletions tests/test-submissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Tests for src/modules/submission.js
*/

/* eslint-env jest */

require( './scaffold.js' );

resetToAFCApplicablePage();

require( './../src/modules/core.js' );
require( './../src/modules/submissions.js' );

// It's always good to start simple :)
describe( 'AFCH', () => {
it( 'is an object', () => {
expect( typeof AFCH ).toBe( 'object' );
} );
} );

describe( 'AFCH.Text.cleanUp', () => {
it( 'should handle empty input', () => {
const wikicode = '';
const isAccept = true;
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( '' );
} );

it( 'should clean up {{Draft categories|[[Category:Test]]}} when isAccept is true', () => {
const wikicode = '{{Draft categories|[[Category:Test]]}}';
const isAccept = true;
const expectedOutput = '[[Category:Test]]';
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( expectedOutput );
} );

it( 'should not clean up {{Draft categories|[[Category:Test]]}} when isAccept is false', () => {
const wikicode = '{{Draft categories|[[Category:Test]]}}';
const isAccept = false;
const expectedOutput = '{{Draft categories|[[:Category:Test]]}}';
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( expectedOutput );
} );

it( 'should clean up {{draft categories|[[Category:Test]]}} (case insensitive) when isAccept is true', () => {
const wikicode = '{{draft categories|[[Category:Test]]}}';
const isAccept = true;
const expectedOutput = '[[Category:Test]]';
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( expectedOutput );
} );

it( 'should not clean up {{draft categories|[[Category:Test]]}} (case insensitive) when isAccept is false', () => {
const wikicode = '{{draft categories|[[Category:Test]]}}';
const isAccept = false;
const expectedOutput = '{{draft categories|[[:Category:Test]]}}';
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( expectedOutput );
} );

it( 'should clean up {{Draftcat|[[Category:Test]]}} when isAccept is true', () => {
const wikicode = '{{Draftcat|[[Category:Test]]}}';
const isAccept = true;
const expectedOutput = '[[Category:Test]]';
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( expectedOutput );
} );

it( 'should not clean up {{Draftcat|[[Category:Test]]}} when isAccept is false', () => {
const wikicode = '{{Draftcat|[[Category:Test]]}}';
const isAccept = false;
const expectedOutput = '{{Draftcat|[[:Category:Test]]}}';
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( expectedOutput );
} );

it( 'should clean up multiple categories in {{Draft categories}} when isAccept is true', () => {
const wikicode = '{{Draft categories|[[Category:Test1]] [[Category:Test2]]}}';
const isAccept = true;
const expectedOutput = '[[Category:Test1]] [[Category:Test2]]';
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( expectedOutput );
} );

it( 'should not clean up {{Draft categories}} without categories when isAccept is false', () => {
const wikicode = '{{Draft categories}}';
const isAccept = false;
const expectedOutput = '{{Draft categories}}';
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( expectedOutput );
} );

it( 'should clean up {{Draft categories}} with text outside the template when isAccept is true', () => {
const wikicode = 'Some text {{Draft categories|[[Category:Test]]}} more text';
const isAccept = true;
const expectedOutput = 'Some text [[Category:Test]] more text';
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( expectedOutput );
} );

it( 'should not alter non-draft templates', () => {
const wikicode = '{{NonDraft|[[Category:Test]]}}';
const isAccept = true;
const expectedOutput = '{{NonDraft|[[Category:Test]]}}';
const output = ( new AFCH.Text( wikicode ) ).cleanUp( isAccept );
expect( output ).toBe( expectedOutput );
} );
} );

0 comments on commit 72f9c47

Please sign in to comment.