/**
* Elementor Data Service Unit Tests
*/
import { ElementorDataService } from '../../src/services/elementor/elementor-data-service.js';
describe('ElementorDataService', () => {
let elementorDataService: ElementorDataService;
let mockAxios: any;
beforeEach(() => {
// Mock axios instance
mockAxios = {
get: jest.fn(),
post: jest.fn(),
put: jest.fn()
};
elementorDataService = new ElementorDataService(mockAxios);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('getElementorData', () => {
it('should fetch Elementor data for a page', async () => {
const postId = 123;
const mockPageData = {
id: postId,
meta: {
_elementor_data: JSON.stringify([
{
id: 'section1',
elType: 'section',
settings: { background_color: '#ffffff' },
elements: []
}
])
}
};
mockAxios.get.mockResolvedValue({ data: mockPageData });
const result = await elementorDataService.getElementorData(postId);
expect(mockAxios.get).toHaveBeenCalledWith(`/wp-json/wp/v2/pages/${postId}`, {
params: { context: 'edit' }
});
expect(result).toEqual(JSON.parse(mockPageData.meta._elementor_data));
});
it('should handle missing Elementor data', async () => {
const postId = 123;
const mockPageData = {
id: postId,
meta: {}
};
mockAxios.get.mockResolvedValue({ data: mockPageData });
const result = await elementorDataService.getElementorData(postId);
expect(result).toEqual([]);
});
it('should handle invalid JSON data', async () => {
const postId = 123;
const mockPageData = {
id: postId,
meta: {
_elementor_data: 'invalid json'
}
};
mockAxios.get.mockResolvedValue({ data: mockPageData });
await expect(elementorDataService.getElementorData(postId)).rejects.toThrow();
});
});
describe('updateElementorData', () => {
it('should update Elementor data for a page', async () => {
const postId = 123;
const elementorData = [
{
id: 'section1',
elType: 'section',
settings: { background_color: '#ffffff' },
elements: []
}
];
const mockResponse = {
data: { id: postId, updated: true }
};
mockAxios.put.mockResolvedValue(mockResponse);
const result = await elementorDataService.updateElementorData(postId, elementorData);
expect(mockAxios.put).toHaveBeenCalledWith(`/wp-json/wp/v2/pages/${postId}`, {
meta: {
_elementor_data: JSON.stringify(elementorData),
_elementor_edit_mode: 'builder',
_elementor_template_type: 'wp-page',
_elementor_version: '3.16.0'
}
});
expect(result).toEqual(mockResponse.data);
});
it('should handle update errors', async () => {
const postId = 123;
const elementorData = [];
const mockError = new Error('Update failed');
mockAxios.put.mockRejectedValue(mockError);
await expect(elementorDataService.updateElementorData(postId, elementorData)).rejects.toThrow('Update failed');
});
});
describe('getElementorDataChunked', () => {
it('should get Elementor data in chunks', async () => {
const postId = 123;
const chunkSize = 2;
const largeElementorData = [
{ id: 'section1', elType: 'section' },
{ id: 'section2', elType: 'section' },
{ id: 'section3', elType: 'section' },
{ id: 'section4', elType: 'section' }
];
const mockPageData = {
id: postId,
meta: {
_elementor_data: JSON.stringify(largeElementorData)
}
};
mockAxios.get.mockResolvedValue({ data: mockPageData });
const result = await elementorDataService.getElementorDataChunked(postId, chunkSize);
expect(result).toEqual({
chunks: [
[largeElementorData[0], largeElementorData[1]],
[largeElementorData[2], largeElementorData[3]]
],
totalElements: 4,
totalChunks: 2
});
});
it('should handle empty Elementor data', async () => {
const postId = 123;
const mockPageData = {
id: postId,
meta: {
_elementor_data: JSON.stringify([])
}
};
mockAxios.get.mockResolvedValue({ data: mockPageData });
const result = await elementorDataService.getElementorDataChunked(postId, 2);
expect(result).toEqual({
chunks: [],
totalElements: 0,
totalChunks: 0
});
});
});
describe('backupElementorData', () => {
it('should backup Elementor data', async () => {
const postId = 123;
const elementorData = [
{
id: 'section1',
elType: 'section',
settings: { background_color: '#ffffff' },
elements: []
}
];
const mockPageData = {
id: postId,
meta: {
_elementor_data: JSON.stringify(elementorData)
}
};
mockAxios.get.mockResolvedValue({ data: mockPageData });
const result = await elementorDataService.backupElementorData(postId);
expect(result).toEqual({
postId,
elementorData,
backupTimestamp: expect.any(String),
version: '3.16.0'
});
});
});
describe('clearElementorCache', () => {
it('should clear Elementor cache', async () => {
const mockResponse = {
data: { success: true, message: 'Cache cleared' }
};
mockAxios.post.mockResolvedValue(mockResponse);
const result = await elementorDataService.clearElementorCache();
expect(mockAxios.post).toHaveBeenCalledWith('/wp-json/elementor/v1/cache/clear');
expect(result).toEqual(mockResponse.data);
});
it('should clear cache for specific page', async () => {
const postId = 123;
const mockResponse = {
data: { success: true, message: `Cache cleared for page ${postId}` }
};
mockAxios.post.mockResolvedValue(mockResponse);
const result = await elementorDataService.clearElementorCache(postId);
expect(mockAxios.post).toHaveBeenCalledWith(`/wp-json/elementor/v1/cache/clear/${postId}`);
expect(result).toEqual(mockResponse.data);
});
});
});