asset-interceptor.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context.host';
  2. import { of } from 'rxjs';
  3. import { MockConfigService } from '../../config/config.service.mock';
  4. import { Asset } from '../../entity/asset/asset.entity';
  5. import { AssetInterceptor } from './asset-interceptor';
  6. describe('AssetInterceptor', () => {
  7. function testInterceptor<T>(
  8. response: T,
  9. assertFn: (response: T, result: T, toAbsoluteUrlFn: jest.Mock) => void,
  10. ) {
  11. return (done: jest.DoneCallback) => {
  12. const toAbsoluteUrl = jest.fn().mockReturnValue('visited');
  13. const configService = new MockConfigService();
  14. configService.assetStorageStrategy = { toAbsoluteUrl };
  15. const interceptor = new AssetInterceptor(configService as any);
  16. const executionContext = new ExecutionContextHost([0, 0, { req: {} }]);
  17. const call$ = of(response);
  18. interceptor.intercept(executionContext, call$).subscribe(result => {
  19. assertFn(response, result, toAbsoluteUrl);
  20. done();
  21. });
  22. };
  23. }
  24. function mockAsset() {
  25. return new Asset({ preview: 'original', source: 'original' });
  26. }
  27. it(
  28. 'passes through responses without Assets',
  29. testInterceptor(
  30. {
  31. foo: 1,
  32. bar: {
  33. baz: false,
  34. },
  35. },
  36. (response, result, toAbsoluteUrl) => {
  37. expect(result).toBe(response);
  38. expect(toAbsoluteUrl).toHaveBeenCalledTimes(0);
  39. },
  40. ),
  41. );
  42. it(
  43. 'visits a top-level Asset',
  44. testInterceptor(
  45. {
  46. foo: 1,
  47. bar: mockAsset(),
  48. },
  49. (response, result, toAbsoluteUrl) => {
  50. expect(result.bar.source).toBe('visited');
  51. expect(result.bar.preview).toBe('visited');
  52. expect(toAbsoluteUrl).toHaveBeenCalledTimes(2);
  53. },
  54. ),
  55. );
  56. it(
  57. 'visits a top-level array of Assets',
  58. testInterceptor(
  59. {
  60. foo: 1,
  61. bar: [mockAsset(), mockAsset()],
  62. },
  63. (response, result, toAbsoluteUrl) => {
  64. expect(result.bar[0].source).toBe('visited');
  65. expect(result.bar[0].preview).toBe('visited');
  66. expect(result.bar[1].source).toBe('visited');
  67. expect(result.bar[1].preview).toBe('visited');
  68. expect(toAbsoluteUrl).toHaveBeenCalledTimes(4);
  69. },
  70. ),
  71. );
  72. it(
  73. 'visits a nested Asset',
  74. testInterceptor(
  75. {
  76. foo: 1,
  77. bar: [
  78. {
  79. baz: {
  80. quux: mockAsset(),
  81. },
  82. },
  83. ],
  84. },
  85. (response, result, toAbsoluteUrl) => {
  86. expect(result.bar[0].baz.quux.source).toBe('visited');
  87. expect(result.bar[0].baz.quux.preview).toBe('visited');
  88. expect(toAbsoluteUrl).toHaveBeenCalledTimes(2);
  89. },
  90. ),
  91. );
  92. it(
  93. 'handles null values',
  94. testInterceptor(
  95. {
  96. foo: 1,
  97. bar: null,
  98. },
  99. (response, result, toAbsoluteUrl) => {
  100. expect(result).toEqual({ foo: 1, bar: null });
  101. expect(toAbsoluteUrl).toHaveBeenCalledTimes(0);
  102. },
  103. ),
  104. );
  105. it(
  106. 'handles undefined values',
  107. testInterceptor(
  108. {
  109. foo: 1,
  110. bar: undefined,
  111. },
  112. (response, result, toAbsoluteUrl) => {
  113. expect(result).toEqual({ foo: 1, bar: undefined });
  114. expect(toAbsoluteUrl).toHaveBeenCalledTimes(0);
  115. },
  116. ),
  117. );
  118. });