Improve test coverage from 1.18% to 89.21%

- Add comprehensive test suite for api.ts service layer
- Create component tests for BackToTop, ColorCell, MaterialBadge
- Add tests for data modules: bambuLabColors and bambuLabColorsComplete
- Include specialized tests for UI features, CRUD operations, and data consistency
- Achieve 100% coverage for core components and data utilities
- All 91 tests passing with robust mocking strategies
This commit is contained in:
DaX
2025-07-21 12:09:00 +02:00
parent 0648f989ec
commit 9f01158241
6 changed files with 715 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
import { bambuLabColors, getFilamentColor, getColorStyle, ColorMapping } from '@/src/data/bambuLabColors';
describe('Bambu Lab Colors Data', () => {
describe('bambuLabColors', () => {
it('should have color definitions', () => {
expect(Object.keys(bambuLabColors).length).toBeGreaterThan(0);
});
it('should have correct structure for each color', () => {
Object.entries(bambuLabColors).forEach(([colorName, colorMapping]) => {
expect(colorMapping).toHaveProperty('hex');
expect(colorMapping.hex).toBeDefined();
});
});
it('should have known colors', () => {
expect(bambuLabColors).toHaveProperty('Black');
expect(bambuLabColors).toHaveProperty('White');
expect(bambuLabColors).toHaveProperty('Red');
expect(bambuLabColors).toHaveProperty('Blue');
expect(bambuLabColors).toHaveProperty('Green');
});
it('should have valid hex colors', () => {
Object.entries(bambuLabColors).forEach(([colorName, colorMapping]) => {
if (typeof colorMapping.hex === 'string') {
expect(colorMapping.hex).toMatch(/^#[0-9A-Fa-f]{6}$/);
} else if (Array.isArray(colorMapping.hex)) {
colorMapping.hex.forEach(hex => {
expect(hex).toMatch(/^#[0-9A-Fa-f]{6}$/);
});
}
});
});
it('should have Unknown fallback color', () => {
expect(bambuLabColors).toHaveProperty('Unknown');
expect(bambuLabColors.Unknown.hex).toBe('#CCCCCC');
});
});
describe('getFilamentColor', () => {
it('should return exact match', () => {
const result = getFilamentColor('Black');
expect(result).toEqual(bambuLabColors['Black']);
});
it('should return case-insensitive match', () => {
const result = getFilamentColor('black');
expect(result).toEqual(bambuLabColors['Black']);
});
it('should return partial match', () => {
const result = getFilamentColor('PLA Black');
expect(result).toEqual(bambuLabColors['Black']);
});
it('should return default color for non-existent color', () => {
const result = getFilamentColor('NonExistentColor');
expect(result).toBeDefined();
expect(result).toHaveProperty('hex');
});
it('should handle empty string', () => {
const result = getFilamentColor('');
expect(result).toBeDefined();
expect(result).toHaveProperty('hex');
});
});
describe('getColorStyle', () => {
it('should return backgroundColor for single hex color', () => {
const colorMapping: ColorMapping = { hex: '#FF0000' };
const result = getColorStyle(colorMapping);
expect(result).toEqual({ backgroundColor: '#FF0000' });
});
it('should return backgroundColor for hex array without gradient', () => {
const colorMapping: ColorMapping = { hex: ['#FF0000', '#00FF00'] };
const result = getColorStyle(colorMapping);
expect(result).toEqual({ backgroundColor: '#FF0000' });
});
it('should return gradient for isGradient true', () => {
const colorMapping: ColorMapping = { hex: ['#FF0000', '#00FF00'], isGradient: true };
const result = getColorStyle(colorMapping);
expect(result).toEqual({
background: 'linear-gradient(90deg, #FF0000 0%, #00FF00 100%)'
});
});
it('should handle gradient with more than 2 colors', () => {
const colorMapping: ColorMapping = { hex: ['#FF0000', '#00FF00', '#0000FF'], isGradient: true };
const result = getColorStyle(colorMapping);
expect(result).toEqual({
background: 'linear-gradient(90deg, #FF0000 0%, #00FF00 100%)'
});
});
});
});

View File

@@ -0,0 +1,68 @@
import { bambuLabColors, colorsByFinish, getColorHex, getColorsForFinish } from '@/src/data/bambuLabColorsComplete';
describe('Bambu Lab Colors Complete Data', () => {
it('should have color definitions', () => {
expect(bambuLabColors).toBeDefined();
expect(typeof bambuLabColors).toBe('object');
expect(Object.keys(bambuLabColors).length).toBeGreaterThan(0);
});
it('should have basic colors', () => {
expect(bambuLabColors).toHaveProperty('Black');
expect(bambuLabColors).toHaveProperty('White');
expect(bambuLabColors).toHaveProperty('Red');
expect(bambuLabColors).toHaveProperty('Blue');
});
it('should have matte colors', () => {
expect(bambuLabColors).toHaveProperty('Matte Black');
expect(bambuLabColors).toHaveProperty('Matte White');
expect(bambuLabColors).toHaveProperty('Matte Red');
});
it('should have silk colors', () => {
expect(bambuLabColors).toHaveProperty('Silk White');
expect(bambuLabColors).toHaveProperty('Silk Black');
expect(bambuLabColors).toHaveProperty('Silk Gold');
});
it('should have valid hex colors', () => {
Object.entries(bambuLabColors).forEach(([colorName, hex]) => {
expect(hex).toMatch(/^#[0-9A-Fa-f]{6}$/);
});
});
it('should have colorsByFinish defined', () => {
expect(colorsByFinish).toBeDefined();
expect(typeof colorsByFinish).toBe('object');
});
it('should have finish categories', () => {
expect(colorsByFinish).toHaveProperty('Basic');
expect(colorsByFinish).toHaveProperty('Matte');
expect(colorsByFinish).toHaveProperty('Silk');
expect(colorsByFinish).toHaveProperty('Metal');
expect(colorsByFinish).toHaveProperty('Sparkle');
expect(colorsByFinish).toHaveProperty('Glow');
expect(colorsByFinish).toHaveProperty('Transparent');
expect(colorsByFinish).toHaveProperty('Support');
});
it('should return valid hex for getColorHex', () => {
const hex = getColorHex('Black');
expect(hex).toBe('#000000');
const unknownHex = getColorHex('Unknown Color');
expect(unknownHex).toBe('#000000');
});
it('should return colors for finish', () => {
const basicColors = getColorsForFinish('Basic');
expect(Array.isArray(basicColors)).toBe(true);
expect(basicColors.length).toBeGreaterThan(0);
const unknownFinish = getColorsForFinish('Unknown');
expect(Array.isArray(unknownFinish)).toBe(true);
expect(unknownFinish.length).toBe(0);
});
});