summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/qunit/smw/api/ext.smw.api.test.js
blob: 8960e7d8b9a7f0d5a81fed6dfb76a6eb60ec91a6 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
 * This file is part of the Semantic MediaWiki QUnit test suite
 * @see https://semantic-mediawiki.org/wiki/QUnit
 *
 * @section LICENSE
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * @since 1.9
 *
 * @file
 * @ignore
 *
 * @ingroup SMW
 * @ingroup Test
 *
 * @licence GNU GPL v2+
 * @author mwjames
 */

/**
 * Tests methods provided by ext.smw.api.js
 * @ignore
 */
( function ( $, mw, smw ) {
	'use strict';

	QUnit.module( 'ext.smw.Api', QUnit.newMwEnvironment() );

	var pass = 'Passes because ';

	/**
	 * Test accessibility
	 *
	 * @since: 1.9
	 * @ignore
	 */
	QUnit.test( 'instance', function ( assert ) {
		assert.expect( 1 );

		var result = new smw.api();
		assert.ok( result instanceof Object, pass + 'the api instance was accessible' );

	} );

	/**
	 * Test fetch
	 *
	 * @since: 1.9
	 * @ignore
	 */
	QUnit.test( 'fetch()', function ( assert ) {
		assert.expect( 4 );

		var done = assert.async();

		var smwApi = new smw.api();

		assert.throws( function() { smwApi.fetch( '' , '' ); }, pass + 'an error was raised (query was empty)' );
		assert.throws( function() { smwApi.fetch( {'foo': 'bar' } , '' ); }, pass + 'an error was raised (query was an object)' );

		// Ajax
		var query = '[[Modification date::+]]|?Modification date|limit=10|offset=0';

		smwApi.fetch( query )
		.done( function ( results ) {

			assert.ok( true, pass + 'of an positive server response' );
			assert.ok( results instanceof Object, pass + 'an object was returned' );

			done();
		} );

	} );

	/**
	 * Test caching
	 *
	 * @since: 1.9
	 * @ignore
	 */
	QUnit.test( 'fetch() cache test', function ( assert ) {
		assert.expect( 4 );

		var smwApi = new smw.api();

		// Ajax
		var queryString = '[[Modification date::+]]|?Modification date|?Modification date|?Modification date|limit=100';
		var undefCacheDone = assert.async();

		smwApi.fetch( queryString )
		.done( function ( results ) {
			assert.equal( results.isCached, false , pass + ' caching is set "undefined" and results are not cached' );
			undefCacheDone();
		} );

		var falseCacheDone = assert.async();
		smwApi.fetch( queryString, false )
		.done( function ( results ) {
			assert.equal( results.isCached , false , pass + ' caching is set "false" and results are not cached' );
			falseCacheDone();
		} );

		// Make sure the cache is initialized otherwise the asserts will fail
		// for the first test run
		var cacheDone = assert.async();
		smwApi.fetch( queryString, true )
		.done( function () {
			var firstCacheDone = assert.async();

			smwApi.fetch( queryString, 60 * 1000 )
			.done( function ( results ) {
				assert.equal( results.isCached , true , pass + ' caching is set to "60 * 1000" and results are cached' );
				firstCacheDone();
			} );

			var otherCacheDone = assert.async();
			smwApi.fetch( queryString, true )
			.done( function ( results ) {
				assert.equal( results.isCached , true , pass + ' caching is set "true" and results are cached' );
				otherCacheDone();
			} );

			cacheDone();
		} );

	} );

	/**
	 * Test fetch vs. a normal $.ajax call
	 *
	 * @since: 1.9
	 * @ignore since 2013 - should this test be removed?
	 */
	QUnit.test( 'fetch() vs. $.ajax', function ( assert ) {
		assert.expect( 3 );

		var ajaxDone = assert.async();
		var fetchDone = assert.async();
		var cachedFetchDone = assert.async();

		var smwApi = new smw.api();
		var startDate;

		// Ajax
		var queryString = '[[Modification date::+]]|?Modification date|?Modification date|?Modification date|limit=100';

		startDate = new Date();

		$.ajax( {
			url: mw.util.wikiScript( 'api' ),
			dataType: 'json',
			data: {
				'action': 'ask',
				'format': 'json',
				'query' : queryString
				}
			} )
			.done( function ( results ) {
				assert.ok( results, 'Fetch ' + results.query.meta.count + ' items using $.ajax which took: ' + ( new Date().getTime() - startDate.getTime() ) + ' ms' );
				startDate = new Date();
				ajaxDone();
			} );

		smwApi.fetch( queryString )
		.done( function ( results ) {
			assert.ok( results, 'Fetch ' + results.query.meta.count + ' items using smw.Api.fetch() which took: ' + ( new Date().getTime() - startDate.getTime() ) + ' ms' );
			fetchDone();
		} );

		smwApi.fetch( queryString, true )
		.done( function ( results ) {
			assert.ok( results, 'Fetch ' + results.query.meta.count + ' items using smw.Api.fetch() which were cached and took: ' + ( new Date().getTime() - startDate.getTime() ) + ' ms' );
			cachedFetchDone();
		} );

	} );

}( jQuery, mediaWiki, semanticMediaWiki ) );