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-updates-js.diff';
12const DIFF_CONTENT = fs.readFileSync(path.join(dirname, '../../../public', DIFF_PATH)).toString();
13
14const validateDiffContent = (screen: Screen) => {
15  expect(screen.getByText('app.json')).toBeInTheDocument();
16  expect(screen.getByText('index.js')).toBeInTheDocument();
17  expect(screen.getByText('metro.config.js')).toBeInTheDocument();
18
19  expect(screen.getByText('"slug": "my-app",')).toBeInTheDocument();
20  expect(screen.getByText("import 'expo-asset';")).toBeInTheDocument();
21  expect(
22    screen.getByText("assetPlugins: ['expo-asset/tools/hashAssetFiles'],")
23  ).toBeInTheDocument();
24};
25
26describe(DiffBlock, () => {
27  it('renders diff from file correctly', async () => {
28    global.fetch = jest.fn(() =>
29      Promise.resolve({
30        text: async () => DIFF_CONTENT,
31      } as Response)
32    );
33
34    render(<DiffBlock source={DIFF_PATH} />);
35
36    await screen.findByText('app.json');
37
38    validateDiffContent(screen);
39  });
40
41  it('renders raw diff correctly on first render', () => {
42    render(<DiffBlock raw={DIFF_CONTENT} />);
43
44    validateDiffContent(screen);
45  });
46
47  it('renders diff correctly when no commit data', () => {
48    const noCommitDataDiff = DIFF_CONTENT.replaceAll(/\s+index.+/g, '');
49
50    expect(noCommitDataDiff.includes('index ')).toBe(false);
51
52    render(<DiffBlock raw={noCommitDataDiff} />);
53
54    validateDiffContent(screen);
55  });
56});
57