module-collection.spec.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import ModuleCollection from '@/module/module-collection'
  2. describe('ModuleCollection', () => {
  3. it('get', () => {
  4. const collection = new ModuleCollection({
  5. state: { value: 1 },
  6. modules: {
  7. a: {
  8. state: { value: 2 }
  9. },
  10. b: {
  11. state: { value: 3 },
  12. modules: {
  13. c: {
  14. state: { value: 4 }
  15. }
  16. }
  17. }
  18. }
  19. })
  20. expect(collection.get([]).state.value).toBe(1)
  21. expect(collection.get(['a']).state.value).toBe(2)
  22. expect(collection.get(['b']).state.value).toBe(3)
  23. expect(collection.get(['b', 'c']).state.value).toBe(4)
  24. })
  25. it('getNamespace', () => {
  26. const module = (namespaced, children) => {
  27. return {
  28. namespaced,
  29. modules: children
  30. }
  31. }
  32. const collection = new ModuleCollection({
  33. namespace: 'ignore/', // root module namespace should be ignored
  34. modules: {
  35. a: module(true, {
  36. b: module(false, {
  37. c: module(true)
  38. }),
  39. d: module(true)
  40. })
  41. }
  42. })
  43. const check = (path, expected) => {
  44. const type = 'test'
  45. const namespace = collection.getNamespace(path)
  46. expect(namespace + type).toBe(expected)
  47. }
  48. check(['a'], 'a/test')
  49. check(['a', 'b'], 'a/test')
  50. check(['a', 'b', 'c'], 'a/c/test')
  51. check(['a', 'd'], 'a/d/test')
  52. })
  53. it('register', () => {
  54. const collection = new ModuleCollection({})
  55. collection.register(['a'], {
  56. state: { value: 1 }
  57. })
  58. collection.register(['b'], {
  59. state: { value: 2 }
  60. })
  61. collection.register(['a', 'b'], {
  62. state: { value: 3 }
  63. })
  64. expect(collection.get(['a']).state.value).toBe(1)
  65. expect(collection.get(['b']).state.value).toBe(2)
  66. expect(collection.get(['a', 'b']).state.value).toBe(3)
  67. })
  68. it('unregister', () => {
  69. const collection = new ModuleCollection({})
  70. collection.register(['a'], {
  71. state: { value: true }
  72. })
  73. expect(collection.get(['a']).state.value).toBe(true)
  74. collection.unregister(['a'])
  75. expect(collection.get(['a'])).toBe(undefined)
  76. })
  77. it('does not unregister initial modules', () => {
  78. const collection = new ModuleCollection({
  79. modules: {
  80. a: {
  81. state: { value: true }
  82. }
  83. }
  84. })
  85. collection.unregister(['a'])
  86. expect(collection.get(['a']).state.value).toBe(true)
  87. })
  88. it('warns when unregistering non existing module', () => {
  89. const spy = jest.spyOn(console, 'warn').mockImplementation()
  90. const collection = new ModuleCollection({})
  91. collection.unregister(['a'])
  92. expect(spy).toHaveBeenCalled()
  93. })
  94. })