AddScriptCommand.tests.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. QUnit.module( "AddScriptCommand" );
  2. QUnit.test( "Test AddScriptCommand (Undo and Redo)", function( assert ) {
  3. var editor = new Editor();
  4. // prepare
  5. var box = aBox( "The scripted box" );
  6. var sphere = aSphere( "The scripted sphere" );
  7. var objects = [ box, sphere ];
  8. var xMove = { name: "", source: "function update( event ) { this.position.x = this.position.x + 1; }" };
  9. var yMove = { name: "", source: "function update( event ) { this.position.y = this.position.y + 1; }" };
  10. var scripts = [ xMove, yMove ];
  11. // add objects to editor
  12. objects.map( function( item ) {
  13. editor.execute( new AddObjectCommand( item ) );
  14. } );
  15. assert.ok( editor.scene.children.length == 2, "OK, the box and the sphere have been added" );
  16. // add scripts to the objects
  17. for ( var i = 0; i < scripts.length; i ++ ) {
  18. var cmd = new AddScriptCommand( objects[ i ], scripts[ i ] );
  19. cmd.updatable = false;
  20. editor.execute( cmd );
  21. }
  22. var scriptsKeys = Object.keys( editor.scripts );
  23. assert.ok( getScriptCount( editor ) == scripts.length, "OK, correct number of scripts have been added" );
  24. for ( var i = 0; i < objects.length; i ++ ) {
  25. assert.ok( objects[ i ].uuid == scriptsKeys[ i ], "OK, script key #" + i + " matches the object's UUID" );
  26. }
  27. editor.undo();
  28. assert.ok( getScriptCount( editor ) == scripts.length - 1, "OK, one script has been removed by undo" );
  29. editor.redo();
  30. assert.ok( getScriptCount( editor ) == scripts.length, "OK, one script has been added again by redo" );
  31. for ( var i = 0; i < scriptsKeys.length; i ++ ) {
  32. assert.ok( editor.scripts[ scriptsKeys[ i ] ][ 0 ] == scripts[ i ], "OK, script #" + i + " is still assigned correctly" );
  33. }
  34. } );