capture_change_apply_elsewhere.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*jshint indent:2, laxcomma:true, laxbreak:true*/
  2. var util = require('util')
  3. , assert = require('assert')
  4. , diff = require('..')
  5. , data = require('./practice-data')
  6. ;
  7. var i = Math.floor(Math.random() * data.length) + 1;
  8. var j = Math.floor(Math.random() * data.length) + 1;
  9. while (j === i) {
  10. j = Math.floor(Math.random() * data.length) + 1;
  11. }
  12. var source = data[i];
  13. var comparand = data[j];
  14. // source and comparand are different objects
  15. assert.notEqual(source, comparand);
  16. // source and comparand have differences in their structure
  17. assert.notDeepEqual(source, comparand);
  18. // record the differences between source and comparand
  19. var changes = diff(source, comparand);
  20. // apply the changes to the source
  21. changes.forEach(function (change) {
  22. diff.applyChange(source, true, change);
  23. });
  24. // source and copmarand are now deep equal
  25. assert.deepEqual(source, comparand);
  26. // Simulate serializing to a remote copy of the object (we've already go a copy, copy the changes)...
  27. var remote = JSON.parse(JSON.stringify(source));
  28. var remoteChanges = JSON.parse(JSON.stringify(changes));
  29. // source and remote are different objects
  30. assert.notEqual(source, remote);
  31. // changes and remote changes are different objects
  32. assert.notEqual(changes, remoteChanges);
  33. // remote and comparand are different objects
  34. assert.notEqual(remote, comparand);
  35. remoteChanges.forEach(function (change) {
  36. diff.applyChange(remote, true, change);
  37. });
  38. assert.deepEqual(remote, comparand);