Skip to content

Commit 3c2df01

Browse files
committed
refactor: rename createComponent to render
BREAKING CHANGE: The createComponent function has been renamed to render
1 parent d39b758 commit 3c2df01

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Lightweight utility functions to test Angular components.
2424
- [Why](#why)
2525
- [What](#what)
2626
- [How](#how)
27-
- [`createComponent`](#createcomponent)
27+
- [`render`](#render)
2828
- [`container: HTMLElement`](#container-htmlelement)
2929
- [`debug(element: HTMLElement) => void`](#debug--void)
3030
- [`fixture: any`](#fixture-any)
@@ -49,26 +49,26 @@ which provides lightweight utility functions to test UI components. Your tests w
4949

5050
## How
5151

52-
### `createComponent`
52+
### `render`
5353

54-
This library only consists of one function, `createComponent` which is used to setup the Angular `TestBed` and creates the component fixture.
54+
This library only consists of one function, `render` which is used to setup the Angular `TestBed` and creates the component fixture.
5555

5656
This method can be used in two ways:
5757

5858
Based on a template:
5959

6060
```ts
61-
import { createComponent } from '@angular-extensions/testing-library';
61+
import { render } from '@angular-extensions/testing-library';
6262

63-
createComponent('<my-component [prop]="1"></my-component>', options);
63+
render('<my-component [prop]="1"></my-component>', options);
6464
```
6565

6666
Based on a component type:
6767

6868
```ts
69-
import { createComponent } from '@angular-extensions/testing-library';
69+
import { render } from '@angular-extensions/testing-library';
7070

71-
createComponent(
71+
render(
7272
{
7373
component: MyComponent,
7474
parameters: {
@@ -79,7 +79,7 @@ createComponent(
7979
);
8080
```
8181

82-
The second parameter in `createComponent` is the `options` parameter, which looks like this:
82+
The second parameter in `render` is the `options` parameter, which looks like this:
8383

8484
```ts
8585
{
@@ -101,7 +101,7 @@ The second parameter in `createComponent` is the `options` parameter, which look
101101

102102
`schemas`: passed to the `TestBed`
103103

104-
The `createComponent` function returns an object consisting all of the query functions from [dom-testing-library][dom-testing-library], all the event functions exposed from `fireEvent`, and adds the following properties:
104+
The `render` function returns an object consisting all of the query functions from [dom-testing-library][dom-testing-library], all the event functions exposed from `fireEvent`, and adds the following properties:
105105

106106
> Every event runs `detectChanges` on the fixture.
107107
@@ -139,13 +139,13 @@ describe('AppComponent', () => {
139139
}));
140140

141141
it(`should have as title 'my-awesome-app'`, async(() => {
142-
const fixture = TestBed.createComponent(AppComponent);
142+
const fixture = TestBed.render(AppComponent);
143143
const app = fixture.debugElement.componentInstance;
144144
expect(app.title).toEqual('my-awesome-app');
145145
}));
146146

147147
it(`should render title in a h1 tag`, async(() => {
148-
const fixture = TestBed.createComponent(AppComponent);
148+
const fixture = TestBed.render(AppComponent);
149149
fixture.detectChanges();
150150
const compiled = fixture.debugElement.nativeElement;
151151
expect(compiled.querySelector('h1').textContent).toContain('Welcome to my-awesome-app!');
@@ -156,18 +156,18 @@ describe('AppComponent', () => {
156156
After:
157157

158158
```ts
159-
import { createComponent } from '@angular-extensions/testing-library';
159+
import { render } from '@angular-extensions/testing-library';
160160
import { AppComponent } from './app.component';
161161

162162
it(`should have as title 'my-awesome-app'`, async () => {
163-
const { getByText } = await createComponent('<app-root></app-root>', {
163+
const { getByText } = await render('<app-root></app-root>', {
164164
declarations: [AppComponent],
165165
});
166166
expect(getByText('Welcome to my-awesome-app!')).toBeDefined();
167167
});
168168

169169
it(`should render title in a h1 tag`, async () => {
170-
const { container } = await createComponent(
170+
const { container } = await render(
171171
{
172172
component: AppComponent,
173173
},

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class TestComponent implements OnInit {
1313
}
1414
}
1515

16-
export async function createComponent<T>(template: string, options: Options): Promise<RenderResult>;
17-
export async function createComponent<T>(component: ComponentInput<T>, options: Options): Promise<RenderResult>;
18-
export async function createComponent<T>(
16+
export async function render<T>(template: string, options: Options): Promise<RenderResult>;
17+
export async function render<T>(component: ComponentInput<T>, options: Options): Promise<RenderResult>;
18+
export async function render<T>(
1919
templateOrComponent: string | ComponentInput<T>,
2020
{ detectChanges = true, declarations = [], providers = [], imports = [], schemas = [] }: Options,
2121
): Promise<RenderResult> {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { Component, Input } from '@angular/core';
2-
import { createComponent } from '../../src/public_api';
2+
import { render } from '../../src/public_api';
33

44
@Component({
55
selector: 'counter',
66
template: `
7-
<button (click)="decrement()">-</button>
8-
<span data-testid="count">Current Count: {{ counter }}</span>
7+
<button (click)="decrement()">-</button> <span data-testid="count">Current Count: {{ counter }}</span>
98
<button (click)="increment()">+</button>
109
`,
1110
})
@@ -21,7 +20,7 @@ export class CounterComponent {
2120
}
2221

2322
test('Counter actions via template syntax', async () => {
24-
const { getByText, getByTestId, click } = await createComponent('<counter [counter]="10"></counter>', {
23+
const { getByText, getByTestId, click } = await render('<counter [counter]="10"></counter>', {
2524
declarations: [CounterComponent],
2625
});
2726

@@ -35,7 +34,7 @@ test('Counter actions via template syntax', async () => {
3534
});
3635

3736
test('Counter actions via component syntax', async () => {
38-
const { getByText, getByTestId, click } = await createComponent(
37+
const { getByText, getByTestId, click } = await render(
3938
{
4039
component: CounterComponent,
4140
parameters: {
@@ -57,7 +56,7 @@ test('Counter actions via component syntax', async () => {
5756
});
5857

5958
test('Counter actions via component syntax without parameters', async () => {
60-
const { getByText, getByTestId, click } = await createComponent(
59+
const { getByText, getByTestId, click } = await render(
6160
{
6261
component: CounterComponent,
6362
},

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { Component, Input } from '@angular/core';
2-
import { createComponent } from '../src/public_api';
2+
import { render } from '../src/public_api';
33

44
@Component({
55
selector: 'fixture',
6-
template: `<p>rawr</p>`,
6+
template: `
7+
<p>rawr</p>
8+
`,
79
})
810
class FixtureComponent {}
911

1012
test('debug', async () => {
1113
jest.spyOn(console, 'log').mockImplementation(() => {});
12-
const { debug } = await createComponent('<fixture></fixture>', {
14+
const { debug } = await render('<fixture></fixture>', {
1315
declarations: [FixtureComponent],
1416
});
1517
debug();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ReactiveFormsModule } from '@angular/forms';
2-
import { createComponent } from '../../src/public_api';
2+
import { render } from '../../src/public_api';
33
import { LoginFormComponent } from './form.component';
44

55
test('login form submits using the component syntax', async () => {
@@ -8,7 +8,7 @@ test('login form submits using the component syntax', async () => {
88
emit: jest.fn(),
99
};
1010

11-
const { container, getByLabelText, getByText, input, submit } = await createComponent(
11+
const { container, getByLabelText, getByText, input, submit } = await render(
1212
{
1313
component: LoginFormComponent,
1414
parameters: {

src/app/app.component.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import { AppComponent } from './app.component';
2-
import { createComponent, configureJestSetup } from '@angular-extensions/testing-library';
2+
import { render, configureJestSetup } from '@angular-extensions/testing-library';
33

44
configureJestSetup();
55

66
test(`matches snapshot`, async () => {
7-
const { container } = await createComponent('<app-root></app-root>', {
7+
const { container } = await render('<app-root></app-root>', {
88
declarations: [AppComponent],
99
});
1010
expect(container).toMatchSnapshot();
1111
});
1212

1313
test(`should have a title`, async () => {
14-
const { getByText } = await createComponent('<app-root></app-root>', {
14+
const { getByText } = await render('<app-root></app-root>', {
1515
declarations: [AppComponent],
1616
});
1717
expect(getByText('Welcome to app!')).toBeDefined();
1818
});
1919

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

0 commit comments

Comments
 (0)