aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/helpers/convert-wp-image-to-img.test.ts
blob: ca58a4ff14b1bd09ee28476fe61dc3b7916a8d76 (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
import { describe, expect, it } from '@jest/globals';
import type { WPImage } from '../../../types';
import { convertWPImgToImg } from './convert-wp-image-to-img';

describe('convert-wp-image-to-img', () => {
  it('converts a WPImage object to an Img object', () => {
    const img: WPImage = {
      altText: 'molestiae praesentium animi',
      mediaDetails: {
        height: 480,
        width: 640,
      },
      sourceUrl: 'https://picsum.photos/640/480',
      title: null,
    };

    const transformedImg = convertWPImgToImg(img);

    expect(transformedImg.alt).toBe(img.altText);
    expect(transformedImg.height).toBe(img.mediaDetails.height);
    expect(transformedImg.src).toBe(img.sourceUrl);
    expect(transformedImg.title).toBeUndefined();
    expect(transformedImg.width).toBe(img.mediaDetails.width);
  });

  it('can return an empty string if altText is missing', () => {
    const img: WPImage = {
      altText: null,
      mediaDetails: {
        height: 480,
        width: 640,
      },
      sourceUrl: 'https://picsum.photos/640/480',
      title: null,
    };

    const transformedImg = convertWPImgToImg(img);

    expect(transformedImg.alt).toBe('');
  });
});