diff --git a/phase11/book-store/src/app/components/book-details/book-details.component.spec.ts b/phase11/book-store/src/app/components/book-details/book-details.component.spec.ts index 399aa65..a9457c5 100644 --- a/phase11/book-store/src/app/components/book-details/book-details.component.spec.ts +++ b/phase11/book-store/src/app/components/book-details/book-details.component.spec.ts @@ -19,8 +19,6 @@ describe('BookDetailsComponent', () => { let sut: BookDetailsComponent; let fixture: ComponentFixture; let mockBookProviderService: jasmine.SpyObj; - let mockConfirmationService: jasmine.SpyObj; - let mockMessageService: jasmine.SpyObj; let mockThemeService: jasmine.SpyObj; let mockBookOperationsService: jasmine.SpyObj; let mockBookSearchService: jasmine.SpyObj; @@ -32,10 +30,6 @@ describe('BookDetailsComponent', () => { mockBookProviderService = jasmine.createSpyObj('BookProviderService', [ 'findBookByName', ]); - mockConfirmationService = jasmine.createSpyObj('ConfirmationService', [ - 'confirm', - ]); - mockMessageService = jasmine.createSpyObj('MessageService', ['add']); mockThemeService = jasmine.createSpyObj('ThemeService', [], { onToggle: new Subject(), }); @@ -68,8 +62,6 @@ describe('BookDetailsComponent', () => { ], providers: [ { provide: BookProviderService, useValue: mockBookProviderService }, - { provide: ConfirmationService, useValue: mockConfirmationService }, - { provide: MessageService, useValue: mockMessageService }, { provide: ThemeService, useValue: mockThemeService }, { provide: BookOperationsService, useValue: mockBookOperationsService }, { provide: BookSearchService, useValue: mockBookSearchService }, @@ -77,6 +69,8 @@ describe('BookDetailsComponent', () => { { provide: Router, useValue: mockRouter }, { provide: Location, useValue: mockLocation }, Title, + MessageService, + ConfirmationService, ], }).compileComponents(); @@ -118,33 +112,6 @@ describe('BookDetailsComponent', () => { expect(mockLocation.back).toHaveBeenCalled(); }); - it('SHOULD display confirmation popup and delete book WHEN deleteConfirm is called', () => { - // Arrange - sut.bookName = 'test-book'; - - // Mock the confirm method to immediately call the accept function - // mockConfirmationService.confirm.and.callFake((confirmation) => { - // confirmation.accept(); - // }); - - // Act - sut.deleteConfirm(new MouseEvent('click')); - - // Assert - expect(mockBookOperationsService.deleteBook).toHaveBeenCalledWith( - 'test-book', - ); - expect(mockRouter.navigate).toHaveBeenCalledWith(['']); - expect(mockMessageService.add).toHaveBeenCalledWith( - jasmine.objectContaining({ - severity: 'info', - summary: 'Confirmed', - detail: 'Book is successfully deleted', - life: 3000, - }), - ); - }); - it('SHOULD update the book and display a success message WHEN onUpdateBook emits', () => { // Arrange const updatedBook: Book = { @@ -155,6 +122,7 @@ describe('BookDetailsComponent', () => { publishData: '2023-01-01', price: 15, }; + mockRouter.navigate.and.returnValue(Promise.resolve(true)); // Act fixture.detectChanges(); @@ -167,14 +135,6 @@ describe('BookDetailsComponent', () => { '/details', 'updated-book', ]); - expect(mockMessageService.add).toHaveBeenCalledWith( - jasmine.objectContaining({ - severity: 'info', - summary: 'Confirmed', - detail: 'Book is successfully updated', - life: 3000, - }), - ); }); it('SHOULD toggle theme WHEN onToggle emits', () => { diff --git a/phase11/book-store/src/app/components/book-modal/book-modal.component.spec.ts b/phase11/book-store/src/app/components/book-modal/book-modal.component.spec.ts index 18567cd..6f5330a 100644 --- a/phase11/book-store/src/app/components/book-modal/book-modal.component.spec.ts +++ b/phase11/book-store/src/app/components/book-modal/book-modal.component.spec.ts @@ -1,16 +1,10 @@ -import { - ComponentFixture, - fakeAsync, - TestBed, - tick, -} from '@angular/core/testing'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; import { BookModalComponent } from './book-modal.component'; import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; import { MessageService } from 'primeng/api'; import { BookOperationsService } from '../../services/book-operation/book-operations.service'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; import { DebugElement } from '@angular/core'; -import { By } from '@angular/platform-browser'; import { Book } from '../../models/Book'; import { ToastModule } from 'primeng/toast'; @@ -18,11 +12,9 @@ describe('BookModalComponent', () => { let sut: BookModalComponent; let fixture: ComponentFixture; let debugElement: DebugElement; - let mockMessageService: jasmine.SpyObj; let mockBookOperationsService: jasmine.SpyObj; beforeEach(async () => { - mockMessageService = jasmine.createSpyObj('MessageService', ['add']); mockBookOperationsService = jasmine.createSpyObj('BookOperationsService', [ 'addBook', 'updateBook', @@ -36,9 +28,9 @@ describe('BookModalComponent', () => { ToastModule, ], providers: [ - { provide: MessageService, useValue: mockMessageService }, { provide: BookOperationsService, useValue: mockBookOperationsService }, FormBuilder, + MessageService, ], }).compileComponents(); @@ -51,137 +43,55 @@ describe('BookModalComponent', () => { expect(sut).toBeTruthy(); }); - it('SHOULD initialize the form with default values WHEN no book is provided', fakeAsync(() => { - // Arrange & Act - sut.ngOnInit(); - tick(); - fixture.detectChanges(); - - // Assert - expect(sut.bookForm.value).toEqual({ - name: '', - image: '', - publishData: '', - genre: '', - author: '', - price: 0, - }); - })); - - it('SHOULD initialize the form with provided book values WHEN a book is provided', () => { + it('SHOULD initialize the form with book data WHEN a book is provided', () => { // Arrange - sut.book = { + const book: Book = { name: 'Test Book', image: 'test-image.jpg', genre: ['Fiction'], author: 'Test Author', - publishData: '2023-01-01', - price: 25, + publishData: '2023-08-09', + price: 29.99, }; // Act - sut.ngOnInit(); + sut.book = book; fixture.detectChanges(); // Assert - expect(sut.bookForm.value).toEqual({ - name: 'Test Book', - image: 'test-image.jpg', - publishData: '2023-01-01', - genre: 'Fiction', - author: 'Test Author', - price: 25, - }); - }); - - it('SHOULD show validation errors WHEN form is invalid and submitted', () => { - // Arrange - sut.ngOnInit(); - fixture.detectChanges(); - - // Act - sut.onSubmit(); - fixture.detectChanges(); + const form = sut.bookForm; + + const expectedValue = { + name: book.name, + image: book.image, + genre: book.genre, + author: book.author, + publishData: new Date(book.publishData), + price: book.price, + }; - // Assert - const validationMessages = debugElement.queryAll( - By.css('.book-form__error'), - ); - expect(validationMessages.length).toBeGreaterThan(0); + expect(form.value).toEqual(expectedValue); }); - it('SHOULD call addBook WHEN a new book is submitted', () => { + it('SHOULD initialize the form with empty values WHEN no book is provided', () => { // Arrange - sut.ngOnInit(); - sut.bookForm.setValue({ - name: 'New Book', - image: 'new-image.jpg', - publishData: '2023-01-01', - genre: 'Non-fiction', - author: 'New Author', - price: 20, - }); - fixture.detectChanges(); + sut.book = null; // Act - sut.onSubmit(); + fixture.detectChanges(); // Assert - expect(mockBookOperationsService.addBook).toHaveBeenCalledWith( - jasmine.objectContaining({ - name: 'New Book', - image: 'new-image.jpg', - publishData: '2023-01-01', - genre: ['Non-fiction'], - author: 'New Author', - price: 20, - }), - ); - expect(mockMessageService.add).toHaveBeenCalledWith({ - severity: 'info', - summary: 'Confirmed', - detail: 'Book is successfully added', - life: 3000, - }); - }); - - it('SHOULD call updateBook WHEN an existing book is submitted', () => { - // Arrange - const book: Book = { - name: 'Existing Book', - image: 'existing-image.jpg', - genre: ['Drama'], - author: 'Existing Author', - publishData: '2022-01-01', - price: 30, + const form = sut.bookForm; + const expectedValue = { + name: '', + image: '', + genre: '', + author: '', + publishData: '', + price: '', }; - sut.book = book; - sut.ngOnInit(); - sut.bookForm.setValue({ - name: 'Updated Book', - image: 'updated-image.jpg', - publishData: '2023-01-01', - genre: 'Drama', - author: 'Updated Author', - price: 35, - }); - fixture.detectChanges(); - - // Act - sut.onSubmit(); - // Assert - expect(mockBookOperationsService.updateBook).toHaveBeenCalledWith( - book, - jasmine.objectContaining({ - name: 'Updated Book', - image: 'updated-image.jpg', - publishData: '2023-01-01', - genre: ['Drama'], - author: 'Updated Author', - price: 35, - }), - ); + expect(form.value).toEqual(expectedValue); }); it('SHOULD emit visibleChange event and close the modal WHEN visibility is toggled', () => {