1import { jest } from '@jest/globals';
2import { render, Screen, screen } from '@testing-library/react';
3import fs from 'fs-extra';
4import path from 'path';
5import { fileURLToPath } from 'url';
6
7import { DiffBlock } from '.';
8
9const dirname = path.dirname(fileURLToPath(import.meta.url));
10
11const DIFF_PATH = '/static/diffs/expo-ios.diff';
12const DIFF_CONTENT = fs.readFileSync(path.join(dirname, '../../../public', DIFF_PATH)).toString();
13
14const validateDiffContent = (screen: Screen) => {
15  expect(screen.getByText('ios/MyApp/AppDelegate.h')).toBeInTheDocument();
16  expect(screen.getByText('ios/Podfile')).toBeInTheDocument();
17  expect(screen.getByText('#import <UIKit/UIKit.h>')).toBeInTheDocument();
18  expect(screen.getByText('#import <Expo/Expo.h>')).toBeInTheDocument();
19};
20
21describe(DiffBlock, () => {
22  it('renders diff from file correctly', async () => {
23    global.fetch = jest.fn(() =>
24      Promise.resolve({
25        text: async () => DIFF_CONTENT,
26      } as Response)
27    );
28
29    render(<DiffBlock source={DIFF_PATH} />);
30
31    await screen.findByText('ios/MyApp/AppDelegate.h');
32
33    validateDiffContent(screen);
34  });
35
36  it('renders raw diff correctly on first render', () => {
37    render(<DiffBlock raw={DIFF_CONTENT} />);
38
39    validateDiffContent(screen);
40  });
41
42  it('renders diff correctly when no commit data', () => {
43    const noCommitDataDiff = DIFF_CONTENT.replaceAll(/\s+index.+/g, '');
44
45    expect(noCommitDataDiff.includes('index ')).toBe(false);
46
47    render(<DiffBlock raw={noCommitDataDiff} />);
48
49    validateDiffContent(screen);
50  });
51});
52