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

Have StringCalculatorPage use host document #51

Merged
merged 1 commit into from
Dec 17, 2023

Conversation

mbland
Copy link
Owner

@mbland mbland commented Dec 17, 2023

StringCalculatorPage no longer creates its own DocumentFragment with a new top level app element (<div id="app">).

The constructor now takes the top level app element as its first argument, and an optional Document as its second argument. This enables it to continue wrapping documents created via the PageLoader from test/helpers.js, as used in main.test.js.

The new static factory, StringCalculatorPage.new(), creates a new <div> with the specified ID, appends it document.body, and returns a new StaticCalculatorPage wrapping that app element. This tries to ensure that the <div>s created by tests don't collide with existing <div>s in the document (short of randomly generating its own IDs).

StringCalculatorPage.new() also keeps track of instances it creates, and clients should call StringCalculatorPage.cleanup() in their afterEach() handlers. This ensures the host document returns to its original state after each test.


As mentioned in the previous couple of commits, a <form> element attached to a DocumentFragment would exhibit inconsistent behavior across jsdom, Chrome, and Firefox. This new implementation solves that problem (as will be seen in the upcoming Calculator component tests).

It would've been nice to use a DocumentFragment for all our tests instead of modifying the global document in any way. However, this way we can be more confident that our components will exhibit consistent behavior regardless of the test environment.

StringCalculatorPage no longer creates its own DocumentFragment with a
new top level app element (<div id="app">).

The constructor now takes the top level app element as its first
argument, and an optional Document as its second argument. This enables
it to continue wrapping documents created via the PageLoader from
test/helpers.js, as used in main.test.js.

The new static factory, StringCalculatorPage.new(), creates a new <div>
with the specified ID, appends it document.body, and returns a new
StaticCalculatorPage wrapping that app element. This tries to ensure
that the <div>s created by tests don't collide with existing <div>s in
the document (short of randomly generating its own IDs).

StringCalculatorPage.new() also  keeps track of instances it creates,
and clients should call StringCalculatorPage.cleanup() in their
afterEach() handlers. This ensures the host document returns to its
original state after each test.

---

As mentioned in the previous couple of commits, a <form> element
attached to a DocumentFragment would exhibit inconsistent behavior
across jsdom, Chrome, and Firefox. This new implementation solves that
problem (as will be seen in the upcoming Calculator component tests).

It would've been nice to use a DocumentFragment for all our tests
instead of modifying the global document in any way. However, this way
we can be more confident that our components will exhibit consistent
behavior regardless of the test environment.
@mbland mbland self-assigned this Dec 17, 2023
@mbland mbland merged commit 26e7f3f into main Dec 17, 2023
3 checks passed
@mbland mbland deleted the page-object-uses-host-document branch December 17, 2023 00:05
mbland added a commit that referenced this pull request Dec 17, 2023
Now all parameters that initApp() may pass through to each individual
component's init() function are bundled into a single object.

Other small but significant changes:

- Removes window and document from the parameter list for now, as
  there's no components that need to inject them.

- Sets up a list of components in initApp() so that it can call each
  component's init() via forEach(). This eliminates the need to add a
  new line for each init() call for each added component.

- Sets initApp() to run only once on DOMContentLoaded.

- Removes the now unused fragment() test helper (should've been part of
  commit 5f2a917 from #51).

---

While adding the Calculator component, it became apparent that adding
more params to main.js and initApp() to pass through to init() was
becoming unwieldy. This solution makes future extension easier and the
code cleaner while decoupling component initializer signatures from one
another.
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

1 participant