|
1 |
| -# NgxTestingLibraryApp |
| 1 | +# ngx-testing-library |
2 | 2 |
|
3 |
| -Test your Angular components with the [dom-testing-library](https://github.com/kentcdodds/dom-testing-library). |
| 3 | +> Lightweight ulitity functions to test Angular components. |
4 | 4 |
|
5 |
| -Go from |
| 5 | +[![Styled with prettier][prettier-badge]][prettier] |
| 6 | +[![npm][npm-badge]][npm] |
| 7 | +[![MIT License][license-badge]][license] |
| 8 | +[![Code of Conduct][coc-badge]][coc] |
| 9 | + |
| 10 | +## Table of Contents |
| 11 | + |
| 12 | +- [Installation](#installation) |
| 13 | +- [Why](#why) |
| 14 | +- [What](#what) |
| 15 | +- [How](#how) |
| 16 | +- [Usage](#usage) |
| 17 | +- [Licence](#license) |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +Install `ngx-testing-library` from [npm] and add it your `devDependencies`: |
| 22 | + |
| 23 | +`npm install ngx-testing-library --save-dev` |
| 24 | + |
| 25 | +## Why |
| 26 | + |
| 27 | +- test your UI components the way your users are using it |
| 28 | +- making your tests resilient to implementation changes |
| 29 | + |
| 30 | +## What |
| 31 | + |
| 32 | +ngx-testing-library is an Angular adapter around [dom-testing-library][dom-testing-library], |
| 33 | +which provides lightweight ulitity functions to test UI components. Your tests will work with actual DOM nodes. |
| 34 | + |
| 35 | +## How |
| 36 | + |
| 37 | +### `createComponent` |
| 38 | + |
| 39 | +This library only consists of one function, `createComponent` which is used to setup the Angular `TestBed` and creates the component fixture. |
| 40 | + |
| 41 | +This method can be used in two ways: |
| 42 | + |
| 43 | +Based on a template: |
| 44 | + |
| 45 | +```ts |
| 46 | +import { createComponent } from 'ngx-testing-library'; |
| 47 | + |
| 48 | +createComponent('<my-component [prop]="1"></my-component>', options); |
| 49 | +``` |
| 50 | + |
| 51 | +Based on a component type: |
| 52 | + |
| 53 | +```ts |
| 54 | +import { createComponent } from 'ngx-testing-library'; |
| 55 | + |
| 56 | +createComponent( |
| 57 | + { |
| 58 | + component: MyComponent, |
| 59 | + parameters: { |
| 60 | + prop: 1, |
| 61 | + }, |
| 62 | + }, |
| 63 | + options, |
| 64 | +); |
| 65 | +``` |
| 66 | + |
| 67 | +The second parameter in `createComponent` is the `options` parameter, which looks like this: |
| 68 | + |
| 69 | +```ts |
| 70 | +{ |
| 71 | + detectChanges?: boolean = true; |
| 72 | + declarations: any[] = []; |
| 73 | + providers?: any[] = []; |
| 74 | + imports?: any[] = []; |
| 75 | + schemas?: any[] = []; |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +`detectChanges`: runs `detectChanges` on the fixture<br/> |
| 80 | +`declarations`: passed to the `TestBed`<br/> |
| 81 | +`providers`: passed to the `TestBed`<br/> |
| 82 | +`imports`: passed to the `TestBed`<br/> |
| 83 | +`schemas`: passed to the `TestBed`<br/> |
| 84 | + |
| 85 | +The `createComponent` function returns an object consisting all of the query functions from [dom-testing-library][dom-testing-library] and adds the following properties: |
| 86 | + |
| 87 | +#### `container: HTMLElement` |
| 88 | + |
| 89 | +The DOM node containing the Angular component. |
| 90 | + |
| 91 | +All of the [dom-testing-library][dom-testing-library] query functions are binded to this container. |
| 92 | + |
| 93 | +#### `debug() => void` |
| 94 | + |
| 95 | +Prints out the container. |
| 96 | + |
| 97 | +#### `detectChanges(checkNoChanges?: boolean) => void` |
| 98 | + |
| 99 | +Runs `detectChanges` on the fixture. |
| 100 | + |
| 101 | +#### `fixture: any` |
| 102 | + |
| 103 | +The Angular fixture. |
| 104 | + |
| 105 | +#### `get(token: any, notFoundValue?: any) => any` |
| 106 | + |
| 107 | +Is the Angular `TestBed.get` function. |
| 108 | + |
| 109 | +#### `getComponentInstance(selector?: string) => T` |
| 110 | + |
| 111 | +Gets the Angular component instance. |
| 112 | + |
| 113 | +The `selector` is required when the template syntax is being used, in order to get the component. |
| 114 | + |
| 115 | +### `fireEvent` |
| 116 | + |
| 117 | +Exposes the `fireEvent` from [dom-testing-library](dom-testing-library). |
| 118 | + |
| 119 | +```ts |
| 120 | +import { fireEvent } from 'ngx-testing-library'; |
| 121 | + |
| 122 | +fireEvent.click(buttonNode); |
| 123 | +``` |
| 124 | + |
| 125 | +## Usage |
| 126 | + |
| 127 | +You can find some examples in the [tests folder](https://github.com/tdeschryver/ngx-testing-library/tree/master/projects/ngx-testing-library/tests). |
| 128 | + |
| 129 | +Here is how "default" specifications are written with `ngx-testing-library`. |
| 130 | + |
| 131 | +Before: |
6 | 132 |
|
7 | 133 | ```ts
|
8 | 134 | import { TestBed, async } from '@angular/core/testing';
|
@@ -30,23 +156,42 @@ describe('AppComponent', () => {
|
30 | 156 | });
|
31 | 157 | ```
|
32 | 158 |
|
33 |
| -to |
| 159 | +After: |
34 | 160 |
|
35 | 161 | ```ts
|
36 | 162 | import { AppComponent } from './app.component';
|
37 | 163 | import { createComponent } from 'ngx-testing-library';
|
38 | 164 |
|
39 |
| -test(`should have as title 'my-awesome-app'`, async () => { |
| 165 | +it(`should have as title 'my-awesome-app'`, async () => { |
40 | 166 | const { detectChanges, getByText } = await createComponent('<app-root></app-root>', {
|
41 | 167 | declarations: [AppComponent],
|
42 | 168 | });
|
43 | 169 | expect(getByText('Welcome to my-awesome-app!')).toBeDefined();
|
44 | 170 | });
|
45 | 171 |
|
46 |
| -test(`should render title in a h1 tag`, async () => { |
47 |
| - const { container } = await createComponent('<app-root></app-root>', { |
48 |
| - declarations: [AppComponent], |
49 |
| - }); |
| 172 | +it(`should render title in a h1 tag`, async () => { |
| 173 | + const { container } = await createComponent( |
| 174 | + { |
| 175 | + component: AppComponent, |
| 176 | + }, |
| 177 | + { |
| 178 | + declarations: [AppComponent], |
| 179 | + }, |
| 180 | + ); |
50 | 181 | expect(container.querySelector('h1').textContent).toContain('Welcome to my-awesome-app!');
|
51 | 182 | });
|
52 | 183 | ```
|
| 184 | + |
| 185 | +## LICENSE |
| 186 | + |
| 187 | +MIT |
| 188 | + |
| 189 | +[prettier-badge]: https://img.shields.io/badge/styled_with-prettier-ff69b4.svg |
| 190 | +[prettier]: https://github.com/prettier/prettier |
| 191 | +[npm-badge]: https://img.shields.io/npm/v/ngx-testing-library.svg |
| 192 | +[npm]: https://www.npmjs.com/package/ngx-testing-library |
| 193 | +[license-badge]: https://img.shields.io/npm/l/ngx-testing-library.svg?style=flat-square |
| 194 | +[license]: https://github.com/tdeschryver/ngx-testing-library/blob/master/LICENSE |
| 195 | +[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square |
| 196 | +[coc]: https://github.com/tdeschryver/ngx-testing-library/blob/master/CODE_OF_CONDUCT.md |
| 197 | +[dom-testing-library]: https://github.com/kentcdodds/dom-testing-library |
0 commit comments