Skip to content

refactor: enable strict mode #274

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 2 commits into from
Dec 9, 2021
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
6 changes: 1 addition & 5 deletions apps/example-app-karma/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import JasmineDOM from '@testing-library/jasmine-dom/dist';

beforeAll(() => {
(jasmine.getEnv() as any).addMatchers(JasmineDOM);
});
import '@testing-library/jasmine-dom';

declare const require: any;

Expand Down
4 changes: 2 additions & 2 deletions apps/example-app/src/app/examples/01-nested-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Component, Input, Output, EventEmitter } from '@angular/core';
template: ' <button (click)="raise.emit()">{{ name }}</button> ',
})
export class NestedButtonComponent {
@Input() name: string;
@Input() name = '';
@Output() raise = new EventEmitter<void>();
}

Expand All @@ -14,7 +14,7 @@ export class NestedButtonComponent {
template: ' <span data-testid="value">{{ value }}</span> ',
})
export class NestedValueComponent {
@Input() value: number;
@Input() value?: number;
}

@Component({
Expand Down
6 changes: 3 additions & 3 deletions apps/example-app/src/app/examples/03-forms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from '@angular/core';
import { FormBuilder, Validators, ValidationErrors } from '@angular/forms';
import { FormBuilder, Validators } from '@angular/forms';

@Component({
selector: 'app-fixture',
Expand Down Expand Up @@ -46,8 +46,8 @@ export class FormsComponent {
get formErrors() {
return Object.keys(this.form.controls)
.map((formKey) => {
const controlErrors: ValidationErrors = this.form.get(formKey).errors;
if (controlErrors != null) {
const controlErrors = this.form.get(formKey)?.errors;
if (controlErrors) {
return Object.keys(controlErrors).map((keyError) => {
const error = controlErrors[keyError];
switch (keyError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ test('is possible to fill in a form and verify error messages (with the help of
score: 7,
});

// not added to the form?
expect((fixture.componentInstance as MaterialFormsComponent).form.get('color').value).toBe('G');
expect((fixture.componentInstance as MaterialFormsComponent).form?.get('color')?.value).toBe('G');
});
6 changes: 3 additions & 3 deletions apps/example-app/src/app/examples/04-forms-with-material.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from '@angular/core';
import { FormBuilder, Validators, ValidationErrors } from '@angular/forms';
import { FormBuilder, Validators } from '@angular/forms';

@Component({
selector: 'app-fixture',
Expand Down Expand Up @@ -68,8 +68,8 @@ export class MaterialFormsComponent {
get formErrors() {
return Object.keys(this.form.controls)
.map((formKey) => {
const controlErrors: ValidationErrors = this.form.get(formKey).errors;
if (controlErrors != null) {
const controlErrors = this.form.get(formKey)?.errors;
if (controlErrors) {
return Object.keys(controlErrors).map((keyError) => {
const error = controlErrors[keyError];
switch (keyError) {
Expand Down
6 changes: 1 addition & 5 deletions apps/example-app/src/app/examples/06-with-ngrx-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import { createSelector, Store, createAction, createReducer, on, select } from '

const increment = createAction('increment');
const decrement = createAction('decrement');
const counterReducer = createReducer(
export const reducer = createReducer(
0,
on(increment, (state) => state + 1),
on(decrement, (state) => state - 1),
);

export function reducer(state, action) {
return counterReducer(state, action);
}

const selectValue = createSelector(
(state: any) => state.value,
(value) => value * 10,
Expand Down
8 changes: 4 additions & 4 deletions apps/example-app/src/app/examples/08-directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ test('it is possible to test directives with props', async () => {
expect(screen.queryByText(visible)).not.toBeInTheDocument();
expect(screen.queryByText(hidden)).toBeInTheDocument();

fireEvent.mouseOver(screen.queryByText(hidden));
fireEvent.mouseOver(screen.getByText(hidden));
expect(screen.queryByText(hidden)).not.toBeInTheDocument();
expect(screen.queryByText(visible)).toBeInTheDocument();

fireEvent.mouseLeave(screen.queryByText(visible));
fireEvent.mouseLeave(screen.getByText(visible));
expect(screen.queryByText(hidden)).toBeInTheDocument();
expect(screen.queryByText(visible)).not.toBeInTheDocument();
});
Expand All @@ -56,11 +56,11 @@ test('it is possible to test directives with props in template', async () => {
expect(screen.queryByText(visible)).not.toBeInTheDocument();
expect(screen.queryByText(hidden)).toBeInTheDocument();

fireEvent.mouseOver(screen.queryByText(hidden));
fireEvent.mouseOver(screen.getByText(hidden));
expect(screen.queryByText(hidden)).not.toBeInTheDocument();
expect(screen.queryByText(visible)).toBeInTheDocument();

fireEvent.mouseLeave(screen.queryByText(visible));
fireEvent.mouseLeave(screen.getByText(visible));
expect(screen.queryByText(hidden)).toBeInTheDocument();
expect(screen.queryByText(visible)).not.toBeInTheDocument();
});
4 changes: 2 additions & 2 deletions apps/example-app/src/app/examples/12-service-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Component, Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';

export class Customer {
id: string;
name: string;
id!: string;
name!: string;
}

@Injectable({
Expand Down
4 changes: 2 additions & 2 deletions apps/example-app/src/app/examples/15-dialog.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ test('closes the dialog via the backdrop', async () => {

// using fireEvent because of:
// unable to click element as it has or inherits pointer-events set to "none"
// eslint-disable-next-line testing-library/no-node-access
fireEvent.click(document.querySelector('.cdk-overlay-backdrop'));
// eslint-disable-next-line testing-library/no-node-access, @typescript-eslint/no-non-null-assertion
fireEvent.click(document.querySelector('.cdk-overlay-backdrop')!);

await waitForElementToBeRemoved(() => screen.getByRole('dialog'));

Expand Down
4 changes: 2 additions & 2 deletions apps/example-app/src/app/examples/16-input-getter-setter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export class InputGetterSetter {
return 'I am value from getter ' + this.originalValue;
}

private originalValue: string;
derivedValue: string;
private originalValue?: string;
derivedValue?: string;
}
45 changes: 22 additions & 23 deletions apps/example-app/src/app/issues/issue-254.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Component, Inject, OnInit } from '@angular/core';
import { render, screen } from '@testing-library/angular';
import { createMock } from '@testing-library/angular/jest-utils';

interface Division {
JobType: string;
JobBullets: string[];
Description: string;
jobType?: string;
jobBullets?: string[];
description?: string;
}

@Inject({
Expand All @@ -21,46 +20,46 @@ class JobsService {
@Component({
selector: 'app-home-career-oportunities',
template: ` <ul class="popu-category-bullets">
<li class="text-dark" *ngFor="let bullet of dedicated.JobBullets">
<li class="text-dark" *ngFor="let bullet of dedicated?.jobBullets">
{{ bullet }}
</li>
</ul>`,
})
class CareerOportunitiesComponent implements OnInit {
dedicated = {} as Division;
intermodal = {} as Division;
noCdl = {} as Division;
otr = {} as Division;
dedicated?: Division;
intermodal?: Division;
noCdl?: Division;
otr?: Division;

constructor(private jobsService: JobsService) {}

ngOnInit(): void {
this.jobsService.divisions().then((apiDivisions) => {
this.dedicated = apiDivisions.find((c) => c.JobType === 'DEDICATED');
this.intermodal = apiDivisions.find((c) => c.JobType === 'INTERMODAL');
this.noCdl = apiDivisions.find((c) => c.JobType === 'NO_CDL');
this.otr = apiDivisions.find((c) => c.JobType === 'OVER_THE_ROAD');
this.dedicated = apiDivisions.find((c) => c.jobType === 'DEDICATED');
this.intermodal = apiDivisions.find((c) => c.jobType === 'INTERMODAL');
this.noCdl = apiDivisions.find((c) => c.jobType === 'NO_CDL');
this.otr = apiDivisions.find((c) => c.jobType === 'OVER_THE_ROAD');
});
}
}

test('Render Component', async () => {
const divisions2: Division[] = [
{
JobType: 'INTERMODAL',
JobBullets: ['Local Routes', 'Flexible Schedules', 'Competitive Pay'],
Description: '',
jobType: 'INTERMODAL',
jobBullets: ['Local Routes', 'Flexible Schedules', 'Competitive Pay'],
description: '',
},
{ JobType: 'NO_CDL', JobBullets: ['We Train', 'We Hire', 'We Pay'], Description: '' },
{ jobType: 'NO_CDL', jobBullets: ['We Train', 'We Hire', 'We Pay'], description: '' },
{
JobType: 'OVER_THE_ROAD',
JobBullets: ['Great Miles', 'Competitive Pay', 'Explore the Country'],
Description: '',
jobType: 'OVER_THE_ROAD',
jobBullets: ['Great Miles', 'Competitive Pay', 'Explore the Country'],
description: '',
},
{
JobType: 'DEDICATED',
JobBullets: ['Regular Routes', 'Consistent Miles', 'Great Pay'],
Description: '',
jobType: 'DEDICATED',
jobBullets: ['Regular Routes', 'Consistent Miles', 'Great Pay'],
description: '',
},
];
const jobService = createMock(JobsService);
Expand Down
7 changes: 7 additions & 0 deletions projects/testing-library/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
"extends": "../../.eslintrc.json",
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"rules": {
"@typescript-eslint/ban-ts-comment": "off"
}
},
{
"files": ["*.ts"],
"extends": ["plugin:@nrwl/nx/angular", "plugin:@angular-eslint/template/process-inline-templates"],
"parserOptions": {
"project": ["projects/testing-library/tsconfig.*?.json"]
},
"rules": {
"@typescript-eslint/ban-ts-comment": "off",
"@angular-eslint/directive-selector": [
"error",
{
Expand Down
4 changes: 2 additions & 2 deletions projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ export interface RenderTemplateOptions<WrapperType, Properties extends object =
* `WrapperComponent`, an empty component that strips the `ng-version` attribute
*
* @example
* const component = await render(SpoilerDirective, {
* template: `<div spoiler message='SPOILER'></div>`
* const component = await render(`<div spoiler message='SPOILER'></div>`, {
* declarations: [SpoilerDirective]
* wrapper: CustomWrapperComponent
* })
*/
Expand Down
Loading