Skip to content

test(docs-infra): add unit tests for rxjs examples - #34190

Closed
sonukapoor wants to merge 2 commits into
angular:masterfrom
sonukapoor:unit-tests-for-stand-alone-code
Closed

sonukapoor wants to merge 2 commits into
angular:masterfrom
sonukapoor:unit-tests-for-stand-alone-code

Conversation

@sonukapoor

@sonukapoor sonukapoor commented Dec 2, 2019 •

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@sonukapoor
sonukapoor requested review from a team December 2, 2019 18:25
@sonukapoor
sonukapoor force-pushed the unit-tests-for-stand-alone-code branch from 48592a0 to b1fcd71 Compare December 2, 2019 18:46
@sonukapoor

sonukapoor commented Dec 2, 2019 •

Copy link
Copy Markdown
Contributor Author

@gkalpak Please review. The assumption is that the step to run the stand-alone tests runs after the e2e tests have run. This will ensure that the js files have been created. Let me know what you think.

@sonukapoor
sonukapoor force-pushed the unit-tests-for-stand-alone-code branch from b1fcd71 to dff4783 Compare December 2, 2019 19:19
@ngbot ngbot Bot modified the milestone: needsTriage Dec 2, 2019
@gkalpak

gkalpak commented Dec 3, 2019 •

Copy link
Copy Markdown
Member

This lgtm for now. We will re-work the infrastructure before merging the PR, but this is good enough for now so that you can keep working on the actual tests. Thx for sorting it out 👍

@sonukapoor
sonukapoor force-pushed the unit-tests-for-stand-alone-code branch 3 times, most recently from 2e3651b to 747e9d0 Compare December 9, 2019 14:39
@gkalpak

gkalpak commented Dec 9, 2019

Copy link
Copy Markdown
Member

As discussed "offline", here are my thoughts (and suggestions for next steps):

  • A proper solution might involve setting up a different project type for Observable-related projects (e.g. not involving Angular dependencies and providing dependencies needed for observables only). This would be a little expensive in terms of both initial implementation and maintenance, though.

  • I realized that the observables examples are not offered for download/StackBlitz, so the users only really see the code snippets (not the whole files). This gives us some flexibility on putting stuff around the docregions that will help us with testing, but without confusing the users (which will never see that extra stuff).

  • I thought about using Karma for the tests. This would give us access to a real browser. I then realized that that would only solve part of the problem. We would need to add appropriate HTML files and that would take more time/maintenance/complexity and would most likely still require infrastructure changes. Not ideal.

  • Finally, I noticed another issue: The code in the files executes eagerly (i.e. as soon as the file is loaded), which means that we would have a hard time testing stuff in the test files, because it would have happened before even loading the test files.


So, given the above, I think a good solution ("good" in the sense that it is fairly simple, will not take a lot of time to implement infrastructure for and will allow us to focus on tests (which is the primary focus for now)) would be the following:

  • We could wrap each docregion (i.e. code snippet) in a function and export that function. This way, the code snippets that the user sees wouldn't change (because they would only see the function body), but the code would not be executed as soon as the file is loaded during tests.

  • In tests, we could call the exported functions to run the code. This gives us the freedom to:
    a. Set up spies and mocks before executing the function.
    b. Pass mocks for globals as arguments to the function. For example, we could pass a mock document to the function etc.

As an example, here is what a code snippet could look like before my suggested change:

// #docregion foo
const elem = document.querySelector('foo');
const subscription = fromEvent(elem, 'click').subscribe(() => console.log('*click*'));

setTimeout(() => subscription.unsubscribe(), 10000);
// #enddocregion foo

And here is how it would be transformed with my suggestion:

// By wrapping a function around the code, we make it "lazy"
// (i.e. it won't be executed immediately).
//
// We can pass in "globals" (in TS we can give them proper types).
// Here for example, we can pass in a mock `console` to capture the logs.
// (We could even pass a mock `setTimeout()`, but that is unnecessary,
// since we can mock that with `jasmine.clock().install()` etc.)
export function docregionFoo(console, document) {

  // The code-snippet that the user will see remains unchanged.
  // (The indentation might change, but that is fine.)
  // #docregion foo
  const elem = document.querySelector('foo');
  const subscription = fromEvent(elem, 'click').subscribe(() => console.log('*click*'));

  setTimeout(() => subscription.unsubscribe(), 10000);
  // #enddocregion foo

  // Here, return the `subscription`, if we need to run assertions against it.
  return subscription;
}

@petebacondarwin

Copy link
Copy Markdown
Contributor

@sonukapoor can we close this PR?

@sonukapoor

Copy link
Copy Markdown
Contributor Author

@sonukapoor can we close this PR?

This PR is awaiting @gkalpak's review :)

@petebacondarwin petebacondarwin added action: review The PR is still awaiting reviews from at least one requested reviewer and removed state: WIP labels Jun 19, 2020
@pullapprove
pullapprove Bot requested review from IgorMinar and alxhub June 19, 2020 11:39
@petebacondarwin
petebacondarwin removed request for a team and IgorMinar June 19, 2020 11:39
@mary-poppins

Copy link
Copy Markdown

You can preview 230b42f at https://pr34190-230b42f.ngbuilds.io/.

@gkalpak gkalpak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thx, @sonukapoor! Here's another round of review comments 😃
(I have reviewed all examples except for the rx-library one.)

Pending comments from previous review (for my reference):

@sonukapoor
sonukapoor force-pushed the unit-tests-for-stand-alone-code branch from 628cd35 to e7bc538 Compare September 8, 2020 12:57
@mary-poppins

Copy link
Copy Markdown

You can preview e7bc538 at https://pr34190-e7bc538.ngbuilds.io/.

@gkalpak gkalpak left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We are getting there 😃

In addition to my other comments, can you also:

  • Move the change of using searchTerm in the typeahead request to a separate commit (as discussed in #34190 (comment)) and adjust the tests accordingly (i.e. each commit should include the tests that are relevant to the source code changes it introduces).
  • Move the changes to the rx-library example to a new PR (in the interest of getting this one in faster and avoiding its getting stale).

@sonukapoor
sonukapoor force-pushed the unit-tests-for-stand-alone-code branch from 1bfa192 to 9bb142e Compare September 14, 2020 13:16
This commit adds missing unit tests for all rxjs examples from the docs.

Closes angular#28017
@sonukapoor
sonukapoor force-pushed the unit-tests-for-stand-alone-code branch from 9bb142e to 02c43e5 Compare September 14, 2020 16:35
@sonukapoor
sonukapoor requested a review from gkalpak September 14, 2020 17:19

@gkalpak gkalpak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Almost ready 🎉

BTW, please update the second commit message to use imperative, present tesne (per our guidelines):

improves typeahead example and adds unit test --> improve typeahead example and add unit test …

@gkalpak gkalpak added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Sep 15, 2020
@sonukapoor
sonukapoor requested a review from gkalpak September 15, 2020 15:23
@sonukapoor
sonukapoor force-pushed the unit-tests-for-stand-alone-code branch from c125ab9 to 435cf42 Compare September 15, 2020 15:23
This commit improves the typeahead example, by using the emitted input
value. It also adds a unit test to ensure that the example is working
as intended.
@sonukapoor
sonukapoor force-pushed the unit-tests-for-stand-alone-code branch from 435cf42 to 1939ada Compare September 15, 2020 19:09

@gkalpak gkalpak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice 🎉

Reviewed-for: global-docs-approvers

@gkalpak
gkalpak removed the request for review from alxhub September 15, 2020 20:04
@sonukapoor sonukapoor added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Sep 16, 2020
AndrewKushnir pushed a commit that referenced this pull request Sep 16, 2020
This commit adds missing unit tests for all rxjs examples from the docs.

Closes #28017

PR Close #34190
AndrewKushnir pushed a commit that referenced this pull request Sep 16, 2020
This commit improves the typeahead example, by using the emitted input
value. It also adds a unit test to ensure that the example is working
as intended.

PR Close #34190
AndrewKushnir pushed a commit that referenced this pull request Sep 16, 2020
This commit improves the typeahead example, by using the emitted input
value. It also adds a unit test to ensure that the example is working
as intended.

PR Close #34190
@sonukapoor
sonukapoor deleted the unit-tests-for-stand-alone-code branch September 20, 2020 14:00
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Oct 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker cla: yes target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants