config.spec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import Config from '../../../src/util/config.js'
  2. import { clearFixture, getFixture } from '../../helpers/fixture.js'
  3. class DummyConfigClass extends Config {
  4. static get NAME() {
  5. return 'dummy'
  6. }
  7. }
  8. describe('Config', () => {
  9. let fixtureEl
  10. const name = 'dummy'
  11. beforeAll(() => {
  12. fixtureEl = getFixture()
  13. })
  14. afterEach(() => {
  15. clearFixture()
  16. })
  17. describe('NAME', () => {
  18. it('should return plugin NAME', () => {
  19. expect(DummyConfigClass.NAME).toEqual(name)
  20. })
  21. })
  22. describe('DefaultType', () => {
  23. it('should return plugin default type', () => {
  24. expect(DummyConfigClass.DefaultType).toEqual(jasmine.any(Object))
  25. })
  26. })
  27. describe('Default', () => {
  28. it('should return plugin defaults', () => {
  29. expect(DummyConfigClass.Default).toEqual(jasmine.any(Object))
  30. })
  31. })
  32. describe('mergeConfigObj', () => {
  33. it('should parse element\'s data attributes and merge it with default config. Element\'s data attributes must excel Defaults', () => {
  34. fixtureEl.innerHTML = '<div id="test" data-bs-test-bool="false" data-bs-test-int="8" data-bs-test-string1="bar"></div>'
  35. spyOnProperty(DummyConfigClass, 'Default', 'get').and.returnValue({
  36. testBool: true,
  37. testString: 'foo',
  38. testString1: 'foo',
  39. testInt: 7
  40. })
  41. const instance = new DummyConfigClass()
  42. const configResult = instance._mergeConfigObj({}, fixtureEl.querySelector('#test'))
  43. expect(configResult.testBool).toEqual(false)
  44. expect(configResult.testString).toEqual('foo')
  45. expect(configResult.testString1).toEqual('bar')
  46. expect(configResult.testInt).toEqual(8)
  47. })
  48. it('should parse element\'s data attributes and merge it with default config, plug these given during method call. The programmatically given should excel all', () => {
  49. fixtureEl.innerHTML = '<div id="test" data-bs-test-bool="false" data-bs-test-int="8" data-bs-test-string-1="bar"></div>'
  50. spyOnProperty(DummyConfigClass, 'Default', 'get').and.returnValue({
  51. testBool: true,
  52. testString: 'foo',
  53. testString1: 'foo',
  54. testInt: 7
  55. })
  56. const instance = new DummyConfigClass()
  57. const configResult = instance._mergeConfigObj({
  58. testString1: 'test',
  59. testInt: 3
  60. }, fixtureEl.querySelector('#test'))
  61. expect(configResult.testBool).toEqual(false)
  62. expect(configResult.testString).toEqual('foo')
  63. expect(configResult.testString1).toEqual('test')
  64. expect(configResult.testInt).toEqual(3)
  65. })
  66. it('should parse element\'s data attribute `config` and any rest attributes. The programmatically given should excel all. Data attribute `config` should excel only Defaults', () => {
  67. fixtureEl.innerHTML = '<div id="test" data-bs-config=\'{"testBool":false,"testInt":50,"testInt2":100}\' data-bs-test-int="8" data-bs-test-string-1="bar"></div>'
  68. spyOnProperty(DummyConfigClass, 'Default', 'get').and.returnValue({
  69. testBool: true,
  70. testString: 'foo',
  71. testString1: 'foo',
  72. testInt: 7,
  73. testInt2: 600
  74. })
  75. const instance = new DummyConfigClass()
  76. const configResult = instance._mergeConfigObj({
  77. testString1: 'test'
  78. }, fixtureEl.querySelector('#test'))
  79. expect(configResult.testBool).toEqual(false)
  80. expect(configResult.testString).toEqual('foo')
  81. expect(configResult.testString1).toEqual('test')
  82. expect(configResult.testInt).toEqual(8)
  83. expect(configResult.testInt2).toEqual(100)
  84. })
  85. it('should omit element\'s data attribute `config` if is not an object', () => {
  86. fixtureEl.innerHTML = '<div id="test" data-bs-config="foo" data-bs-test-int="8"></div>'
  87. spyOnProperty(DummyConfigClass, 'Default', 'get').and.returnValue({
  88. testInt: 7,
  89. testInt2: 79
  90. })
  91. const instance = new DummyConfigClass()
  92. const configResult = instance._mergeConfigObj({}, fixtureEl.querySelector('#test'))
  93. expect(configResult.testInt).toEqual(8)
  94. expect(configResult.testInt2).toEqual(79)
  95. })
  96. })
  97. describe('typeCheckConfig', () => {
  98. it('should check type of the config object', () => {
  99. spyOnProperty(DummyConfigClass, 'DefaultType', 'get').and.returnValue({
  100. toggle: 'boolean',
  101. parent: '(string|element)'
  102. })
  103. const config = {
  104. toggle: true,
  105. parent: 777
  106. }
  107. const obj = new DummyConfigClass()
  108. expect(() => {
  109. obj._typeCheckConfig(config)
  110. }).toThrowError(TypeError, `${obj.constructor.NAME.toUpperCase()}: Option "parent" provided type "number" but expected type "(string|element)".`)
  111. })
  112. it('should return null stringified when null is passed', () => {
  113. spyOnProperty(DummyConfigClass, 'DefaultType', 'get').and.returnValue({
  114. toggle: 'boolean',
  115. parent: '(null|element)'
  116. })
  117. const obj = new DummyConfigClass()
  118. const config = {
  119. toggle: true,
  120. parent: null
  121. }
  122. obj._typeCheckConfig(config)
  123. expect().nothing()
  124. })
  125. it('should return undefined stringified when undefined is passed', () => {
  126. spyOnProperty(DummyConfigClass, 'DefaultType', 'get').and.returnValue({
  127. toggle: 'boolean',
  128. parent: '(undefined|element)'
  129. })
  130. const obj = new DummyConfigClass()
  131. const config = {
  132. toggle: true,
  133. parent: undefined
  134. }
  135. obj._typeCheckConfig(config)
  136. expect().nothing()
  137. })
  138. })
  139. })