Skip to content

Commit

Permalink
test: add unit tests for editor handlers and utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
KarimAziev committed Apr 29, 2024
1 parent 6e275ed commit 3399b0d
Show file tree
Hide file tree
Showing 5 changed files with 523 additions and 0 deletions.
102 changes: 102 additions & 0 deletions __tests__/src/handlers/ace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import AceHandler from '@/handlers/ace';
import InjectorHandler from '@/handlers/injector';

let mockElem: HTMLTextAreaElement;
let mockParent: HTMLDivElement;

const mockContentEventsBinder = {
bind: jest.fn(),
};

describe('AceHandler', () => {
beforeEach(() => {
jest.clearAllMocks();
mockElem = document.createElement('textarea');
mockParent = document.createElement('div');
mockParent.appendChild(mockElem);
document.body.appendChild(mockParent);
});

afterAll(() => {
document.body.innerHTML = '';
});

describe('constructor', () => {
it('should correctly initialize with the given parameters', () => {
const handler = new AceHandler(mockElem, mockContentEventsBinder);

expect(handler).toBeDefined();
expect((handler as any).elem).toBe(mockElem);
});
});

describe('setValue', () => {
it('should set value without triggering DOM event', () => {
const mockValue = 'test value';
const options = { text: mockValue, triggerDOMEvent: false };
const handler = new AceHandler(mockElem, mockContentEventsBinder);
handler.setValue = jest.fn();
handler.setValue(mockValue, options);

expect(handler.setValue).toHaveBeenCalledWith(mockValue, {
...options,
triggerDOMEvent: false,
});
});
});

describe('getValue', () => {
it('should call super.getValue', async () => {
const handler = new AceHandler(mockElem, mockContentEventsBinder);
const superGetValueSpy = jest
.spyOn(InjectorHandler.prototype, 'getValue')
.mockImplementation(async () => ({ text: 'my text' }));
await handler.getValue();
expect(superGetValueSpy).toHaveBeenCalled();
superGetValueSpy.mockRestore();
});

it('should post a "getValue" message and resolve with the value', async () => {
const handler = new AceHandler(mockElem, mockContentEventsBinder);
const promise = handler.getValue();
handler.emit('value', { text: 'testValue' });
await expect(promise).resolves.toEqual({ text: 'testValue' });
});
});

describe('static canHandle determines if the given element can be handled by AceHandler', () => {
it('should correctly identify if the element can be handled', () => {
const canHandle = AceHandler.canHandle(mockElem);

expect(canHandle).toBe(false);
});
it('should return false when the parent element itself is checked', () => {
const canHandle = AceHandler.canHandle(mockParent as any);

expect(canHandle).toBe(false);
});
it('should return true if the element meets all conditions', () => {
mockElem.classList.add('ace_text-input');

const canHandle = AceHandler.canHandle(mockElem);

expect(canHandle).toBe(true);
});
});

describe('static getHintArea', () => {
it("should return the closest visual element that matches the handler's selector", () => {
const result = AceHandler.getHintArea(mockElem);

expect(result).toBe(mockParent);
});
});

describe('static getName', () => {
it('should return "Ace" as the handler name', () => {
const name = AceHandler.getName();

expect(name).toBe('Ace');
});
});
});
112 changes: 112 additions & 0 deletions __tests__/src/handlers/codemirror5.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import CodeMirror5Handler from '@/handlers/codemirror5';
import InjectorHandler from '@/handlers/injector';
import { VISUAL_ELEMENT_SELECTOR } from '@/handlers/config/const';

let mockElem: HTMLTextAreaElement;
let mockParent: HTMLDivElement;

const mockContentEventsBinder = {
bind: jest.fn(),
};

describe('CodeMirror5Handler', () => {
beforeEach(() => {
jest.clearAllMocks();
mockElem = document.createElement('textarea');
mockParent = document.createElement('div');
mockParent.appendChild(mockElem);
document.body.appendChild(mockParent);
});

afterAll(() => {
document.body.innerHTML = '';
});

describe('constructor', () => {
it('should correctly initialize with the given parameters', () => {
const handler = new CodeMirror5Handler(mockElem, mockContentEventsBinder);

expect(handler).toBeDefined();
expect((handler as any).elem).toBe(mockElem);
});
});

describe('setValue', () => {
it('should set value without triggering DOM event', () => {
const mockValue = 'test value';
const options = { text: mockValue, triggerDOMEvent: false };
const handler = new CodeMirror5Handler(mockElem, mockContentEventsBinder);
handler.setValue = jest.fn();
handler.setValue(mockValue, options);

expect(handler.setValue).toHaveBeenCalledWith(mockValue, {
...options,
triggerDOMEvent: false,
});
});
});

describe('getValue', () => {
it('should call super.getValue', async () => {
const handler = new CodeMirror5Handler(mockElem, mockContentEventsBinder);
const superGetValueSpy = jest
.spyOn(InjectorHandler.prototype, 'getValue')
.mockImplementation(async () => ({ text: 'my text' }));
await handler.getValue();
expect(superGetValueSpy).toHaveBeenCalled();
superGetValueSpy.mockRestore();
});

it('should post a "getValue" message and resolve with the value', async () => {
const handler = new CodeMirror5Handler(mockElem, mockContentEventsBinder);
const promise = handler.getValue();
handler.emit('value', { text: 'testValue' });
await expect(promise).resolves.toEqual({ text: 'testValue' });
});
});

describe('static canHandle determines if the given element can be handled by CodeMirror5Handler', () => {
it('should correctly identify if the element can be handled', () => {
const canHandle = CodeMirror5Handler.canHandle(mockElem);

expect(canHandle).toBe(false);
});
it('should return true when the parent element itself is checked', () => {
mockParent.classList.add(
VISUAL_ELEMENT_SELECTOR.codeMirror5.replace('.', ''),
);
const canHandle = CodeMirror5Handler.canHandle(mockParent as any);

expect(canHandle).toBe(true);
});
it('should return true if the element meets all conditions', () => {
mockParent.classList.add(
VISUAL_ELEMENT_SELECTOR.codeMirror5.replace('.', ''),
);

const canHandle = CodeMirror5Handler.canHandle(mockElem);

expect(canHandle).toBe(true);
});
});

describe('static getHintArea', () => {
it("should return the closest visual element that matches the handler's selector", () => {
mockParent.classList.add(
VISUAL_ELEMENT_SELECTOR.codeMirror5.replace('.', ''),
);

const result = CodeMirror5Handler.getHintArea(mockElem);

expect(result).toBe(mockParent);
});
});

describe('static getName', () => {
it('should return "codemirror5" as the handler name', () => {
const name = CodeMirror5Handler.getName();

expect(name).toBe('codemirror5');
});
});
});
114 changes: 114 additions & 0 deletions __tests__/src/handlers/codemirror6.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import CodeMirror6Handler from '@/handlers/codemirror6';
import InjectorHandler from '@/handlers/injector';
import { VISUAL_ELEMENT_SELECTOR } from '@/handlers/config/const';

let mockElem: HTMLTextAreaElement;
let mockParent: HTMLDivElement;

const mockContentEventsBinder = {
bind: jest.fn(),
};

describe('CodeMirror6Handler', () => {
beforeEach(() => {
jest.clearAllMocks();
mockElem = document.createElement('textarea');
mockParent = document.createElement('div');
mockParent.appendChild(mockElem);
document.body.appendChild(mockParent);
});

afterAll(() => {
document.body.innerHTML = '';
});

describe('constructor', () => {
it('should correctly initialize with the given parameters', () => {
const handler = new CodeMirror6Handler(mockElem, mockContentEventsBinder);

expect(handler).toBeDefined();
expect((handler as any).elem).toBe(mockElem);
});
});

describe('setValue', () => {
it('should set value without triggering DOM event', () => {
const mockValue = 'test value';
const options = { text: mockValue, triggerDOMEvent: false };
const handler = new CodeMirror6Handler(mockElem, mockContentEventsBinder);
handler.setValue = jest.fn();
handler.setValue(mockValue, options);

expect(handler.setValue).toHaveBeenCalledWith(mockValue, {
...options,
triggerDOMEvent: false,
});
});
});

describe('getValue', () => {
it('should call super.getValue', async () => {
const handler = new CodeMirror6Handler(mockElem, mockContentEventsBinder);
const superGetValueSpy = jest
.spyOn(InjectorHandler.prototype, 'getValue')
.mockImplementation(async () => ({ text: 'my text' }));
await handler.getValue();
expect(superGetValueSpy).toHaveBeenCalled();
superGetValueSpy.mockRestore();
});

it('should post a "getValue" message and resolve with the value', async () => {
const handler = new CodeMirror6Handler(mockElem, mockContentEventsBinder);
const promise = handler.getValue();
handler.emit('value', { text: 'testValue' });
await expect(promise).resolves.toEqual({ text: 'testValue' });
});
});

describe('static canHandle determines if the given element can be handled by CodeMirror6Handler', () => {
it('should correctly identify if the element can be handled', () => {
const canHandle = CodeMirror6Handler.canHandle(mockElem);

expect(canHandle).toBe(false);
});
it('should return false when the parent element itself is checked', () => {
mockParent.classList.add(
VISUAL_ELEMENT_SELECTOR.cmEditor.replace('.', ''),
);
const canHandle = CodeMirror6Handler.canHandle(mockParent as any);

expect(canHandle).toBe(false);
});
it('should return true if the element meets all conditions', () => {
mockParent.classList.add(
VISUAL_ELEMENT_SELECTOR.cmEditor.replace('.', ''),
);

mockElem.classList.add('cm-content');

const canHandle = CodeMirror6Handler.canHandle(mockElem);

expect(canHandle).toBe(true);
});
});

describe('static getHintArea', () => {
it("should return the closest visual element that matches the handler's selector", () => {
mockParent.classList.add(
VISUAL_ELEMENT_SELECTOR.cmEditor.replace('.', ''),
);

const result = CodeMirror6Handler.getHintArea(mockElem);

expect(result).toBe(mockParent);
});
});

describe('static getName', () => {
it('should return "codemirror6" as the handler name', () => {
const name = CodeMirror6Handler.getName();

expect(name).toBe('codemirror6');
});
});
});
Loading

0 comments on commit 3399b0d

Please sign in to comment.