RemoveScriptCommand.tests.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. QUnit.module( "RemoveScriptCommand" );
  2. QUnit.test( "Test RemoveScriptCommand (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. for ( var i = 0; i < scripts.length; i ++ ) {
  23. var cmd = new RemoveScriptCommand( objects[ i ], scripts[ i ] );
  24. cmd.updatable = false;
  25. editor.execute( cmd );
  26. }
  27. assert.ok( getScriptCount( editor ) == 0, "OK, all scripts have been removed" );
  28. scripts.map( function() {
  29. editor.undo();
  30. } );
  31. assert.ok( getScriptCount( editor ) == scripts.length, "OK, all scripts have been added again by undo(s)" );
  32. var scriptsKeys = Object.keys( editor.scripts );
  33. for ( var i = 0; i < scriptsKeys.length; i ++ ) {
  34. assert.ok( editor.scripts[ scriptsKeys[ i ] ][ 0 ] == scripts[ i ], "OK, script #" + i + " is still assigned correctly" );
  35. }
  36. editor.redo();
  37. assert.ok( getScriptCount( editor ) == scripts.length - 1, "OK, one script has been removed again by redo" );
  38. } );