Skip to content

Commit

Permalink
fix: test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandepass committed Dec 29, 2023
1 parent 4cbd0f8 commit 25806ff
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
39 changes: 39 additions & 0 deletions test/wtr/app/store/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
} from '../../fixtures/helix-admin.js';
import { mockFetchEnglishMessagesSuccess } from '../../fixtures/i18n.js';
import { defaultSidekickConfig } from '../../fixtures/stubs/sidekick-config.js';
import { EventBus } from '../../../../src/extension/app/utils/event-bus.js';
import { EVENTS, MODALS } from '../../../../src/extension/app/constants.js';

// @ts-ignore
window.chrome = chromeMock;
Expand Down Expand Up @@ -333,4 +335,41 @@ describe('Test App Store', () => {
expect(appStore.status.error).to.equal('error_status_500');
});
});

describe('wait dialog', async () => {
it('showWait()', async () => {
const callback = sinon.spy();
const eventBus = EventBus.instance;
eventBus.addEventListener(EVENTS.OPEN_MODAL, callback);
appStore.showWait('test');
expect(callback.calledOnce).to.be.true;
expect(callback.args[0][0].detail).to.deep.equal({
type: MODALS.WAIT,
data: { message: 'test' },
});
});

it('hideWait()', async () => {
const callback = sinon.spy();
const eventBus = EventBus.instance;
eventBus.addEventListener(EVENTS.CLOSE_MODAL, callback);
appStore.hideWait();
expect(callback.calledOnce).to.be.true;
});
});

describe('show toast', async () => {
it('showWait()', async () => {
const callback = sinon.spy();
const eventBus = EventBus.instance;
eventBus.addEventListener(EVENTS.SHOW_TOAST, callback);
appStore.showToast('test');
expect(callback.calledOnce).to.be.true;
expect(callback.args[0][0].detail).to.deep.equal({
message: 'test',
variant: 'info',
timeout: 2000,
});
});
});
});
69 changes: 69 additions & 0 deletions test/wtr/app/utils/event-bus.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import { expect } from '@open-wc/testing';
import sinon from 'sinon';
import { EventBus } from '../../../../src/extension/app/utils/event-bus.js';

describe('EventBus', () => {
let eventBus;

before(() => {
eventBus = EventBus.instance;
});

it('should be a singleton', () => {
const anotherInstance = EventBus.instance;
expect(eventBus).to.equal(anotherInstance);
});

it('should add event listeners', () => {
const callback = sinon.spy();
eventBus.addEventListener('test-event', callback);
expect(eventBus.listeners).to.deep.include({ type: 'test-event', callback });
});

it('should remove a specific event listener', () => {
const callback = sinon.spy();
const listener = eventBus.addEventListener('test-remove', callback);
eventBus.removeEventListener(listener);
expect(eventBus.listeners).to.not.deep.include(listener);
});

it('should remove multiple event listeners', () => {
const callbacks = [sinon.spy(), sinon.spy()];
const listeners = callbacks.map((cb) => eventBus.addEventListener('multi-remove', cb));
eventBus.removeEventListeners(listeners);
listeners.forEach((listener) => {
expect(eventBus.listeners).to.not.deep.include(listener);
});
});

it('should call the correct listener on event dispatch', () => {
const callback = sinon.spy();
eventBus.addEventListener('dispatch-event', callback);
eventBus.dispatchEvent(new CustomEvent('dispatch-event'));
sinon.assert.calledOnce(callback);
});

it('should not call any listener when no matching event', () => {
const callback = sinon.spy();
eventBus.addEventListener('no-call-event', callback);
eventBus.dispatchEvent(new CustomEvent('other-event'));
sinon.assert.notCalled(callback);
});

afterEach(() => {
// Reset the event listeners after each test
eventBus.listeners = [];
});
});

0 comments on commit 25806ff

Please sign in to comment.