Debian Codegraph @

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

raw symbols SCIP
1 'use strict';
2
3 const { mount } = require( '@vue/test-utils' );
4 const { createTestingPinia } = require( '@pinia/testing' );
5 const { mockMwConfigGet } = require( './SpecialBlock.setup.js' );
6 const ExpiryField = require( '../../../resources/src/mediawiki.special.block/components/ExpiryField.vue' );
7
8 const presetTestCases = [
9 {
10 title: 'no default, wpExpiry=indefinite [preset]',
11 config: {
12 isInfinity: true,
13 blockExpiryPreset: 'indefinite'
14 },
15 expected: {
16 expiryType: 'preset-duration',
17 presetDuration: 'infinite'
18 }
19 },
20 {
21 title: 'default 9 weeks, wpExpiry=1 week [custom]',
22 config: {
23 blockExpiryDefault: '9 weeks',
24 blockExpiryPreset: '1 week'
25 },
26 expected: {
27 expiryType: 'custom-duration',
28 customDurationNumber: 1,
29 customDurationUnit: 'weeks'
30 }
31 },
32 {
33 title: 'default 31 hours, no wpExpiry [preset]',
34 config: {
35 blockExpiryDefault: '31 hours',
36 blockExpiryPreset: null
37 },
38 expected: {
39 expiryType: 'preset-duration',
40 presetDuration: '31 hours'
41 }
42 },
43 {
44 title: 'no default, wpExpiry=2100-01-01T12:59 [datetime]',
45 config: {
46 blockExpiryPreset: '2100-01-01T12:59'
47 },
48 expected: {
49 expiryType: 'datetime',
50 datetime: '2100-01-01T12:59'
51 }
52 },
53 {
54 title: 'no default, wpExpiry=invalid [preset]',
55 config: {
56 blockExpiryPreset: 'invalid'
57 },
58 expected: {
59 expiryType: 'preset-duration',
60 presetDuration: null
61 }
62 }
63 ];
64
65 describe( 'ExpiryField', () => {
66 it( 'should show an error message if no expiry is provided after form submission', async () => {
67 mockMwConfigGet();
68 const wrapper = mount( ExpiryField, {
69 propsData: { modelValue: {} },
70 global: { plugins: [ createTestingPinia() ] }
71 } );
72 await wrapper.setProps( { formSubmitted: true } );
73 expect( wrapper.find( '.cdx-message--error' ).text() )
74 .toStrictEqual( 'ipb_expiry_invalid' );
75 } );
76
77 it.each( presetTestCases )(
78 'should preselect from fields and set values ($title)', ( { config, expected } ) => {
79 mockMwConfigGet( config );
80 mw.util.isInfinity = jest.fn().mockReturnValue( !!config.isInfinity );
81 const wrapper = mount( ExpiryField, {
82 propsData: { modelValue: {} },
83 global: { plugins: [ createTestingPinia() ] }
84 } );
85 Object.keys( expected ).forEach( ( key ) => {
86 // Test against the app instance
87 expect( wrapper.vm[ key ] ).toStrictEqual( expected[ key ] );
88
89 // Test against the rendered DOM
90 switch ( key ) {
91 case 'expiryType':
92 expect( wrapper.find( '[name=expiryType]:checked' ).element.value )
93 .toStrictEqual( expected[ key ] );
94 break;
95 case 'presetDuration':
96 // No input; Only testable on wrapper.vm (see above)
97 break;
98 case 'customDurationNumber':
99 expect( wrapper.find( 'input[type=number]' ).element.value )
100 .toStrictEqual( expected[ key ].toString() );
101 break;
102 case 'customDurationUnit':
103 // No input.
104 break;
105 case 'datetime':
106 expect( wrapper.find( 'input[type=datetime-local]' ).element.value )
107 .toStrictEqual( expected[ key ] );
108 break;
109 }
110 } );
111 }
112 );
113 } );