Skip to content

Commit 57ced23

Browse files
committed
test: write Jest tests with provideZoneChangeDetection
1 parent 67f29a1 commit 57ced23

28 files changed

+111
-105
lines changed

apps/example-app-jest/jest.config.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ module.exports = {
33
setupFilesAfterEnv: ['<rootDir>/apps/example-app-jest/src/test-setup.ts'],
44
modulePathIgnorePatterns: ['<rootDir>/dist'],
55
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/dist/'],
6-
globals: {
7-
'ts-jest': {
8-
tsconfig: '<rootDir>/apps/example-app-jest/tsconfig.spec.json',
9-
stringifyContentPathRegex: '\\.(html|svg)$',
10-
},
6+
transform: {
7+
'^.+.ts?$': [
8+
'ts-jest',
9+
{
10+
tsconfig: '<rootDir>/apps/example-app-jest/tsconfig.spec.json',
11+
stringifyContentPathRegex: '\\.(html|svg)$',
12+
},
13+
],
1114
},
1215
coverageDirectory: 'coverage/apps/example-app-jest',
1316
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],

apps/example-app-jest/src/app/examples/00-single-component.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { provideZoneChangeDetection } from '@angular/core';
12
import { render, screen } from '@testing-library/angular';
23
import userEvent from '@testing-library/user-event';
34

45
import { SingleComponent } from './00-single-component';
56

67
test('renders the current value and can increment and decrement', async () => {
78
const user = userEvent.setup();
8-
await render(SingleComponent);
9+
await render(SingleComponent, {
10+
providers: [provideZoneChangeDetection()],
11+
});
912

1013
const incrementControl = screen.getByRole('button', { name: /increment/i });
1114
const decrementControl = screen.getByRole('button', { name: /decrement/i });

apps/example-app-jest/src/app/examples/01-nested-component.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { provideZoneChangeDetection } from '@angular/core';
12
import { render, screen } from '@testing-library/angular';
23
import userEvent from '@testing-library/user-event';
34

45
import { NestedContainerComponent } from './01-nested-component';
56

67
test('renders the current value and can increment and decrement', async () => {
78
const user = userEvent.setup();
8-
await render(NestedContainerComponent);
9+
await render(NestedContainerComponent, {
10+
providers: [provideZoneChangeDetection()],
11+
});
912

1013
const incrementControl = screen.getByRole('button', { name: /increment/i });
1114
const decrementControl = screen.getByRole('button', { name: /decrement/i });

apps/example-app-jest/src/app/examples/02-input-output.spec.ts

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { provideZoneChangeDetection } from '@angular/core';
12
import { render, screen } from '@testing-library/angular';
23
import userEvent from '@testing-library/user-event';
34

@@ -14,6 +15,7 @@ test('is possible to set input and listen for output', async () => {
1415
on: {
1516
sendValue,
1617
},
18+
providers: [provideZoneChangeDetection()],
1719
});
1820

1921
const incrementControl = screen.getByRole('button', { name: /increment/i });
@@ -32,33 +34,6 @@ test('is possible to set input and listen for output', async () => {
3234
expect(sendValue).toHaveBeenCalledWith(50);
3335
});
3436

35-
test.skip('is possible to set input and listen for output with the template syntax', async () => {
36-
const user = userEvent.setup();
37-
const sendSpy = jest.fn();
38-
39-
await render('<atl-fixture [value]="47" (sendValue)="sendValue($event)" />', {
40-
imports: [InputOutputComponent],
41-
on: {
42-
sendValue: sendSpy,
43-
},
44-
});
45-
46-
const incrementControl = screen.getByRole('button', { name: /increment/i });
47-
const sendControl = screen.getByRole('button', { name: /send/i });
48-
const valueControl = screen.getByTestId('value');
49-
50-
expect(valueControl).toHaveTextContent('47');
51-
52-
await user.click(incrementControl);
53-
await user.click(incrementControl);
54-
await user.click(incrementControl);
55-
expect(valueControl).toHaveTextContent('50');
56-
57-
await user.click(sendControl);
58-
expect(sendSpy).toHaveBeenCalledTimes(1);
59-
expect(sendSpy).toHaveBeenCalledWith(50);
60-
});
61-
6237
test('is possible to set input and listen for output (deprecated)', async () => {
6338
const user = userEvent.setup();
6439
const sendValue = jest.fn();
@@ -72,6 +47,7 @@ test('is possible to set input and listen for output (deprecated)', async () =>
7247
emit: sendValue,
7348
} as any,
7449
},
50+
providers: [provideZoneChangeDetection()],
7551
});
7652

7753
const incrementControl = screen.getByRole('button', { name: /increment/i });
@@ -99,6 +75,7 @@ test('is possible to set input and listen for output with the template syntax (d
9975
componentProperties: {
10076
sendValue: sendSpy,
10177
},
78+
providers: [provideZoneChangeDetection()],
10279
});
10380

10481
const incrementControl = screen.getByRole('button', { name: /increment/i });

apps/example-app-jest/src/app/examples/03-forms.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { provideZoneChangeDetection } from '@angular/core';
12
import { render, screen, fireEvent } from '@testing-library/angular';
23
import userEvent from '@testing-library/user-event';
34

45
import { FormsComponent } from './03-forms';
56

67
test('is possible to fill in a form and verify error messages (with the help of jest-dom https://testing-library.com/docs/ecosystem-jest-dom)', async () => {
78
const user = userEvent.setup();
8-
await render(FormsComponent);
9+
await render(FormsComponent, {
10+
providers: [provideZoneChangeDetection()],
11+
});
912

1013
const nameControl = screen.getByRole('textbox', { name: /name/i });
1114
const scoreControl = screen.getByRole('spinbutton', { name: /score/i });

apps/example-app-jest/src/app/examples/04-forms-with-material.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { provideZoneChangeDetection } from '@angular/core';
12
import { render, screen } from '@testing-library/angular';
23
import userEvent from '@testing-library/user-event';
34

@@ -6,7 +7,9 @@ import { MaterialFormsComponent } from './04-forms-with-material';
67
test('is possible to fill in a form and verify error messages (with the help of jest-dom https://testing-library.com/docs/ecosystem-jest-dom)', async () => {
78
const user = userEvent.setup();
89

9-
const { fixture } = await render(MaterialFormsComponent);
10+
const { fixture } = await render(MaterialFormsComponent, {
11+
providers: [provideZoneChangeDetection()],
12+
});
1013

1114
const nameControl = screen.getByLabelText(/name/i);
1215
const scoreControl = screen.getByRole('spinbutton', { name: /score/i });
@@ -66,7 +69,9 @@ test('is possible to fill in a form and verify error messages (with the help of
6669
test('set and show pre-set form values', async () => {
6770
const user = userEvent.setup();
6871

69-
const { fixture, detectChanges } = await render(MaterialFormsComponent);
72+
const { fixture, detectChanges } = await render(MaterialFormsComponent, {
73+
providers: [provideZoneChangeDetection()],
74+
});
7075

7176
fixture.componentInstance.form.setValue({
7277
name: 'Max',

apps/example-app-jest/src/app/examples/05-component-provider.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { provideZoneChangeDetection } from '@angular/core';
12
import { TestBed } from '@angular/core/testing';
23
import { render, screen } from '@testing-library/angular';
34
import { provideMock, Mock, createMock } from '@testing-library/angular/jest-utils';
@@ -15,6 +16,7 @@ test('renders the current value and can increment and decrement', async () => {
1516
useValue: new CounterService(),
1617
},
1718
],
19+
providers: [provideZoneChangeDetection()],
1820
});
1921

2022
const incrementControl = screen.getByRole('button', { name: /increment/i });
@@ -47,6 +49,7 @@ test('renders the current value and can increment and decrement with a mocked je
4749
useValue: counter,
4850
},
4951
],
52+
providers: [provideZoneChangeDetection()],
5053
});
5154

5255
const incrementControl = screen.getByRole('button', { name: /increment/i });
@@ -68,6 +71,7 @@ test('renders the current value and can increment and decrement with provideMock
6871

6972
await render(ComponentWithProviderComponent, {
7073
componentProviders: [provideMock(CounterService)],
74+
providers: [provideZoneChangeDetection()],
7175
});
7276

7377
const incrementControl = screen.getByRole('button', { name: /increment/i });

apps/example-app-jest/src/app/examples/06-with-ngrx-store.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { StoreModule } from '@ngrx/store';
33
import userEvent from '@testing-library/user-event';
44

55
import { WithNgRxStoreComponent, reducer } from './06-with-ngrx-store';
6+
import { provideZoneChangeDetection } from '@angular/core';
67

78
test('works with ngrx store', async () => {
89
const user = userEvent.setup();
@@ -18,6 +19,7 @@ test('works with ngrx store', async () => {
1819
},
1920
),
2021
],
22+
providers: [provideZoneChangeDetection()],
2123
});
2224

2325
const incrementControl = screen.getByRole('button', { name: /increment/i });

apps/example-app-jest/src/app/examples/07-with-ngrx-mock-store.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { render, screen } from '@testing-library/angular';
44
import userEvent from '@testing-library/user-event';
55

66
import { WithNgRxMockStoreComponent, selectItems } from './07-with-ngrx-mock-store';
7+
import { provideZoneChangeDetection } from '@angular/core';
78

89
test('works with provideMockStore', async () => {
910
const user = userEvent.setup();
@@ -18,6 +19,7 @@ test('works with provideMockStore', async () => {
1819
},
1920
],
2021
}),
22+
provideZoneChangeDetection(),
2123
],
2224
});
2325

apps/example-app-jest/src/app/examples/08-directive.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { Component, provideZoneChangeDetection } from '@angular/core';
22
import { render, screen } from '@testing-library/angular';
33
import userEvent from '@testing-library/user-event';
44

@@ -34,6 +34,7 @@ test('it is possible to test directives', async () => {
3434

3535
await render('<div atlSpoiler data-testid="dir"></div>', {
3636
imports: [SpoilerDirective],
37+
providers: [provideZoneChangeDetection()],
3738
});
3839

3940
const directive = screen.getByTestId('dir');
@@ -61,6 +62,7 @@ test('it is possible to test directives with props', async () => {
6162
hidden,
6263
visible,
6364
},
65+
providers: [provideZoneChangeDetection()],
6466
});
6567

6668
expect(screen.queryByText(visible)).not.toBeInTheDocument();
@@ -82,6 +84,7 @@ test('it is possible to test directives with props in template', async () => {
8284

8385
await render(`<div atlSpoiler hidden="${hidden}" visible="${visible}"></div>`, {
8486
imports: [SpoilerDirective],
87+
providers: [provideZoneChangeDetection()],
8588
});
8689

8790
expect(screen.queryByText(visible)).not.toBeInTheDocument();

0 commit comments

Comments
 (0)