Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/tests/init/targets/ve.init.mw.DesktopArticleTarget.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki Initialization DesktopArticleTarget tests.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 QUnit.module( 've.init.mw.DesktopArticleTarget', ve.test.utils.newMwEnvironment( {
9 config: {
10 wgVisualEditor: ve.extendObject( {}, mw.config.get( 'wgVisualEditor' ), {
11 pageLanguageCode: 'he',
12 pageLanguageDir: 'rtl'
13 } ),
14 wgVisualEditorConfig: ve.extendObject( {}, mw.config.get( 'wgVisualEditorConfig' ), {
15 // Disable welcome dialog
16 showBetaWelcome: false
17 } ),
18 wgAction: 'view',
19 wgNamespaceNumber: 0,
20 wgCanonicalNamespace: ''
21 },
22 beforeEach: function () {
23 this.originalLocation = location.href;
24 },
25 afterEach: function () {
26 // Clean up after history.pushState in ve.init.mw.DesktopArticleTarget#restorePage,
27 // which replaces JavaScriptTest with Badtitle and leaves it behind
28 if ( location.href !== this.originalLocation ) {
29 history.replaceState( null, '', this.originalLocation );
30 }
31 }
32 } ) );
33
34 QUnit.test( 'init', ( assert ) => {
35 const response = {
36 visualeditor: {
37 result: 'success',
38 notices: [
39 '<b>HTML string notice</b> message',
40 {
41 type: 'object notice',
42 message: '<b>object notice</b> message'
43 }
44 ],
45 copyrightWarning: '<div id="editpage-copywarn">Blah blah</div>',
46 checkboxesDef: {
47 wpMinoredit: {
48 id: 'wpMinoredit',
49 'label-message': 'minoredit',
50 tooltip: 'minoredit',
51 'label-id': 'mw-editpage-minoredit',
52 'legacy-name': 'minor',
53 default: false
54 },
55 wpWatchthis: {
56 id: 'wpWatchthis',
57 'label-message': 'watchthis',
58 tooltip: 'watch',
59 'label-id': 'mw-editpage-watch',
60 'legacy-name': 'watch',
61 default: true
62 }
63 },
64 checkboxesMessages: {
65 'accesskey-minoredit': 'i',
66 'tooltip-minoredit': 'Mark this as a minor edit',
67 minoredit: 'This is a minor edit',
68 'accesskey-watch': 'w',
69 'tooltip-watch': 'Add this page to your watchlist',
70 watchthis: 'Watch this page'
71 },
72 protectedClasses: '',
73 basetimestamp: '20161119005107',
74 starttimestamp: '20180831122319',
75 oldid: 1804,
76 blockinfo: null,
77 wouldautocreate: false,
78 canEdit: true,
79 content: '<!DOCTYPE html>\n' + ve.dm.example.singleLine`
80 <html prefix="dc: http://purl.org/dc/terms/ mw: http://mediawiki.org/rdf/" about="http://localhost/MediaWiki/core/index.php/Special:Redirect/revision/1804">
81 <head prefix="mwr: http://localhost/MediaWiki/core/index.php/Special:Redirect/"><meta property="mw:TimeUuid" content="a4fc0409-ad18-11e8-9b45-dd8cefbedb6d"/>
82 <meta charset="utf-8"/>
83 <meta property="mw:pageNamespace" content="0"/>
84 <meta property="mw:pageId" content="643"/>
85 <link rel="dc:replaces" resource="mwr:revision/0"/>
86 <meta property="dc:modified" content="2016-11-19T00:51:07.000Z"/>
87 <meta property="mw:revisionSHA1" content="da39a3ee5e6b4b0d3255bfef95601890afd80709"/>
88 <meta property="mw:html:version" content="1.7.0"/>
89 <link rel="dc:isVersionOf" href="http://localhost/MediaWiki/core/index.php/Empty"/>
90 <title>Empty</title>
91 <base href="http://localhost/MediaWiki/core/index.php/"/>
92 <link rel="stylesheet" href="//localhost/MediaWiki/core/load.php?modules=mediawiki.legacy.commonPrint%2Cshared%7Cmediawiki.skinning.content.parsoid%7Cmediawiki.skinning.interface%7Cskins.vector.styles%7Csite.styles%7Cext.cite.style%7Cext.cite.styles%7Cmediawiki.page.gallery.styles&amp;only=styles&amp;skin=vector"/>
93 </head>
94 <body id="mwAA" lang="he" class="mw-content-rtl sitedir-rtl rtl mw-body-content parsoid-body mediawiki mw-parser-output" dir="rtl">
95 <section data-mw-section-id="0" id="mwAQ"></section>
96 </body>
97 </html>
98 `,
99 preloaded: false,
100 etag: '"1804/a4fc0409-ad18-11e8-9b45-dd8cefbedb6d"'
101 }
102 },
103 target = new ve.init.mw.DesktopArticleTarget(),
104 dataPromise = ve.createDeferred().resolve( response ).promise(),
105 done = assert.async();
106
107 // eslint-disable-next-line no-jquery/no-global-selector
108 $( '#qunit-fixture' ).append( target.$element );
109
110 target.on( 'surfaceReady', () => {
111 assert.strictEqual( target.getSurface().getModel().getDocument().getLang(), 'he', 'Page language is passed through from config' );
112 assert.strictEqual( target.getSurface().getModel().getDocument().getDir(), 'rtl', 'Page direction is passed through from config' );
113 target.activatingDeferred.then( async () => {
114 assert.equalDomElement(
115 target.toolbar.tools.notices.noticeItems[ 0 ].$element[ 0 ],
116 $( '<div class="ve-ui-mwNoticesPopupTool-item"><b>HTML string notice</b> message</div>' )[ 0 ],
117 'HTML string notice message is passed through from API'
118 );
119 assert.strictEqual( target.toolbar.tools.notices.noticeItems[ 0 ].type, undefined, 'Plain text notice type is undefined' );
120 assert.equalDomElement(
121 target.toolbar.tools.notices.noticeItems[ 1 ].$element[ 0 ],
122 $( '<div class="ve-ui-mwNoticesPopupTool-item"><b>object notice</b> message</div>' )[ 0 ],
123 'Object notice message is passed through from API'
124 );
125 assert.strictEqual( target.toolbar.tools.notices.noticeItems[ 1 ].type, 'object notice', 'Object notice type is passed through from API' );
126
127 // Open the save dialog and examine it (this bypasses a bunch of stuff, and may fail in funny
128 // ways, but #showSaveDialog has many dependencies that I don't want to simulate here).
129 const dialogs = target.getSurface().getDialogs();
130 const instance = dialogs.openWindow( 'mwSave', target.getSaveDialogOpeningData() );
131 await instance.opened;
132 const dialog = dialogs.getCurrentWindow();
133 assert.equalDomElement(
134 dialog.$element.find( '#editpage-copywarn' )[ 0 ],
135 $( '<div id="editpage-copywarn">Blah blah</div>' )[ 0 ],
136 'Copyright warning message is passed through from API'
137 );
138 dialogs.closeWindow( 'mwSave' );
139 await instance.closed;
140
141 // Store doc state and examine it
142 target.storeDocState();
143 const storedData = JSON.parse( sessionStorage.getItem( 've-docstate' ) );
144 const ignoredKeys = {
145 // Not stored because it's always 'success'
146 result: true,
147 // Not stored because it's stored elsewhere
148 content: true,
149 // Not stored because if you're blocked, the editor opens in read-only mode (or doesn't open
150 // at all, on mobile), so we'll never have to restore from auto-save
151 blockinfo: true
152 };
153 for ( const key in response.visualeditor ) {
154 if ( !ignoredKeys[ key ] ) {
155 assert.deepEqual(
156 storedData.response[ key ],
157 response.visualeditor[ key ],
158 key + ' can be restored from auto-save data'
159 );
160 }
161 }
162
163 await target.destroy();
164 done();
165 } );
166 } );
167 target.activate( dataPromise );
168 } );