wp-api.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* global wp, JSON */
  2. ( function( QUnit ) {
  3. QUnit.module( 'wpapi' );
  4. QUnit.test( 'API Loaded correctly', function( assert ) {
  5. var done = assert.async();
  6. assert.expect( 2 );
  7. assert.ok( wp.api.loadPromise );
  8. wp.api.loadPromise.done( function() {
  9. assert.ok( wp.api.models );
  10. done();
  11. } );
  12. } );
  13. // The list of collections we should check.
  14. var collectionClassNames = [
  15. 'Categories',
  16. 'Comments',
  17. 'Media',
  18. 'Pages',
  19. 'Posts',
  20. 'Statuses',
  21. 'Tags',
  22. 'Taxonomies',
  23. 'Types',
  24. 'Users'
  25. ];
  26. // Collections that should get helpers tested.
  27. var collectionHelperTests = [
  28. {
  29. 'collectionType': 'Posts',
  30. 'returnsModelType': 'post',
  31. 'supportsMethods': {
  32. 'getDate': 'getDate',
  33. 'getRevisions': 'getRevisions',
  34. 'getTags': 'getTags',
  35. 'getCategories': 'getCategories',
  36. 'getAuthorUser': 'getAuthorUser',
  37. 'getFeaturedMedia': 'getFeaturedMedia'
  38. /*'getMeta': 'getMeta', currently not supported */
  39. }
  40. },
  41. {
  42. 'collectionType': 'Pages',
  43. 'returnsModelType': 'page',
  44. 'supportsMethods': {
  45. 'getDate': 'getDate',
  46. 'getRevisions': 'getRevisions',
  47. 'getAuthorUser': 'getAuthorUser',
  48. 'getFeaturedMedia': 'getFeaturedMedia'
  49. }
  50. }
  51. ];
  52. _.each( collectionClassNames, function( className ) {
  53. QUnit.test( 'Testing ' + className + ' collection.', function( assert ) {
  54. var done = assert.async();
  55. wp.api.loadPromise.done( function() {
  56. var theCollection = new wp.api.collections[ className ]();
  57. assert.ok(
  58. theCollection,
  59. 'We can instantiate wp.api.collections.' + className
  60. );
  61. theCollection.fetch().done( function() {
  62. assert.equal(
  63. 1,
  64. theCollection.state.currentPage,
  65. 'We should be on page 1 of the collection in ' + className
  66. );
  67. // Should this collection have helper methods?
  68. var collectionHelperTest = _.findWhere( collectionHelperTests, { 'collectionType': className } );
  69. // If we found a match, run the tests against it.
  70. if ( ! _.isUndefined( collectionHelperTest ) ) {
  71. // Test the first returned model.
  72. var firstModel = theCollection.at( 0 );
  73. // Is the model the right type?
  74. assert.equal(
  75. collectionHelperTest.returnsModelType,
  76. firstModel.get( 'type' ),
  77. 'The wp.api.collections.' + className + ' is of type ' + collectionHelperTest.returnsModelType
  78. );
  79. // Does the model have all of the expected supported methods?
  80. _.each( collectionHelperTest.supportsMethods, function( method ) {
  81. assert.equal(
  82. 'function',
  83. typeof firstModel[ method ],
  84. className + '.' + method + ' is a function.'
  85. );
  86. } );
  87. }
  88. // Trigger Qunit async completion.
  89. done();
  90. } );
  91. } );
  92. } );
  93. } );
  94. // The list of models we should check.
  95. var modelsWithIdsClassNames = [
  96. 'Category',
  97. 'Media',
  98. 'Page',
  99. 'Post',
  100. 'Tag',
  101. 'User',
  102. 'UsersMe',
  103. 'Settings'
  104. ];
  105. _.each( modelsWithIdsClassNames, function( className ) {
  106. QUnit.test( 'Checking ' + className + ' model.', function( assert ) {
  107. var done = assert.async();
  108. assert.expect( 2 );
  109. wp.api.loadPromise.done( function() {
  110. var theModel = new wp.api.models[ className ]();
  111. assert.ok( theModel, 'We can instantiate wp.api.models.' + className );
  112. theModel.fetch().done( function( ) {
  113. var theModel2 = new wp.api.models[ className ]();
  114. theModel2.set( 'id', theModel.attributes.id );
  115. theModel2.fetch().done( function() {
  116. // We were able to retrieve the model.
  117. assert.equal(
  118. theModel.attributes.id,
  119. theModel2.get( 'id' ) ,
  120. 'We should be able to get a ' + className
  121. );
  122. // Trigger Qunit async completion.
  123. done();
  124. } );
  125. } );
  126. } );
  127. } );
  128. } );
  129. var modelsWithIndexes = [
  130. 'Taxonomy',
  131. 'Status',
  132. 'Type'
  133. ];
  134. _.each( modelsWithIndexes, function( className ) {
  135. QUnit.test( 'Testing ' + className + ' model.', function( assert ) {
  136. var done = assert.async();
  137. assert.expect( 2 );
  138. wp.api.loadPromise.done( function( ) {
  139. var theModel = new wp.api.models[ className ]();
  140. assert.ok( theModel, 'We can instantiate wp.api.models.' + className );
  141. theModel.fetch().done( function( ) {
  142. var theModel2 = new wp.api.models[ className ]();
  143. if ( ! _.isUndefined( theModel.attributes[0] ) ) {
  144. theModel2.set( 'id', theModel.attributes[0].id );
  145. }
  146. theModel2.fetch().done( function() {
  147. // We were able to retrieve the model.
  148. assert.notEqual(
  149. 0,
  150. _.keys( theModel2.attributes ).length ,
  151. 'We should be able to get a ' + className
  152. );
  153. // Trigger Qunit async completion.
  154. done();
  155. } );
  156. } );
  157. } );
  158. } );
  159. } );
  160. // Find models by route.
  161. var modelsToFetchByRoute = [
  162. 'Category',
  163. 'Comment',
  164. 'Media',
  165. 'Page',
  166. 'PageRevision',
  167. 'Post',
  168. 'PostRevision',
  169. 'Status',
  170. 'Tag',
  171. 'Taxonomy',
  172. 'Type',
  173. 'User'
  174. ];
  175. _.each( modelsToFetchByRoute, function( model ) {
  176. QUnit.test( 'Test fetching ' + model + ' by route.', function( assert ) {
  177. var done = assert.async();
  178. assert.expect( 1 );
  179. wp.api.loadPromise.done( function() {
  180. var theModel = wp.api.models[ model ];
  181. var route = theModel.prototype.route.index;
  182. assert.equal(
  183. wp.api.getModelByRoute( route ),
  184. theModel,
  185. 'wp.api.models.' + model + ' found at route ' + route
  186. );
  187. // Trigger Qunit async completion.
  188. done();
  189. } );
  190. } );
  191. } );
  192. // Find collections by route.
  193. var collectionsToFetchByRoute = [
  194. 'Categories',
  195. 'Comments',
  196. 'Media',
  197. 'PageRevisions',
  198. 'Pages',
  199. 'PostRevisions',
  200. 'Posts',
  201. 'Statuses',
  202. 'Tags',
  203. 'Taxonomies',
  204. 'Types',
  205. 'Users'
  206. ];
  207. _.each( collectionsToFetchByRoute, function( collection ) {
  208. QUnit.test( 'Test fetching ' + collection + ' by route.', function( assert ) {
  209. var done = assert.async();
  210. assert.expect( 1 );
  211. wp.api.loadPromise.done( function() {
  212. var theCollection = wp.api.collections[ collection ];
  213. var route = theCollection.prototype.route.index;
  214. assert.equal(
  215. wp.api.getCollectionByRoute( route ),
  216. theCollection,
  217. 'wp.api.collections.' + collection + ' found at ' + route
  218. );
  219. // Trigger Qunit async completion.
  220. done();
  221. } );
  222. } );
  223. } );
  224. // Test the jswidget custom namespace and endpoints.
  225. wp.api.init( {
  226. 'versionString': 'js-widgets/v1/'
  227. } ).done( function() {
  228. var customClasses = [
  229. 'WidgetsArchives',
  230. 'WidgetsCalendar',
  231. 'WidgetsCategories',
  232. 'WidgetsMeta',
  233. 'WidgetsNav_menu',
  234. 'WidgetsPages',
  235. 'WidgetsPostCollection',
  236. 'WidgetsRecentComments',
  237. 'WidgetsRecentPosts',
  238. 'WidgetsRss',
  239. 'WidgetsSearch',
  240. 'WidgetsTag_cloud',
  241. 'WidgetsText'
  242. ];
  243. // Check that we have and can get each model type.
  244. _.each( customClasses, function( className ) {
  245. QUnit.test( 'Checking ' + className + ' class name.', function( assert ) {
  246. var done = assert.async();
  247. assert.expect( 2 );
  248. wp.api.loadPromise.done( function() {
  249. var theModel = new wp.api.models[ className ]();
  250. assert.ok( theModel, 'We can instantiate wp.api.models.' + className );
  251. var theCollection = new wp.api.collections[ className ]();
  252. assert.ok( theCollection, 'We can instantiate wp.api.collections.' + className );
  253. // Trigger Qunit async completion.
  254. done();
  255. } );
  256. } );
  257. } );
  258. } );
  259. // Check connecting to a second URL.
  260. wp.api.loadPromise.done( function() {
  261. QUnit.test( 'Checking connecting to a remote url.', function( assert ) {
  262. var done = assert.async();
  263. wp.api.init({
  264. 'apiRoot': 'http://remotehost/wp-json/'
  265. } ).done( function(){
  266. var lastEndpoint = wp.api.endpoints.last(),
  267. models = lastEndpoint.get( 'models' ),
  268. post = new models.Post();
  269. assert.equal( 'http://remotehost/wp-json/wp/v2/posts', post.url(), 'The remote API objects should have their own URLs' );
  270. wp.api.init({
  271. 'apiRoot': 'http://localhost/wp-json/'
  272. } ).done( function(){
  273. var lastEndpoint = wp.api.endpoints.first(),
  274. models = lastEndpoint.get( 'models' ),
  275. post = new models.Post();
  276. assert.equal( 'http://localhost/wp-json/wp/v2/posts', post.url(), 'The local API objects should have their own URLs' );
  277. done();
  278. } );
  279. } );
  280. } );
  281. });
  282. // Test that models have the correct requireForceForDelete setting.
  283. var modelsThatNeedrequireForceForDelete = [
  284. { name: 'Category', expect: true },
  285. { name: 'Comment', expect: undefined },
  286. { name: 'Media', expect: undefined },
  287. { name: 'Page', expect: undefined },
  288. { name: 'PageRevision', expect: true },
  289. { name: 'Post', expect: undefined },
  290. { name: 'PostRevision', expect: true },
  291. { name: 'Status', expect: undefined },
  292. { name: 'Tag', expect: true },
  293. { name: 'Taxonomy', expect: undefined },
  294. { name: 'Type', expect: undefined },
  295. { name: 'User', expect: true }
  296. ];
  297. _.each( modelsThatNeedrequireForceForDelete, function( model ) {
  298. QUnit.test( 'Test requireForceForDelete is correct for ' + model.name, function( assert ) {
  299. var done = assert.async();
  300. assert.expect( 1 );
  301. wp.api.loadPromise.done( function() {
  302. // Instantiate the model.
  303. var theModel = new wp.api.models[ model.name ]();
  304. // Verify the model's requireForceForDelete is set as expected.
  305. assert.equal(
  306. theModel.requireForceForDelete,
  307. model.expect,
  308. 'wp.api.models.' + model.name + '.requireForceForDelete should be ' + model.expect + '.'
  309. );
  310. // Trigger Qunit async completion.
  311. done();
  312. } );
  313. } );
  314. } );
  315. var theModelTypesWithMeta = [
  316. 'Posts',
  317. 'Comments',
  318. 'Tags',
  319. 'Users'
  320. ];
  321. _.each( theModelTypesWithMeta, function( modelType ) {
  322. // Test post meta.
  323. wp.api.loadPromise.done( function() {
  324. QUnit.test( 'Check meta support for ' + modelType + '.', function( assert ) {
  325. var theModels = new wp.api.collections[ modelType ]();
  326. theModels.fetch().done( function() {
  327. // Get the main endpoint.
  328. var endpoint = theModels.at(0);
  329. var expectedMetas = '{"meta_key":"meta_value"}';
  330. if ( 'Tags' === modelType ) {
  331. expectedMetas = '{"test_single":"","test_multi":[],"meta_key":"meta_value","test_tag_meta":""}';
  332. }
  333. // Verify the meta object returned correctly from `getMetas()`.
  334. assert.equal( JSON.stringify( endpoint.getMetas() ), expectedMetas, 'Full meta key/values object should be readable.' );
  335. // Verify single meta returned correctly from `getMeta()`
  336. assert.equal( endpoint.getMeta( 'meta_key' ), 'meta_value', 'Single meta should be readable by key.' );
  337. // Verify setting meta values with `setMetas()`.
  338. endpoint.setMetas( { 'test_key':'test_value' } );
  339. assert.equal( endpoint.getMeta( 'test_key' ), 'test_value', 'Multiple meta should be writable via setMetas.' );
  340. // Verify setting a single meta value with `setMeta()`.
  341. endpoint.setMeta( 'test_key2', 'test_value2' );
  342. assert.equal( endpoint.getMeta( 'test_key2' ), 'test_value2', 'Single meta should be writable via setMeta.' );
  343. } );
  344. } );
  345. } );
  346. } );
  347. } )( window.QUnit );