File size: 2,980 Bytes
1fe7075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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);
  });
});