patch.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. var patch = require("../patch")
  3. exports["test patch delete"] = function(assert) {
  4. var hash = { a: 1, b: 2 }
  5. assert.deepEqual(patch(hash, { a: null }), { b: 2 }, "null removes property")
  6. }
  7. exports["test patch delete with void"] = function(assert) {
  8. var hash = { a: 1, b: 2 }
  9. assert.deepEqual(patch(hash, { a: void(0) }), { b: 2 },
  10. "void(0) removes property")
  11. }
  12. exports["test patch delete missing"] = function(assert) {
  13. assert.deepEqual(patch({ a: 1, b: 2 }, { c: null }),
  14. { a: 1, b: 2 },
  15. "null removes property if exists");
  16. assert.deepEqual(patch({ a: 1, b: 2 }, { c: void(0) }),
  17. { a: 1, b: 2 },
  18. "void removes property if exists");
  19. }
  20. exports["test delete deleted"] = function(assert) {
  21. assert.deepEqual(patch({ a: null, b: 2, c: 3, d: void(0)},
  22. { a: void(0), b: null, d: null }),
  23. {c: 3},
  24. "removed all existing and non existing");
  25. }
  26. exports["test update deleted"] = function(assert) {
  27. assert.deepEqual(patch({ a: null, b: void(0), c: 3},
  28. { a: { b: 2 } }),
  29. { a: { b: 2 }, c: 3 },
  30. "replace deleted");
  31. }
  32. exports["test patch delete with void"] = function(assert) {
  33. var hash = { a: 1, b: 2 }
  34. assert.deepEqual(patch(hash, { a: void(0) }), { b: 2 },
  35. "void(0) removes property")
  36. }
  37. exports["test patch addition"] = function(assert) {
  38. var hash = { a: 1, b: 2 }
  39. assert.deepEqual(patch(hash, { c: 3 }), { a: 1, b: 2, c: 3 },
  40. "new properties are added")
  41. }
  42. exports["test patch addition"] = function(assert) {
  43. var hash = { a: 1, b: 2 }
  44. assert.deepEqual(patch(hash, { c: 3 }), { a: 1, b: 2, c: 3 },
  45. "new properties are added")
  46. }
  47. exports["test hash on itself"] = function(assert) {
  48. var hash = { a: 1, b: 2 }
  49. assert.deepEqual(patch(hash, hash), hash,
  50. "applying hash to itself returns hash itself")
  51. }
  52. exports["test patch with empty delta"] = function(assert) {
  53. var hash = { a: 1, b: 2 }
  54. assert.deepEqual(patch(hash, {}), hash,
  55. "applying empty delta results in no changes")
  56. }
  57. exports["test patch nested data"] = function(assert) {
  58. assert.deepEqual(patch({ a: { b: 1 }, c: { d: 2 } },
  59. { a: { b: null, e: 3 }, c: { d: 4 } }),
  60. { a: { e: 3 }, c: { d: 4 } },
  61. "nested structures can also be patched")
  62. }