render()
Renders a component, or a directive on a Lynx element, as a Lynx app in a unit test, and waits for Angular to update it. import { render, renderDirective, waitForUpdate } from '@blotch/angular-lynx-testing-library';
render(component, options?)
Bootstraps component as a Lynx app and renders it into the test's DOM. It resolves once the first render is done.
- It starts clean. It first destroys the app a previous
render()left, and clearsdocument.body. - Providers: the app gets
provideRenderer()and youroptions.providers. Change detection is zoneless, Angular's default, as in your app. - The tree: elements render under a
<page>element, the result'scontainer. - The splash screen: like
bootstrapApplication(), it hides the splash screen once the component is rendered.- It doesn't when
options.providershasprovideSplashScreen(). - A spec reads
LynxSplashScreen.visible()to check when the app hides it.
- It doesn't when
options.inputs
inputs are set before the first change detection, as a parent template binds them.
- A required input has its value when the template first reads it.
ngOnInitsees them all.render()rejects a name that isn't one of the component's inputs, and lists the ones it has.- Change them later with
rerender({ inputs }).
options.bindings
bindings are Angular's own, as TestBed.createComponent() and createComponent() take them. Use them to hear an output or to bind a model() to a signal.
outputBinding()listens from the moment Angular creates the component. It hears an output emitted inngOnInit, which subscribing oncomponentRef.instanceafterrender()would miss.twoWayBinding()sets the input from the signal, and sets the signal when the component emits itsChangeoutput. Set the signal, then awaitwaitForUpdate(), to change the component.inputBinding()works too. Preferinputsunless the value comes from a signal:render()checks each name ininputs, but it can't read a binding's name.- A wrong name: an output the component doesn't have fails
render()with Angular's error (NG0316). A wrong input name in a binding goes only to theErrorHandler. componentRef.setInput()fails after aninputBinding()ortwoWayBinding(), as afterinputs(NG0317). Output bindings leave it working.
options.initialURL
initialURL is the link that started the app, such as myapp://orders/42.
LynxLinking.getInitialURL()returns it.- With
provideRouter(), the app's first screen is the link's route, with nothing under it. - A link the router has no route for starts the app at
/, as on a phone. - Left out,
getInitialURL()isnull. - A link that arrives while the app is open is
openLink()'s.
renderDirective(directive, options?)
Renders a standalone directive on a Lynx element, with no host component to write. It's the Lynx counterpart of Angular's TestBed.createDirective(), as render() is of TestBed.createComponent().
- The element:
options.tagName, such as'view'. Left out, it's the element the selector names:text[appBold]goes on a<text>. - A selector with no element or several needs
tagName.[appHighlight]names none, andview[appX], text[appX]names two. WithouttagName, it throws Angular's error for each. - The tree: the element is the only child of the
<page>, the result'scontainer. - The rest is
render()'s.inputs,bindings,providers,queriesandinitialURLwork as they do there, and so dorerender({ inputs }),fireEvent,waitForUpdate()andcleanup(). - It rejects a component, pointing to
render(), andrender()rejects a directive, pointing here.
Unlike TestBed.createDirective(), the directive's element isn't the root. On the web, the fixture's nativeElement is the directive's element. A Lynx app's root is always a <page>, which can't be a <view> or a <text>, so the element sits inside it. Read it as container.firstElementChild.
Unlike TestBed.createDirective(), it takes only standalone directives. TestBed applies one declared in an NgModule through an option Angular keeps private. renderDirective() uses Angular's public createComponent() instead, so an Angular update can't break it, and that applies only standalone directives (NG0907). Directives are standalone unless they say standalone: false, so remove that, as render() asks of components.
It takes inputs next to bindings. As with render(), you key input values by name, and it binds each with inputBinding(). Pass outputBinding() and twoWayBinding() in bindings, as for TestBed.createDirective().
cleanup()
Destroys the rendered app.
- It runs after every test, so you rarely call it.
- To turn that off, set the environment variable
ATL_SKIP_AUTO_CLEANUP=true.
waitForUpdate()
Waits one task (a setTimeout of 0), so the change detection Angular scheduled can run. Then it runs change detection once more.
Await it after fireEvent or after setting a signal, before you check the rendered output.
Types
RenderOptions
RenderInputs
Each of the component's properties takes the value its input does.
- For
readonly count = input(0),inputs: { count: 'three' }doesn't compile. - Other names are allowed too, such as
classforinput('', { alias: 'class' }). render()checks every name when the test runs.
RenderResult
rerender({ inputs }) sets the inputs you pass, runs change detection, and resolves when the tree shows them. The component keeps its state.
- Inputs you leave out keep their values. Angular has no way to put an input back to its initial value.
- It only changes inputs
render()set. Angular sets those through input bindings, and a component with input bindings takes no other inputs. To change an input later, giverender()its starting value. componentRef.setInput()fails afterrender()sets inputs, for the same reason (NG0317). Usererender({ inputs }).
ng test compiles components ahead of time, so inputs, bindings, rerender({ inputs }) and componentRef.setInput() all reach input(), model() and output().
Plain Vitest compiles components at runtime, where Angular sees only @Input(), @Output() and inputs and outputs metadata. Set a writable signal on componentRef.instance there instead.
RenderDirectiveOptions
render()'s options, plus:
