Skip to content

Commit

Permalink
test(SFINT-3352): Safer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-bompart committed Jul 28, 2020
1 parent 0d3f6ae commit 52f8ca3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
45 changes: 35 additions & 10 deletions tests/components/ActionButton/ToggleActionButton.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ActionButton } from '../../../src/components/ActionButton/ActionButton'
import { IComponentOptions } from 'coveo-search-ui';
import { IToggleableButtonOptions } from '../../../src/components/ActionButton/ToggleableButton';
import { StatefulActionButton } from '../../../src/components/ActionButton/StatefulActionButton';
import { defer } from '../../utils';

describe('ToggleActionButton', () => {
let sandbox: SinonSandbox;
Expand Down Expand Up @@ -153,18 +154,28 @@ describe('ToggleActionButton', () => {
expect(option.alias).toContain(legacy);
});
});
});

describe('event-loop test', () => {
let eventCompletionPromises: Array<Promise<void>>;
beforeEach(() => {
eventCompletionPromises = [];
});

describe(`if activated triggers an event that would activate the button. `, () => {
const activateEvent = 'activate-event';

beforeEach(() => {
const activateWithEvent: (this: ToggleActionButton) => void = function () {
this.element.dispatchEvent(new CustomEvent(activateEvent));
const deferred = defer();
eventCompletionPromises.push(deferred.promise);
this.element.dispatchEvent(new CustomEvent(activateEvent, { detail: deferred.resolve }));
};
activateSpy = sandbox.spy(activateWithEvent);
options.activate = activateWithEvent;
testSubject = createToggleButton(options);
testSubject.element.addEventListener(activateEvent, () => {
testSubject.element.addEventListener(activateEvent, (e: CustomEvent<() => void>) => {
testSubject.setActivated(true);
e.detail();
});
});

Expand All @@ -174,8 +185,11 @@ describe('ToggleActionButton', () => {
sandbox.reset();
});

it('should not call switchTo when setActivated is called with true', () => {
it('should not call switchTo when setActivated is called with true', async () => {
testSubject.setActivated(true);
for (let index = 0; index < eventCompletionPromises.length; index++) {
await eventCompletionPromises[index];
}
expect(switchToSpy.called).toBeFalse();
});
});
Expand All @@ -186,8 +200,11 @@ describe('ToggleActionButton', () => {
sandbox.reset();
});

it('should call switchTo only once when setActivated is called with true', () => {
it('should call switchTo only once when setActivated is called with true', async () => {
testSubject.setActivated(true);
for (let index = 0; index < eventCompletionPromises.length; index++) {
await eventCompletionPromises[index];
}
expect(switchToSpy.calledOnce).toBeTrue();
});
});
Expand All @@ -197,13 +214,15 @@ describe('ToggleActionButton', () => {
const deactivateEvent = 'deactivate-event';
beforeEach(() => {
const deactivateWithEvent: (this: ToggleActionButton) => void = function () {
this.element.dispatchEvent(new CustomEvent(deactivateEvent));
const deferred = defer();
eventCompletionPromises.push(deferred.promise);
this.element.dispatchEvent(new CustomEvent(deactivateEvent, { detail: deferred.resolve }));
};
activateSpy = sandbox.spy(deactivateWithEvent);
options.deactivate = deactivateWithEvent;
testSubject = createToggleButton(options);
testSubject.element.addEventListener(deactivateEvent, () => {
testSubject.element.addEventListener(deactivateEvent, (e: CustomEvent<() => void>) => {
testSubject.setActivated(false);
e.detail();
});
});

Expand All @@ -213,8 +232,11 @@ describe('ToggleActionButton', () => {
sandbox.reset();
});

it('should not call switchTo when setActivated is called with false', () => {
it('should not call switchTo when setActivated is called with false', async () => {
testSubject.setActivated(false);
for (let index = 0; index < eventCompletionPromises.length; index++) {
await eventCompletionPromises[index];
}
expect(switchToSpy.called).toBeFalse();
});
});
Expand All @@ -225,8 +247,11 @@ describe('ToggleActionButton', () => {
sandbox.reset();
});

it('should call switchTo only once when setActivated is called with false', () => {
it('should call switchTo only once when setActivated is called with false', async () => {
testSubject.setActivated(false);
for (let index = 0; index < eventCompletionPromises.length; index++) {
await eventCompletionPromises[index];
}
expect(switchToSpy.calledOnce).toBeTrue();
});
});
Expand Down
17 changes: 17 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,20 @@ export function fakeUserProfileModel(root: HTMLElement, sandbox: SinonSandbox) {
(root as any)[`Coveo${UserProfileModel.ID}`] = sandbox.createStubInstance(UserProfileModel);
return (root as any)[`Coveo${UserProfileModel.ID}`];
}

/**
* Create a deferred promise
*/
export function defer<T = void>() {
let resolve: (arg?: T) => void;
let reject: (arg: any) => void;
const promise = new Promise<T>((p_resolve, p_reject) => {
resolve = p_resolve;
reject = p_reject;
});
return {
resolve,
reject,
promise,
};
}

0 comments on commit 52f8ca3

Please sign in to comment.