83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { createAnonClient, createServiceRoleClient } from './setup.ts';
|
|
|
|
const client = createAnonClient();
|
|
const adminClient = createServiceRoleClient();
|
|
|
|
describe('Data API (PostgREST-lite)', () => {
|
|
const todoTitle = `Task ${Date.now()}`;
|
|
|
|
it('should create a todo', async () => {
|
|
const { data: rows, error } = await client
|
|
.from('todos')
|
|
.insert({ title: todoTitle, completed: false })
|
|
.select();
|
|
|
|
expect(error).toBeNull();
|
|
expect(rows).toBeDefined();
|
|
expect(rows?.length).toBe(1);
|
|
|
|
const data = rows![0];
|
|
expect(data.title).toBe(todoTitle);
|
|
expect(data.completed).toBe(false);
|
|
});
|
|
|
|
it('should list todos', async () => {
|
|
const { data, error } = await client.from('todos').select('*');
|
|
|
|
expect(error).toBeNull();
|
|
expect(data).toBeDefined();
|
|
expect(Array.isArray(data)).toBe(true);
|
|
expect(data?.length).toBeGreaterThan(0);
|
|
expect(data?.some((t) => t.title === todoTitle)).toBe(true);
|
|
});
|
|
|
|
it('should update a todo', async () => {
|
|
// First get the todo
|
|
const { data: todos } = await client
|
|
.from('todos')
|
|
.select('id')
|
|
.eq('title', todoTitle)
|
|
.limit(1);
|
|
|
|
expect(todos).toBeDefined();
|
|
if (!todos || todos.length === 0) throw new Error('Todo not found');
|
|
const id = todos[0].id;
|
|
|
|
const { error } = await client
|
|
.from('todos')
|
|
.update({ completed: true })
|
|
.eq('id', id);
|
|
|
|
expect(error).toBeNull();
|
|
|
|
// Verify update
|
|
const { data: rows } = await client.from('todos').select('*').eq('id', id);
|
|
expect(rows).toBeDefined();
|
|
expect(rows?.length).toBe(1);
|
|
const updated = rows![0];
|
|
expect(updated.completed).toBe(true);
|
|
});
|
|
|
|
it('should delete a todo', async () => {
|
|
// First get the todo
|
|
const { data: todos } = await client
|
|
.from('todos')
|
|
.select('id')
|
|
.eq('title', todoTitle)
|
|
.limit(1);
|
|
|
|
expect(todos).toBeDefined();
|
|
if (!todos || todos.length === 0) throw new Error('Todo not found');
|
|
const id = todos[0].id;
|
|
|
|
const { error } = await client.from('todos').delete().eq('id', id);
|
|
|
|
expect(error).toBeNull();
|
|
|
|
// Verify deletion
|
|
const { data } = await client.from('todos').select('*').eq('id', id);
|
|
expect(data?.length).toBe(0);
|
|
});
|
|
});
|