Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoan Nguyen committed Jun 13, 2024
1 parent ceb2a30 commit 5fa6304
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
56 changes: 56 additions & 0 deletions src/app/modules/product/product.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ const router = {
describe('ProductComponent', () => {
let component: ProductComponent;
let fixture: ComponentFixture<ProductComponent>;
let mockIntersectionObserver: any;

beforeAll(() => {
mockIntersectionObserver = jasmine.createSpyObj('IntersectionObserver', ['observe', 'unobserve', 'disconnect']);
mockIntersectionObserver.observe.and.callFake(() => { });
mockIntersectionObserver.unobserve.and.callFake(() => { });
mockIntersectionObserver.disconnect.and.callFake(() => { });

(window as any).IntersectionObserver = function (callback: IntersectionObserverCallback) {
mockIntersectionObserver.callback = callback;
return mockIntersectionObserver;
};
});

afterAll(() => {
delete (window as any).IntersectionObserver;
});

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand Down Expand Up @@ -100,4 +117,43 @@ describe('ProductComponent', () => {
component.ngAfterViewInit();
expect(component.criteria.nextPageHref).toBeUndefined();
});

it('should call loadProductItems when observerElement is intersecting and has more products', () => {
spyOn(component, 'loadProductItems').and.callThrough();
spyOn(component, 'hasMore').and.returnValue(true);

const entries = [{ isIntersecting: true }];
const callback = mockIntersectionObserver.callback;

callback(entries as IntersectionObserverEntry[]);

expect(component.hasMore).toHaveBeenCalled();
expect(component.loadProductItems).toHaveBeenCalled();
});

it('should not call loadProductItems when observerElement is not intersecting', () => {
spyOn(component, 'loadProductItems').and.callThrough();
spyOn(component, 'hasMore').and.returnValue(true);

const entries = [{ isIntersecting: false }];
const callback = mockIntersectionObserver.callback;

callback(entries as IntersectionObserverEntry[]);

expect(component.hasMore).not.toHaveBeenCalled();
expect(component.loadProductItems).not.toHaveBeenCalled();
});

it('should not call loadProductItems when there are no more products', () => {
spyOn(component, 'loadProductItems').and.callThrough();
spyOn(component, 'hasMore').and.returnValue(false);

const entries = [{ isIntersecting: true }];
const callback = mockIntersectionObserver.callback;

callback(entries as IntersectionObserverEntry[]);

expect(component.hasMore).toHaveBeenCalled();
expect(component.loadProductItems).not.toHaveBeenCalled();
});
});
1 change: 0 additions & 1 deletion src/app/shared/mocks/mock-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Criteria } from '../models/criteria.model';
import { FilterType } from '../enums/filter-type.enum';
import { MOCK_PRODUCTS, MOCK_PRODUCTS_FILTER_CONNECTOR, MOCK_PRODUCTS_NEXT_PAGE } from './mock-data';
import { ProductApiResponse } from '../models/apis/product-response.model';
import { ProductApiResponse } from '../models/apis/product-response.model';

const products = MOCK_PRODUCTS._embedded.products as Product[];
export class MockProductService {
Expand Down

0 comments on commit 5fa6304

Please sign in to comment.