@@ -4,23 +4,96 @@ import { beforeEach, describe, expect, it } from '@jest/globals';
44import { GalleryComponent } from './gallery.component' ;
55import { HttpClientTestingModule } from '@angular/common/http/testing' ;
66import { TranslateModule } from '@ngx-translate/core' ;
7+ import { BehaviorSubject , Subject } from 'rxjs' ;
8+ import { AuthService } from 'src/app/guard/auth.service' ;
9+ import { LocalStorageService } from 'src/app/services/local-storage.service' ;
10+ import { EventMessageService } from 'src/app/services/event-message.service' ;
11+ import { PaginationService } from 'src/app/services/pagination.service' ;
12+ import { ChangeDetectorRef } from '@angular/core' ;
13+ import { ActivatedRoute } from '@angular/router' ;
14+ import { MarkdownModule } from 'ngx-markdown' ;
715
816describe ( 'GalleryComponent' , ( ) => {
917 let component : GalleryComponent ;
1018 let fixture : ComponentFixture < GalleryComponent > ;
1119
20+ const authMock = { isAuthenticated$ : new BehaviorSubject < boolean > ( true ) } ;
21+ const cdrMock = { detectChanges : jest . fn ( ) } ;
22+ const routeMock = { snapshot : { paramMap : { get : jest . fn ( ( ) => null ) } } } as unknown as Partial < ActivatedRoute > ;
23+ const localStorageMock = { getObject : jest . fn ( ( ) => [ ] ) , setItem : jest . fn ( ) } ;
24+ const messages$ = new Subject < any > ( ) ;
25+ const eventMessageMock = { messages$ : messages$ , emitFilterShown : jest . fn ( ) , emitFilterShownCategory : jest . fn ( ) } as Partial < EventMessageService > ;
26+ const paginationServiceMock = {
27+ getItemsPaginated : jest . fn ( ( ) =>
28+ Promise . resolve ( {
29+ page_check : true ,
30+ items : [ { id : '1' , name : 'p1' } ] ,
31+ nextItems : [ ] ,
32+ page : 0
33+ } )
34+ ) ,
35+ getProducts : jest . fn ( )
36+ } as Partial < PaginationService > ;
37+
1238 beforeEach ( async ( ) => {
1339 await TestBed . configureTestingModule ( {
14- imports : [ GalleryComponent , HttpClientTestingModule , TranslateModule . forRoot ( ) ]
15- } )
16- . compileComponents ( ) ;
17-
40+ imports : [ GalleryComponent , HttpClientTestingModule , TranslateModule . forRoot ( ) , MarkdownModule . forRoot ( ) ] ,
41+ providers : [
42+ { provide : AuthService , useValue : authMock } ,
43+ { provide : ChangeDetectorRef , useValue : cdrMock } ,
44+ { provide : ActivatedRoute , useValue : routeMock } ,
45+ { provide : LocalStorageService , useValue : localStorageMock } ,
46+ { provide : EventMessageService , useValue : eventMessageMock } ,
47+ { provide : PaginationService , useValue : paginationServiceMock }
48+ ]
49+ } ) . compileComponents ( ) ;
50+
1851 fixture = TestBed . createComponent ( GalleryComponent ) ;
1952 component = fixture . componentInstance ;
53+ jest . clearAllMocks ( ) ;
2054 fixture . detectChanges ( ) ;
2155 } ) ;
2256
2357 it ( 'should create' , ( ) => {
2458 expect ( component ) . toBeTruthy ( ) ;
2559 } ) ;
60+
61+ it ( 'should load products on init via paginationService and set products' , async ( ) => {
62+ await fixture . whenStable ( ) ;
63+ expect ( paginationServiceMock . getItemsPaginated ) . toHaveBeenCalled ( ) ;
64+ expect ( component . products ) . toEqual ( [ { id : '1' , name : 'p1' } ] ) ;
65+ expect ( component . page_check ) . toBe ( true ) ;
66+ expect ( component . loading ) . toBe ( false ) ;
67+ } ) ;
68+
69+ it ( 'checkPanel should emit and persist when filters present' , ( ) => {
70+ ( localStorageMock . getObject as jest . Mock ) . mockReturnValue ( [ { id : 'cat1' } ] ) ;
71+ ( eventMessageMock . emitFilterShown as jest . Mock ) . mockClear ( ) ;
72+ component . showPanel = false ;
73+ component . checkPanel ( ) ;
74+ expect ( eventMessageMock . emitFilterShown ) . toHaveBeenCalledWith ( true ) ;
75+ expect ( localStorageMock . setItem ) . toHaveBeenCalledWith ( 'is_filter_panel_shown' , 'true' ) ;
76+ } ) ;
77+
78+ it ( 'filterSearch should set keywords and call getProducts' , async ( ) => {
79+ const getProductsSpy = jest . spyOn ( component , 'getProducts' ) . mockResolvedValue ( undefined ) ;
80+ component . searchField . setValue ( 'search-term' ) ;
81+ await component . filterSearch ( { preventDefault : ( ) => { } } as any ) ;
82+ expect ( component . keywords ) . toBe ( 'search-term' ) ;
83+ expect ( getProductsSpy ) . toHaveBeenCalledWith ( false ) ;
84+ getProductsSpy . mockRestore ( ) ;
85+ } ) ;
86+
87+ it ( 'filterSearch should clear keywords and call getProducts when empty' , async ( ) => {
88+ const getProductsSpy = jest . spyOn ( component , 'getProducts' ) . mockResolvedValue ( undefined ) ;
89+ component . searchField . setValue ( '' ) ;
90+ await component . filterSearch ( { preventDefault : ( ) => { } } as any ) ;
91+ expect ( component . keywords ) . toBeUndefined ( ) ;
92+ expect ( getProductsSpy ) . toHaveBeenCalledWith ( false ) ;
93+ getProductsSpy . mockRestore ( ) ;
94+ } ) ;
95+
96+ afterAll ( ( ) => {
97+ messages$ . complete ( ) ;
98+ } ) ;
2699} ) ;
0 commit comments