Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(SFINT-3353): Test cleaning #87

Merged
merged 3 commits into from
Jul 30, 2020

Conversation

louis-bompart
Copy link
Collaborator

@louis-bompart louis-bompart commented Jul 28, 2020

What

While investigating SFINT-3353, I discovered several issues in the UT
The actual 'fix' for SFINT-3353 will be done in a second term to have a clear split between refacto' and fix.

Improper use of expect:

https://jasmine.github.io/api/3.6/global.html#expect

TL;DR: Use matchers after expect. Always.

it('sometest'()=>{
   expect(false);
   expect(true);
}

This will pass. expect is not Assert. This will not pass:

it('sometest'()=>{
   expect(false).toBeTrue();
   expect(true).toBeTrue();
}

Issue with Stub, jasmine and yield function.

Various, various issues. My goal was to, whenever possible to 'flatten' and synchronize the tests, and avoid delay as much as possible (except when used to 'flush' the promise stack see waitForPromiseCompletions).

How to flatten delay

Doing an await(delay(()=>{}) ensure that all Promises are resolved before continuing. (assuming there's no setTimeout&co going on).
This is due to how microtasks and (macro)tasks work https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide

First, each time a task exits, the event loop checks to see if the task is returning control to other JavaScript code. If not, it runs all of the microtasks in the microtask queue. The microtask queue is, then, processed multiple times per iteration of the event loop, including after handling events and other callbacks.

So, long story short:

  • All Promises before the await(delay(()=>{}) are ran (if they encounter a setTimeout so be it, they add it to the (macro)task queue, which will be executed once all promises are executed. If a promise is added, however, see the article, but from what I understand, it's executed before the next (macro)task
  • SetTimeout, because it's at a 0 timeout is ready for cookin' on the pile, and resolve thus resolve the Promise that returns delay, freeing up the main process and, in our case, letting the other statement executes (often the assertions).

How did you test your tests.

By hand, most of them failed at first so, not a lot of choices (I won't say I checked all of them by hand, but a good 75%)

Further Improvements

  • Apply proper unit-testing methodology by using Inversion of Control/Dependency Injections. This would allow proper 'black-boxing' and ensure that you can have more control over what's going on (e.g. with the ClickedDocumentList and the ExpendableList)
  • Isolate the display logic, the state logic, and the query logic from each other. It would make the code more readable, and allow to test -thus maintain- more easily.

Copy link
Collaborator

@mikegron mikegron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catches!

Copy link
Collaborator

@lbergeron lbergeron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job!

@@ -109,16 +104,16 @@ describe('ExpandableList', () => {
const el = list.element.querySelector<HTMLElement>('.coveo-more-less');
el.click();

return delay(() => {
delay(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since delay() returns a Promise, shouldn't we use await delay(...) instead?

The way I understand it, the test currently exits before the expectations are verified.

});

it('should pass the user id option to each of it sub components', () => {
it('should pass the user id option to each of it sub components', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...to each of it ... -> ...to each of its ...


expect(spyDispatchEvent.calledOnceWith(new CustomEvent('userActionsPanelHide')));
expect(spyDispatchEvent.calledOnce);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be expect(spyDispatchEvent.calledOnce).toBe(true) instead?

const spyDispatchEvent = sandbox.spy(mock.cmp.element, 'dispatchEvent');

mock.cmp.hide();

expect(spyDispatchEvent.calledOnceWith(new CustomEvent('userActionsPanelHide')));
expect(spyDispatchEvent.calledOnce);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be expect(spyDispatchEvent.calledOnce).toBe(true) instead?

@louis-bompart louis-bompart merged commit 593f7f8 into coveo:master Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants