Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…FE-Team07 into phase11
  • Loading branch information
asAlwaysZahra committed Aug 9, 2024
2 parents 1f01b46 + ecd3903 commit f02af5f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ describe('BookDetailsComponent', () => {
let sut: BookDetailsComponent;
let fixture: ComponentFixture<BookDetailsComponent>;
let mockBookProviderService: jasmine.SpyObj<BookProviderService>;
let mockConfirmationService: jasmine.SpyObj<ConfirmationService>;
let mockMessageService: jasmine.SpyObj<MessageService>;
let mockThemeService: jasmine.SpyObj<ThemeService>;
let mockBookOperationsService: jasmine.SpyObj<BookOperationsService>;
let mockBookSearchService: jasmine.SpyObj<BookSearchService>;
Expand All @@ -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<boolean>(),
});
Expand Down Expand Up @@ -68,15 +62,15 @@ 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 },
{ provide: ActivatedRoute, useValue: mockActivatedRoute },
{ provide: Router, useValue: mockRouter },
{ provide: Location, useValue: mockLocation },
Title,
MessageService,
ConfirmationService,
],
}).compileComponents();

Expand Down Expand Up @@ -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 = {
Expand All @@ -155,6 +122,7 @@ describe('BookDetailsComponent', () => {
publishData: '2023-01-01',
price: 15,
};
mockRouter.navigate.and.returnValue(Promise.resolve(true));

// Act
fixture.detectChanges();
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
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';

describe('BookModalComponent', () => {
let sut: BookModalComponent;
let fixture: ComponentFixture<BookModalComponent>;
let debugElement: DebugElement;
let mockMessageService: jasmine.SpyObj<MessageService>;
let mockBookOperationsService: jasmine.SpyObj<BookOperationsService>;

beforeEach(async () => {
mockMessageService = jasmine.createSpyObj('MessageService', ['add']);
mockBookOperationsService = jasmine.createSpyObj('BookOperationsService', [
'addBook',
'updateBook',
Expand All @@ -36,9 +28,9 @@ describe('BookModalComponent', () => {
ToastModule,
],
providers: [
{ provide: MessageService, useValue: mockMessageService },
{ provide: BookOperationsService, useValue: mockBookOperationsService },
FormBuilder,
MessageService,
],
}).compileComponents();

Expand All @@ -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', () => {
Expand Down

0 comments on commit f02af5f

Please sign in to comment.