You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

List.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* globals describe, expect, it, jasmine */
  2. import { List } from '../../../src/main.js'
  3. const { any, createSpy, objectContaining } = jasmine
  4. describe('List.js', () => {
  5. describe('()', () => {
  6. it('creates a new List from Array', () => {
  7. const list = new List([1, 2, 3])
  8. expect(list).toEqual(any(List))
  9. })
  10. it('creates preallocated List like Array(1)', () => {
  11. const list = new List(1)
  12. expect(list.length).toBe(1)
  13. })
  14. it('is instance of Array', () => {
  15. const list = new List([1, 2, 3])
  16. expect(list).toEqual(any(Array))
  17. })
  18. it('allows index access', () => {
  19. const list = new List([1, 2, 3])
  20. expect(list[1]).toBe(2)
  21. })
  22. })
  23. describe('each()', () => {
  24. it('works like map but with context set to the element when a function is passed', () => {
  25. const list = new List([1, 2, 3]).each((el) => el * 2)
  26. expect(list).toEqual(any(List))
  27. expect(list).toEqual([2, 4, 6])
  28. const spy = createSpy()
  29. const obj = {}
  30. const list2 = new List([obj])
  31. list2.each(spy)
  32. expect(spy.calls.first()).toEqual(
  33. objectContaining({
  34. object: obj,
  35. args: [obj, 0, list2]
  36. })
  37. )
  38. })
  39. it('calls a method on every element in the list and passes arguments when a string is passed', () => {
  40. const list = new List([10, 11, 12])
  41. expect(list.each('toString', 16)).toEqual(['a', 'b', 'c'])
  42. })
  43. })
  44. describe('toArray()', () => {
  45. it('returns a plain array from the contents of the list', () => {
  46. const list = new List([1, 2, 3])
  47. const arr = list.toArray()
  48. expect(arr).toEqual(any(Array))
  49. expect(arr).not.toEqual(any(List))
  50. expect(arr).toEqual([1, 2, 3])
  51. })
  52. })
  53. describe('static extend()', () => {
  54. it('adds new method names to the List', () => {
  55. List.extend(['fooBar'])
  56. expect(new List().fooBar).toEqual(any(Function))
  57. const obj = { fooBar: createSpy() }
  58. new List([obj]).fooBar()
  59. expect(obj.fooBar).toHaveBeenCalled()
  60. delete List.prototype.fooBar
  61. })
  62. it('keeps Array prototype names prefixed with $', () => {
  63. // We're picking a function that we know isn't part of core svg.js
  64. // If we implement an 'unshift' function at some point, change this to something else
  65. if (List.prototype.hasOwnProperty('unshift')) {
  66. fail('List.unshift is already a function - change this test to use a different name!');
  67. return;
  68. }
  69. List.extend([ 'unshift' ])
  70. expect(new List().unshift).toEqual(any(Function))
  71. expect(new List().$unshift).toEqual(Array.prototype.unshift)
  72. // Check that it works!
  73. const sourceArray = [
  74. { 'unshift': () => 1 },
  75. { 'unshift': () => 2 },
  76. { 'unshift': () => 3 }
  77. ];
  78. const list = new List(sourceArray)
  79. expect(list).toEqual(sourceArray)
  80. expect(list.unshift(0)).toEqual([1,2,3])
  81. expect(list.$unshift(0)).toEqual(4)
  82. expect(list).toEqual([0].concat(sourceArray))
  83. delete List.prototype.unshift;
  84. });
  85. it('skips reserved names', () => {
  86. const { constructor, each, toArray } = List.prototype
  87. List.extend(['constructor', 'each', 'toArray'])
  88. expect(List.prototype).toEqual(
  89. objectContaining({ constructor, each, toArray })
  90. )
  91. })
  92. it('skips private methods starting with an underscore', () => {
  93. List.extend(['_private'])
  94. expect(new List()._private).toBe(undefined)
  95. })
  96. })
  97. })