Make WordPress Core

Ticket #41111: 41111.diff

File 41111.diff, 3.0 KB (added by rcutmore, 8 years ago)

Add helper methods and unit tests

  • src/wp-includes/js/wp-api.js

    diff --git src/wp-includes/js/wp-api.js src/wp-includes/js/wp-api.js
    index 634db43..35997cf 100644
     
    14021402        // The wp.api.init function returns a promise that will resolve with the endpoint once it is ready.
    14031403        wp.api.loadPromise = wp.api.init();
    14041404
     1405        /**
     1406         * Determine model based on API route.
     1407         *
     1408         * @param {string} route    The API route.
     1409         *
     1410         * @return {Backbone Model} The model found at given route.
     1411         */
     1412        wp.api.getModelByRoute = function( route ) {
     1413                return _.first( _.filter( wp.api.models, function( model ) {
     1414                        return model.prototype.route && route === model.prototype.route.index;
     1415                } ) );
     1416        };
     1417
     1418        /**
     1419         * Determine collection based on API route.
     1420         *
     1421         * @param {string} route    The API route.
     1422         *
     1423         * @return {Backbone Model} The collection found at given route.
     1424         */
     1425        wp.api.getCollectionByRoute = function( route ) {
     1426                return _.first( _.filter( wp.api.collections, function( collection ) {
     1427                        return collection.prototype.route && route === collection.prototype.route.index;
     1428                } ) );
     1429        };
     1430
    14051431} )();
  • tests/qunit/wp-includes/js/wp-api.js

    diff --git tests/qunit/wp-includes/js/wp-api.js tests/qunit/wp-includes/js/wp-api.js
    index a64e406..e2658ff 100644
     
    194194                } );
    195195        } );
    196196
     197        // Find models by route.
     198        var modelsToFetchByRoute = [
     199                'Category',
     200                'Comment',
     201                'Media',
     202                'Page',
     203                'PageRevision',
     204                'Post',
     205                'PostRevision',
     206                'Status',
     207                'Tag',
     208                'Taxonomy',
     209                'Type',
     210                'User',
     211        ];
     212
     213        _.each( modelsToFetchByRoute, function( model ) {
     214                QUnit.test( 'Test fetching ' + model + ' by route.', function( assert ) {
     215
     216                        var done = assert.async();
     217
     218                        assert.expect( 1 );
     219
     220                        wp.api.loadPromise.done( function() {
     221
     222                                var theModel = wp.api.models[ model ];
     223                                var route = theModel.prototype.route.index;
     224
     225                                assert.equal(
     226                                        wp.api.getModelByRoute( route ),
     227                                        theModel,
     228                                        'wp.api.models.' + model + ' found at route ' + route
     229                                );
     230
     231                                // Trigger Qunit async completion.
     232                                done();
     233                        } );
     234                } );
     235        } );
     236
     237        // Find collections by route.
     238        var collectionsToFetchByRoute = [
     239                'Categories',
     240                'Comments',
     241                'Media',
     242                'PageRevisions',
     243                'Pages',
     244                'PostRevisions',
     245                'Posts',
     246                'Statuses',
     247                'Tags',
     248                'Taxonomies',
     249                'Types',
     250                'Users',
     251        ];
     252
     253        _.each( collectionsToFetchByRoute, function( collection ) {
     254                QUnit.test( 'Test fetching ' + collection + ' by route.', function( assert ) {
     255
     256                        var done = assert.async();
     257
     258                        assert.expect( 1 );
     259
     260                        wp.api.loadPromise.done( function() {
     261
     262                                var theCollection = wp.api.collections[ collection ];
     263                                var route = theCollection.prototype.route.index;
     264
     265                                assert.equal(
     266                                        wp.api.getCollectionByRoute( route ),
     267                                        theCollection,
     268                                        'wp.api.collections.' + collection + ' found at ' + route
     269                                );
     270
     271                                // Trigger Qunit async completion.
     272                                done();
     273                        } );
     274                } );
     275        } );
     276
    197277        // Test the jswidget custom namespace and endpoints.
    198278        wp.api.init( {
    199279                'versionString': 'js-widgets/v1/'