Commit graph

291 commits

Author SHA1 Message Date
Andrew Scott
3cf612c857 fix(core): update imports to be compatible with rxjs 6 (#54193)
Peer dependency range allows for rxjs 6. We cannot use features only
available in rxjs 7 unless that changes.

fixes #54192

PR Close #54193
2024-02-01 20:48:19 +00:00
Andrew Scott
3ca34e606d refactor(core): Update ComponentFixture behavior when using zoneless scheduler (#54024)
When the zoneless scheduler is provided, we want to update the behavior
of `ComponentFixture` to address common issues and painpoints in testing.
Developers should never have to call `detectChanges` on a fixture
manually. Instead of calling `detectChanges` after performing an
action that updates state and requies a template refresh, developers
should wait for change detection to run because the update needs to also have
notified the scheduler. If this was not the case, the component would
not work correctly in the application. Calling `detectChanges` to force
an update could hide real bugs.

This commit also updates the zoneless tests to uses `ComponentFixture`
instead of manually attaching to the `ApplicationRef` and rewriting a
lot of the helpers (`getDebugNode`, `isStable` as a value, `whenStable` as a
Promise).

PR Close #54024
2024-01-29 20:21:47 +00:00
cexbrayat
037b79b72e fix(core): change defer block fixture default behavior to playthrough (#54088)
This is a followup to #53956

The default behavior needs to be changed in `TestBedCompiler` as well to have an effect.

PR Close #54088
2024-01-26 15:44:40 +00:00
Jessica Janiuk
df6c2057f2 fix(core): Change defer block fixture default behavior to playthrough (#53956)
This inverts the default behavior of test bed to use playthrough for defer blocks instead of manual.

fixes: #53686

PR Close #53956
2024-01-17 10:45:42 -08:00
Andrew Scott
1f8c53cd0c fix(core): TestBed should still use the microtask queue to schedule effects (#53843)
Prior to this commit, `TestBed` would require tests call `flushEffects`
or `fixture.detectChanges` in order to execute effects. In general, we
want to discourage authoring tests like this because it makes the timing
of change detection and effects differ from what happens in the
application. Instead, developers should perform actions and `await` (or
`flush`/`tick` when using `fakeAsync`) some `Promise` so that Angular
can react to the changes in the same way that it does in the
application.

Note that this still _allows_ developers to flush effects synchronously
with `flushEffects` and `detectChanges` but also enables the <action>,
`await` pattern described above.

PR Close #53843
2024-01-11 12:05:57 -08:00
Andrew Scott
dfcf0d5882 fix(core): afterRender hooks now only run on ApplicationRef.tick (#52455)
The `afterRender` hooks currently run after `ApplicationRef.tick` but
also run after any call to `ChangeDetectorRef.detectChanges`. This is
problematic because code which uses `afterRender` cannot expect the
component it's registered from to be rendered when the callback
executes. If there is a call to `ChangeDetectorRef.detectChanges` before
the global change detection, that will cause the hooks to run earlier
than expected.

This behavior is somewhat of a blocker for the zoneless project. There
is plenty of application code that do things like `setTimeout(() =>
doSomethingThatExpectsComponentToBeRendered())`, `NgZone.onStable(() =>
...)` or `ApplicationRef.onStable...`. `ApplicationRef.onStable` is a
should likely work similarly, but all of these are really wanting an API
that is `afterRender` with the requirement that the hook runs after the
global render, not an individual CDRef instance.

This change updates the `afterRender` hooks to only run when
`ApplicationRef.tick` happens.

fixes #52429
fixes #53232

PR Close #52455
2024-01-08 11:30:27 -08:00
Jeremy Elbourn
91f250dab7 build: configure cross-pkg resolution for api extraction (#52499)
This commit adds path mapping and source dependencies necessary to fully
resolve types during api doc extraction.

PR Close #52499
2024-01-05 11:27:34 -08:00
Andrew Scott
c59a4dcd77 refactor(core): Use NoopNgZone in componentFixture (#53670)
The `ComponentFixture` code needlessly dances around the `ngZone` being
`null` when the `ComponentFixtureNoNgZone` option is set. Instead, it
can use the `NoopNgZone` to get the same effect without needing to have
checks all over the place for its presence.

PR Close #53670
2024-01-03 11:28:08 -08:00
cexbrayat
160363a626 refactor(core): remove no longer needed FlushableEffectRunner private export (#53525)
It is no longer needed after 70a442eb02

PR Close #53525
2023-12-13 09:22:42 -08:00
Andrew Scott
8cccc7c5be refactor(core): Clean up subscription handling in ComponentFixture (#53426)
The subscriptions can be managed by a single subscription and unsubscribe on destroy

PR Close #53426
2023-12-08 14:30:14 -08:00
Andrew Scott
70a442eb02 refactor(core): Run ComponentFixture constructor in injection context (#53400)
The component fixture dependencies have to be passed in manually. This
is a bit annoying to manage as we expand which dependencies are needed.
Instead, we can run the constructor in the TestBed injection context and
move the dependencies into the component fixture code, as is done with
other constructors in Angular.

PR Close #53400
2023-12-07 09:35:24 -08:00
Andrew Kushnir
58cf389d80 fix(core): avoid stale provider info when TestBed.overrideProvider is used (#52918)
This commit updates the logic to preserve previous value of cached TView before applying overrides. This helps ensure that the next tests that uses the same component has correct provider info.

PR Close #52918
2023-11-29 09:48:53 +01:00
Matthieu Riegler
e00ae2d07a refactor(core): replace runInContext by runInInjectionContext (#53035)
Saves a few bytes since function names can be mangled.

PR Close #53035
2023-11-20 12:05:00 -08:00
Andrew Kushnir
ee892ee294 fix(core): reset cached scope for components that were overridden using TestBed (#52916)
Currently, when a component is overriden using `TestBed.overrideComponent`, Angular retains calculated scope for that component (a set of components and directives used within a component). This may cause stale information to be used in tests in some cases. This commit updates the logic to reset overridden component scope, so it gets re-computed during the next invocation.

Resolves #52817.

PR Close #52916
2023-11-15 14:03:55 +00:00
Andrew Kushnir
55d2c427c2 refactor(core): delay applying component metadata until it's needed in tests (#52708)
When a component contains `@defer` blocks, Angular compiler generates the code to apply component metadata (from the `@Component` decorator) after resolving all dynamic dependencies. Currently, this function is invoked eagerly at runtime, which causes dynamic imports to be kicked off earlier than expected. With the change in this commit, Angular will start resolving async metadata when it becomes necessary during testing.

PR Close #52708
2023-11-09 16:58:06 +00:00
Alex Rickabaugh
01b79356fb Revert "refactor(core): rename ViewRef<T> to InternalViewRef<T> and remove existing InternalViewRef (#52430)" (#52484)
This reverts commit a568bc5d97 as it breaks
tests in g3.

PR Close #52484
2023-11-01 10:13:12 -07:00
Alex Rickabaugh
ee9605f3c8 fix(core): effects wait for ngOnInit for their first run (#52473)
When an effect is created in a component constructor, it might read signals
which are derived from component inputs. These signals may be unreliable or
(in the case of the proposed input signals) may throw if accessed before the
component is first change detected (which is what makes required inputs
available).

Depending on the scenario involved, the effect may or may not run before
this initialization takes place, which isn't a great developer experience.
In particular, effects created during CD (e.g. via control flow) work fine,
as do effects created in bootstrap thanks to the sync CD it performs. When
an effect is created through dynamic component creation outside of CD though
(such as on router navigations), it runs before the component is first CD'd,
causing the issue.

In fact, in the signal components RFC we described how effects would wait
until ngOnInit for their first execution for exactly this reason, but this
behavior was never implemented as it was thought our effect scheduling
design made it unnecessary. This is true of the regular execution of effects
but the above scenario shows that *creation* of the effect is still
vulnerable. Thus, this logic is needed.

This commit makes effects sensitive to their creation context, by injecting
`ChangeDetectorRef` optionally. An effect created with an injector that's
tied to a component will wait until that component is initialized before
initially being scheduled. TestBed effect flushing is also adjusted to
account for the additional interaction with change detection.

PR Close #52473
2023-11-01 08:07:35 -07:00
Andrew Scott
a568bc5d97 refactor(core): rename ViewRef<T> to InternalViewRef<T> and remove existing InternalViewRef (#52430)
The `ViewRef<T>` interface extends `InternalViewRef` and is already not
part of the public API. There is no need for the extra `InternalViewRef`
interface. This confusing setup is likely leftover from the types
necessary to support both Ivy and ViewEngine.

PR Close #52430
2023-10-31 13:22:56 -07:00
Andrew Kushnir
bdc4266d2d refactor(core): ignore after and minimum when transition between states in tests (#52314)
This commit updates the logic to ignore `after` and `minimum` conditions when `DeferBlockFixture.render` method is used in tests.

Resolves #52313.

PR Close #52314
2023-10-24 09:25:13 -07:00
Andrew Kushnir
54766fb35f refactor(core): defer triggers cleanup (#52291)
This commit adds the logic to cleanup all triggers once defer block is triggered.

When a trigger is created, its cleanup function is stored alongside other defer block info. Prefetch and regular triggers are store in different slots, since we need to invoke them at different time.

PR Close #52291
2023-10-23 12:00:18 -07:00
Jeremy Elbourn
fcc000e803 build: add targets for api doc generation (#52034)
This adds `generate_api_docs` targets to all of the packages for which we publish api reference docs. One known issue here is that any type information that comes from another package (e.g. router depending on core) currently resolve to `any` because the other sources are not available in the program. This can be tackled in a follow-up commit.

This commit also updates the install patch for `@angular/build-tools` to use the local version of compiler-cli.

PR Close #52034
2023-10-10 16:18:50 -07:00
Kristiyan Kostadinov
8be2c48b7c feat(core): implement new block syntax (#51891)
Switches the syntax for blocks from `{#block}{/block}` to `@block {}` based on the feedback from the community.

Read more about the decision-making process in our blog: https://blog.angular.io/meet-angulars-new-control-flow-a02c6eee7843

The existing block types changed in the following ways:

**Conditional blocks:**
```html
<!-- Before -->
{#if cond}
  Main content
  {:else if otherCond}
    Else if content
  {:else}
    Else content
{/if}

<!-- After -->
@if (cond) {
  Main content
} @else if (otherCond) {
  Else if content
} @else {
  Else content
}
```

**Deferred blocks**
```html
<!-- Before -->
{#defer when isLoaded}
  Main content
  {:loading} Loading...
  {:placeholder} <icon>pending</icon>
  {:error} Failed to load
{/defer}

<!-- After -->
@defer (when isLoaded) {
  Main content
} @loading {
  Loading...
} @placeholder {
  <icon>pending</icon>
} @error {
  Failed to load
}
```

**Switch blocks:**
```html
<!-- Before -->
{#switch value}
  {:case 1}
    One
  {:case 2}
    Two
  {:default}
    Default
{/switch}

<!-- After -->
@switch (value) {
  @case (1) {
    One
  }

  @case (2) {
    Two
  }

  @default {
    Default
  }
}
```

**For loops**
```html
<!-- Before -->
{#for item of items; track item}
  {{item.name}}
  {:empty} No items
{/for}

<!-- After -->
@for (item of items; track item) {
  {{item.name}}
} @empty {
  No items
}
```

PR Close #51891
2023-09-26 09:10:04 -07:00
Andrew Kushnir
4599642a87 docs: annotate ComponentFixture.getDeferBlocks as a developer preview API (#51855)
PR Close #51855
2023-09-22 09:52:13 -07:00
Jessica Janiuk
06bbc2fc4e refactor(core): Add defer block testing fixture (#51698)
This adds a fixture for being able to access and test defer blocks.

PR Close #51698
2023-09-13 10:47:04 -07:00
Kristiyan Kostadinov
59387ee476 feat(core): support styles and styleUrl as strings (#51715)
Adds support for passing in `@Component.styles` as a string. Also introduces a new `styleUrl` property on `@Component` for providing a single stylesheet. This is more convenient for the most common case where a component only has one stylesheet associated with it.

PR Close #51715
2023-09-12 13:57:07 -07:00
Alex Rickabaugh
38c9f08c8d refactor(core): decouple effects from change detection (#51049)
Previously effects were queued as they became dirty, and this queue was
flushed at various checkpoints during the change detection cycle. The result
was that change detection _was_ the effect runner, and without executing CD,
effects would not execute. This leads a particular tradeoff:

* effects are subject to unidirectional data flow (bad for dx)
* effects don't cause a new round of CD (good/bad depending on use case)
* effects can be used to implement control flow efficiently (desirable)

This commit changes the scheduling mechanism. Effects are now scheduled via
the microtask queue. This changes the tradeoffs:

* effects are no longer limited by unidirectional data flow (easy dx)
* effects registered in the Angular zone will trigger CD after they run
  (same as `Promise.resolve` really)
* the public `effect()` type of effect probably isn't a good building block
  for our built-in control flow, and we'll need a new internal abstraction.

As `effect()` is in developer preview, changing the execution timing is not
considered breaking even though it may impact current users.

PR Close #51049
2023-09-12 08:12:56 -07:00
Paul Gschwendtner
dbffdc09c2 fix(core): avoid duplicated code between entry-points (primary, testing, rxjs-interop) (#51500)
Fixes that there was code duplication between the primary entry-point,
the testing entry-point and the rxjs-interop entry-point.

This code duplication resulted in additional code size (really
neglibible here because rxjs-interop did not duplicate large parts of
core, and `testing` is not used in production).

On the other hand though, the duplication resulted in a subtle JIT
dependency tracking issue due to the `depsTracker` no longer being a
singleton. This caused test failures as in:
https://github.com/angular/angular/pull/51415.

PR Close #51500
2023-08-29 17:55:34 +00:00
Matthieu Riegler
a9b3c006f8 fix(core): guard the jasmine hooks (#51394)
This commit fixes a regression introduced in #50063

Fixes #51382

PR Close #51394
2023-08-21 08:14:53 -07:00
Matthieu Riegler
e6503930f1 docs: fix see also links. (#51379)
These were all the @see with no links.

PR Close #51379
2023-08-17 10:18:33 -07:00
Andrew Kushnir
bcc3c43fca refactor(core): update TestBed to handle async component metadata (#51182)
This commit updates TestBed to wait for async component metadata resolution before compiling components.
Async metadata is added by the compiler in case a component uses defer blocks, which contain deferrable
symbols.

PR Close #51182
2023-08-15 11:32:09 -07:00
Payam Valadkhan
6a7c1ab9e9 refactor(core): migrate JIT to use deps tracker behind a flag (#51122)
The current change is done behind a flag which is set to false. So no change in code path took place here. In a followup PR the flag is changed to true which will make the actual change.

PR Close #51122
2023-08-07 09:25:27 -07:00
Matthieu Riegler
fa80975832 refactor(core): use globalThis for global (#50063)
`globalThis` is now available on every runtime supported by Angular

PR Close #50063
2023-07-14 18:32:01 +00:00
Alan Agius
4550fe42f7 refactor: use queueMicrotask to schedule micro tasks instead of various helpers (#50485)
`queueMicrotask` is an API which is supported by all browser and Node.js versions.

PR Close #50485
2023-06-15 16:38:21 +02:00
Matthieu Riegler
5db170c501 docs: replace absolute links to aio with relative links. (#50213)
This change follows the introduction of the warning message from a transform processor to prevent absolute links to angular.io.

PR Close #50213
2023-05-10 14:29:50 -07:00
Andrew Kushnir
2180a692b0 refactor(core): minor updates to TestBed error messages (#50072)
This commit updates TestBed error messages to avoid using `R3TestBed` reference and just use `TestBed` instead.

PR Close #50072
2023-05-04 08:53:04 +02:00
Matthieu Riegler
5607e0f529 fix(core): typing of TestBed Common token. (#49997)
Both `ComponentFixtureAutoDetect`  and `ComponentFixtureNoNgZone` are mistyped. Tokens are only instantiated with booleans.

PR Close #49997
2023-04-25 09:27:08 -07:00
Matthieu Riegler
195aaa6b11 refactor(core): Create TestBed injector without a module (#49864)
We can create the injector without relying on th module factory.

PR Close #49864
2023-04-19 15:30:23 +00:00
Matthieu Riegler
da5071fc20 refactor(core): Remove ununsed Zone mock from testing internals. (#49873)
The last time it was used in was on the v10 branch.

PR Close #49873
2023-04-18 14:00:15 +00:00
Matthieu Riegler
38fe1b91fc refactor(core): drop IE workarounds (#49763)
Angular doesn't support IE anymore. We can remove the workarounds related to IE.

Some workarounds are keep because of the support of domino but the comments related to IE are removed.

PR Close #49763
2023-04-13 14:01:45 +00:00
Sandra Limacher
c912294979 docs: fix typo (#49669)
PR Close #49669
2023-04-03 19:18:40 -07:00
Andrew Scott
d7d6514add feat(core): Add ability to configure NgZone in bootstrapApplication (#49557)
This commit adds a provider function that allows developers to configure
the `NgZone` instance for the application. In the future, this provider
will be used for applications to specifically opt-in to change detection
powered by ZoneJS rather than it being provided by default.

This API does _not_ specifically provide support for developers to define their own
`NgZone` implementation or opt in to `NoopNgZone` directly. Both of
these are possible today, but are effectively unsupported (applications
that use these are left to their own devices to run change detection at
the appropriate times). That said, developers can still use DI in
`bootstrapApplication` to provide an `NgZone` implementation instead,
it's just not specifically available in the
`provideZoneChangeDetection` function.

PR Close #49557
2023-03-31 11:56:10 -07:00
Matthieu Riegler
230345876c fix(core): Allow TestBed.configureTestingModule to work with recursive cycle of standalone components. (#49473)
When having a recursive circle of imports on standalone components, `queueTypesFromModulesArrayRecur` triggered a `Maximum call stack size exceeded` error.
This commit fixes this.

Fixes #49469

PR Close #49473
2023-03-20 10:02:15 +01:00
Andrew Scott
4e098fa8a7 refactor(core): move Zone providers to a single provider function (#49373)
This commit moves the providers for `NgZone`-based change detection to a
single provider function. This function is currently called by default
in all places where `NgZone` was provided
(`bootstrapApplication`, `bootstrapModule`, and `TestBed`).

When we want to make Angular applications zoneless by default, we
can make a public provider method that has to be used in order to enable
the zone change detection features. When this method is not called,
Angular would use `NoopNgZone` by default and not initialize any
subscriptions to the `NgZone` stability events.

Side note: There are actually two places that `NgZone` is provided for `TestBed`
(providers in `compileTestModule` and `BrowserTestingModule`). This
likely doesn't need to be in both locations.

PR Close #49373
2023-03-14 09:20:53 -07:00
Matthieu Riegler
c8310a842d refactor(core): cleanup type any (#48623)
Removing every type any in core with a reference to #9100

PR Close #48623
2023-01-04 12:15:16 -08:00
Andrew Scott
e362214924 fix(common): Fix TestBed.overrideProvider type to include multi (#48424)
TestBed.overrideProvider should include `multi` support in its type. The
underlying implementation already supports it, as documented by the
tests which are currently casting the override to `any` to get around
the bad type.

PR Close #48424
2022-12-12 09:40:22 -08:00
Andrew Scott
6acae1477a feat(core): Add TestBed.runInInjectionContext to help test functions which use inject (#47955)
This commit adds `TestBed.runInInjectionContext` which is equivalent to
`TestBed.inject(EnvironmentInjector).runInContext`. This function will
help make tests for functions which call `inject` from `@angular/core` a
little bit less verbose.

PR Close #47955
2022-11-22 16:53:41 -08:00
Joey Perrott
303bb4d27c build: reformat BUILD files (#48181)
Reformat BUILD file usage of globs.

PR Close #48181
2022-11-22 21:22:34 +00:00
Derek Cormier
431c562815 build(bazel): add bazel targets for aio doc generation
This is an incremental step to produce dgeni output with bazel. The
generated outputs are not yet used by other targets.
2022-11-22 13:51:16 -07:00
Alex Rickabaugh
c5a1b90b25 refactor(core): support EnvironmentProviders types internally (#47669)
This commit modifies `R3Injector` and other code in Angular that deals with
providers, to handle `EnvironmentProviders` objects as well as normal
`Provider` types. There is no user-visible impact to this change, but it
prepares the core of the DI system for the introduction of
`EnvironmentProviders` as a public feature.

PR Close #47669
2022-10-07 14:03:13 -07:00
Andrew Kushnir
120555a626 feat(core): support object-based DI flags in TestBed.inject() (#46761)
This commit applies the changes similar to the ones performed for the `inject()` function in df246bb235.

The `TestBed.inject` function is updated to use previously added object-based API for options: now the flags argument supports passing an object which configures injection flags.

DEPRECATED:

The bit field signature of `TestBed.inject()` has been deprecated, in favor of the new options object.

PR Close #46761
2022-09-27 10:09:53 -07:00