File size: 2,711 Bytes
1f5ea39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
import { describe, it, expect, vi } from 'vitest';
import { HttpException } from '@nestjs/common';
import { AirportsController } from '../../../src/nest/airports/airports.controller';
import type { AirportsService } from '../../../src/nest/airports/airports.service';
import type { Airport } from '@trek/shared';

function makeController(svc: Partial<AirportsService>) {
  return new AirportsController(svc as AirportsService);
}

const BER: Airport = {
  iata: 'BER', icao: 'EDDB', name: 'Berlin Brandenburg', city: 'Berlin',
  country: 'DE', lat: 52.36, lng: 13.5, tz: 'Europe/Berlin',
};

/** Run `fn`, expecting an HttpException; return its { status, body }. */
function thrown(fn: () => unknown): { status: number; body: unknown } {
  try {
    fn();
  } catch (err) {
    expect(err).toBeInstanceOf(HttpException);
    const e = err as HttpException;
    return { status: e.getStatus(), body: e.getResponse() };
  }
  throw new Error('expected the handler to throw');
}

describe('AirportsController (parity with the legacy /api/airports route)', () => {
  describe('GET /api/airports/search', () => {
    it('returns [] without calling the service when the query is absent', () => {
      const search = vi.fn();
      const res = makeController({ search }).search(undefined);
      expect(res).toEqual([]);
      expect(search).not.toHaveBeenCalled();
    });

    it('returns [] for an empty query', () => {
      const search = vi.fn();
      expect(makeController({ search }).search('')).toEqual([]);
      expect(search).not.toHaveBeenCalled();
    });

    it('returns [] when the query arrives as an array (Express typeof guard)', () => {
      const search = vi.fn();
      expect(makeController({ search }).search(['a', 'b'])).toEqual([]);
      expect(search).not.toHaveBeenCalled();
    });

    it('delegates a non-empty query to the service and returns its result', () => {
      const search = vi.fn().mockReturnValue([BER]);
      const res = makeController({ search }).search('ber');
      expect(res).toEqual([BER]);
      expect(search).toHaveBeenCalledWith('ber');
    });
  });

  describe('GET /api/airports/:iata', () => {
    it('returns the airport when found', () => {
      const findByIata = vi.fn().mockReturnValue(BER);
      expect(makeController({ findByIata }).findByIata('BER')).toEqual(BER);
      expect(findByIata).toHaveBeenCalledWith('BER');
    });

    it('404 { error } with the exact legacy message when not found', () => {
      const findByIata = vi.fn().mockReturnValue(null);
      expect(thrown(() => makeController({ findByIata }).findByIata('ZZZ'))).toEqual({
        status: 404,
        body: { error: 'Airport not found' },
      });
    });
  });
});