fix(seo): keep whole words when truncating slugs, detect unique violations as Prisma errors
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { BadRequestException, ConflictException, Logger, NotFoundException } from '@nestjs/common';
|
||||
import { SlugService } from './slug.service';
|
||||
import { SlugEntityType } from '../../../prisma/generated/client';
|
||||
import { Prisma, SlugEntityType } from '../../../prisma/generated/client';
|
||||
import { SlugCandidateRow } from '../../database/slug.database.repository';
|
||||
|
||||
const row = (hiboutikId: number, name: string): SlugCandidateRow => ({ id: `uuid-${hiboutikId}`, hiboutikId, name });
|
||||
@@ -93,10 +93,22 @@ describe('SlugService', () => {
|
||||
slugDatabaseRepository.changeSlug.mockResolvedValueOnce('TAKEN');
|
||||
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'bts')).rejects.toThrow(ConflictException);
|
||||
|
||||
slugDatabaseRepository.changeSlug.mockRejectedValueOnce(Object.assign(new Error('Unique constraint failed'), { code: 'P2002' }));
|
||||
slugDatabaseRepository.changeSlug.mockRejectedValueOnce(
|
||||
new Prisma.PrismaClientKnownRequestError('Unique constraint failed', { code: 'P2002', clientVersion: 'test' }),
|
||||
);
|
||||
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'bts')).rejects.toThrow(ConflictException);
|
||||
});
|
||||
|
||||
it('rethrows any other error, even one carrying a P2002 code without being a Prisma error', async () => {
|
||||
const notPrisma = Object.assign(new Error('boom'), { code: 'P2002' });
|
||||
slugDatabaseRepository.changeSlug.mockRejectedValueOnce(notPrisma);
|
||||
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'bts')).rejects.toBe(notPrisma);
|
||||
|
||||
const otherPrismaError = new Prisma.PrismaClientKnownRequestError('Record not found', { code: 'P2025', clientVersion: 'test' });
|
||||
slugDatabaseRepository.changeSlug.mockRejectedValueOnce(otherPrismaError);
|
||||
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'bts')).rejects.toBe(otherPrismaError);
|
||||
});
|
||||
|
||||
it('accepts a valid new slug', async () => {
|
||||
slugDatabaseRepository.changeSlug.mockResolvedValue('CHANGED');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user