module-collection.spec.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import ModuleCollection from '../../../src/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. })