aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/common.js
blob: 7d3b3db2ab90e980c58b0f7414c8c35e54f1b913 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
define( [
	"qunit",
	"jquery" ],
function( QUnit, $ ) {

var exports = {};

function testWidgetDefaults( widget, defaults ) {
	var pluginDefaults = $.ui[ widget ].prototype.options;

	// Ensure that all defaults have the correct value
	QUnit.test( "defined defaults", function( assert ) {
		var count = 0;
		$.each( defaults, function( key, val ) {
			assert.expect( ++count );
			if ( typeof val === "function" ) {
				assert.ok( typeof pluginDefaults[ key ] === "function", key );
				return;
			}
			assert.deepEqual( pluginDefaults[ key ], val, key );
		} );
	} );

	// Ensure that all defaults were tested
	QUnit.test( "tested defaults", function( assert ) {
		var count = 0;
		$.each( pluginDefaults, function( key ) {
			assert.expect( ++count );
			assert.ok( key in defaults, key );
		} );
	} );
}

function testWidgetOverrides( widget ) {
	if ( $.uiBackCompat === false ) {
		QUnit.test( "$.widget overrides", function( assert ) {
			assert.expect( 4 );
			$.each( [
				"_createWidget",
				"destroy",
				"option",
				"_trigger"
			], function( i, method ) {
				assert.strictEqual( $.ui[ widget ].prototype[ method ],
					$.Widget.prototype[ method ], "should not override " + method );
			} );
		} );
	}
}

function testBasicUsage( widget ) {
	QUnit.test( "basic usage", function( assert ) {
		assert.expect( 3 );

		var defaultElement = $.ui[ widget ].prototype.defaultElement;
		$( defaultElement ).appendTo( "body" )[ widget ]().remove();
		assert.ok( true, "initialized on element" );

		$( defaultElement )[ widget ]().remove();
		assert.ok( true, "initialized on disconnected DOMElement - never connected" );

		// Ensure manipulating removed elements works (#3664)
		$( defaultElement ).appendTo( "body" ).remove()[ widget ]().remove();
		assert.ok( true, "initialized on disconnected DOMElement - removed" );
	} );
}

exports.testWidget = function( widget, settings ) {
	QUnit.module( widget + ": common widget" );

	exports.testJshint( "/widgets/" + widget );
	testWidgetDefaults( widget, settings.defaults );
	testWidgetOverrides( widget );
	if ( !settings.noDefaultElement ) {
		testBasicUsage( widget );
	}
	QUnit.test( "version", function( assert ) {
		assert.expect( 1 );
		assert.ok( "version" in $.ui[ widget ].prototype, "version property exists" );
	} );
};

exports.testJshint = function( module ) {

	// Function.prototype.bind check is needed because JSHint doesn't work in ES3 browsers anymore
	// https://github.com/jshint/jshint/issues/1384
	if ( QUnit.urlParams.nojshint || !Function.prototype.bind ) {
		return;
	}

	QUnit.test( "JSHint", function( assert ) {
		var ready = assert.async();
		require( [ "jshint" ], function() {
			assert.expect( 1 );

			$.when(
				$.ajax( {
					url: "../../../ui/.jshintrc",
					dataType: "json"
				} ),
				$.ajax( {
					url: "../../../ui/" + module + ".js",
					dataType: "text"
				} )
			)
				.done( function( hintArgs, srcArgs ) {
					var globals, passed, errors,
						jshintrc = hintArgs[ 0 ],
						source = srcArgs[ 0 ];

					globals = jshintrc.globals || {};
					delete jshintrc.globals;
					passed = JSHINT( source, jshintrc, globals );
					errors = $.map( JSHINT.errors, function( error ) {

						// JSHINT may report null if there are too many errors
						if ( !error ) {
							return;
						}

						return "[L" + error.line + ":C" + error.character + "] " +
							error.reason + "\n" + error.evidence + "\n";
					} ).join( "\n" );
					assert.ok( passed, errors );
					ready();
				} )
				.fail( function( hintError, srcError ) {
					assert.ok( false, "error loading source: " + ( hintError || srcError ).statusText );
					ready();
				} );
		} );
	} );
};

return exports;

} );