From 5fa630444cc9274d322d3537206adf24752d5c88 Mon Sep 17 00:00:00 2001 From: Hoan Nguyen Date: Fri, 14 Jun 2024 01:58:44 +0700 Subject: [PATCH] Add test --- .../modules/product/product.component.spec.ts | 56 +++++++++++++++++++ src/app/shared/mocks/mock-services.ts | 1 - 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/app/modules/product/product.component.spec.ts b/src/app/modules/product/product.component.spec.ts index d3d880e..8854abd 100644 --- a/src/app/modules/product/product.component.spec.ts +++ b/src/app/modules/product/product.component.spec.ts @@ -22,6 +22,23 @@ const router = { describe('ProductComponent', () => { let component: ProductComponent; let fixture: ComponentFixture; + 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({ @@ -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(); + }); }); diff --git a/src/app/shared/mocks/mock-services.ts b/src/app/shared/mocks/mock-services.ts index da7afc1..01a203c 100644 --- a/src/app/shared/mocks/mock-services.ts +++ b/src/app/shared/mocks/mock-services.ts @@ -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 {