parent
fe42b4b6ce
commit
f2e1d0080c
@ -0,0 +1,50 @@
|
|||||||
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
|
||||||
|
const dobles = vi.hoisted(() => {
|
||||||
|
const returning = vi.fn();
|
||||||
|
const onConflictDoNothing = vi.fn(() => ({ returning }));
|
||||||
|
const values = vi.fn(() => ({ onConflictDoNothing }));
|
||||||
|
const insert = vi.fn(() => ({ values }));
|
||||||
|
const idDeCancion = vi.fn();
|
||||||
|
return { returning, onConflictDoNothing, values, insert, idDeCancion };
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.mock('./db', () => ({ db: { insert: dobles.insert } }));
|
||||||
|
vi.mock('./canciones', () => ({ idDeCancion: dobles.idDeCancion }));
|
||||||
|
vi.mock('./db/schema', () => ({
|
||||||
|
cancion: { id: 'cancion.id', slug: 'cancion.slug' },
|
||||||
|
valoracion: {
|
||||||
|
usuarioId: 'valoracion.usuarioId',
|
||||||
|
cancionId: 'valoracion.cancionId',
|
||||||
|
puntuacion: 'valoracion.puntuacion'
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
import { valorar } from './valoraciones';
|
||||||
|
|
||||||
|
describe('persistencia de valoraciones', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
dobles.idDeCancion.mockResolvedValue('cancion-1');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('inserta el primer voto pero no actualiza uno existente', async () => {
|
||||||
|
dobles.returning.mockResolvedValueOnce([{ puntuacion: 4 }]).mockResolvedValueOnce([]);
|
||||||
|
|
||||||
|
await expect(valorar('usuario-1', 'prometiste', 4)).resolves.toBe('guardada');
|
||||||
|
await expect(valorar('usuario-1', 'prometiste', 2)).resolves.toBe('ya-valorada');
|
||||||
|
expect(dobles.onConflictDoNothing).toHaveBeenCalledTimes(2);
|
||||||
|
expect(dobles.values).toHaveBeenNthCalledWith(2, {
|
||||||
|
usuarioId: 'usuario-1',
|
||||||
|
cancionId: 'cancion-1',
|
||||||
|
puntuacion: 2
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('no intenta guardar el voto de una canción inexistente', async () => {
|
||||||
|
dobles.idDeCancion.mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
await expect(valorar('usuario-1', 'no-existe', 4)).resolves.toBe('cancion-inexistente');
|
||||||
|
expect(dobles.insert).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in new issue