Skip to content

feat: use render without render options #21

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
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ class WrapperComponent implements OnInit {
}
}

export async function render<T>(template: string, renderOptions: RenderOptions<T>);
export async function render<T>(component: Type<T>, renderOptions?: RenderOptions<T>);
export async function render<T>(
templateOrComponent: string | Type<T>,
{
renderOptions: RenderOptions<T> = {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't clear in the issue description, but this should only be possible when we define our components as a type, for example of you would use render('<component></component>') we should still make it required to define render options. This is because we have to have to component's type to add it to the declarations in the Angular DI.

That's why I would propose the following types if you agree:

export async function render<T>(templateOrComponent: string, renderOptions: RenderOptions<T>);
export async function render<T>(templateOrComponent: Type<T>, renderOptions?: RenderOptions<T>);
export async function render<T>(
  templateOrComponent: string | Type<T>,
  renderOptions: RenderOptions<T> = {},
): Promise<RenderResult>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Tim.
I was aware of this issue, but I didn't want to change what was already there. At the moment it's possible to create a component with a string template and an empty object. That will fail because of an unknown element. Making it not optional with string templates will still allow to pass an empty object. I agree this would give a better hint.

also: Bug or feature, these tests will pass (haven't done more research yet)

test(`should render title in a h1 tag`, async () => {
  const { container } = await render('<app-root></app-root>', {
    declarations: [AppComponent],
  });
  expect(container.querySelector('h1').textContent).toContain('Welcome to app!');
});

// string template and no options
test(`should render title in a h1 tag`, async () => {
  const { container } = await render('<app-root></app-root>');
  expect(container.querySelector('h1').textContent).toContain('Welcome to app!');
});

removing the first test, will make the second one fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about the string template with an empty object. To solve this we could create a new RenderOptions interface.

For the failing test, it's an issue. And probably because of the configureJest() method that does not clear up correctly after a test. This isn't "published" yet and was more a POC for now 🙂

): Promise<RenderResult> {
const {
detectChanges = true,
declarations = [],
imports = [],
Expand All @@ -24,8 +28,8 @@ export async function render<T>(
queries,
wrapper = WrapperComponent,
componentProperties = {},
}: RenderOptions<T>,
): Promise<RenderResult> {
} = renderOptions;

const isTemplate = typeof templateOrComponent === 'string';
const componentDeclarations = isTemplate ? [wrapper] : [templateOrComponent];

Expand Down
12 changes: 12 additions & 0 deletions projects/testing-library/tests/counter/counter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ test('Counter actions via component syntax', async () => {
expect(getByTestId('count').textContent).toBe('Current Count: 0');
});

test('Counter actions via component syntax without render options', async () => {
const { getByText, getByTestId, click } = await render(CounterComponent);

click(getByText('+'));
expect(getByText('Current Count: 1')).toBeTruthy();
expect(getByTestId('count').textContent).toBe('Current Count: 1');

click(getByText('-'));
expect(getByText('Current Count: 0')).toBeTruthy();
expect(getByTestId('count').textContent).toBe('Current Count: 0');
});

test('Counter actions via component syntax with parameters', async () => {
const { getByText, getByTestId, click } = await render(CounterComponent, {
declarations: [CounterComponent],
Expand Down