Skip to content

Commit 46bc4a0

Browse files
author
Simon Mumenthaler
committed
rename option to
1 parent b935d10 commit 46bc4a0

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

projects/testing-library/src/lib/models.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ComponentFixture, DeferBlockBehavior, DeferBlockState, TestBed } from '
33
import { Routes } from '@angular/router';
44
import { BoundFunction, Queries, queries, Config as dtlConfig, PrettyDOMOptions } from '@testing-library/dom';
55

6-
export type SubscribeToOutputsKeysWithCallback<T> = {
6+
export type OutputRefKeysWithCallback<T> = {
77
[key in keyof T as T[key] extends OutputRef<any> ? key : never]?: T[key] extends OutputRef<infer U>
88
? (val: U) => void
99
: never;
@@ -66,7 +66,7 @@ export interface RenderResult<ComponentType, WrapperType = ComponentType> extend
6666
rerender: (
6767
properties?: Pick<
6868
RenderTemplateOptions<ComponentType>,
69-
'componentProperties' | 'componentInputs' | 'componentOutputs' | 'subscribeToOutputs' | 'detectChangesOnRender'
69+
'componentProperties' | 'componentInputs' | 'componentOutputs' | 'on' | 'detectChangesOnRender'
7070
> & { partialUpdate?: boolean },
7171
) => Promise<void>;
7272
/**
@@ -211,7 +211,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
211211
/**
212212
* @description
213213
* An object to set `@Output` properties of the component
214-
* @deprecated use the `subscribeToOutputs` option instead. When actually wanting to override properties, use the `componentProperties` option.
214+
* @deprecated use the `on` option instead. When it is necessary to override properties, use the `componentProperties` option.
215215
* @default
216216
* {}
217217
*
@@ -237,12 +237,12 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
237237
* @example
238238
* const sendValue = (value) => { ... }
239239
* await render(AppComponent, {
240-
* subscribeToOutputs: {
240+
* on: {
241241
* send: (_v:any) => void
242242
* }
243243
* })
244244
*/
245-
subscribeToOutputs?: SubscribeToOutputsKeysWithCallback<ComponentType>;
245+
on?: OutputRefKeysWithCallback<ComponentType>;
246246

247247
/**
248248
* @description

projects/testing-library/src/lib/testing-library.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
RenderComponentOptions,
3333
RenderResult,
3434
RenderTemplateOptions,
35-
SubscribeToOutputsKeysWithCallback,
35+
OutputRefKeysWithCallback,
3636
} from './models';
3737
import { getConfig } from './config';
3838

@@ -67,7 +67,7 @@ export async function render<SutType, WrapperType = SutType>(
6767
componentProperties = {},
6868
componentInputs = {},
6969
componentOutputs = {},
70-
subscribeToOutputs = {},
70+
on = {},
7171
componentProviders = [],
7272
childComponentOverrides = [],
7373
componentImports: componentImports,
@@ -185,7 +185,7 @@ export async function render<SutType, WrapperType = SutType>(
185185
properties: Partial<SutType>,
186186
inputs: Partial<SutType>,
187187
outputs: Partial<SutType>,
188-
subscribeTo: SubscribeToOutputsKeysWithCallback<SutType>,
188+
subscribeTo: OutputRefKeysWithCallback<SutType>,
189189
): Promise<ComponentFixture<SutType>> => {
190190
const createdFixture: ComponentFixture<SutType> = await createComponent(componentContainer);
191191
setComponentProperties(createdFixture, properties);
@@ -224,7 +224,7 @@ export async function render<SutType, WrapperType = SutType>(
224224
return createdFixture;
225225
};
226226

227-
const fixture = await renderFixture(componentProperties, componentInputs, componentOutputs, subscribeToOutputs);
227+
const fixture = await renderFixture(componentProperties, componentInputs, componentOutputs, on);
228228

229229
if (deferBlockStates) {
230230
if (Array.isArray(deferBlockStates)) {
@@ -239,7 +239,7 @@ export async function render<SutType, WrapperType = SutType>(
239239
const rerender = async (
240240
properties?: Pick<
241241
RenderTemplateOptions<SutType>,
242-
'componentProperties' | 'componentInputs' | 'componentOutputs' | 'subscribeToOutputs' | 'detectChangesOnRender'
242+
'componentProperties' | 'componentInputs' | 'componentOutputs' | 'on' | 'detectChangesOnRender'
243243
> & { partialUpdate?: boolean },
244244
) => {
245245
const newComponentInputs = properties?.componentInputs ?? {};
@@ -262,15 +262,15 @@ export async function render<SutType, WrapperType = SutType>(
262262
renderedOutputKeys = Object.keys(newComponentOutputs);
263263

264264
// first unsubscribe the no longer available or changed callback-fns
265-
const newSubscribeToOutputs: SubscribeToOutputsKeysWithCallback<SutType> = properties?.subscribeToOutputs ?? {};
265+
const newObservableSubscriptions: OutputRefKeysWithCallback<SutType> = properties?.on ?? {};
266266
for (const [key, cb, subscription] of subscribedOutputs) {
267267
// when no longer provided or when the callback has changed
268-
if (!(key in newSubscribeToOutputs) || cb !== (newSubscribeToOutputs as any)[key]) {
268+
if (!(key in newObservableSubscriptions) || cb !== (newObservableSubscriptions as any)[key]) {
269269
subscription.unsubscribe();
270270
}
271271
}
272272
// then subscribe the new callback-fns
273-
subscribedOutputs = Object.entries(newSubscribeToOutputs).map(([key, cb]) => {
273+
subscribedOutputs = Object.entries(newObservableSubscriptions).map(([key, cb]) => {
274274
const existing = subscribedOutputs.find(([k]) => k === key);
275275
return existing && existing[1] === cb
276276
? existing // nothing to do
@@ -388,7 +388,7 @@ function setComponentInputs<SutType>(
388388

389389
function subscribeToComponentOutputs<SutType>(
390390
fixture: ComponentFixture<SutType>,
391-
listeners: SubscribeToOutputsKeysWithCallback<SutType>,
391+
listeners: OutputRefKeysWithCallback<SutType>,
392392
): SubscribedOutput<SutType>[] {
393393
// with Object.entries we lose the type information of the key and callback, therefore we need to cast them
394394
return Object.entries(listeners).map(([key, cb]) =>

projects/testing-library/tests/render.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe('componentOutputs', () => {
187187
});
188188
});
189189

190-
describe('subscribeToOutputs', () => {
190+
describe('on', () => {
191191
@Component({ template: ``, standalone: true })
192192
class TestFixtureWithEventEmitterComponent {
193193
@Output() readonly event = new EventEmitter<void>();
@@ -205,15 +205,15 @@ describe('subscribeToOutputs', () => {
205205

206206
it('should subscribe passed listener to the component EventEmitter', async () => {
207207
const spy = jest.fn();
208-
const { fixture } = await render(TestFixtureWithEventEmitterComponent, { subscribeToOutputs: { event: spy } });
208+
const { fixture } = await render(TestFixtureWithEventEmitterComponent, { on: { event: spy } });
209209
fixture.componentInstance.event.emit();
210210
expect(spy).toHaveBeenCalled();
211211
});
212212

213213
it('should unsubscribe on rerender without listener', async () => {
214214
const spy = jest.fn();
215215
const { fixture, rerender } = await render(TestFixtureWithEventEmitterComponent, {
216-
subscribeToOutputs: { event: spy },
216+
on: { event: spy },
217217
});
218218

219219
await rerender({});
@@ -225,10 +225,10 @@ describe('subscribeToOutputs', () => {
225225
it('should not unsubscribe when same listener function is used on rerender', async () => {
226226
const spy = jest.fn();
227227
const { fixture, rerender } = await render(TestFixtureWithEventEmitterComponent, {
228-
subscribeToOutputs: { event: spy },
228+
on: { event: spy },
229229
});
230230

231-
await rerender({ subscribeToOutputs: { event: spy } });
231+
await rerender({ on: { event: spy } });
232232

233233
fixture.componentInstance.event.emit();
234234
expect(spy).toHaveBeenCalled();
@@ -237,11 +237,11 @@ describe('subscribeToOutputs', () => {
237237
it('should unsubscribe old and subscribe new listener function on rerender', async () => {
238238
const firstSpy = jest.fn();
239239
const { fixture, rerender } = await render(TestFixtureWithEventEmitterComponent, {
240-
subscribeToOutputs: { event: firstSpy },
240+
on: { event: firstSpy },
241241
});
242242

243243
const newSpy = jest.fn();
244-
await rerender({ subscribeToOutputs: { event: newSpy } });
244+
await rerender({ on: { event: newSpy } });
245245

246246
fixture.componentInstance.event.emit();
247247

@@ -252,7 +252,7 @@ describe('subscribeToOutputs', () => {
252252
it('should subscribe passed listener to a derived component output', async () => {
253253
const spy = jest.fn();
254254
const { fixture } = await render(TestFixtureWithDerivedEventComponent, {
255-
subscribeToOutputs: { event: spy },
255+
on: { event: spy },
256256
});
257257
fireEvent.click(fixture.nativeElement);
258258
expect(spy).toHaveBeenCalled();
@@ -261,7 +261,7 @@ describe('subscribeToOutputs', () => {
261261
it('should subscribe passed listener to a functional component output', async () => {
262262
const spy = jest.fn();
263263
const { fixture } = await render(TestFixtureWithFunctionalOutputComponent, {
264-
subscribeToOutputs: { event: spy },
264+
on: { event: spy },
265265
});
266266
fixture.componentInstance.event.emit('test');
267267
expect(spy).toHaveBeenCalledWith('test');
@@ -274,7 +274,7 @@ describe('subscribeToOutputs', () => {
274274
}
275275
const spy = jest.fn();
276276
const { fixture } = await render(TestFixtureWithFunctionalDerivedEventComponent, {
277-
subscribeToOutputs: { event: spy },
277+
on: { event: spy },
278278
});
279279
fireEvent.click(fixture.nativeElement);
280280
expect(spy).toHaveBeenCalled();

0 commit comments

Comments
 (0)