Skip to content

Commit 3d05889

Browse files
committed
There are two things I like dry: my code and my clothes. I'm a bald dev, that's why.
1 parent 1fab845 commit 3d05889

File tree

1 file changed

+53
-160
lines changed

1 file changed

+53
-160
lines changed

test/unit.js

Lines changed: 53 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,180 +1,73 @@
11
module( "main", { teardown: moduleTeardown } );
22

3-
test( "success", function() {
4-
stop();
5-
expect( 1 );
6-
$.jsonp({
7-
url: "http://gdata.youtube.com/feeds/api/users/julianaubourg?callback=?",
8-
data: {
9-
alt: "json-in-script"
10-
},
11-
success: function() {
12-
ok( true, "Success" );
13-
},
14-
error: function() {
15-
ok( false, "Error" );
16-
},
17-
complete: function() {
18-
start();
19-
}
20-
});
21-
});
22-
23-
test( "error (HTTP Code)", function() {
24-
stop();
25-
expect( 1 );
26-
$.jsonp({
27-
url: "http://gdata.youtube.com/feeds/api/users/hgfusyggbvbdbrfvurgbirhtiytjrhjsrhk66?callback=?",
28-
data: {
29-
alt: "json-in-script"
30-
},
31-
success: function() {
32-
ok( false, "Success" );
33-
},
34-
error: function() {
35-
ok( true, "Error" );
36-
},
37-
complete: function() {
38-
start();
39-
}
40-
});
41-
});
3+
var hasDeferred = $.Deferred ? 1 : 0;
424

43-
test( "error (Syntax Error)", function() {
44-
stop();
45-
expect( 2 );
46-
var oldOnError = window.onerror;
47-
window.onerror = function() {
48-
ok( true, "Syntax Error Thrown" );
49-
return false;
50-
};
51-
$.jsonp({
52-
url: "data/syntax-error.js",
53-
cache: true,
54-
success: function() {
55-
ok( false, "Success" );
56-
},
57-
error: function() {
58-
ok( true, "Error" );
59-
},
60-
complete: function() {
61-
window.onerror = oldOnError;
62-
start();
63-
}
64-
});
65-
});
66-
67-
test( "error (callback not called)", function() {
68-
stop();
69-
expect( 2 );
70-
$.jsonp({
71-
url: "data/no-callback.js",
72-
cache: true,
73-
success: function() {
74-
ok( false, "Success" );
75-
},
76-
error: function() {
77-
ok( true, "Error" );
78-
},
79-
complete: function() {
80-
strictEqual( window.bob, 33, "script was executed" );
81-
window.bob = false;
82-
start();
83-
}
84-
});
85-
});
86-
87-
if ( $.Deferred ) {
88-
89-
test( "promise - success", function() {
5+
function testJSONP( name, outcome, options ) {
6+
test( name, function() {
907
stop();
91-
expect( 2 );
92-
$.jsonp({
93-
url: "http://gdata.youtube.com/feeds/api/users/julianaubourg?callback=?",
94-
data: {
95-
alt: "json-in-script"
96-
},
8+
expect( ( options.expect || 0 ) + 1 + hasDeferred );
9+
var xOptions = $.jsonp( $.extend( {}, options, {
9710
success: function() {
98-
ok( true, "Success (options)" );
99-
},
100-
complete: function() {
101-
start();
102-
}
103-
}).done(function() {
104-
ok( true, "Success" );
105-
}).fail(function() {
106-
ok( false, "Error" );
107-
});
108-
});
109-
110-
test( "promise - error (HTTP Code)", function() {
111-
stop();
112-
expect( 2 );
113-
$.jsonp({
114-
url: "http://gdata.youtube.com/feeds/api/users/hgfusyggbvbdbrfvurgbirhtiytjrhjsrhk66?callback=?",
115-
data: {
116-
alt: "json-in-script"
11+
ok( outcome === "success", "Success" );
11712
},
11813
error: function() {
119-
ok( true, "Error (options)" );
14+
ok( outcome === "error", "Error" );
12015
},
12116
complete: function() {
17+
if ( options.complete ) {
18+
options.complete.call( this );
19+
}
12220
start();
12321
}
124-
}).done(function() {
125-
ok( false, "Success" );
126-
}).fail(function() {
127-
ok( true, "Error" );
128-
});
22+
}) );
23+
if ( hasDeferred ) {
24+
xOptions.done(function() {
25+
ok( outcome === "success", "Done" );
26+
}).fail(function() {
27+
ok( outcome === "error", "Fail" );
28+
});
29+
}
12930
});
31+
}
13032

131-
test( "promise - error (Syntax Error)", function() {
132-
stop();
133-
expect( 3 );
134-
var oldOnError = window.onerror;
33+
testJSONP( "success", "success", {
34+
url: "http://gdata.youtube.com/feeds/api/users/julianaubourg?callback=?",
35+
data: {
36+
alt: "json-in-script"
37+
}
38+
});
39+
40+
testJSONP( "error (HTTP Code)", "error", {
41+
url: "http://gdata.youtube.com/feeds/api/users/hgfusyggbvbdbrfvurgbirhtiytjrhjsrhk66?callback=?",
42+
data: {
43+
alt: "json-in-script"
44+
}
45+
});
46+
47+
testJSONP( "error (Syntax Error)", "error", {
48+
expect: 1,
49+
url: "data/syntax-error.js",
50+
cache: true,
51+
beforeSend: function() {
52+
this.oldOnError = window.onerror;
13553
window.onerror = function() {
13654
ok( true, "Syntax Error Thrown" );
137-
return false;
13855
};
139-
$.jsonp({
140-
url: "data/syntax-error.js",
141-
cache: true,
142-
error: function() {
143-
ok( true, "Error (options)" );
144-
},
145-
complete: function() {
146-
window.onerror = oldOnError;
147-
start();
148-
}
149-
}).done(function() {
150-
ok( false, "Success" );
151-
}).fail(function() {
152-
ok( true, "Error" );
153-
});
154-
});
155-
156-
test( "promise - error (callback not called)", function() {
157-
stop();
158-
expect( 3 );
159-
$.jsonp({
160-
url: "data/no-callback.js",
161-
cache: true,
162-
error: function() {
163-
ok( true, "Error (options)" );
164-
},
165-
complete: function() {
166-
strictEqual( window.bob, 33, "script was executed" );
167-
window.bob = false;
168-
start();
169-
}
170-
}).done(function() {
171-
ok( false, "Success" );
172-
}).fail(function() {
173-
ok( true, "Error" );
174-
});
175-
});
56+
},
57+
complete: function() {
58+
window.onerror = this.oldOnError;
59+
}
60+
});
17661

177-
}
62+
testJSONP( "error (callback not called)", "error", {
63+
expect: 1,
64+
url: "data/no-callback.js",
65+
cache: true,
66+
complete: function() {
67+
strictEqual( window.bob, 33, "script was executed" );
68+
window.bob = false;
69+
}
70+
});
17871

17972
test( "stress test", function() {
18073

0 commit comments

Comments
 (0)