From b22e7f36c327101fecf4174b1d9ba7bab6a36e29 Mon Sep 17 00:00:00 2001 From: Mike Ryan Date: Sun, 16 Jan 2022 07:42:40 -0500 Subject: [PATCH] 11-sifers --- .../src/integration/books-page.spec.ts | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/projects/earnings-app-e2e/src/integration/books-page.spec.ts b/projects/earnings-app-e2e/src/integration/books-page.spec.ts index 6a022c8..8c2df73 100644 --- a/projects/earnings-app-e2e/src/integration/books-page.spec.ts +++ b/projects/earnings-app-e2e/src/integration/books-page.spec.ts @@ -6,43 +6,47 @@ import * as BookListComponent from '../support/book-list-component.harness'; import * as BookFormComponent from '../support/book-form-component.harness'; import * as BooksPage from '../support/books-page.harness'; -describe('Books Page', () => { - let book: BookModel; - - beforeEach(() => { - book = { - id: uuid.v4(), - name: 'The Lord of the Rings', - earnings: '100', - description: - 'The Lord of the Rings is an epic high fantasy novel written by English author and scholar J. R. R. Tolkien.', - }; - - BooksApi.deleteAllBooks(); - BooksApi.createBook(book); - +function setup(options: { throwErrorWhenLoadingBooks?: boolean } = {}) { + const book = { + id: uuid.v4(), + name: 'The Lord of the Rings', + earnings: '100', + description: + 'The Lord of the Rings is an epic high fantasy novel written by English author and scholar J. R. R. Tolkien.', + }; + + BooksApi.deleteAllBooks(); + BooksApi.createBook(book); + + if (options.throwErrorWhenLoadingBooks) { + cy.intercept('GET', 'http://localhost:3000/books', { + statusCode: 500, + body: { + error: 'Internal Server Error', + }, + }).as('getBooks'); + } else { cy.intercept('GET', 'http://localhost:3000/books').as('getBooks'); + } - AuthApi.login('Admin', 'password'); + AuthApi.login('Admin', 'password'); - cy.visit('/'); + cy.visit('/'); - cy.wait('@getBooks'); - }); + cy.wait('@getBooks'); + + return book; +} +describe('Books Page', () => { it('should show a list all of the books', () => { + const book = setup(); + BookListComponent.getBook(book.id).should('contain', book.name); }); it('should gracefully show an error message when loading the books fails', () => { - cy.intercept('GET', 'http://localhost:3000/books', { - statusCode: 500, - body: { - error: 'Internal Server Error', - }, - }).as('getBooks'); - cy.visit('/'); - cy.wait('@getBooks'); + setup({ throwErrorWhenLoadingBooks: true }); BooksPage.getError().should('contain', 'Error'); });