Skip to content

feat: allow to update wrapper properties #187

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

Merged
merged 3 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: allow to update wrapper properties
  • Loading branch information
timdeschryver committed Mar 14, 2021
commit 9b7cce1c1a9a5a52ccbace7f998af8e5daadd675
6 changes: 3 additions & 3 deletions projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
removeAngularAttributes?: boolean;
}

export interface RenderDirectiveOptions<DirectiveType, WrapperType, Q extends Queries = typeof queries>
extends RenderComponentOptions<DirectiveType, Q> {
export interface RenderDirectiveOptions<WrapperType, Properties extends object = {}, Q extends Queries = typeof queries>
extends RenderComponentOptions<Properties, Q> {
/**
* @description
* The template to render the directive.
Expand All @@ -278,7 +278,7 @@ export interface RenderDirectiveOptions<DirectiveType, WrapperType, Q extends Qu
* })
*/
wrapper?: Type<WrapperType>;
componentProperties?: Partial<any>;
componentProperties?: Partial<WrapperType & Properties>;
}

export interface Config {
Expand Down
21 changes: 14 additions & 7 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export async function render<ComponentType>(
): Promise<RenderResult<ComponentType, ComponentType>>;
export async function render<DirectiveType, WrapperType = WrapperComponent>(
component: Type<DirectiveType>,
renderOptions?: RenderDirectiveOptions<DirectiveType, WrapperType>,
): Promise<RenderResult<DirectiveType, WrapperType>>;
renderOptions?: RenderDirectiveOptions<WrapperType>,
): Promise<RenderResult<WrapperType>>;

export async function render<SutType, WrapperType = SutType>(
sut: Type<SutType>,
renderOptions: RenderComponentOptions<SutType> | RenderDirectiveOptions<SutType, WrapperType> = {},
renderOptions: RenderComponentOptions<SutType> | RenderDirectiveOptions<WrapperType> = {},
): Promise<RenderResult<SutType>> {
const {
detectChanges: detectChangesOnRender = true,
Expand All @@ -55,7 +55,7 @@ export async function render<SutType, WrapperType = SutType>(
excludeComponentDeclaration = false,
routes,
removeAngularAttributes = false,
} = renderOptions as RenderDirectiveOptions<SutType, WrapperType>;
} = renderOptions as RenderDirectiveOptions<WrapperType>;

const config = getConfig();

Expand Down Expand Up @@ -191,7 +191,7 @@ async function createComponent<SutType>(component: Type<SutType>): Promise<Compo

async function createComponentFixture<SutType>(
component: Type<SutType>,
{ template, wrapper }: Pick<RenderDirectiveOptions<SutType, any>, 'template' | 'wrapper'>,
{ template, wrapper }: Pick<RenderDirectiveOptions<any>, 'template' | 'wrapper'>,
): Promise<ComponentFixture<SutType>> {
if (template) {
TestBed.overrideTemplate(wrapper, template);
Expand All @@ -205,7 +205,14 @@ function setComponentProperties<SutType>(
{ componentProperties = {} }: Pick<RenderDirectiveOptions<SutType, any>, 'componentProperties'>,
) {
for (const key of Object.keys(componentProperties)) {
fixture.componentInstance[key] = componentProperties[key];
let _value = componentProperties[key];
Object.defineProperty(fixture.componentInstance, key, {
get: () => _value ,
set: (value) => {
_value = value;
fixture.detectChanges();
}
});
}
return fixture;
}
Expand Down Expand Up @@ -235,7 +242,7 @@ function addAutoDeclarations<SutType>(
template,
wrapper,
}: Pick<
RenderDirectiveOptions<SutType, any>,
RenderDirectiveOptions<any>,
'declarations' | 'excludeComponentDeclaration' | 'template' | 'wrapper'
>,
) {
Expand Down
27 changes: 27 additions & 0 deletions projects/testing-library/tests/directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ export class OnOffDirective {
this.clicked.emit(this.el.nativeElement.textContent);
}
}

@Directive({
selector: '[update]',
})
export class UpdateInputDirective {
@Input()
set update(value: any) {
this.el.nativeElement.textContent = value;
}

constructor(private el: ElementRef) {}
}

test('the directive renders', async () => {
const component = await render(OnOffDirective, {
template: '<div onOff></div>',
Expand Down Expand Up @@ -98,3 +111,17 @@ describe('removeAngularAttributes', () => {
expect(document.querySelector('[id]')).not.toBeNull();
});
});


test('updates properties and invokes change detection', async () => {
const component = await render(UpdateInputDirective, {
template: '<div [update]="value" ></div>',
componentProperties: {
value: 'value1'
}
});

component.getByText('value1')
component.fixture.componentInstance.value = 'updated value'
component.getByText('updated value')
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Injectable } from '@angular/core';
import { Component } from '@angular/core';
import { render } from '../../src/public_api';

// tslint:disable: no-use-before-declare
test('shows the service value', async () => {
const { getByText } = await render(FixtureComponent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Injectable } from '@angular/core';
import { Component } from '@angular/core';
import { render } from '../../src/public_api';

// tslint:disable: no-use-before-declare
test('shows the service value', async () => {
const { getByText } = await render(FixtureComponent, {
providers: [Service],
Expand Down