122 lines
3.8 KiB
TypeScript
122 lines
3.8 KiB
TypeScript
/**
|
|
* Excel Integration Tests
|
|
* Verifies that Excel data is correctly parsed and integrated into products
|
|
*/
|
|
|
|
import { describe, it, expect } from 'vitest';
|
|
import { getExcelTechnicalDataForProduct, getExcelRowsForProduct } from '../lib/excel-products';
|
|
import { enrichProductWithExcelData, getAllProducts } from '../lib/data';
|
|
|
|
describe('Excel Integration', () => {
|
|
it('should parse Excel files and return technical data', () => {
|
|
const testProduct = {
|
|
name: 'NA2XS(FL)2Y',
|
|
slug: 'na2xsfl2y-3',
|
|
sku: 'NA2XS(FL)2Y-high-voltage-cables',
|
|
translationKey: 'na2xsfl2y-3'
|
|
};
|
|
|
|
const excelData = getExcelTechnicalDataForProduct(testProduct);
|
|
|
|
expect(excelData).toBeTruthy();
|
|
// Avoid non-null assertions here because ESLint may parse this file without TS syntax support.
|
|
if (!excelData) throw new Error('Expected excelData to be defined');
|
|
expect(excelData.configurations).toBeInstanceOf(Array);
|
|
expect(excelData.configurations.length).toBeGreaterThan(0);
|
|
expect(excelData.attributes).toBeInstanceOf(Array);
|
|
});
|
|
|
|
it('should return correct structure for Excel data', () => {
|
|
const testProduct = {
|
|
name: 'NA2XS(FL)2Y',
|
|
slug: 'na2xsfl2y-3',
|
|
sku: 'NA2XS(FL)2Y-high-voltage-cables',
|
|
translationKey: 'na2xsfl2y-3'
|
|
};
|
|
|
|
const excelData = getExcelTechnicalDataForProduct(testProduct);
|
|
|
|
expect(excelData).toHaveProperty('configurations');
|
|
expect(excelData).toHaveProperty('attributes');
|
|
|
|
// Check that configurations are properly formatted
|
|
if (excelData && excelData.configurations.length > 0) {
|
|
const firstConfig = excelData.configurations[0];
|
|
// Should contain cross-section and optionally voltage
|
|
expect(typeof firstConfig).toBe('string');
|
|
expect(firstConfig.length).toBeGreaterThan(0);
|
|
}
|
|
});
|
|
|
|
it('should enrich products with Excel data', () => {
|
|
const products = getAllProducts();
|
|
const testProduct = products.find(p => p.slug === 'na2xsfl2y-3');
|
|
|
|
if (!testProduct) {
|
|
// Skip test if product not found
|
|
return;
|
|
}
|
|
|
|
const enriched = enrichProductWithExcelData(testProduct);
|
|
|
|
expect(enriched.excelConfigurations).toBeDefined();
|
|
expect(enriched.excelAttributes).toBeDefined();
|
|
|
|
if (enriched.excelConfigurations) {
|
|
expect(enriched.excelConfigurations.length).toBeGreaterThan(0);
|
|
}
|
|
});
|
|
|
|
it('should handle products without Excel data gracefully', () => {
|
|
const testProduct = {
|
|
id: 99999,
|
|
translationKey: 'nonexistent-product',
|
|
locale: 'en',
|
|
slug: 'nonexistent-product',
|
|
path: '/product/nonexistent-product',
|
|
name: 'Nonexistent Product',
|
|
shortDescriptionHtml: '<p>Test</p>',
|
|
descriptionHtml: '<p>Test</p>',
|
|
images: [],
|
|
featuredImage: null,
|
|
sku: 'TEST-001',
|
|
regularPrice: '',
|
|
salePrice: '',
|
|
currency: 'EUR',
|
|
stockStatus: 'instock',
|
|
categories: [],
|
|
attributes: [],
|
|
variations: [],
|
|
updatedAt: new Date().toISOString(),
|
|
translation: null
|
|
};
|
|
|
|
const enriched = enrichProductWithExcelData(testProduct);
|
|
|
|
// Should not have Excel data for non-existent product
|
|
expect(enriched.excelConfigurations).toBeUndefined();
|
|
expect(enriched.excelAttributes).toBeUndefined();
|
|
});
|
|
|
|
it('should get raw Excel rows for inspection', () => {
|
|
const testProduct = {
|
|
name: 'NA2XS(FL)2Y',
|
|
slug: 'na2xsfl2y-3',
|
|
sku: 'NA2XS(FL)2Y-high-voltage-cables',
|
|
translationKey: 'na2xsfl2y-3'
|
|
};
|
|
|
|
const rows = getExcelRowsForProduct(testProduct);
|
|
|
|
expect(rows).toBeInstanceOf(Array);
|
|
expect(rows.length).toBeGreaterThan(0);
|
|
|
|
if (rows.length > 0) {
|
|
// Should have valid row structure
|
|
const firstRow = rows[0];
|
|
expect(typeof firstRow).toBe('object');
|
|
expect(Object.keys(firstRow).length).toBeGreaterThan(0);
|
|
}
|
|
});
|
|
});
|