File size: 2,157 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
import { describe, it, expect, vi } from 'vitest';
import { HttpException } from '@nestjs/common';
import { SystemNoticesController } from '../../../src/nest/system-notices/system-notices.controller';
import type { SystemNoticesService } from '../../../src/nest/system-notices/system-notices.service';
import type { User } from '../../../src/types';
import type { SystemNoticeDto } from '@trek/shared';

function makeController(svc: Partial<SystemNoticesService>) {
  return new SystemNoticesController(svc as SystemNoticesService);
}

const user = { id: 7 } as User;

const notice: SystemNoticeDto = {
  id: 'welcome', display: 'modal', severity: 'info',
  titleKey: 'notice.welcome.title', bodyKey: 'notice.welcome.body', dismissible: true,
};

/** 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('SystemNoticesController (parity with the legacy /api/system-notices route)', () => {
  describe('GET /active', () => {
    it('returns the evaluated notices for the current user', () => {
      const getActiveFor = vi.fn().mockReturnValue([notice]);
      expect(makeController({ getActiveFor }).active(user)).toEqual([notice]);
      expect(getActiveFor).toHaveBeenCalledWith(7);
    });
  });

  describe('POST /:id/dismiss', () => {
    it('returns nothing (204) when the dismiss succeeds', () => {
      const dismiss = vi.fn().mockReturnValue(true);
      expect(makeController({ dismiss }).dismiss(user, 'welcome')).toBeUndefined();
      expect(dismiss).toHaveBeenCalledWith(7, 'welcome');
    });

    it('404 { error: NOTICE_NOT_FOUND } when the id is unknown', () => {
      const dismiss = vi.fn().mockReturnValue(false);
      expect(thrown(() => makeController({ dismiss }).dismiss(user, 'nope'))).toEqual({
        status: 404,
        body: { error: 'NOTICE_NOT_FOUND' },
      });
    });
  });
});