Skip to content

docs: add example for issue #304 #305

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
Jul 1, 2022
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
67 changes: 67 additions & 0 deletions apps/example-app/src/app/issues/issue-304.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Location } from '@angular/common';
import { render, screen } from '@testing-library/angular';
import userEvent from '@testing-library/user-event';
import { TestBed } from '@angular/core/testing';

// with goBackSpy, the implementation of goBack won't be invoked (because it's using the spy)
test('should call a goBack when user click in the button', async () => {
const goBackSpy = jest.fn();
await render(HeaderBackButtonComponent, {
declarations: [IconButtonComponent],
componentProperties: {
goBack: goBackSpy,
},
});

const button = screen.getByLabelText(/icon button/i);
userEvent.click(button);
expect(goBackSpy).toHaveBeenCalled();
});

// don't spy on goBack, this way the implementation of goBack is invoked, and you can test if location.back() is called
test('should call a Location.back when user click in the button', async () => {
await render(HeaderBackButtonComponent, {
declarations: [IconButtonComponent],
});

const location = TestBed.inject(Location);
jest.spyOn(location, 'back');

const button = screen.getByLabelText(/icon button/i);
userEvent.click(button);
expect(location.back).toHaveBeenCalled();
});

@Component({
selector: 'app-cebs-header-back-button',
template: `
<header>
<app-cebs-icon-button icon="chevron_left" aria-label="back button" (clickEvent)="goBack()"></app-cebs-icon-button>
</header>
`,
})
class HeaderBackButtonComponent {
constructor(private location: Location) {}

goBack(): void {
this.location.back();
}
}

@Component({
selector: 'app-cebs-icon-button',
template: `
<button (click)="onClick()" aria-label="icon button">
<span class="material-symbols-outlined icon">{{ icon }}</span>
</button>
`,
})
class IconButtonComponent {
@Output() clickEvent = new EventEmitter();
@Input() icon!: string;

onClick(): void {
this.clickEvent.emit();
}
}
2 changes: 1 addition & 1 deletion apps/example-app/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"outDir": "../../dist/out-tsc",
"types": [],
"allowJs": true,
"target": "ES2017"
"target": "ES2020"
},
"files": ["src/main.ts", "src/polyfills.ts"],
"include": ["src/**/*.d.ts"],
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@
},
"devDependencies": {
"@angular-devkit/build-angular": "14.0.0",
"@angular-eslint/eslint-plugin": "13.0.1",
"@angular-eslint/eslint-plugin-template": "13.0.1",
"@angular-eslint/template-parser": "13.0.1",
"@angular-eslint/builder": "14.0.0",
"@angular-eslint/eslint-plugin": "14.0.0",
"@angular-eslint/eslint-plugin-template": "14.0.0",
"@angular-eslint/schematics": "14.0.0",
"@angular-eslint/template-parser": "14.0.0",
"@angular/cli": "14.0.0",
"@angular/compiler-cli": "14.0.0",
"@angular/forms": "14.0.0",
Expand Down
2 changes: 1 addition & 1 deletion projects/testing-library/tsconfig.schematics.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"strict": true,
"target": "es6",
"target": "ES2020",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"moduleResolution": "node",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"target": "es2015",
"target": "ES2020",
"typeRoots": ["node_modules/@types"],
"strict": true,
"exactOptionalPropertyTypes": true,
Expand Down