prevision / apps /prevision-api /test /tiles.e2e-spec.ts
github-actions[bot]
Automated Deploy: 23110475c742a5d4f03930b55bca09042215c1dd
1fe7075
Raw History Blame Contribute Delete
2.98 kB
import type { INestApplication } from '@nestjs/common';
import { api, createApp, insertTiles, resetDatabase, signIn } from './helpers.js';
describe('Tiles (e2e)', () => {
let app: INestApplication;
let headers: Record<string, string>;
beforeAll(async () => {
app = await createApp();
});
afterAll(() => app.close());
beforeEach(async () => {
await resetDatabase();
headers = await signIn('user_tiles');
await insertTiles(
{ slug: 'kd-1', name: 'Juraworld Grey', surfaces: ['floor', 'walls'], collection: 'Juraworld' },
{ slug: 'kd-2', name: 'Juraworld White', surfaces: ['walls'], collection: 'Juraworld' },
{ slug: 'marble', name: 'Black Marble' },
{ slug: 'deleted', name: 'Deleted', deleted: true },
);
});
it('lists tiles that are not deleted, paginated', async () => {
const response = await api(app).get('/api/tiles').query({ pageSize: 2 }).set(headers).expect(200);
expect(response.body.success).toBe(true);
expect(response.body.data.map((tile: { slug: string }) => tile.slug)).toEqual(['marble', 'kd-1']);
expect(response.body.meta).toEqual({ page: 1, pageSize: 2, total: 3, totalPages: 2 });
expect(response.body.data[0]).toMatchObject({ imageUrl: 'https://cdn.test/marble.jpg', render: { layout: 'grid' } });
const second = await api(app).get('/api/tiles').query({ pageSize: 2, page: 2 }).set(headers);
expect(second.body.data.map((tile: { slug: string }) => tile.slug)).toEqual(['kd-2']);
});
it('filters by search, surface and collection, and sorts', async () => {
const response = await api(app).get('/api/tiles').query({ q: 'jura', surface: 'walls', sort: '-name' }).set(headers);
expect(response.body.data.map((tile: { slug: string }) => tile.slug)).toEqual(['kd-2', 'kd-1']);
expect((await api(app).get('/api/tiles').query({ collection: 'Juraworld' }).set(headers)).body.meta.total).toBe(2);
expect((await api(app).get('/api/tiles').query({ q: '%' }).set(headers)).body.meta.total).toBe(0);
});
it('counts facets in one response', async () => {
const response = await api(app).get('/api/tiles/facets').set(headers).expect(200);
expect(response.body.data).toEqual({
total: 3,
surfaces: [
{ value: 'floor', count: 2 },
{ value: 'walls', count: 2 },
],
collections: [{ value: 'Juraworld', count: 2 }],
});
});
it('finds one tile by slug', async () => {
expect((await api(app).get('/api/tiles/kd-1').set(headers).expect(200)).body.data.slug).toBe('kd-1');
expect((await api(app).get('/api/tiles/deleted').set(headers).expect(404)).body.error.code).toBe('TILE_NOT_FOUND');
});
it('rejects invalid queries', async () => {
await api(app).get('/api/tiles').query({ pageSize: 500 }).set(headers).expect(400);
await api(app).get('/api/tiles').query({ sort: 'price' }).set(headers).expect(400);
await api(app).get('/api/tiles/Not_A_Slug').set(headers).expect(400);
});
});