Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(state): allow to pass injector to rxState and rxEffects #1840

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

michaelbe812
Copy link
Contributor

This PR will add the possibility to pass a Injector to rxState and rxEffects to make it easier to instantiate them outside of an injection context.

Right now users need to do:

const injector = inject(Injector)

// outside injection context
runInInjectionContext(injector, () => {
 const state = rxState()
} )

@github-actions github-actions bot added the { } State @rx-angular/state related label Jan 8, 2025
Copy link

nx-cloud bot commented Jan 8, 2025

View your CI Pipeline Execution β†— for commit b2d2be5.

Command Status Duration Result
nx affected -t lint build test component-test e... βœ… Succeeded 2m 38s View β†—
nx build docs βœ… Succeeded 1m 13s View β†—
nx-cloud record -- npx nx format:check βœ… Succeeded 2s View β†—

☁️ Nx Cloud last updated this comment at 2025-01-29 06:49:03 UTC

@michaelbe812
Copy link
Contributor Author

michaelbe812 commented Jan 8, 2025

I did not add any test cases so far.
Will work on it

  • Add test cases

@github-actions github-actions bot added the πŸ›‚ Test Unit tests, e2e tests, integration tests, test coverage label Jan 9, 2025
@michaelbe812
Copy link
Contributor Author

michaelbe812 commented Jan 9, 2025

@edbzn
I added a few test cased for rxState, see commit 7d38500

Let me know what you think. A few words here from my side:

  • not sure if these tests bring some real value
  • pretty sure it can be done in a better way, would appreciate some hints here
  • I created a separate .spec file because afaik it is not possible to scope jest.mock. Adding these tests e.g. in rx-state.spec.ts leaded to the result that all other tests where broken due to the mocking of runInInjectionContext

Depending on the outcome of this discussion here I'll continue to add tests for rxEffects as well.

@edbzn
Copy link
Member

edbzn commented Jan 9, 2025

@michaelbe812 I think we need to test only two things here:

  • we can pass a custom injector to rxState and it still works
  • it should throw when running without an injection context

Instead of creating a new file and mocking runInInjectionContext, I'd make a new utility fn to create rxState without an injection context, then test the two test cases I mentioned. Here's a piece of code where we did that, I think you can take it as an inspiration. Wdyt?

@michaelbe812
Copy link
Contributor Author

@michaelbe812 I think we need to test only two things here:

  • we can pass a custom injector to rxState and it still works
  • it should throw when running without an injection context

Instead of creating a new file and mocking runInInjectionContext, I'd make a new utility fn to create rxState without an injection context, then test the two test cases I mentioned. Here's a piece of code where we did that, I think you can take it as an inspiration. Wdyt?

Sounds good! Thank you for giving me another perspective. Will look into the snippet you provided and update the test cases.

Will let you know once I'm done

@michaelbe812 michaelbe812 force-pushed the f/rx-effects-options branch 2 times, most recently from 5a0bf28 to e2fcc4b Compare January 9, 2025 19:03
@michaelbe812
Copy link
Contributor Author

@edbzn just updated the test cases following your recommendation.

Let me know if it meets your expectations or some polishing is needed.

@michaelbe812
Copy link
Contributor Author

@edbzn
just to quickly ask for feedback: anything missing so that we can merge this PR?

@hoebbelsB
Copy link
Member

@michaelbe812 I would love to see some docs about this? Just adding a section to the rx state docs would be fair enough

@github-actions github-actions bot added the πŸ“š Docs Web Documentation hosted on github pages label Jan 29, 2025
@michaelbe812
Copy link
Contributor Author

@michaelbe812 I would love to see some docs about this? Just adding a section to the rx state docs would be fair enough

Oh how could i miss that! πŸ˜…

Done! :)

@michaelbe812
Copy link
Contributor Author

@edbzn
I saw that this PR is approved - you might wanna merge it once u have a minute :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
πŸ“š Docs Web Documentation hosted on github pages { } State @rx-angular/state related πŸ›‚ Test Unit tests, e2e tests, integration tests, test coverage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants