|
| 1 | +import { jest, expect } from '@jest/globals'; |
| 2 | + |
| 3 | +import { Configuration, InputType } from '../types'; |
| 4 | +import { Diff2HtmlConfig } from 'diff2html'; |
| 5 | + |
| 6 | +let command: typeof import('../main'); |
| 7 | + |
| 8 | +const getInputSpy: jest.Mock<(inputType: InputType, inputArgs: string[], ignore: string[]) => Promise<string>> = |
| 9 | + jest.fn(); |
| 10 | +const getOutputSpy: jest.Mock<(options: Diff2HtmlConfig, config: Configuration, input: string) => string> = jest.fn(); |
| 11 | +const previewSpy: jest.Mock<(content: string, format: string) => void> = jest.fn(); |
| 12 | +jest.unstable_mockModule('../cli', async () => ({ |
| 13 | + getInput: getInputSpy, |
| 14 | + getOutput: getOutputSpy, |
| 15 | + preview: previewSpy, |
| 16 | +})); |
| 17 | + |
| 18 | +beforeEach(async () => { |
| 19 | + command = await import('../main'); |
| 20 | +}); |
| 21 | + |
| 22 | +afterEach(() => { |
| 23 | + jest.clearAllMocks(); |
| 24 | +}); |
| 25 | + |
| 26 | +process.argv = ['node', 'diff2html.js', '-i', 'file', '--', 'test']; |
| 27 | + |
| 28 | +describe('cli', () => { |
| 29 | + test('should parse input and run', async () => { |
| 30 | + getInputSpy.mockReturnValue(Promise.resolve('input')); |
| 31 | + getOutputSpy.mockReturnValue('output'); |
| 32 | + previewSpy.mockReturnValue(); |
| 33 | + |
| 34 | + await command.main(); |
| 35 | + |
| 36 | + expect(getInputSpy).toHaveBeenCalledTimes(1); |
| 37 | + expect(getInputSpy).toHaveBeenCalledWith('file', ['test'], []); |
| 38 | + |
| 39 | + expect(getOutputSpy).toHaveBeenCalledTimes(1); |
| 40 | + expect(getOutputSpy).toHaveBeenCalledWith( |
| 41 | + { |
| 42 | + diffMaxChanges: undefined, |
| 43 | + diffMaxLineLength: undefined, |
| 44 | + diffStyle: 'word', |
| 45 | + drawFileList: true, |
| 46 | + matchWordsThreshold: 0.25, |
| 47 | + matching: 'none', |
| 48 | + matchingMaxComparisons: 1000, |
| 49 | + maxLineLengthHighlight: 10000, |
| 50 | + maxLineSizeInBlockForComparison: 200, |
| 51 | + outputFormat: 'line-by-line', |
| 52 | + renderNothingWhenEmpty: false, |
| 53 | + }, |
| 54 | + { |
| 55 | + diffyType: undefined, |
| 56 | + fileContentToggle: true, |
| 57 | + formatType: 'html', |
| 58 | + highlightCode: true, |
| 59 | + htmlWrapperTemplate: expect.stringContaining('diff2html-cli/template.html'), |
| 60 | + ignore: [], |
| 61 | + inputSource: 'file', |
| 62 | + outputDestinationFile: undefined, |
| 63 | + outputDestinationType: 'preview', |
| 64 | + pageHeader: 'Diff to HTML by <a href="https://github.com/rtfpessoa">rtfpessoa</a>', |
| 65 | + pageTitle: 'Diff to HTML by rtfpessoa', |
| 66 | + showFilesOpen: false, |
| 67 | + synchronisedScroll: true, |
| 68 | + }, |
| 69 | + 'input', |
| 70 | + ); |
| 71 | + }); |
| 72 | +}); |
0 commit comments