-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(edit-content): fix unit tests #30062
- Loading branch information
Showing
7 changed files
with
279 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 48 additions & 67 deletions
115
core-web/libs/ui/src/lib/components/dot-ai-image-prompt/ai-image-prompt.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,91 @@ | ||
import { byTestId, createComponentFactory, mockProvider, Spectator } from '@ngneat/spectator'; | ||
import { | ||
byTestId, | ||
createComponentFactory, | ||
mockProvider, | ||
Spectator, | ||
SpyObject | ||
} from '@ngneat/spectator'; | ||
import { patchState } from '@ngrx/signals'; | ||
import { of } from 'rxjs'; | ||
|
||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { fakeAsync } from '@angular/core/testing'; | ||
import { provideHttpClient } from '@angular/common/http'; | ||
|
||
import { ConfirmationService } from 'primeng/api'; | ||
import { Dialog } from 'primeng/dialog'; | ||
import { DynamicDialogConfig, DynamicDialogRef } from 'primeng/dynamicdialog'; | ||
|
||
import { | ||
PromptType, | ||
AIImagePrompt, | ||
DotAIImageOrientation, | ||
DotGeneratedAIImage | ||
} from '@dotcms/dotcms-models'; | ||
import { DotAiService } from '@dotcms/data-access'; | ||
import { PromptType, AIImagePrompt, DotAIImageOrientation } from '@dotcms/dotcms-models'; | ||
|
||
import { DotAIImagePromptComponent } from './ai-image-prompt.component'; | ||
import { DotAiImagePromptStore } from './ai-image-prompt.store'; | ||
import { AiImagePromptFormComponent } from './components/ai-image-prompt-form/ai-image-prompt-form.component'; | ||
import { DotAiImagePromptStore } from './store/ai-image-prompt.store'; | ||
import { MOCK_AI_IMAGE_CONTENT, MOCK_GENERATED_AI_IMAGE } from './utils/mocks'; | ||
|
||
describe('DotAIImagePromptComponent', () => { | ||
let spectator: Spectator<DotAIImagePromptComponent>; | ||
let store: DotAiImagePromptStore; | ||
|
||
const imagesMock: DotGeneratedAIImage[] = [ | ||
{ name: 'image1', url: 'image_url' }, | ||
{ name: 'image2', url: 'image_url_2' } | ||
] as unknown as DotGeneratedAIImage[]; | ||
let store: InstanceType<typeof DotAiImagePromptStore>; | ||
let dynamicDialogRef: SpyObject<DynamicDialogRef>; | ||
let dotAiService: SpyObject<DotAiService>; | ||
let confirmationService: SpyObject<ConfirmationService>; | ||
|
||
const createComponent = createComponentFactory({ | ||
component: DotAIImagePromptComponent, | ||
providers: [ | ||
{ | ||
provide: DotAiImagePromptStore, | ||
useValue: { | ||
vm$: of({ | ||
showDialog: true, | ||
isLoading: false, | ||
images: imagesMock, | ||
galleryActiveIndex: 0, | ||
orientation: DotAIImageOrientation.VERTICAL | ||
}), | ||
generateImage: jasmine.createSpy('generateImage'), | ||
hideDialog: jasmine.createSpy('hideDialog'), | ||
patchState: jasmine.createSpy('patchState'), | ||
cleanError: jasmine.createSpy('cleanError'), | ||
setSelectedImage: jasmine.createSpy('setSelectedImage') | ||
} | ||
}, | ||
mockProvider(ConfirmationService) | ||
componentProviders: [ | ||
DotAiImagePromptStore, | ||
mockProvider(DynamicDialogRef), | ||
mockProvider(DynamicDialogConfig), | ||
ConfirmationService | ||
], | ||
imports: [HttpClientTestingModule] | ||
providers: [provideHttpClient(), mockProvider(DotAiService)] | ||
}); | ||
|
||
beforeEach(() => { | ||
spectator = createComponent(); | ||
store = spectator.inject(DotAiImagePromptStore); | ||
store = spectator.inject(DotAiImagePromptStore, true); | ||
dynamicDialogRef = spectator.inject(DynamicDialogRef, true); | ||
dotAiService = spectator.inject(DotAiService, true); | ||
confirmationService = spectator.inject(ConfirmationService, true); | ||
}); | ||
|
||
it('should hide dialog', () => { | ||
const dialog = spectator.query(Dialog); | ||
dialog.onHide.emit('true'); | ||
expect(store.hideDialog).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should the modal have 1040px in maxWidth', () => { | ||
spectator.detectChanges(); | ||
const dialog = spectator.query(Dialog); | ||
const width = dialog.style.width; | ||
const maxWidth = dialog.style.maxWidth; | ||
expect(width).toBe('90%'); | ||
expect(maxWidth).toBe('1040px'); | ||
it('should create', () => { | ||
expect(spectator.component).toBeTruthy(); | ||
}); | ||
|
||
it('should generate image', () => { | ||
const promptForm = spectator.query(AiImagePromptFormComponent); | ||
const generateImageSpy = spyOn(store, 'generateImage'); | ||
dotAiService.generateAndPublishImage.and.returnValue(of(MOCK_AI_IMAGE_CONTENT)); | ||
|
||
const formMock: AIImagePrompt = { | ||
text: 'Test', | ||
type: PromptType.INPUT, | ||
size: DotAIImageOrientation.VERTICAL | ||
}; | ||
promptForm.valueChange.emit(formMock); | ||
promptForm.generate.emit(); | ||
patchState(store, { formValue: formMock }); | ||
|
||
expect(store.generateImage).toHaveBeenCalledWith(); | ||
spectator.triggerEventHandler(AiImagePromptFormComponent, 'generate', null); | ||
expect(generateImageSpy).toHaveBeenCalledWith(); | ||
}); | ||
|
||
it('should inset image', async () => { | ||
it('should inset an image', async () => { | ||
patchState(store, { images: [MOCK_GENERATED_AI_IMAGE] }); | ||
spectator.detectChanges(); | ||
|
||
const submitBtn = spectator.query(byTestId('submit-btn')); | ||
|
||
spectator.click(submitBtn); | ||
await spectator.fixture.whenStable(); | ||
expect(store.setSelectedImage).toHaveBeenCalled(); | ||
expect(dynamicDialogRef.close).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should clear error on hide confirm', () => { | ||
const dialog = spectator.query(Dialog); | ||
dialog.onHide.emit('true'); | ||
expect(store.hideDialog).toHaveBeenCalled(); | ||
}); | ||
it('should call confirm dialog when try to close dialog', async () => { | ||
patchState(store, { images: [MOCK_GENERATED_AI_IMAGE] }); | ||
const confirmSpy = spyOn(confirmationService, 'confirm'); | ||
spectator.detectChanges(); | ||
|
||
it('should call confirm dialog when try to close dialog', fakeAsync(() => { | ||
const closeBtn = spectator.query(byTestId('close-btn')); | ||
const spyCloseDialog = spyOn(spectator.component, 'closeDialog'); | ||
|
||
spectator.click(closeBtn); | ||
spectator.tick(); | ||
expect(spyCloseDialog).toHaveBeenCalled(); | ||
})); | ||
await spectator.fixture.whenStable(); | ||
expect(confirmSpy).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
core-web/libs/ui/src/lib/components/dot-ai-image-prompt/store/ai-image-prompt.store.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { SpyObject, mockProvider } from '@ngneat/spectator'; | ||
import { patchState } from '@ngrx/signals'; | ||
import { of, throwError } from 'rxjs'; | ||
|
||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { DotAiService } from '@dotcms/data-access'; | ||
import { ComponentStatus, DotAIImageOrientation, PromptType } from '@dotcms/dotcms-models'; | ||
|
||
import { DotAiImagePromptStore } from './ai-image-prompt.store'; | ||
|
||
import { MOCK_AI_IMAGE_CONTENT, MOCK_GENERATED_AI_IMAGE } from '../utils/mocks'; | ||
|
||
describe('DotAiImagePromptStore', () => { | ||
let store: InstanceType<typeof DotAiImagePromptStore>; | ||
let dotAiService: SpyObject<DotAiService>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [DotAiImagePromptStore, mockProvider(DotAiService)] | ||
}); | ||
|
||
store = TestBed.inject(DotAiImagePromptStore); | ||
dotAiService = TestBed.inject(DotAiService) as SpyObject<DotAiService>; | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(store).toBeTruthy(); | ||
}); | ||
|
||
describe('Initial State', () => { | ||
it('should have the correct initial state', () => { | ||
expect(store.status()).toBe(ComponentStatus.INIT); | ||
expect(store.images()).toEqual([]); | ||
expect(store.context()).toBeNull(); | ||
expect(store.galleryActiveIndex()).toBe(0); | ||
expect(store.error()).toBeNull(); | ||
expect(store.formValue()).toEqual({ | ||
text: '', | ||
type: PromptType.INPUT, | ||
size: DotAIImageOrientation.HORIZONTAL | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Computed Properties', () => { | ||
it('should compute isLoading correctly', () => { | ||
expect(store.isLoading()).toBe(false); | ||
patchState(store, { status: ComponentStatus.LOADING }); | ||
expect(store.isLoading()).toBe(true); | ||
}); | ||
|
||
it('should compute hasContext correctly', () => { | ||
expect(store.hasContext()).toBe(false); | ||
patchState(store, { context: 'context' }); | ||
expect(store.hasContext()).toBe(true); | ||
}); | ||
|
||
it('should compute currentImage correctly', () => { | ||
expect(store.currentImage()).toBeUndefined(); | ||
patchState(store, { | ||
images: [MOCK_GENERATED_AI_IMAGE] | ||
}); | ||
expect(store.currentImage()).toEqual(MOCK_GENERATED_AI_IMAGE); | ||
}); | ||
}); | ||
|
||
describe('Methods', () => { | ||
it('should set galleryActiveIndex correctly', () => { | ||
store.setGalleryActiveIndex(1); | ||
expect(store.galleryActiveIndex()).toBe(1); | ||
}); | ||
|
||
it('should set context correctly', () => { | ||
store.setContext('new context'); | ||
expect(store.context()).toBe('new context'); | ||
}); | ||
|
||
it('should set formValue correctly', () => { | ||
const formValue = { | ||
text: 'new text', | ||
type: PromptType.INPUT, | ||
size: DotAIImageOrientation.VERTICAL | ||
}; | ||
store.setFormValue(formValue); | ||
expect(store.formValue()).toEqual(formValue); | ||
}); | ||
|
||
it('should handle generateImage correctly on success', () => { | ||
dotAiService.generateAndPublishImage.and.returnValue(of(MOCK_AI_IMAGE_CONTENT)); | ||
|
||
store.setFormValue({ | ||
text: 'prompt', | ||
type: PromptType.INPUT, | ||
size: DotAIImageOrientation.HORIZONTAL | ||
}); | ||
store.generateImage(); | ||
|
||
expect(dotAiService.generateAndPublishImage).toHaveBeenCalledWith( | ||
'prompt', | ||
DotAIImageOrientation.HORIZONTAL | ||
); | ||
expect(store.status()).toBe(ComponentStatus.IDLE); | ||
expect(store.images().length).toBe(1); | ||
}); | ||
|
||
it('should handle generateImage correctly on error', () => { | ||
dotAiService.generateAndPublishImage.and.returnValue(throwError('error')); | ||
|
||
store.setFormValue({ | ||
text: 'prompt', | ||
type: PromptType.INPUT, | ||
size: DotAIImageOrientation.HORIZONTAL | ||
}); | ||
store.generateImage(); | ||
|
||
expect(dotAiService.generateAndPublishImage).toHaveBeenCalledWith( | ||
'prompt', | ||
DotAIImageOrientation.HORIZONTAL | ||
); | ||
expect(store.status()).toBe(ComponentStatus.ERROR); | ||
expect(store.error()).toBe('error'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.