summaryrefslogtreecommitdiff
path: root/spec/frontend/static_site_editor/services/renderers/render_image_spec.js
blob: d3298aa0b261529efc0d220d615d96ef032eb375 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import imageRenderer from '~/static_site_editor/services/renderers/render_image';
import { mounts, project, branch, baseUrl } from '../../mock_data';

describe('rich_content_editor/renderers/render_image', () => {
  let renderer;
  let imageRepository;

  beforeEach(() => {
    renderer = imageRenderer.build(mounts, project, branch, baseUrl, imageRepository);
    imageRepository = { get: () => null };
  });

  describe('build', () => {
    it('builds a renderer object containing `canRender` and `render` functions', () => {
      expect(renderer).toHaveProperty('canRender', expect.any(Function));
      expect(renderer).toHaveProperty('render', expect.any(Function));
    });
  });

  describe('canRender', () => {
    it.each`
      input                    | result
      ${{ type: 'image' }}     | ${true}
      ${{ type: 'text' }}      | ${false}
      ${{ type: 'htmlBlock' }} | ${false}
    `('returns $result when input is $input', ({ input, result }) => {
      expect(renderer.canRender(input)).toBe(result);
    });
  });

  describe('render', () => {
    let skipChildren;
    let context;
    let node;

    beforeEach(() => {
      skipChildren = jest.fn();
      context = { skipChildren };
      node = {
        firstChild: {
          type: 'img',
          literal: 'Some Image',
        },
      };
    });

    it.each`
      destination                                      | isAbsolute | src
      ${'http://test.host/absolute/path/to/image.png'} | ${true}    | ${'http://test.host/absolute/path/to/image.png'}
      ${'/relative/path/to/image.png'}                 | ${false}   | ${'http://test.host/user1/project1/-/raw/main/default/source/relative/path/to/image.png'}
      ${'/target/image.png'}                           | ${false}   | ${'http://test.host/user1/project1/-/raw/main/source/with/target/image.png'}
      ${'relative/to/current/image.png'}               | ${false}   | ${'http://test.host/user1/project1/-/raw/main/relative/to/current/image.png'}
      ${'./relative/to/current/image.png'}             | ${false}   | ${'http://test.host/user1/project1/-/raw/main/./relative/to/current/image.png'}
      ${'../relative/to/current/image.png'}            | ${false}   | ${'http://test.host/user1/project1/-/raw/main/../relative/to/current/image.png'}
    `('returns an image with the correct attributes', ({ destination, isAbsolute, src }) => {
      node.destination = destination;

      const result = renderer.render(node, context);

      expect(result).toEqual({
        type: 'openTag',
        tagName: 'img',
        selfClose: true,
        attributes: {
          'data-original-src': !isAbsolute ? destination : '',
          src,
          alt: 'Some Image',
        },
      });

      expect(skipChildren).toHaveBeenCalled();
    });

    it('renders an image if a cached image is found in the repository, use the base64 content as the source', () => {
      const imageContent = 'some-content';
      const originalSrc = 'path/to/image.png';

      imageRepository.get = () => imageContent;
      renderer = imageRenderer.build(mounts, project, branch, baseUrl, imageRepository);
      node.destination = originalSrc;

      const result = renderer.render(node, context);

      expect(result).toEqual({
        type: 'openTag',
        tagName: 'img',
        selfClose: true,
        attributes: {
          'data-original-src': originalSrc,
          src: `data:image;base64,${imageContent}`,
          alt: 'Some Image',
        },
      });
    });
  });
});