123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import WaveSurfer from '../src/wavesurfer.js';
- /** @test {WaveSurfer} */
- describe('WaveSurfer/plugin API:', () => {
- jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
- let waveformDiv;
- let dummyPlugin;
- let wavesurfer;
- // clean up after each test
- afterEach(done => {
- wavesurfer.destroy();
- waveformDiv.parentNode.removeChild(waveformDiv);
- done();
- });
- // utility function to generate a mock plugin object
- function mockPlugin(name, deferInit = false) {
- class MockPlugin {
- constructor(params, ws) {
- this.ws = ws;
- // using the instance factory unfortunately makes it
- // difficult to use the spyOn function, so we use this
- // instead
- this.isInitialised = false;
- }
- init() {
- this.isInitialised = true;
- }
- destroy() { }
- }
- return {
- name,
- deferInit,
- staticProps: {
- [`${name}Static`]: 'static property value'
- },
- instance: MockPlugin
- };
- }
- // utility function to generate wavesurfer instances for testing
- function __createWaveform(options = {}) {
- waveformDiv = document.createElement('div');
- document.getElementsByTagName('body')[0].appendChild(waveformDiv);
- wavesurfer = WaveSurfer.create(Object.assign({
- container: waveformDiv
- }, options));
- wavesurfer.load('/base/spec/support/demo.wav');
- }
- // plugin methods
- /** @test {WaveSurfer#addPlugin} */
- it('addPlugin adds staticProps and correctly builds and instantiates plugin class', () => {
- dummyPlugin = mockPlugin('dummy');
- __createWaveform();
- wavesurfer.addPlugin(dummyPlugin);
- expect(wavesurfer.dummyStatic).toEqual(dummyPlugin.staticProps.dummyStatic);
- expect(wavesurfer.dummy.ws).toEqual(wavesurfer);
- expect(typeof Object.getPrototypeOf(wavesurfer.dummy).on).toEqual('function');
- });
- /** @test {WaveSurfer#initPlugin} */
- it('initPlugin calls init function of the plugin and adds its name to the initialisedPluginList', () => {
- dummyPlugin = mockPlugin('dummy');
- __createWaveform();
- wavesurfer.addPlugin(dummyPlugin);
- spyOn(wavesurfer.dummy, 'init');
- wavesurfer.initPlugin('dummy');
- expect(wavesurfer.dummy.init).toHaveBeenCalled();
- expect(wavesurfer.initialisedPluginList.dummy).toBeTrue();
- });
- /** @test {WaveSurfer#destroyPlugin} */
- it('destroyPlugin calls plugin destroy function and removes the plugin name from the initialisedPluginList', () => {
- dummyPlugin = mockPlugin('dummy');
- __createWaveform();
- wavesurfer.addPlugin(dummyPlugin);
- wavesurfer.initPlugin('dummy');
- spyOn(wavesurfer.dummy, 'destroy');
- wavesurfer.destroyPlugin('dummy');
- expect(wavesurfer.dummy.destroy).toHaveBeenCalled();
- expect(wavesurfer.initialisedPluginList.dummy).toBeUndefined();
- });
- // auto-adding and initialising of plugins (registerPlugins)
- /** @test {WaveSurfer#registerPlugins} */
- it('registerPlugin adds a plugin but does not call plugin init function if the plugin property deferInit is truethy', () => {
- dummyPlugin = mockPlugin('dummy', true);
- __createWaveform({
- plugins: [
- dummyPlugin
- ]
- });
- expect(wavesurfer.dummyStatic).toEqual(dummyPlugin.staticProps.dummyStatic);
- expect(wavesurfer.dummy.ws).toEqual(wavesurfer);
- expect(wavesurfer.dummy.isInitialised).toBeFalse();
- });
- /** @test {WaveSurfer#registerPlugins} */
- it('registerPlugin adds a plugin ands calls plugin init function if the plugin property deferInit is falsey', () => {
- dummyPlugin = mockPlugin('dummy');
- __createWaveform({
- plugins: [
- dummyPlugin
- ]
- });
- expect(wavesurfer.dummyStatic).toEqual(dummyPlugin.staticProps.dummyStatic);
- expect(wavesurfer.dummy.ws).toEqual(wavesurfer);
- expect(wavesurfer.dummy.isInitialised).toBeTrue();
- });
- });
|