Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

116 wiersze
3.3 KiB

  1. import brcast from './index'
  2. test('default export is a function', () => {
  3. expect(typeof brcast).toBe('function')
  4. })
  5. test('exposes the public API', () => {
  6. const broadcast = brcast()
  7. const methods = Object.keys(broadcast)
  8. expect(methods.length).toBe(4)
  9. expect(methods).toContain('subscribe')
  10. expect(methods).toContain('unsubscribe')
  11. expect(methods).toContain('getState')
  12. expect(methods).toContain('setState')
  13. })
  14. test('throws if listener is not a function', () => {
  15. const broadcast = brcast()
  16. expect(() => broadcast.subscribe()).toThrow()
  17. expect(() => broadcast.subscribe('throw')).toThrow()
  18. expect(() => broadcast.subscribe({})).toThrow()
  19. expect(() => broadcast.subscribe(() => {})).not.toThrow()
  20. })
  21. test('is able to start with an undefined state and update it accordingly', () => {
  22. const broadcast = brcast()
  23. expect(broadcast.getState()).toBeUndefined()
  24. broadcast.setState(2)
  25. expect(broadcast.getState()).toBe(2)
  26. })
  27. test('it updates the state', () => {
  28. const handler = jest.fn()
  29. const broadcast = brcast()
  30. broadcast.subscribe(handler)
  31. broadcast.setState(2)
  32. expect(handler.mock.calls.length).toBe(1)
  33. expect(handler.mock.calls[0][0]).toBe(2)
  34. })
  35. test('it unsubscribes only relevant listeners', () => {
  36. const handler = jest.fn()
  37. const handler1 = jest.fn()
  38. const broadcast = brcast(1)
  39. const subscriptionId = broadcast.subscribe(handler)
  40. broadcast.subscribe(handler1)
  41. broadcast.unsubscribe(subscriptionId)
  42. broadcast.setState(2)
  43. broadcast.setState(3)
  44. expect(handler.mock.calls.length).toBe(0)
  45. expect(handler1.mock.calls.length).toBe(2)
  46. })
  47. test('removes listeners only once when unsubscribing more than once', () => {
  48. const handler = jest.fn()
  49. const broadcast = brcast(1)
  50. const subscriptionId = broadcast.subscribe(handler)
  51. broadcast.unsubscribe(subscriptionId)
  52. broadcast.unsubscribe(subscriptionId)
  53. broadcast.setState(2)
  54. expect(handler.mock.calls.length).toBe(0)
  55. })
  56. test('supports removing a subscription within a subscription', () => {
  57. const broadcast = brcast(1)
  58. const handler = jest.fn()
  59. const handler1 = jest.fn()
  60. const handler2 = jest.fn()
  61. broadcast.subscribe(handler)
  62. const sub1Id = broadcast.subscribe(() => {
  63. handler1()
  64. broadcast.unsubscribe(sub1Id)
  65. })
  66. broadcast.subscribe(handler2)
  67. broadcast.setState(2)
  68. broadcast.setState(3)
  69. expect(handler.mock.calls.length).toBe(2)
  70. expect(handler1.mock.calls.length).toBe(1)
  71. expect(handler2.mock.calls.length).toBe(2)
  72. })
  73. test('do not notify subscribers getting unsubscribed in the middle of a setState', () => {
  74. const broadcast = brcast()
  75. const unsubscribeIds = []
  76. const doUnsubscribeAll = () =>
  77. unsubscribeIds.forEach(id => broadcast.unsubscribe(id))
  78. const handler = jest.fn()
  79. const handler1 = jest.fn()
  80. const handler2 = jest.fn()
  81. unsubscribeIds.push(broadcast.subscribe(handler))
  82. unsubscribeIds.push(
  83. broadcast.subscribe(() => {
  84. handler1()
  85. doUnsubscribeAll()
  86. })
  87. )
  88. unsubscribeIds.push(broadcast.subscribe(handler2))
  89. broadcast.setState(2)
  90. expect(handler.mock.calls.length).toBe(1)
  91. expect(handler1.mock.calls.length).toBe(1)
  92. expect(handler2.mock.calls.length).toBe(0)
  93. broadcast.setState(3)
  94. expect(handler.mock.calls.length).toBe(1)
  95. expect(handler1.mock.calls.length).toBe(1)
  96. expect(handler2.mock.calls.length).toBe(0)
  97. })