Debian Codegraph @

tests/jest/mediawiki.special.block/SpecialBlock.test.js

raw symbols SCIP
1 'use strict';
2
3 const { nextTick } = require( 'vue' );
4 const { getSpecialBlock } = require( './SpecialBlock.setup.js' );
5
6 describe( 'SpecialBlock', () => {
7 let wrapper;
8
9 /**
10 * Mock postWithEditToken to return an actual Deferred object,
11 * so that jQuery promise chain methods (e.g. always()) will execute in the test.
12 *
13 * @param {Object} [config] Configuration to override the defaults.
14 */
15 const withSubmission = ( config ) => {
16 const jQuery = jest.requireActual( '../../../resources/lib/jquery/jquery.js' );
17 mw.Api.prototype.postWithEditToken = jest.fn( () => jQuery.Deferred().resolve().promise() );
18 HTMLFormElement.prototype.checkValidity = jest.fn().mockReturnValue( true );
19 wrapper = getSpecialBlock( config );
20 };
21
22 beforeEach( () => {
23 wrapper = getSpecialBlock();
24 } );
25
26 it( 'should show no banner and "Block this user" button on page load', () => {
27 expect( wrapper.find( '.cdx-message__content' ).exists() ).toBeFalsy();
28 expect( wrapper.find( '.mw-block-submit' ).text() ).toStrictEqual( 'ipbsubmit' );
29 } );
30
31 it( 'should show a banner and a submit button with text based on if user is already blocked', () => {
32 expect( wrapper.find( '.mw-block-error' ).exists() ).toBeFalsy();
33 wrapper = getSpecialBlock( {
34 blockAlreadyBlocked: true,
35 blockTargetUser: 'ExampleUser',
36 blockPreErrors: [ 'ExampleUser is already blocked.' ]
37 } );
38 expect( wrapper.find( '.mw-block-error' ).exists() ).toBeTruthy();
39 expect( wrapper.find( 'button.cdx-button' ).text() ).toStrictEqual( 'ipb-change-block' );
40 } );
41
42 it( 'should submit an API request to block the user', async () => {
43 withSubmission();
44 await wrapper.find( '[name=wpTarget]' ).setValue( 'ExampleUser' );
45 await wrapper.find( '.cdx-radio__input[value=datetime]' ).setValue( true );
46 await wrapper.find( '[name=wpExpiry-other]' ).setValue( '2999-01-23T12:34:56' );
47 await wrapper.find( '[name=wpReason-other]' ).setValue( 'This is a test' );
48 const spy = jest.spyOn( mw.Api.prototype, 'postWithEditToken' );
49 const submitButton = wrapper.find( '.mw-block-submit' );
50 expect( wrapper.find( '.mw-block-success' ).exists() ).toBeFalsy();
51 await submitButton.trigger( 'click' );
52 expect( spy ).toHaveBeenCalledWith( {
53 action: 'block',
54 user: 'ExampleUser',
55 expiry: '2999-01-23T12:34:56',
56 reason: 'This is a test',
57 reblock: 0,
58 autoblock: 1,
59 allowusertalk: 1,
60 errorlang: 'en',
61 errorsuselocal: true,
62 uselang: 'en',
63 format: 'json'
64 } );
65 expect( wrapper.find( '.mw-block-success' ).exists() ).toBeTruthy();
66 } );
67
68 it( 'should add an error state to invalid fields on submission', async () => {
69 withSubmission();
70 await wrapper.find( '[name=wpTarget]' ).setValue( 'ExampleUser' );
71 await wrapper.find( '.cdx-radio__input[value=datetime]' ).setValue( true );
72 // Add invalid date
73 await wrapper.find( '[name=wpExpiry-other]' ).setValue( '0000-01-23T12:34:56' );
74 await wrapper.find( '.mw-block-submit' ).trigger( 'click' );
75 await nextTick();
76 expect( wrapper.find( '.mw-block-success' ).exists() ).toBeFalsy();
77 expect( wrapper.find( '.mw-block-expiry-field__datetime .cdx-text-input' ).attributes().class )
78 .toContain( 'cdx-text-input--status-error' );
79 expect( wrapper.find( '.mw-block-expiry-field__datetime .cdx-message--error' ).exists() )
80 .toBeTruthy();
81 } );
82
83 it( 'should require confirmation for the hide-user option', async () => {
84 withSubmission( { blockHideUser: true } );
85 await wrapper.find( '[name=wpTarget]' ).setValue( 'ExampleUser' );
86 // Assert 'hide username' is not yet clickable.
87 expect( wrapper.find( '.mw-block-hideuser input' ).attributes().disabled ).toStrictEqual( '' );
88 // Set the expiry to 'infinite' to enable the hide-user option.
89 wrapper.vm.store.expiry = 'infinite';
90 mw.util.isInfinity = jest.fn().mockReturnValue( true );
91 await nextTick();
92 // Assert confirmation checkbox is not shown.
93 expect( wrapper.find( '.mw-block-confirm' ).exists() ).toBeFalsy();
94 // Assert 'hide username' is now clickable.
95 expect( wrapper.find( '.mw-block-hideuser input' ).attributes().disabled ).toBeUndefined();
96 await wrapper.find( '.mw-block-hideuser input' ).trigger( 'click' );
97 // Assert confirmation checkbox is now shown.
98 expect( wrapper.find( '.mw-block-confirm' ).exists() ).toBeTruthy();
99 // Once clicked, assert the submit button is disabled.
100 expect( wrapper.find( '.mw-block-submit' ).attributes().disabled ).toStrictEqual( '' );
101 await wrapper.find( '.mw-block-submit' ).trigger( 'click' );
102 // Assert submission is not possible.
103 expect( wrapper.find( '.mw-block-success' ).exists() ).toBeFalsy();
104 await wrapper.find( '.mw-block-confirm input' ).trigger( 'click' );
105 expect( wrapper.find( '.mw-block-submit' ).attributes().disabled ).toBeUndefined();
106 } );
107
108 it( 'should require confirmation for self-blocking', async () => {
109 withSubmission( { wgUserName: 'ExampleUser' } );
110 expect( wrapper.find( '.mw-block-error' ).exists() ).toBeFalsy();
111 expect( wrapper.find( '.mw-block-submit' ).attributes().disabled ).toBeUndefined();
112 await wrapper.find( '[name=wpTarget]' ).setValue( 'ExampleUser' );
113 expect( wrapper.find( '.mw-block-submit' ).attributes().disabled ).toStrictEqual( '' );
114 expect( wrapper.find( '.mw-block-error' ).text() ).toStrictEqual( 'ipb-blockingself' );
115 } );
116 } );