Comments (14)
Fair enough, my comment was jest-centric. I have no idea about jasmine
from ng-mocks.
I believe it might be a good idea to migrate to reflectComponentType
from @angular/core to fetch inputs / outputs (and other data regarding the component). That is however quite some work, and I'm not sure if it is the correct path or even where / how / if that should start. But I have been able to confirm that reflectComponentType
does return inputs and outputs for signals, decorators and host-properties.
Do you have any input @satanTime?
from ng-mocks.
meta.inputs
is empty if we use input()
instead of @Input
__ngMocksDeclarations
doesn't contain the inputs (see libs/ng-mocks/src/lib/resolve/collect-declarations.ts
).
from ng-mocks.
It looks like, at the moment, Angular themselves test out signal inputs by manually defining and using wrapper components (i.e. without any reflection on the available inputs and types):
from ng-mocks.
Any news about this issue ?
from ng-mocks.
Potentially useful: the Spectator library is being updated to support signal inputs: ngneat/spectator#638
from ng-mocks.
Seems to me like this can be regarded as resolved.
jest-preset-angular added support for input signals, queries and models in ^14.0.1
For comparison, this is @Sebastian-G 's stackblitz with @angular-builders/[email protected]
which uses an older version of jest-preset-angular:
https://stackblitz.com/edit/ex-base-reusable-component-c33uk8?file=package.json ; the tests fail
This is the same stackblitz with @angular-builders/[email protected]
which uses jest-preset-angular@^14.0.1
:
https://stackblitz.com/edit/ex-base-reusable-component-zpyjgn?file=package.json ; the tests pass, aside from a snapshot test not matching, which I don't care about in this context
TL;DR: Update jest-preset-angular or dependencies that use jest-preset-angular
from ng-mocks.
How about when using Jasmine? (The default out-of-the-box experience that Angular provides)
from ng-mocks.
@cuddlecake — it's useful to know, thanks for sharing.
Aside: Angular does have official experimental support for Jest; I wonder where that will go.
from ng-mocks.
Not sure if this is a seperate issue or related, but I'm getting Error: NG0303
from signal components. Note that input.required
isn't needed, a simple input
gives the same error. Here is a test-case:
import { CommonModule } from '@angular/common';
import { Component, NgModule, input } from '@angular/core';
import { MockBuilder, MockRender } from 'ng-mocks';
@Component({
selector: 'signal-component',
template: '<h1>{{ header() }}</h1>',
})
class SignalComponent {
public readonly header = input<string>();
}
@Component({
selector: 'target-component',
template: '<signal-component [header]="hello"></signal-component>',
})
class TargetComponent {
public readonly hello = 'Hello!';
}
@NgModule({
declarations: [TargetComponent, SignalComponent],
imports: [CommonModule],
})
class TargetModule {}
describe('signal inputs', () => {
beforeEach(() => MockBuilder(TargetComponent, TargetModule));
it('should be created', () => {
expect(
MockRender(TargetComponent)?.point?.componentInstance,
).toBeTruthy();
});
});
from ng-mocks.
Hi @andreandersson, it used to be like that, but it doesn't represent all the data which is needed for mocks, that's why ng-mocks
replies on decorators data instead.
from ng-mocks.
Done some digging. This probably relates to angular/angular#54013
For example, this does not work in ng-mocks, but in a new Angular-repository:
import {
Component,
input,
reflectComponentType,
} from '@angular/core';
@Component({
selector: 'test-component',
template: '<h1>{{ header() }}</h1>',
})
class TestComponent {
public readonly header = input.required<string>();
}
describe('signals', () => {
it('should print header', () => {
expect(reflectComponentType(TestComponent)?.inputs?.length).toBe(1);
});
});
from ng-mocks.
I've noticed that Signal Inputs are just treated as Decorator Input.
I have the following component:
@Component({
selector: 'app-list-item',
standalone: true,
templateUrl: './list-item.component.html',
styleUrl: './list-item.component.scss',
})
export class ListItemComponent {
task = input.required<Task>();
}
I mock the component using MockComponent function:
await TestBed.configureTestingModule({
imports: [ListComponent],
providers: [
{
provide: TasksService,
useValue: tasksServiceStub,
},
],
})
.overrideComponent(ListComponent, {
remove: {
imports: [ListItemComponent],
},
add: {
imports: [MockComponent(ListItemComponent)], // I mock here
},
})
.compileComponents();
And my test try to access the Singal Input from ListItemComponent
to assert its value:
expect((<ListItemComponent>item.componentInstance).task()).toEqual(completedTasksStub[index]);
But the test breaks because the task
property (it's a Signal Input) is not a function.
The complete error:
TypeError: completedItemDebugEl.componentInstance.task is not a function
Logging the <ListItemComponent>item.componentInstance).task
in the test, I see that it's a property
task: { id: '1', name: 'Buy milk', completed: true }
Working with Signal Inputs, we need to test it as a Signal and not as a property as it was with Decorator Inputs.
Any news about that @satanTime?
Thank for your amazing work btw
from ng-mocks.
I just created an issue related to my last comment
#8887
from ng-mocks.
Related Issues (20)
- Feature Request: MockBuilder strict mode with standalone components
- docs: how to mock with `MockBuilder.provide`
- support vitest
- ngMocksUniverse.global
- Bug: MockMiddleware{Index} HOT 1
- update links in the repo HOT 2
- update sponsor links in the repo HOT 2
- Bug: cannot set return value of DI'ed pipe HOT 1
- Bug: `.keep(ReactiveFormsModule)` does not actually keep HOT 3
- Feature Request: Documentation for functional route guards without the use of NgModule
- Bug: Injection of mocked service fails for nested service ... but only sometimes ...
- Bug: cannot `.replace()` a component that has an abstract base class
- Bug: formatHtml on MockedComponentFixture / DebugElement won't give expected value
- Bug: try to avoid recursive parsing when a standalone component imports itself
- Bug: Incompatibility of MockComponent with new viewChild signal function HOT 8
- Bug: MockComponent (Standalone) mocks all components not just the ones specified HOT 3
- Bug: MockRender throws an error on mat-tab HOT 3
- MockComponent function set Signal Inputs as Decorator Inputs
- Bug: Erroneous test setup due to the usage of contentChildren HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ng-mocks.