Skip to content

Commit

Permalink
feedback: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rjvelazco committed Aug 23, 2024
1 parent 94ca344 commit 1ee7b85
Showing 1 changed file with 39 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -70,9 +69,8 @@ describe('EditEmaLayoutComponent', () => {
let dotRouter: SpyObject<DotRouterService>;
let store: SpyObject<InstanceType<typeof UVEStore>>;
let templateBuilder: TemplateBuilderComponent;
let layoutService: DotPageLayoutService;
let dotPageLayoutService: DotPageLayoutService;
let messageService: MessageService;
let addMock: jest.SpyInstance;

globalThis.structuredClone = jest.fn().mockImplementation((obj) => obj);

Expand All @@ -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'
)
]
});

Expand All @@ -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',
Expand All @@ -162,48 +143,40 @@ 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'
});
}));

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

0 comments on commit 1ee7b85

Please sign in to comment.