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/MyApp/AppDelegate.mm')).toBeInTheDocument();
17  expect(screen.getByText('ios/Podfile')).toBeInTheDocument();
18
19  expect(screen.getByText('#import <UIKit/UIKit.h>')).toBeInTheDocument();
20  expect(
21    screen.getByText(
22      '@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>'
23    )
24  ).toBeInTheDocument();
25  expect(screen.getByText('#import <Expo/Expo.h>')).toBeInTheDocument();
26};
27
28describe(DiffBlock, () => {
29  it('renders diff from file correctly', async () => {
30    global.fetch = jest.fn(() =>
31      Promise.resolve({
32        text: async () => DIFF_CONTENT,
33      } as Response)
34    );
35
36    render(<DiffBlock source={DIFF_PATH} />);
37
38    await screen.findByText('ios/MyApp/AppDelegate.h');
39
40    validateDiffContent(screen);
41  });
42
43  it('renders raw diff correctly on first render', () => {
44    render(<DiffBlock raw={DIFF_CONTENT} />);
45
46    validateDiffContent(screen);
47  });
48
49  it('renders diff correctly when no commit data', () => {
50    const noCommitDataDiff = DIFF_CONTENT.replaceAll(/\s+index.+/g, '');
51
52    expect(noCommitDataDiff.includes('index ')).toBe(false);
53
54    render(<DiffBlock raw={noCommitDataDiff} />);
55
56    validateDiffContent(screen);
57  });
58});
59