utils_test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. suite('lunr.utils', function () {
  2. suite('#clone', function () {
  3. var subject = function (obj) {
  4. setup(function () {
  5. this.obj = obj
  6. this.clone = lunr.utils.clone(obj)
  7. })
  8. }
  9. suite('handles null', function () {
  10. subject(null)
  11. test('returns null', function () {
  12. assert.equal(null, this.clone)
  13. assert.equal(this.obj, this.clone)
  14. })
  15. })
  16. suite('handles undefined', function () {
  17. subject(undefined)
  18. test('returns null', function () {
  19. assert.equal(undefined, this.clone)
  20. assert.equal(this.obj, this.clone)
  21. })
  22. })
  23. suite('object with primatives', function () {
  24. subject({
  25. number: 1,
  26. string: 'foo',
  27. bool: true
  28. })
  29. test('clones number correctly', function () {
  30. assert.equal(this.obj.number, this.clone.number)
  31. })
  32. test('clones string correctly', function () {
  33. assert.equal(this.obj.string, this.clone.string)
  34. })
  35. test('clones bool correctly', function () {
  36. assert.equal(this.obj.bool, this.clone.bool)
  37. })
  38. })
  39. suite('object with array property', function () {
  40. subject({
  41. array: [1, 2, 3]
  42. })
  43. test('clones array correctly', function () {
  44. assert.deepEqual(this.obj.array, this.clone.array)
  45. })
  46. test('mutations on clone do not affect orginial', function () {
  47. this.clone.array.push(4)
  48. assert.notDeepEqual(this.obj.array, this.clone.array)
  49. assert.equal(this.obj.array.length, 3)
  50. assert.equal(this.clone.array.length, 4)
  51. })
  52. })
  53. suite('nested object', function () {
  54. test('throws type error', function () {
  55. assert.throws(function () {
  56. lunr.utils.clone({
  57. 'foo': {
  58. 'bar': 1
  59. }
  60. })
  61. }, TypeError)
  62. })
  63. })
  64. })
  65. })