Skip to content

Commit

Permalink
chore(block-editor): fix some unit tests #29672
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobytes committed Sep 4, 2024
1 parent e622545 commit 2e9be12
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const BLOCK_EDITOR_FIELD = {
standalone: true,
template: `
<form [formGroup]="form">
<dot-block-editor formControlName="block"></dot-block-editor>
<dot-block-editor formControlName="block" />
</form>
`
})
Expand All @@ -66,24 +66,9 @@ describe('DotBlockEditorComponent - ControlValueAccesor', () => {

it('should set form value when binary file changes', () => {
const blockEditorComponent = spectator.query(DotBlockEditorComponent);
blockEditorComponent.value = BLOCK_EDITOR_FIELD;
blockEditorComponent.writeValue(BLOCK_EDITOR_FIELD);

const formValue = spectator.component.form.get('block').value;
const expected = {
type: 'doc',
content: [
{
type: 'paragraph',
attrs: { textAlign: 'left' },
content: [
{
type: 'text',
text: '{"attrs":{"charCount":9,"readingTime":1,"wordCount":2},"content":[{"attrs":{"level":1,"textAlign":"left"},"content":[{"text":"A title!!","type":"text"}],"type":"heading"}],"type":"doc"}'
}
]
}
]
};
expect(formValue).toEqual(JSON.stringify(expected));
expect(formValue).toEqual(JSON.stringify(BLOCK_EDITOR_FIELD));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,63 @@ import { of } from 'rxjs';
import { DotAiService } from '@dotcms/data-access';
import { ComponentStatus } from '@dotcms/dotcms-models';

import { AiContentPromptState, AiContentPromptStore } from './ai-content-prompt.store';
import { initialState, AiContentPromptStore } from './ai-content-prompt.store';

describe('AiContentPromptStore', () => {
let spectator: SpectatorService<AiContentPromptStore>;
let store: AiContentPromptStore;
let dotAiService: SpyObject<DotAiService>;

const createStoreService = createServiceFactory({
const createService = createServiceFactory({
service: AiContentPromptStore,
mocks: [DotAiService]
});

beforeEach(() => {
spectator = createStoreService();
spectator = createService();
store = spectator.service;
dotAiService = spectator.inject(DotAiService);
});

it('should set open state', (done) => {
spectator.service.setStatus(ComponentStatus.INIT);
store.setStatus(ComponentStatus.INIT);
store.state$.subscribe((state) => {
expect(state.status).toBe(ComponentStatus.INIT);
done();
});
});

it('should showDialog and set the initial state', (done) => {
const initialState: AiContentPromptState = {
prompt: '',
generatedContent: [],
selectedContent: '',
activeIndex: null,
status: ComponentStatus.INIT,
showDialog: false,
submitLabel: 'block-editor.extension.ai-image.generate'
};

//dirty state
spectator.service.patchState({
store.patchState({
prompt: 'test prompt',
selectedContent: 'test selected content'
});

spectator.service.showDialog();
store.showDialog();

store.state$.subscribe((state) => {
expect(state.showDialog).toEqual(true);
expect(state).toEqual(initialState);
expect(JSON.stringify(state)).toBe(
JSON.stringify({
...initialState,
generatedContent: [],
showDialog: true
})
);
done();
});
});

it('should hideDialog', (done) => {
spectator.service.patchState({ showDialog: true });
spectator.service.hideDialog();
store.patchState({ showDialog: true });
store.hideDialog();
store.state$.subscribe((state) => {
expect(state.showDialog).toEqual(false);
done();
});
});

it('should handle subscription on selected Content', (done) => {
spectator.service.patchState({ selectedContent: 'test selected content' });
store.patchState({ selectedContent: 'test selected content' });

store.selectedContent$.subscribe((selectedContent) => {
expect(selectedContent).toBe('test selected content');
Expand All @@ -81,12 +76,14 @@ describe('AiContentPromptStore', () => {
dotAiService.generateContent.mockReturnValue(of(content));

// Trigger the effect
spectator.service.generateContent(of(prompt));
store.generateContent(of(prompt));

// Check if state is updated correctly
store.state$.subscribe((state) => {
expect(state.status).toBe(ComponentStatus.IDLE);
expect(state.generatedContent).toBe([{ content, prompt }]);
expect(state.generatedContent[0].content).toBe(content);
expect(state.generatedContent[0].prompt).toBe(prompt);

done();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface AiContentPromptState {
submitLabel: string;
}

const initialState: AiContentPromptState = {
export const initialState: AiContentPromptState = {
prompt: '',
generatedContent: [],
selectedContent: '',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';

import { ImageTabviewFormComponent } from './image-tabview-form.component';
import { AssetFormComponent } from './asset-form.component';

describe('ImageTabviewFormComponent', () => {
let component: ImageTabviewFormComponent;
let fixture: ComponentFixture<ImageTabviewFormComponent>;
describe('AssetFormComponent', () => {
let spectator: Spectator<AssetFormComponent>;
const createComponent = createComponentFactory(AssetFormComponent);

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ImageTabviewFormComponent]
}).compileComponents();

fixture = TestBed.createComponent(ImageTabviewFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
beforeEach(() => (spectator = createComponent()));

it('should create', () => {
expect(component).toBeTruthy();
expect(spectator.component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import { By, DomSanitizer } from '@angular/platform-browser';

import { delay } from 'rxjs/operators';

import { DotUploadFileService } from '@dotcms/data-access';
import { DotCMSContentlet } from '@dotcms/dotcms-models';

import { DotUploadAssetComponent, STATUS } from './dot-upload-asset.component';

import { DotUploadFileService } from '../../../../shared';

@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'p-fileUpload',
Expand Down

This file was deleted.

20 changes: 9 additions & 11 deletions core-web/libs/block-editor/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// Configure Jest for Angular [https://medium.com/@kyjungok/setup-jest-in-angular-application-22b22609cbcd]
import 'jest-preset-angular/setup-jest';

import { NgModule } from '@angular/core';

// This is needed to mock the PrimeNG SplitButton component to avoid errors while running tests.
// https://github.com/primefaces/primeng/issues/12945
@NgModule()
export class SplitButtonMockModule {}

jest.mock('primeng/splitbutton', () => ({
SplitButtonModule: SplitButtonMockModule
}));
// Workaround for the following issue:
// https://github.com/jsdom/jsdom/issues/2177#issuecomment-1724971596
const originalConsoleError = console.error;
const jsDomCssError = 'Error: Could not parse CSS stylesheet';
console.error = (...params) => {
if (!params.find((p) => p.toString().includes(jsDomCssError))) {
originalConsoleError(...params);
}
};
27 changes: 0 additions & 27 deletions core-web/libs/block-editor/src/test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion core-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
"@types/googlemaps": "3.40.3",
"@types/jasmine": "4.0.3",
"@types/jasminewd2": "~2.0.8",
"@types/jest": "29.5.10",
"@types/jest": "^29.5.12",
"@types/md5": "^2.3.5",
"@types/node": "^18.16.9",
"@types/puppeteer": "^5.4.2",
Expand Down
Loading

0 comments on commit 2e9be12

Please sign in to comment.