Skip to content
Open
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
50 changes: 45 additions & 5 deletions apps/testing/17-router/src/app/app.component.cy.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,73 @@
import { provideRouter } from '@angular/router';
import { AppComponent } from './app.component';
import { appRoutes } from './app.routes';

describe(AppComponent.name, () => {
beforeEach(() => {
cy.mount(AppComponent, {
providers: [provideRouter(appRoutes)],
});
cy.get('nav a:first').click();
});

describe('Given no search criteria', () => {
it('Then shows error message and disabled button', () => {
//todo
cy.get('input[name=bookName]').as('bookControl').clear();
cy.contains('Search criteria is required!');

cy.get('@bookControl').type('kill');
cy.contains('Search criteria is required!').should('not.exist');
});
});

describe('Given a search criteria with no book match', () => {
it('Then shows No book found', () => {
//todo
cy.get('input[name=bookName]').as('bookControl').type('NonExistingBook');
cy.get('button').contains('Borrow').click();

cy.get('[data-testid="no-book-found-msg"]').should('be.visible');
});
});

describe('Given a search criteria with one book match', () => {
it('Then shows One book and no error', () => {
//todo
cy.get('input[name=bookName]').as('bookControl').type('1984');
cy.get('button').contains('Borrow').click();

cy.get('[data-testid="search-error-msg"]').should('not.exist');
cy.contains('1984 by George Orwell').should('be.visible');
});
});

describe('Given a search criteria in Uppercase with one book match', () => {
it('Then shows One book and no error', () => {
//todo
cy.get('input[name=bookName]').as('bookControl').type('HOBBIT');
cy.get('button').contains('Borrow').click();

cy.get('[data-testid="search-error-msg"]').should('not.exist');
cy.contains('The Hobbit by J.R.R. Tolkien').should('be.visible');
});
});

describe('Given a search criteria with multple books matches', () => {
it('Then shows a list of books', () => {
//todo
cy.get('input[name=bookName]').as('bookControl').type('the');
cy.get('button').contains('Borrow').click();

cy.get('ul li').should('have.length.greaterThan', 1);

cy.contains('The Hobbit by J.R.R. Tolkien').should('be.visible');
cy.contains('The Great Gats by F. Scott Fitzgerald').should('be.visible');
cy.contains('The Lord of the Rings by J.R.R. Tolkien').should(
'be.visible',
);
cy.contains('The Catcher in the Rye by J.D. Salinger').should(
'be.visible',
);
cy.contains('The Hunger Games').should('be.visible');
cy.contains(
"Harry Potter and the Philosopher's Stone by J.K. Rowling",
).should('be.visible');
});
});
});
67 changes: 61 additions & 6 deletions apps/testing/17-router/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,86 @@
describe('AppComponent', () => {
import { render, screen } from '@testing-library/angular';
import userEvent from '@testing-library/user-event';
import { AppComponent } from './app.component';
import { appRoutes } from './app.routes';
import exp = require('constants');

describe.only('AppComponent', () => {
const search = async (text: string) => {
await render(AppComponent, { routes: appRoutes });

const searchControl = screen.getByRole('textbox', {
name: /search book by author or title/i,
});
const borrowBtn = screen.getByRole('button', { name: /borrow/i });

await userEvent.type(searchControl, text);
await userEvent.click(borrowBtn);
};

describe('Given no search criteria', () => {
it('Then shows error message and disabled button', async () => {
//todo
await render(AppComponent, { routes: appRoutes });

const searchControl = screen.getByRole('textbox', {
name: /search book by author or title/i,
});
userEvent.clear(searchControl);

expect(screen.getByTestId('search-error-msg')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /borrow/i })).toBeDisabled();
});
});

describe('Given a search criteria with no book match', () => {
it('Then shows No book found', async () => {
//todo
await search('NonExistingBook');

expect(screen.getByTestId('no-book-found-msg')).toBeInTheDocument();
});
});

describe('Given a search criteria with one book match', () => {
it('Then shows One book and no error', async () => {
//todo
await search('1984');

expect(screen.queryByTestId('search-error-msg')).not.toBeInTheDocument();
expect(screen.getByText(/1984 by George Orwell/i)).toBeInTheDocument();
});
});

describe('Given a search criteria in Uppercase with one book match', () => {
it('Then shows One book and no error', async () => {
//todo
await search('HOBBIT');

expect(screen.queryByTestId('search-error-msg')).not.toBeInTheDocument();
expect(
screen.getByText(/The Hobbit by J.R.R. Tolkien/i),
).toBeInTheDocument();
});
});

describe('Given a search criteria with multple books matches', () => {
it('Then shows a list of books', async () => {
//todo
await search('the');

expect(
screen.getByText(/The Hobbit by J.R.R. Tolkien/i),
).toBeInTheDocument();
expect(
screen.getByText(/The Catcher in the Rye by J.D. Salinger/i),
).toBeInTheDocument();
expect(
screen.getByText(/The Lord of the Rings by J.R.R. Tolkien/i),
).toBeInTheDocument();
expect(
screen.getByText(/The Great Gats by F. Scott Fitzgerald/i),
).toBeInTheDocument();
expect(
screen.getByText(
/Harry Potter and the Philosopher's Stone by J.K. Rowling/i,
),
).toBeInTheDocument();
expect(screen.getByText(/The Hunger Games/i)).toBeInTheDocument();
});
});
});
2 changes: 1 addition & 1 deletion apps/testing/17-router/src/app/no-book-search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, Component } from '@angular/core';

@Component({
template: `
<div>No book found for this search</div>
<div data-testid="no-book-found-msg">No book found for this search</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
Expand Down
4 changes: 3 additions & 1 deletion apps/testing/17-router/src/app/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ import { availableBooks } from './book.model';
[formControl]="searchBook"
required />
@if (searchBook.errors) {
<div class="error">Search criteria is required!</div>
<div class="error" data-testid="search-error-msg">
Search criteria is required!
</div>
}
</div>
<button
Expand Down
2 changes: 1 addition & 1 deletion apps/testing/17-router/tsconfig.editor.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"extends": "./tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"types": ["jest", "node"]
"types": ["jest", "node", "cypress"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The real fix is to exclude cypress test from this file,
but i don't know why this tsconfig was created in the first place, so i will just remove them
thanks 🙏

}
}
Loading