aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/testsuite.js
blob: 345a5a106f2296867d3d5705653ef22b9e110eff (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
// FIXME remove this once updated to jQuery core 1.3.3
var hasDuplicate = false;

function testWidgetDefaults(widget, defaults) {
	var pluginDefaults = $.extend({},
		$.widget.defaults,
		$.ui[widget].defaults
	);
	
	// ensure that all defualts have the correct value
	test('defined defaults', function() {
		$.each(defaults, function(key, val) {
			if ($.isFunction(val)) {
				ok(val !== undefined);
				return;
			}
			same(pluginDefaults[key], val, key);
		});
	});
	
	// ensure that all defaults were tested
	test('tested defaults', function() {
		$.each(pluginDefaults, function(key, val) {
			ok(key in defaults, key);
		});
	});
	
	// defaults after init
	test('defaults on init', function() {
		var el = $('<div/>')[widget](),
			instance = el.data(widget);
		
		$.each(defaults, function(key, val) {
			if ($.isFunction(val)) {
				ok(val !== undefined);
				return;
			}
			same(instance.options[key], val, key);
		});
		el.remove();
	});
}

function testSettingOptions(widget, options) {
	test('option values', function() {
		var el = $('<div/>')[widget](),
			instance = el.data(widget);
		
		$.each(options, function(i, option) {
			$.each({
				'null': null,
				'false': false,
				'true': true,
				zero: 0,
				number: 1,
				'empty string': '',
				string: 'string',
				'empty array': [],
				array: ['array'],
				'empty object': {},
				object: {obj: 'ect'},
				date: new Date(),
				regexp: /regexp/,
				'function': function() {}
			}, function(type, val) {
				el[widget]('option', option, val);
				same(instance.options[option], val, option + ': ' + type);
			});
		});
		
		el.remove();
	});
}

function testWidgetOverrides(widget) {
	test('$.widget overrides', function() {
		$.each(['option', '_getData', '_trigger'], function(i, method) {
			ok($.widget.prototype[method] == $.ui[widget].prototype[method],
				'should not override ' + method);
		});
	});
}
function commonWidgetTests(widget, settings) {
	var options = [];
	$.each(settings.defaults, function(option) {
		options.push(option);
	});
	
	module(widget + ": common widget");

	testWidgetDefaults(widget, settings.defaults);
	testSettingOptions(widget, options);
	testWidgetOverrides(widget);
}

// load testswarm agent
(function() {
    var url = window.location.search;
	url = decodeURIComponent( url.slice( url.indexOf("swarmURL=") + 9 ) );
	if ( !url || url.indexOf("http") !== 0 ) {
		return;
	}
    document.write("<scr" + "ipt src='http://testswarm.com/js/inject.js?" + (new Date).getTime() + "'></scr" + "ipt>");
})();