summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/MultimediaViewer/tests/qunit/mmv/ui/mmv.ui.reuse.share.test.js
blob: 5757d35b16dc6bc851a109b4a569e60b3e46384e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * This file is part of the MediaWiki extension MultimediaViewer.
 *
 * MultimediaViewer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * MultimediaViewer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MultimediaViewer.  If not, see <http://www.gnu.org/licenses/>.
 */

( function ( mw, $ ) {
	function makeShare() {
		return new mw.mmv.ui.reuse.Share( $( '#qunit-fixture' ) );
	}

	QUnit.module( 'mmv.ui.reuse.share', QUnit.newMwEnvironment() );

	QUnit.test( 'Sanity test, object creation and UI construction', function ( assert ) {
		var share = makeShare();

		assert.ok( share, 'Share UI element is created.' );
		assert.strictEqual( share.$pane.length, 1, 'Pane div created.' );
		assert.ok( share.pageInput, 'Text field created.' );
		assert.ok( share.$pageLink, 'Link created.' );
	} );

	QUnit.test( 'set()/empty():', function ( assert ) {
		var share = makeShare(),
			image = { // fake mw.mmv.model.Image
				title: new mw.Title( 'File:Foobar.jpg' ),
				url: 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
				descriptionUrl: '//commons.wikimedia.org/wiki/File:Foobar.jpg'
			};

		assert.notStrictEqual( !share.pageInput.getValue(), '', 'pageInput is empty.' );

		share.select = function () {
			assert.ok( true, 'Text has been selected after data is set.' );
		};

		share.set( image );

		assert.notStrictEqual( share.pageInput.getValue(), '', 'pageInput is not empty.' );

		share.empty();

		assert.notStrictEqual( !share.pageInput.getValue(), '', 'pageInput is empty.' );
	} );

	QUnit.test( 'attach()/unattach():', function ( assert ) {
		var share = makeShare(),
			image = {
				title: new mw.Title( 'File:Foobar.jpg' ),
				url: 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
				descriptionUrl: '//commons.wikimedia.org/wiki/File:Foobar.jpg'
			};

		share.set( image );

		share.selectAllOnEvent = function () {
			assert.ok( false, 'selectAllOnEvent should not have been called.' );
		};

		// Triggering action events before attaching should do nothing
		// use of focus() would run into jQuery bug #14740 and similar issues
		share.pageInput.$element.find( 'input' ).triggerHandler( 'focus' );

		share.selectAllOnEvent = function () {
			assert.ok( true, 'selectAllOnEvent was called.' );
		};

		share.attach();

		// Action events should be handled now
		share.pageInput.$element.find( 'input' ).triggerHandler( 'focus' );

		// Test the unattach part
		share.selectAllOnEvent = function () {
			assert.ok( false, 'selectAllOnEvent should not have been called.' );
		};

		share.unattach();

		// Triggering action events now that we are unattached should do nothing
		share.pageInput.$element.find( 'input' ).triggerHandler( 'focus' );
	} );

}( mediaWiki, jQuery ) );