plugin-api.spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import WaveSurfer from '../src/wavesurfer.js';
  2. /** @test {WaveSurfer} */
  3. describe('WaveSurfer/plugin API:', () => {
  4. jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
  5. let waveformDiv;
  6. let dummyPlugin;
  7. let wavesurfer;
  8. // clean up after each test
  9. afterEach(done => {
  10. wavesurfer.destroy();
  11. waveformDiv.parentNode.removeChild(waveformDiv);
  12. done();
  13. });
  14. // utility function to generate a mock plugin object
  15. function mockPlugin(name, deferInit = false) {
  16. class MockPlugin {
  17. constructor(params, ws) {
  18. this.ws = ws;
  19. // using the instance factory unfortunately makes it
  20. // difficult to use the spyOn function, so we use this
  21. // instead
  22. this.isInitialised = false;
  23. }
  24. init() {
  25. this.isInitialised = true;
  26. }
  27. destroy() { }
  28. }
  29. return {
  30. name,
  31. deferInit,
  32. staticProps: {
  33. [`${name}Static`]: 'static property value'
  34. },
  35. instance: MockPlugin
  36. };
  37. }
  38. // utility function to generate wavesurfer instances for testing
  39. function __createWaveform(options = {}) {
  40. waveformDiv = document.createElement('div');
  41. document.getElementsByTagName('body')[0].appendChild(waveformDiv);
  42. wavesurfer = WaveSurfer.create(Object.assign({
  43. container: waveformDiv
  44. }, options));
  45. wavesurfer.load('/base/spec/support/demo.wav');
  46. }
  47. // plugin methods
  48. /** @test {WaveSurfer#addPlugin} */
  49. it('addPlugin adds staticProps and correctly builds and instantiates plugin class', () => {
  50. dummyPlugin = mockPlugin('dummy');
  51. __createWaveform();
  52. wavesurfer.addPlugin(dummyPlugin);
  53. expect(wavesurfer.dummyStatic).toEqual(dummyPlugin.staticProps.dummyStatic);
  54. expect(wavesurfer.dummy.ws).toEqual(wavesurfer);
  55. expect(typeof Object.getPrototypeOf(wavesurfer.dummy).on).toEqual('function');
  56. });
  57. /** @test {WaveSurfer#initPlugin} */
  58. it('initPlugin calls init function of the plugin and adds its name to the initialisedPluginList', () => {
  59. dummyPlugin = mockPlugin('dummy');
  60. __createWaveform();
  61. wavesurfer.addPlugin(dummyPlugin);
  62. spyOn(wavesurfer.dummy, 'init');
  63. wavesurfer.initPlugin('dummy');
  64. expect(wavesurfer.dummy.init).toHaveBeenCalled();
  65. expect(wavesurfer.initialisedPluginList.dummy).toBeTrue();
  66. });
  67. /** @test {WaveSurfer#destroyPlugin} */
  68. it('destroyPlugin calls plugin destroy function and removes the plugin name from the initialisedPluginList', () => {
  69. dummyPlugin = mockPlugin('dummy');
  70. __createWaveform();
  71. wavesurfer.addPlugin(dummyPlugin);
  72. wavesurfer.initPlugin('dummy');
  73. spyOn(wavesurfer.dummy, 'destroy');
  74. wavesurfer.destroyPlugin('dummy');
  75. expect(wavesurfer.dummy.destroy).toHaveBeenCalled();
  76. expect(wavesurfer.initialisedPluginList.dummy).toBeUndefined();
  77. });
  78. // auto-adding and initialising of plugins (registerPlugins)
  79. /** @test {WaveSurfer#registerPlugins} */
  80. it('registerPlugin adds a plugin but does not call plugin init function if the plugin property deferInit is truethy', () => {
  81. dummyPlugin = mockPlugin('dummy', true);
  82. __createWaveform({
  83. plugins: [
  84. dummyPlugin
  85. ]
  86. });
  87. expect(wavesurfer.dummyStatic).toEqual(dummyPlugin.staticProps.dummyStatic);
  88. expect(wavesurfer.dummy.ws).toEqual(wavesurfer);
  89. expect(wavesurfer.dummy.isInitialised).toBeFalse();
  90. });
  91. /** @test {WaveSurfer#registerPlugins} */
  92. it('registerPlugin adds a plugin ands calls plugin init function if the plugin property deferInit is falsey', () => {
  93. dummyPlugin = mockPlugin('dummy');
  94. __createWaveform({
  95. plugins: [
  96. dummyPlugin
  97. ]
  98. });
  99. expect(wavesurfer.dummyStatic).toEqual(dummyPlugin.staticProps.dummyStatic);
  100. expect(wavesurfer.dummy.ws).toEqual(wavesurfer);
  101. expect(wavesurfer.dummy.isInitialised).toBeTrue();
  102. });
  103. });