This repository was archived by the owner on Mar 6, 2024. It is now read-only.
RFC: Improve the type system around options in widgets#334
Open
ryan-bradford wants to merge 1 commit intovmware-archive:masterfrom
Open
RFC: Improve the type system around options in widgets#334ryan-bradford wants to merge 1 commit intovmware-archive:masterfrom
ryan-bradford wants to merge 1 commit intovmware-archive:masterfrom
Conversation
Signed-off-by: Ryan Bradford <[email protected]>
83abca7 to
7066195
Compare
Codecov Report
@@ Coverage Diff @@
## master #334 +/- ##
==========================================
- Coverage 86.35% 86.33% -0.03%
==========================================
Files 103 103
Lines 3343 3344 +1
Branches 529 531 +2
==========================================
Hits 2887 2887
+ Misses 296 294 -2
- Partials 160 163 +3
Continue to review full report at Codecov.
|
7969755 to
7066195
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a user exposed an element using the
factory, it created a find function that had one parameter ofFindElementOptions. This has an ancestor ofunknownandoptionsofUnknownOptions. Within a widget, this makes sense, but external to a widget, these options should be known. In the case of Cypress, the ancestor is a string, and in the case of Angular the ancestor is a DebugElement.This problem also happens if a user tries to take in options that are passed to a call to
click. There was no way for these options to be type safe. They were always unknown.Solution
It should be possible for these options to be type safe. In a
stepdeffile, we know we are dealing with Cypress, and in aspecwe know we are dealing with Angular.To accomplish this, the solution was to make the type of the options conditional on the type of entity being returned. Meaning, the type of
FindElementOptionsis now conditional on the typeTpassed in. If it is aChainable, it gives a Cypress options interface; if it is aTestElementit gives an Angular options interface.This now means two things:
{ timeout: x }to a method will throw a type error. This makes sense because widget should not know about the options they accept. If a method needs a changed timeout, these options must be passed from the caller as aWidgetActionOptions<T>. Before because these options were not type safe, passingtimeoutwithin a widget would not throw an error.In concrete terms,
findDataExporter({ options: { timeout: 10000 } })is valid from a stepdef, but invalid from a unit test. The type ofancestoris also known and type checked.Example:
Here's a widget that has a few methods that use this new object:
From a stepdef, using this widget would look like:
From a unit test, using this widget would look like:
Cons
FindElementOptionsneeds to know about the different types of widgets so that it can infer which options type to create. This violates some OOP, but is acceptable in my opinion.Other Options
O1andO2which is not ideal.Ex:
Screenshots (if applicable)
Does this PR introduce a breaking change?
FindCypressWidgetOptionsis no longer an exported type.