| 1 | /* global wp */ |
| 2 | ( function( QUnit ) { |
| 3 | module( 'wpapi' ); |
| 4 | |
| 5 | QUnit.test( 'API Loaded correctly', function( assert ) { |
| 6 | var done = assert.async(); |
| 7 | assert.expect( 2 ); |
| 8 | |
| 9 | assert.ok( wp.api.loadPromise ); |
| 10 | |
| 11 | wp.api.init(); |
| 12 | wp.api.loadPromise.done( function() { |
| 13 | assert.ok( wp.api.models ); |
| 14 | done(); |
| 15 | } ); |
| 16 | |
| 17 | } ); |
| 18 | |
| 19 | |
| 20 | // Verify collections loaded. |
| 21 | var collectionClassNames = [ |
| 22 | 'Categories', |
| 23 | 'Comments', |
| 24 | 'Media', |
| 25 | 'Pages', |
| 26 | 'Posts', |
| 27 | 'Statuses', |
| 28 | 'Tags', |
| 29 | 'Taxonomies', |
| 30 | 'Types', |
| 31 | 'Users' |
| 32 | ]; |
| 33 | |
| 34 | _.each( collectionClassNames, function( className ) { |
| 35 | QUnit.test( 'Testing ' + className + ' collection.', function( assert ) { |
| 36 | var done = assert.async(); |
| 37 | |
| 38 | assert.expect( 2 ); |
| 39 | |
| 40 | wp.api.loadPromise.done( function() { |
| 41 | var theCollection = new wp.api.collections[ className ](); |
| 42 | assert.ok( theCollection, 'We can instantiate wp.api.collections.' + className ); |
| 43 | theCollection.fetch().done( function() { |
| 44 | assert.equal( 1, theCollection.state.currentPage, 'We should be on page 1 of the collection in ' + className ); |
| 45 | done(); |
| 46 | } ); |
| 47 | |
| 48 | } ); |
| 49 | |
| 50 | }); |
| 51 | } ); |
| 52 | |
| 53 | var modelsWithIdsClassNames = |
| 54 | [ |
| 55 | 'Category', |
| 56 | 'Media', |
| 57 | 'Page', |
| 58 | 'Post', |
| 59 | 'Tag', |
| 60 | 'User' |
| 61 | ]; |
| 62 | |
| 63 | |
| 64 | _.each( modelsWithIdsClassNames, function( className ) { |
| 65 | |
| 66 | QUnit.test( 'Checking ' + className + ' model.' , function( assert ) { |
| 67 | var done = assert.async(); |
| 68 | |
| 69 | assert.expect( 2 ); |
| 70 | |
| 71 | wp.api.loadPromise.done( function() { |
| 72 | var theModel = new wp.api.models[ className ](); |
| 73 | assert.ok( theModel, 'We can instantiate wp.api.models.' + className ); |
| 74 | theModel.fetch().done( function() { |
| 75 | var theModel2 = new wp.api.models[ className ](); |
| 76 | theModel2.set( 'id', theModel.attributes[0].id ); |
| 77 | theModel2.fetch().done( function() { |
| 78 | assert.equal( theModel.attributes[0].id, theModel2.get( 'id' ) , 'We should be able to get a ' + className ); |
| 79 | done(); |
| 80 | } ); |
| 81 | } ); |
| 82 | |
| 83 | } ); |
| 84 | |
| 85 | }); |
| 86 | } ); |
| 87 | |
| 88 | var modelsWithIndexes = |
| 89 | [ |
| 90 | 'Taxonomy', |
| 91 | 'Status', |
| 92 | 'Type' |
| 93 | ]; |
| 94 | |
| 95 | _.each( modelsWithIndexes, function( className ) { |
| 96 | |
| 97 | QUnit.test( 'Testing ' + className + ' model.' , function( assert ) { |
| 98 | var done = assert.async(); |
| 99 | |
| 100 | assert.expect( 2 ); |
| 101 | |
| 102 | wp.api.loadPromise.done( function() { |
| 103 | var theModel = new wp.api.models[ className ](); |
| 104 | assert.ok( theModel, 'We can instantiate wp.api.models.' + className ); |
| 105 | theModel.fetch().done( function() { |
| 106 | var theModel2 = new wp.api.models[ className ](); |
| 107 | |
| 108 | if ( ! _.isUndefined( theModel.attributes[0] ) ) { |
| 109 | theModel2.set( 'id', theModel.attributes[0].id ); |
| 110 | } |
| 111 | |
| 112 | theModel2.fetch().done( function() { |
| 113 | assert.notEqual( 0, _.keys( theModel2.attributes ).length , 'We should be able to get a ' + className ); |
| 114 | done(); |
| 115 | } ); |
| 116 | } ); |
| 117 | |
| 118 | } ); |
| 119 | |
| 120 | }); |
| 121 | } ); |
| 122 | |
| 123 | } )( window.QUnit ); |