wp-api.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* global mockedApiResponse, Backbone, jsWidgetsEndpointSchema */
  2. /**
  3. * @var mockedApiResponse defined in wp-api-generated.js
  4. */
  5. var pathToData = {
  6. 'wp-json/wp/v2/': mockedApiResponse.Schema,
  7. 'wp-json/wp/v2/categories': mockedApiResponse.CategoriesCollection,
  8. 'wp-json/wp/v2/comments': mockedApiResponse.CommentsCollection,
  9. 'wp-json/wp/v2/media': mockedApiResponse.MediaCollection,
  10. 'wp-json/wp/v2/pages': mockedApiResponse.PagesCollection,
  11. 'wp-json/wp/v2/posts': mockedApiResponse.PostsCollection,
  12. 'wp-json/wp/v2/statuses': mockedApiResponse.StatusesCollection,
  13. 'wp-json/wp/v2/tags': mockedApiResponse.TagsCollection,
  14. 'wp-json/wp/v2/taxonomies': mockedApiResponse.TaxonomiesCollection,
  15. 'wp-json/wp/v2/types': mockedApiResponse.TypesCollection,
  16. 'wp-json/wp/v2/users': mockedApiResponse.UsersCollection,
  17. 'wp-json/wp/v2/category': mockedApiResponse.CategoryModel,
  18. 'wp-json/wp/v2/media1': mockedApiResponse.MediaModel,
  19. 'wp-json/wp/v2/page': mockedApiResponse.PageModel,
  20. 'wp-json/wp/v2/post': mockedApiResponse.PostModel,
  21. 'wp-json/wp/v2/tag': mockedApiResponse.TagModel,
  22. 'wp-json/wp/v2/user': mockedApiResponse.UserModel,
  23. 'wp-json/wp/v2/taxonomy': mockedApiResponse.TaxonomyModel,
  24. 'wp-json/wp/v2/status': mockedApiResponse.StatusModel,
  25. 'wp-json/wp/v2/type': mockedApiResponse.TypeModel,
  26. 'wp-json/js-widgets/v1/': jsWidgetsEndpointSchema,
  27. 'wp-json/wp/v2/users/me': mockedApiResponse.me,
  28. 'wp-json/wp/v2/settings': mockedApiResponse.settings
  29. };
  30. /**
  31. * Mock the ajax callbacks for our tests.
  32. *
  33. * @param {object} param The parameters sent to the ajax request.
  34. *
  35. * @return {Object} A jQuery deferred object that resolves with the mapped data.
  36. */
  37. Backbone.ajax = function ( param ) {
  38. var data,
  39. request = param.url
  40. .replace( 'http://remotehost/', '' )
  41. .replace( 'http://localhost/', '' );
  42. if ( pathToData[ request ] ) {
  43. data = pathToData[ request ];
  44. }
  45. // Call success handler.
  46. param.success( data );
  47. var deferred = jQuery.Deferred();
  48. // Resolve the deferred with the mocked data.
  49. deferred.resolve( data );
  50. // Return the deferred promise that will resolve with the expected data.
  51. return deferred.promise();
  52. };