diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-layout/edit-ema-layout.component.spec.ts b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-layout/edit-ema-layout.component.spec.ts index 518c893c4f8d..07b4fcd9648b 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-layout/edit-ema-layout.component.spec.ts +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-layout/edit-ema-layout.component.spec.ts @@ -1,7 +1,7 @@ import { expect, describe } from '@jest/globals'; import { SpyObject } from '@ngneat/spectator'; import { Spectator, createComponentFactory, mockProvider } from '@ngneat/spectator/jest'; -import { MockModule } from 'ng-mocks'; +import { MockModule, MockProvider } from 'ng-mocks'; import { of } from 'rxjs'; import { HttpClientTestingModule } from '@angular/common/http/testing'; @@ -22,7 +22,6 @@ import { DotRouterService } from '@dotcms/data-access'; import { CoreWebService, LoginService } from '@dotcms/dotcms-js'; -import { DotPageRender } from '@dotcms/dotcms-models'; import { TemplateBuilderComponent, TemplateBuilderModule } from '@dotcms/template-builder'; import { DotExperimentsServiceMock, @@ -70,9 +69,8 @@ describe('EditEmaLayoutComponent', () => { let dotRouter: SpyObject; let store: SpyObject>; let templateBuilder: TemplateBuilderComponent; - let layoutService: DotPageLayoutService; + let dotPageLayoutService: DotPageLayoutService; let messageService: MessageService; - let addMock: jest.SpyInstance; globalThis.structuredClone = jest.fn().mockImplementation((obj) => obj); @@ -81,58 +79,43 @@ describe('EditEmaLayoutComponent', () => { imports: [HttpClientTestingModule, MockModule(TemplateBuilderModule)], providers: [ UVEStore, - MessageService, DotMessageService, DotActionUrlService, + mockProvider(MessageService), mockProvider(Router), mockProvider(ActivatedRoute), - { - provide: DotExperimentsService, - useValue: DotExperimentsServiceMock - }, - { provide: DotRouterService, useValue: new MockDotRouterJestService(jest) }, - { - provide: DotLicenseService, - useValue: { - isEnterprise: () => of(true) - } - }, - { - provide: DotPageApiService, - useValue: { - get: () => { - return of(PAGE_RESPONSE); - } - } - }, - { - provide: DotPageLayoutService, - useValue: { - save: () => { - return of({ - layout: {} - }); - } - } - }, mockProvider(DotContentTypeService), mockProvider(CoreWebService), - { - provide: DotLanguagesService, - useValue: new DotLanguagesServiceMock() - }, - { - provide: DotContentletLockerService, - useValue: { + mockProvider(DotPageLayoutService, { + save: jest.fn(() => of(PAGE_RESPONSE)) + }), + mockProvider(DotPageApiService, { + get: jest.fn(() => of(PAGE_RESPONSE)) + }), + MockProvider(DotExperimentsService, DotExperimentsServiceMock, 'useValue'), + MockProvider(DotRouterService, new MockDotRouterJestService(jest), 'useValue'), + MockProvider(DotLanguagesService, new DotLanguagesServiceMock(), 'useValue'), + MockProvider( + DotLicenseService, + { + isEnterprise: () => of(true) + }, + 'useValue' + ), + MockProvider( + DotContentletLockerService, + { unlock: (_inode: string) => of({}) - } - }, - { - provide: LoginService, - useValue: { + }, + 'useValue' + ), + MockProvider( + LoginService, + { getCurrentUser: () => of({}) - } - } + }, + 'useValue' + ) ] }); @@ -141,11 +124,9 @@ describe('EditEmaLayoutComponent', () => { component = spectator.component; dotRouter = spectator.inject(DotRouterService); store = spectator.inject(UVEStore, true); - layoutService = spectator.inject(DotPageLayoutService); + dotPageLayoutService = spectator.inject(DotPageLayoutService); messageService = spectator.inject(MessageService); - addMock = jest.spyOn(messageService, 'add'); - store.load({ clientHost: 'http://localhost:3000', language_id: '1', @@ -162,35 +143,29 @@ describe('EditEmaLayoutComponent', () => { describe('Template Change', () => { it('should forbid navigation', () => { - const spy = jest.spyOn(dotRouter, 'forbidRouteDeactivation'); - templateBuilder.templateChange.emit(); - - expect(spy).toHaveBeenCalled(); + expect(dotRouter.forbidRouteDeactivation).toHaveBeenCalled(); }); it('should trigger a save after 5 secs', fakeAsync(() => { - const layoutServiceSave = jest - .spyOn(layoutService, 'save') - .mockReturnValue(of(PAGE_RESPONSE as unknown as DotPageRender)); const updatePageResponseSpy = jest.spyOn(store, 'updatePageResponse'); const setUveStatusSpy = jest.spyOn(store, 'setUveStatus'); templateBuilder.templateChange.emit(); tick(5000); - expect(layoutServiceSave).toHaveBeenCalled(); + expect(dotPageLayoutService.save).toHaveBeenCalled(); expect(updatePageResponseSpy).toHaveBeenCalledWith(PAGE_RESPONSE); expect(setUveStatusSpy).toHaveBeenCalledWith(UVE_STATUS.LOADING); - expect(addMock).toHaveBeenNthCalledWith(1, { + expect(messageService.add).toHaveBeenNthCalledWith(1, { severity: 'info', summary: 'Info', detail: 'dot.common.message.saving', life: 1000 }); - expect(addMock).toHaveBeenNthCalledWith(2, { + expect(messageService.add).toHaveBeenNthCalledWith(2, { severity: 'success', summary: 'Success', detail: 'dot.common.message.saved' @@ -198,12 +173,10 @@ describe('EditEmaLayoutComponent', () => { })); it('should unlock navigation after saving', fakeAsync(() => { - const allowRouting = jest.spyOn(dotRouter, 'allowRouteDeactivation'); - templateBuilder.templateChange.emit(); tick(6000); - expect(allowRouting).toHaveBeenCalled(); + expect(dotRouter.allowRouteDeactivation).toHaveBeenCalled(); })); it('should save right away if we request page leave before the 5 secs', () => { @@ -215,14 +188,14 @@ describe('EditEmaLayoutComponent', () => { expect(saveTemplate).toHaveBeenCalled(); - expect(addMock).toHaveBeenNthCalledWith(1, { + expect(messageService.add).toHaveBeenNthCalledWith(1, { severity: 'info', summary: 'Info', detail: 'dot.common.message.saving', life: 1000 }); - expect(addMock).toHaveBeenNthCalledWith(2, { + expect(messageService.add).toHaveBeenNthCalledWith(2, { severity: 'success', summary: 'Success', detail: 'dot.common.message.saved'